Subversion Repositories shark

Rev

Rev 1349 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1162 tavani 1
/*
2
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *               Gerardo Lamastra <gerardo@sssup.it>
6
 *
7
 * Authors     : Paolo Gai <pj@hartik.sssup.it>
8
 * (see authors.txt for full list of hartik's authors)
9
 *
10
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
11
 *
12
 * http://www.sssup.it
13
 * http://retis.sssup.it
14
 * http://hartik.sssup.it
15
 */
16
 
17
/*
18
 * Copyright (C) 2000 Paolo Gai
19
 *
20
 * This program is free software; you can redistribute it and/or modify
21
 * it under the terms of the GNU General Public License as published by
22
 * the Free Software Foundation; either version 2 of the License, or
23
 * (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
 *
34
 */
35
 
36
#include "asteroid.h"
37
 
38
PID pid_RC;
39
int nrock;
40
int kill_rock;
41
rock_pos rocks[ROCK_NMAX];
42
 
43
void draw_rock(int x, int y, int r, int c)
44
{
45
        sem_wait(&mx_grf);
46
        grx_disc(x, y, r, c);
47
        sem_post(&mx_grf);
48
}
49
 
50
TASK rock(rock_ini* ri)
51
{
52
        int   x, y, r;  /* rock graphic position */
53
        int   ox,  oy;  /* rock old position */
54
        int   x0,  y0;  /* rock initial position */
55
        float vx,  vy;  /* rock speed */
56
        float vxo, vyo; /* rock initial speed */
57
        float ty,  tx;  
58
        float dt;      
59
        float rand_ang;
60
        int   rand_side;
61
        int   ab, ax, ay, i, l;
62
 
63
        x = y = x0 = y0 = ox = oy = vx = vy = ab = 0;
64
 
65
        i = ri->i;
66
        r = ri->r;
67
 
68
        rand_ang = ((rand()%120) - 60) * PI / 180.;
69
 
70
        sem_wait(&mx_st_scr);
71
        l = score / 50;
72
        sem_post(&mx_st_scr);
73
 
74
        sem_wait(&mx_mat);
75
        vxo = - (ROCK_VEL + l) * sin(rand_ang);
76
        vyo = - (ROCK_VEL + l) * cos(rand_ang);
77
        sem_post(&mx_mat);
78
 
79
#ifdef ASTRO_MOVE
80
        rand_side = rand()%4;
81
#else
82
        rand_side = 1;
83
#endif
84
 
85
        if (rand_side == 0) { // Bottom
86
                vx =  vxo;
87
                vy =  vyo;
88
        }
89
        if (rand_side == 1) { // Top
90
                vx = -vxo;
91
                vy = -vyo;
92
        }
93
        if (rand_side == 2) { // Right
94
                vx =  vyo;
95
                vy = -vxo;
96
        }
97
        if (rand_side == 3) { // Left
98
                vx = -vyo;
99
                vy =  vxo;
100
        }
101
 
102
        if ( (ri->x == 0) && (ri->y == 0) ) {
103
                if (rand_side < 2) { // Bottom or Top
104
                        x = ox = x0 = GB_XMIN + (GB_XMAX-GB_XMIN)/4 + (rand()%((GB_XMAX-GB_XMIN)/2));
105
                        if (rand_side == 0) { // Bottom
106
                                y = oy = y0 = GB_YMAX - (r+1);
107
                        }
108
                        if (rand_side == 1) { // Top
109
                                y = oy = y0 = GB_YMIN + (r+1);
110
                        }
111
                } else {
112
                        y = oy = y0 = GB_YMIN + (GB_YMAX-GB_YMIN)/4 + (rand()%((GB_YMAX-GB_YMIN)/2));
113
                        if (rand_side == 2) { // Right
114
                                x = ox = x0 = GB_XMAX - (r+1);
115
                        }
116
                        if (rand_side == 3) { // Left
117
                                x = ox = x0 = GB_XMIN + (r+1);
118
                        }
119
                }
120
        } else {
121
                x = ox = x0 = ri->x;
122
                y = oy = y0 = ri->y;
123
        }
124
 
125
        sem_wait(&mx_rk);
126
        rocks[i].x = x;
127
        rocks[i].y = y;
128
        rocks[i].r = r;
129
        sem_post(&mx_rk);
130
 
131
        tx = 0;
132
        ty = 0;
133
        dt = ((float)ROCK_PERIOD)/100000;
134
 
135
        while (1) {
136
                y = y0 + vy * ty;
137
                x = x0 + vx * tx;
138
 
139
                sem_wait(&mx_rk);
140
                rocks[i].x = x;
141
                rocks[i].y = y;
142
                r = rocks[i].r;
143
                sem_post(&mx_rk);
144
 
145
                draw_rock(ox, oy, r, RGB_BLACK);
146
 
147
                if ((kill_rock) || (crash)){
148
                        nrock--;
149
                        sem_wait(&mx_rk);
150
                        rocks[i].pid = NIL;
151
                        sem_post(&mx_rk);
152
                        return 0;
153
                }
154
 
155
                ox = x;
156
                oy = y;
157
 
158
                sem_wait(&mx_xy);
159
                ax = astro_x;
160
                ay = astro_y;
161
                sem_post(&mx_xy);
162
 
163
                if (dist_xy(x, y, ax, ay) < (ASTRO_RADIUS/2+r)) {
164
                        if (!ab) {
165
                                sem_wait(&mx_st_nrg);
166
                                energy -= ENERGY_GOT;
167
                                sem_post(&mx_st_nrg);
168
                                ab = 1;
169
                        }
170
                } else
171
                        ab = 0;
172
 
173
                sem_wait(&mx_xy);
174
                if (dist_xy(x, y, astro_x, astro_y) < (ASTRO_RADIUS+r)/2) crash = 1;
175
                sem_post(&mx_xy);
176
 
177
#ifdef ASTRO_MOVE
178
                if (x <= GB_XMIN + r) {
179
                        x0 = x = GB_XMAX - (r+1);
180
                        y0 = y;
181
                        tx = ty = 0;
182
                }
183
                if (x >= GB_XMAX - r) {
184
                        x0 = x = GB_XMIN + (r+1);
185
                        y0 = y;
186
                        tx = ty = 0;
187
                }
188
                if (y <= GB_YMIN + r) {
189
                        x0 = x;
190
                        y0 = y = GB_YMAX - (r+1);
191
                        tx = ty = 0;
192
                }
193
                if (y >= GB_YMAX - r) {
194
                        x0 = x;
195
                        y0 = y = GB_YMIN + (r+1);
196
                        tx = ty = 0;
197
                }
198
#else
199
                if ( (x <= GB_XMIN + r) || (x >= GB_XMAX - r) || (y >= GB_YMAX - 2*r) ) {
200
                        if (y >= GB_YMAX - 2*r) {
201
                                sem_wait(&mx_st_nrg);
202
                                energy -= ENERGY_GOT;
203
                                sem_post(&mx_st_nrg);
204
                        }
205
                        nrock--;
206
                        sem_wait(&mx_rk);
207
                        rocks[i].pid = NIL;
208
                        sem_post(&mx_rk);
209
                        return 0;
210
                }
211
#endif
212
                draw_rock(ox, oy, r, RGB_YELLOW);
213
 
214
                ty += dt;
215
                tx += dt;
216
 
217
                task_endcycle();
218
        }
219
}
220
 
221
void rock_create(rock_ini* ri)
222
{
223
        SOFT_TASK_MODEL mp;
224
        PID pid;
225
        int i;
226
 
227
        soft_task_default_model(mp);
228
        soft_task_def_level(mp,1);
229
        soft_task_def_ctrl_jet(mp);
230
        soft_task_def_group(mp, ROCK_GROUP);
231
        soft_task_def_met(mp, ROCK_WCET);
232
        soft_task_def_period(mp,ROCK_PERIOD);
233
        soft_task_def_usemath(mp);
234
        i = -1;
235
        do {
236
                i++;
237
                sem_wait(&mx_rk);
238
                pid = rocks[i].pid;
239
                sem_post(&mx_rk);
240
        } while (pid != NIL);
241
        ri->i = i;
242
        soft_task_def_arg(mp, (void *)ri);
243
        pid = task_create("Rock", rock, &mp, NULL);
244
 
245
        if (pid != NIL) {
246
                sem_wait(&mx_rk);
247
                rocks[i].pid = pid;
248
                sem_post(&mx_rk);
249
                task_activate(pid);
250
                nrock++;
251
        }
252
}
253
 
254
TASK rock_creator()
255
{
256
        while (1) {
257
                sem_wait(&mx_rn);
258
                if (rock_new.i == 0) {
259
                        rock_create(&rock_new);
260
                        rock_new.i = -1;
261
                }
262
                sem_post(&mx_rn);
263
 
264
                if ((nrock < ROCK_NMAX/2) && (!kill_rock) && (!crash)) {
265
                        if ((rand()%ROCK_NMAX) > nrock) {
266
                                rock_new.r = ROCK_RADIUS_I;
267
                                rock_new.x = rock_new.y = 0;
268
                                rock_create(&rock_new);
269
                        }
270
                }
271
                task_endcycle();
272
        }
273
}
274
 
275
void reset_rock()
276
{
277
        kill_rock = 1;
278
}
279
 
280
void start_rock()
281
{
282
        kill_rock = 0;
283
}
284
 
285
void create_rock_task()
286
{
287
        SOFT_TASK_MODEL ms;
288
 
289
        soft_task_default_model(ms);
290
        soft_task_def_level(ms,1);
291
        soft_task_def_ctrl_jet(ms);
292
        soft_task_def_met(ms, ROCK_WCET);
293
        soft_task_def_period(ms,ROCK_PERIOD*10);
294
        soft_task_def_usemath(ms);
295
        pid_RC = task_create("RockCreator", rock_creator, &ms, NULL);
296
        if (pid_RC == NIL) {
297
                grx_close();
298
                perror("Could not create task <RockCreator>");
299
                sys_end();
300
        } else
301
                task_activate(pid_RC);
302
}
303
 
304
void init_rock()
305
{
306
        int i;
307
 
308
        nrock = 0;
309
        kill_rock = 1;
310
        for ( i=0; i<ROCK_NMAX; i++ ) rocks[i].pid = NIL;
311
 
312
        create_rock_task();
313
}
314