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