Rev 1349 | Rev 1373 | 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_ctrl_jet(mp); |
||
206 | soft_task_def_group(mp, ROCK_GROUP); |
||
207 | soft_task_def_met(mp, ROCK_WCET); |
||
208 | soft_task_def_period(mp,ROCK_PERIOD); |
||
209 | soft_task_def_usemath(mp); |
||
210 | i = -1; |
||
211 | do { |
||
212 | i++; |
||
213 | sem_wait(&mx_rk); |
||
214 | pid = rocks[i].pid; |
||
215 | sem_post(&mx_rk); |
||
216 | } while (pid != NIL); |
||
217 | ri->i = i; |
||
218 | soft_task_def_arg(mp, (void *)ri); |
||
219 | pid = task_create("Rock", rock, &mp, NULL); |
||
220 | |||
221 | if (pid != NIL) { |
||
222 | sem_wait(&mx_rk); |
||
223 | rocks[i].pid = pid; |
||
224 | sem_post(&mx_rk); |
||
225 | task_activate(pid); |
||
226 | nrock++; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | TASK rock_creator() |
||
231 | { |
||
232 | while (1) { |
||
233 | sem_wait(&mx_rn); |
||
234 | if (rock_new.i == 0) { |
||
235 | rock_create(&rock_new); |
||
236 | rock_new.i = -1; |
||
237 | } |
||
238 | sem_post(&mx_rn); |
||
239 | |||
240 | if ((nrock < ROCK_NMAX/2) && (!kill_rock) && (!crash)) { |
||
241 | if ((rand()%ROCK_NMAX) > nrock) { |
||
242 | rock_new.r = ROCK_RADIUS_I; |
||
243 | rock_new.x = rock_new.y = 0; |
||
244 | rock_create(&rock_new); |
||
245 | } |
||
246 | } |
||
247 | task_endcycle(); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | void reset_rock() |
||
252 | { |
||
253 | kill_rock = 1; |
||
254 | } |
||
255 | |||
256 | void start_rock() |
||
257 | { |
||
258 | kill_rock = 0; |
||
259 | } |
||
260 | |||
261 | void create_rock_task() |
||
262 | { |
||
263 | SOFT_TASK_MODEL ms; |
||
264 | |||
265 | soft_task_default_model(ms); |
||
266 | soft_task_def_ctrl_jet(ms); |
||
267 | soft_task_def_met(ms, ROCK_WCET); |
||
268 | soft_task_def_period(ms,ROCK_PERIOD*10); |
||
269 | soft_task_def_usemath(ms); |
||
270 | pid_RC = task_create("RockCreator", rock_creator, &ms, NULL); |
||
271 | if (pid_RC == NIL) { |
||
1349 | giacomo | 272 | FB26_close(FRAME_BUFFER_DEVICE); |
1162 | tavani | 273 | perror("Could not create task <RockCreator>"); |
274 | sys_end(); |
||
275 | } else |
||
276 | task_activate(pid_RC); |
||
277 | } |
||
278 | |||
279 | void init_rock() |
||
280 | { |
||
281 | int i; |
||
282 | |||
283 | nrock = 0; |
||
284 | kill_rock = 1; |
||
285 | for ( i=0; i<ROCK_NMAX; i++ ) rocks[i].pid = NIL; |
||
286 | |||
287 | create_rock_task(); |
||
288 | } |
||
289 |