Subversion Repositories shark

Rev

Rev 1162 | Rev 1351 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1162 tavani 1
/*
1349 giacomo 2
 * Project: S.Ha.R.K.
1162 tavani 3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *
6
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
7
 *
8
 * http://www.sssup.it
9
 * http://retis.sssup.it
10
 * http://hartik.sssup.it
11
 */
12
 
13
#include "asteroid.h"
14
 
15
PID pid_RC;
16
int nrock;
17
int kill_rock;
18
rock_pos rocks[ROCK_NMAX];
19
 
20
void draw_rock(int x, int y, int r, int c)
21
{
22
        sem_wait(&mx_grf);
23
        grx_disc(x, y, r, c);
24
        sem_post(&mx_grf);
25
}
26
 
27
TASK rock(rock_ini* ri)
28
{
29
        int   x, y, r;  /* rock graphic position */
30
        int   ox,  oy;  /* rock old position */
31
        int   x0,  y0;  /* rock initial position */
32
        float vx,  vy;  /* rock speed */
33
        float vxo, vyo; /* rock initial speed */
34
        float ty,  tx;  
35
        float dt;      
36
        float rand_ang;
37
        int   rand_side;
38
        int   ab, ax, ay, i, l;
39
 
40
        x = y = x0 = y0 = ox = oy = vx = vy = ab = 0;
41
 
42
        i = ri->i;
43
        r = ri->r;
44
 
45
        rand_ang = ((rand()%120) - 60) * PI / 180.;
46
 
47
        sem_wait(&mx_st_scr);
48
        l = score / 50;
49
        sem_post(&mx_st_scr);
50
 
51
        sem_wait(&mx_mat);
52
        vxo = - (ROCK_VEL + l) * sin(rand_ang);
53
        vyo = - (ROCK_VEL + l) * cos(rand_ang);
54
        sem_post(&mx_mat);
55
 
56
#ifdef ASTRO_MOVE
57
        rand_side = rand()%4;
58
#else
59
        rand_side = 1;
60
#endif
61
 
62
        if (rand_side == 0) { // Bottom
63
                vx =  vxo;
64
                vy =  vyo;
65
        }
66
        if (rand_side == 1) { // Top
67
                vx = -vxo;
68
                vy = -vyo;
69
        }
70
        if (rand_side == 2) { // Right
71
                vx =  vyo;
72
                vy = -vxo;
73
        }
74
        if (rand_side == 3) { // Left
75
                vx = -vyo;
76
                vy =  vxo;
77
        }
78
 
79
        if ( (ri->x == 0) && (ri->y == 0) ) {
80
                if (rand_side < 2) { // Bottom or Top
81
                        x = ox = x0 = GB_XMIN + (GB_XMAX-GB_XMIN)/4 + (rand()%((GB_XMAX-GB_XMIN)/2));
82
                        if (rand_side == 0) { // Bottom
83
                                y = oy = y0 = GB_YMAX - (r+1);
84
                        }
85
                        if (rand_side == 1) { // Top
86
                                y = oy = y0 = GB_YMIN + (r+1);
87
                        }
88
                } else {
89
                        y = oy = y0 = GB_YMIN + (GB_YMAX-GB_YMIN)/4 + (rand()%((GB_YMAX-GB_YMIN)/2));
90
                        if (rand_side == 2) { // Right
91
                                x = ox = x0 = GB_XMAX - (r+1);
92
                        }
93
                        if (rand_side == 3) { // Left
94
                                x = ox = x0 = GB_XMIN + (r+1);
95
                        }
96
                }
97
        } else {
98
                x = ox = x0 = ri->x;
99
                y = oy = y0 = ri->y;
100
        }
101
 
102
        sem_wait(&mx_rk);
103
        rocks[i].x = x;
104
        rocks[i].y = y;
105
        rocks[i].r = r;
106
        sem_post(&mx_rk);
107
 
108
        tx = 0;
109
        ty = 0;
110
        dt = ((float)ROCK_PERIOD)/100000;
111
 
112
        while (1) {
113
                y = y0 + vy * ty;
114
                x = x0 + vx * tx;
115
 
116
                sem_wait(&mx_rk);
117
                rocks[i].x = x;
118
                rocks[i].y = y;
119
                r = rocks[i].r;
120
                sem_post(&mx_rk);
121
 
122
                draw_rock(ox, oy, r, RGB_BLACK);
123
 
124
                if ((kill_rock) || (crash)){
125
                        nrock--;
126
                        sem_wait(&mx_rk);
127
                        rocks[i].pid = NIL;
128
                        sem_post(&mx_rk);
129
                        return 0;
130
                }
131
 
132
                ox = x;
133
                oy = y;
134
 
135
                sem_wait(&mx_xy);
136
                ax = astro_x;
137
                ay = astro_y;
138
                sem_post(&mx_xy);
139
 
140
                if (dist_xy(x, y, ax, ay) < (ASTRO_RADIUS/2+r)) {
141
                        if (!ab) {
142
                                sem_wait(&mx_st_nrg);
143
                                energy -= ENERGY_GOT;
144
                                sem_post(&mx_st_nrg);
145
                                ab = 1;
146
                        }
147
                } else
148
                        ab = 0;
149
 
150
                sem_wait(&mx_xy);
151
                if (dist_xy(x, y, astro_x, astro_y) < (ASTRO_RADIUS+r)/2) crash = 1;
152
                sem_post(&mx_xy);
153
 
154
#ifdef ASTRO_MOVE
155
                if (x <= GB_XMIN + r) {
156
                        x0 = x = GB_XMAX - (r+1);
157
                        y0 = y;
158
                        tx = ty = 0;
159
                }
160
                if (x >= GB_XMAX - r) {
161
                        x0 = x = GB_XMIN + (r+1);
162
                        y0 = y;
163
                        tx = ty = 0;
164
                }
165
                if (y <= GB_YMIN + r) {
166
                        x0 = x;
167
                        y0 = y = GB_YMAX - (r+1);
168
                        tx = ty = 0;
169
                }
170
                if (y >= GB_YMAX - r) {
171
                        x0 = x;
172
                        y0 = y = GB_YMIN + (r+1);
173
                        tx = ty = 0;
174
                }
175
#else
176
                if ( (x <= GB_XMIN + r) || (x >= GB_XMAX - r) || (y >= GB_YMAX - 2*r) ) {
177
                        if (y >= GB_YMAX - 2*r) {
178
                                sem_wait(&mx_st_nrg);
179
                                energy -= ENERGY_GOT;
180
                                sem_post(&mx_st_nrg);
181
                        }
182
                        nrock--;
183
                        sem_wait(&mx_rk);
184
                        rocks[i].pid = NIL;
185
                        sem_post(&mx_rk);
186
                        return 0;
187
                }
188
#endif
189
                draw_rock(ox, oy, r, RGB_YELLOW);
190
 
191
                ty += dt;
192
                tx += dt;
193
 
194
                task_endcycle();
195
        }
196
}
197
 
198
void rock_create(rock_ini* ri)
199
{
200
        SOFT_TASK_MODEL mp;
201
        PID pid;
202
        int i;
203
 
204
        soft_task_default_model(mp);
205
        soft_task_def_level(mp,1);
206
        soft_task_def_ctrl_jet(mp);
207
        soft_task_def_group(mp, ROCK_GROUP);
208
        soft_task_def_met(mp, ROCK_WCET);
209
        soft_task_def_period(mp,ROCK_PERIOD);
210
        soft_task_def_usemath(mp);
211
        i = -1;
212
        do {
213
                i++;
214
                sem_wait(&mx_rk);
215
                pid = rocks[i].pid;
216
                sem_post(&mx_rk);
217
        } while (pid != NIL);
218
        ri->i = i;
219
        soft_task_def_arg(mp, (void *)ri);
220
        pid = task_create("Rock", rock, &mp, NULL);
221
 
222
        if (pid != NIL) {
223
                sem_wait(&mx_rk);
224
                rocks[i].pid = pid;
225
                sem_post(&mx_rk);
226
                task_activate(pid);
227
                nrock++;
228
        }
229
}
230
 
231
TASK rock_creator()
232
{
233
        while (1) {
234
                sem_wait(&mx_rn);
235
                if (rock_new.i == 0) {
236
                        rock_create(&rock_new);
237
                        rock_new.i = -1;
238
                }
239
                sem_post(&mx_rn);
240
 
241
                if ((nrock < ROCK_NMAX/2) && (!kill_rock) && (!crash)) {
242
                        if ((rand()%ROCK_NMAX) > nrock) {
243
                                rock_new.r = ROCK_RADIUS_I;
244
                                rock_new.x = rock_new.y = 0;
245
                                rock_create(&rock_new);
246
                        }
247
                }
248
                task_endcycle();
249
        }
250
}
251
 
252
void reset_rock()
253
{
254
        kill_rock = 1;
255
}
256
 
257
void start_rock()
258
{
259
        kill_rock = 0;
260
}
261
 
262
void create_rock_task()
263
{
264
        SOFT_TASK_MODEL ms;
265
 
266
        soft_task_default_model(ms);
267
        soft_task_def_level(ms,1);
268
        soft_task_def_ctrl_jet(ms);
269
        soft_task_def_met(ms, ROCK_WCET);
270
        soft_task_def_period(ms,ROCK_PERIOD*10);
271
        soft_task_def_usemath(ms);
272
        pid_RC = task_create("RockCreator", rock_creator, &ms, NULL);
273
        if (pid_RC == NIL) {
1349 giacomo 274
                FB26_close(FRAME_BUFFER_DEVICE);
1162 tavani 275
                perror("Could not create task <RockCreator>");
276
                sys_end();
277
        } else
278
                task_activate(pid_RC);
279
}
280
 
281
void init_rock()
282
{
283
        int i;
284
 
285
        nrock = 0;
286
        kill_rock = 1;
287
        for ( i=0; i<ROCK_NMAX; i++ ) rocks[i].pid = NIL;
288
 
289
        create_rock_task();
290
}
291