Rev 1086 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | //*******************************tasks.c************************************ |
2 | // task bodies |
||
3 | #include <drivers/glib.h> |
||
4 | #include <stdlib.h> |
||
5 | #include <string.h> |
||
6 | #include <kernel/func.h> |
||
7 | #include <drivers/glib.h> |
||
1109 | pj | 8 | #include <semaphore.h> |
1085 | pj | 9 | #include <math.h> |
10 | #include "include/simcity.h" |
||
11 | #include "include/states.h" |
||
12 | #include "include/draw.h" |
||
13 | #include "include/misc.h" |
||
14 | #include "include/car.h" |
||
15 | #include "include/proc.h" |
||
16 | #define rgb rgb16 |
||
17 | |||
18 | /*extern starting_set starting_set_array[S_POINT]; |
||
19 | extern short maxc; */ |
||
20 | |||
21 | TASK killer(void *arg) { |
||
22 | short i,j=0; |
||
23 | DWORD col[3]; |
||
24 | char flag=0; |
||
25 | |||
26 | col[0]=red; |
||
27 | col[1]=green; |
||
28 | col[2]=white; |
||
29 | |||
30 | while(1) { |
||
31 | if(maxc>0) { |
||
32 | for(i=0;i<MAX_CAR;i++) { |
||
33 | sem_wait(&(kill_mutex[i])); |
||
34 | if(car_data_array[i].boom && car_data_array[i].running) flag=1; |
||
35 | else flag=0; |
||
36 | sem_post(&(kill_mutex[i])); |
||
37 | if(flag) { |
||
38 | del(i); |
||
39 | killing(i); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | j=(j+1)%3; |
||
44 | sem_wait(&mutex); |
||
45 | grx_text("Killer active!",CAMX,CAMY+MAX_CAR*46,col[j],black); |
||
46 | sem_post(&mutex); |
||
47 | task_endcycle(); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | |||
52 | TASK refresher(void *arg) { |
||
53 | int i; |
||
54 | tl_data *d; |
||
55 | |||
56 | while(1) { |
||
57 | sem_wait(&mutex); |
||
58 | grx_putimage(MAPX,MAPY,MAPX+W-1,MAPY+H-1,street); |
||
59 | for(i=0;i<MAX_TL;i++) { |
||
60 | d=&(tl_data_array[i]); |
||
61 | if((d->vpos=='g')) { |
||
62 | draw_tl(d->u.x,d->u.y,g_sem[U]); |
||
63 | draw_tl(d->d.x,d->d.y,g_sem[D]); |
||
64 | draw_tl(d->l.x,d->l.y,r_sem[L]); |
||
65 | draw_tl(d->r.x,d->r.y,r_sem[R]); |
||
66 | } else if(d->vpos=='y') { |
||
67 | draw_tl(d->u.x,d->u.y,y_sem[U]); |
||
68 | draw_tl(d->d.x,d->d.y,y_sem[D]); |
||
69 | draw_tl(d->l.x,d->l.y,r_sem[L]); |
||
70 | draw_tl(d->r.x,d->r.y,r_sem[R]); |
||
71 | } else if(d->hpos=='g') { |
||
72 | draw_tl(d->u.x,d->u.y,r_sem[U]); |
||
73 | draw_tl(d->d.x,d->d.y,r_sem[D]); |
||
74 | draw_tl(d->l.x,d->l.y,g_sem[L]); |
||
75 | draw_tl(d->r.x,d->r.y,g_sem[R]); |
||
76 | } else if(d->hpos=='y') { |
||
77 | draw_tl(d->u.x,d->u.y,r_sem[U]); |
||
78 | draw_tl(d->d.x,d->d.y,r_sem[D]); |
||
79 | draw_tl(d->l.x,d->l.y,y_sem[L]); |
||
80 | draw_tl(d->r.x,d->r.y,y_sem[R]); |
||
81 | } |
||
82 | } |
||
83 | sem_post(&mutex); |
||
84 | task_endcycle(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | TASK blink_arrow(int number) { |
||
89 | char ld=0,prev=0,dir=0; |
||
90 | short c; |
||
91 | BYTE *arrow_img; |
||
92 | |||
93 | c=number*46; |
||
94 | while(1) { |
||
95 | if(kill_flag[number]) { |
||
96 | return 0; |
||
97 | } |
||
98 | if(!car_data_array[number].boom) { |
||
99 | dir=car_data_array[number].dir+1; |
||
100 | if(dir==prev) { |
||
101 | ld=(ld+1)%2; |
||
102 | if(ld==1) |
||
103 | arrow_img=arrow[(int)dir]; |
||
104 | else |
||
105 | arrow_img=arrow[1]; |
||
106 | |||
107 | sem_wait(&mutex); |
||
108 | grx_putimage(CAMX+80,CAMY-1+c,CAMX+80+29,CAMY-1+c+29,arrow_img); |
||
109 | sem_post(&mutex); |
||
110 | } else { |
||
111 | prev=dir; |
||
112 | ld=0; |
||
113 | sem_wait(&mutex); |
||
114 | grx_putimage(CAMX+80,CAMY-1+c,CAMX+80+29,CAMY-1+c+29,arrow[(int)dir]); |
||
115 | sem_post(&mutex); |
||
116 | } |
||
117 | } |
||
118 | task_endcycle(); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | |||
123 | TASK gauge_c(int number) { |
||
124 | int c; |
||
125 | float angle,ro; |
||
126 | int x,y; |
||
127 | char vel[5]; |
||
128 | |||
129 | c=number*46; |
||
130 | while(1) { |
||
131 | if(kill_flag[number]) { |
||
132 | return 0; |
||
133 | } |
||
134 | if(!car_data_array[number].boom) { |
||
135 | angle=180.0*car_data_array[number].speed/MAX_SPEED; |
||
136 | sprintf(vel,"%.1f",car_data_array[number].speed); |
||
137 | ro=9.0; |
||
138 | y=my_rint(ro*sine[my_rint(angle)]); |
||
139 | x=my_rint(ro*cosine[my_rint(angle)]); |
||
140 | sem_wait(&mutex); |
||
141 | grx_putimage(CAMX,CAMY+c,CAMX+29,CAMY+29+c,gauge_img); |
||
142 | grx_line(CAMX+14,CAMY-1+c+25,CAMX+14-x,CAMY-1+c+25-y,blue); |
||
143 | grx_text(vel,CAMX,CAMY+c+30,white,black); |
||
144 | sem_post(&mutex); |
||
145 | } else { |
||
146 | sem_wait(&mutex); |
||
147 | grx_putimage(CAMX,CAMY+c,CAMX+29,CAMY+29+c,brk_gauge); |
||
148 | sem_post(&mutex); |
||
149 | } |
||
150 | task_endcycle(); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | TASK camera(int number) { |
||
155 | int c; |
||
156 | |||
157 | c=number*46; |
||
158 | grx_rect(CAMX+40-1,CAMY-1+c,CAMX+40+30,CAMY+30+c,green); |
||
159 | while(1) { |
||
160 | if(kill_flag[number]) { |
||
161 | return 0; |
||
162 | } |
||
163 | sem_wait(&mutex); |
||
164 | grx_putimage(CAMX+40,CAMY+c,CAMX+40+29,CAMY+c+29,vbuf[number]); |
||
165 | sem_post(&mutex); |
||
166 | task_endcycle(); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | TASK traffic_light(tl_data *d) { |
||
171 | char hpos,vpos,flag; |
||
172 | |||
173 | flag=0; |
||
174 | vpos=d->vpos; |
||
175 | if(vpos=='g') { |
||
176 | hpos='r'; |
||
177 | #ifdef GRAPH |
||
178 | sem_wait(&mutex); |
||
179 | draw_tl(d->l.x,d->l.y,r_sem[L]); |
||
180 | draw_tl(d->r.x,d->r.y,r_sem[R]); |
||
181 | draw_tl(d->u.x,d->u.y,g_sem[U]); |
||
182 | draw_tl(d->d.x,d->d.y,g_sem[D]); |
||
183 | sem_post(&mutex); |
||
184 | #endif |
||
185 | } else { |
||
186 | vpos='r'; |
||
187 | hpos='g'; |
||
188 | #ifdef GRAPH |
||
189 | sem_wait(&mutex); |
||
190 | draw_tl(d->l.x,d->l.y,g_sem[L]); |
||
191 | draw_tl(d->r.x,d->r.y,g_sem[R]); |
||
192 | draw_tl(d->u.x,d->u.y,r_sem[U]); |
||
193 | draw_tl(d->d.x,d->d.y,r_sem[D]); |
||
194 | sem_post(&mutex); |
||
195 | #endif |
||
196 | } |
||
197 | task_endcycle(); |
||
198 | while(1) { |
||
199 | if(vpos=='g') { |
||
200 | vpos='y'; |
||
201 | flag=1; |
||
202 | #ifdef GRAPH |
||
203 | sem_wait(&mutex); |
||
204 | draw_tl(d->u.x,d->u.y,y_sem[U]); |
||
205 | draw_tl(d->d.x,d->d.y,y_sem[D]); |
||
206 | sem_post(&mutex); |
||
207 | #endif |
||
208 | } |
||
209 | if((vpos=='y')&&(!flag)) { |
||
210 | vpos='r'; |
||
211 | hpos='g'; |
||
212 | flag=1; |
||
213 | #ifdef GRAPH |
||
214 | sem_wait(&mutex); |
||
215 | draw_tl(d->u.x,d->u.y,r_sem[U]); |
||
216 | draw_tl(d->d.x,d->d.y,r_sem[D]); |
||
217 | draw_tl(d->l.x,d->l.y,g_sem[L]); |
||
218 | draw_tl(d->r.x,d->r.y,g_sem[R]); |
||
219 | sem_post(&mutex); |
||
220 | #endif |
||
221 | } |
||
222 | if((vpos=='r')&&(!flag)) { |
||
223 | if(hpos=='g') { |
||
224 | hpos='y'; |
||
225 | flag=1; |
||
226 | #ifdef GRAPH |
||
227 | sem_wait(&mutex); |
||
228 | draw_tl(d->l.x,d->l.y,y_sem[L]); |
||
229 | draw_tl(d->r.x,d->r.y,y_sem[R]); |
||
230 | sem_post(&mutex); |
||
231 | #endif |
||
232 | } |
||
233 | if((hpos=='y')&&(!flag)) { |
||
234 | hpos='r'; |
||
235 | vpos='g'; |
||
236 | #ifdef GRAPH |
||
237 | sem_wait(&mutex); |
||
238 | draw_tl(d->l.x,d->l.y,r_sem[L]); |
||
239 | draw_tl(d->r.x,d->r.y,r_sem[R]); |
||
240 | draw_tl(d->u.x,d->u.y,g_sem[U]); |
||
241 | draw_tl(d->d.x,d->d.y,g_sem[D]); |
||
242 | sem_post(&mutex); |
||
243 | #endif |
||
244 | } |
||
245 | } |
||
246 | flag=0; |
||
247 | d->vpos=vpos; |
||
248 | d->hpos=hpos; |
||
249 | #ifndef GRAPH |
||
250 | sem_wait(&mutex); |
||
251 | cprintf("semaforo %s: verticale:%c orizz.:%c\n",d->tl_name,vpos,hpos); |
||
252 | sem_post(&mutex); |
||
253 | #endif |
||
254 | task_endcycle(); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | TASK car(car_data *cd) { |
||
259 | char ob_type=FREE; |
||
260 | int start_point,sat_angle=0; |
||
261 | int tl=0; |
||
262 | short stato=NORM,dir_flag=0; |
||
263 | |||
264 | sem_wait(&(kill_mutex[cd->number])); |
||
265 | while(kill_flag[cd->number]){ |
||
266 | cd->running=0; |
||
267 | sem_post(&(kill_mutex[cd->number])); |
||
268 | return 0; |
||
269 | } |
||
270 | sem_post(&(kill_mutex[cd->number])); |
||
271 | while(1) { |
||
272 | sem_wait(&(kill_mutex[cd->number])); |
||
273 | while(kill_flag[cd->number]){ |
||
274 | cd->running=0; |
||
275 | sem_post(&(kill_mutex[cd->number])); |
||
276 | return 0; |
||
277 | } |
||
278 | sem_post(&(kill_mutex[cd->number])); |
||
279 | if(cd->xp>630||cd->yp>590||cd->xp<18||cd->yp<MAPY+3) { |
||
280 | start_point=rand()%S_POINT; |
||
281 | if(start_point==2) |
||
282 | start_point++; |
||
283 | cd->xpos=cd->xp=starting_set_array[start_point].xpos; |
||
284 | cd->ypos=cd->yp=starting_set_array[start_point].ypos; |
||
285 | cd->angle=starting_set_array[start_point].angles; |
||
286 | } |
||
287 | if (!cd->boom) { |
||
288 | // Control State Machine |
||
289 | if(stato==NORM) { |
||
290 | cd->dir=STRAIGHT; |
||
291 | ob_type=collision_sensor(cd); |
||
292 | ch_spd(cd,ob_type); |
||
293 | if(cd->dist_obs<30 && ob_type==STOP) { |
||
294 | stato=INC1; |
||
295 | } |
||
296 | if(ob_type==FREE || cd->dist_obs>=27 || cd->speed>=2.0) { |
||
297 | cd->angle=sensore(cd); |
||
298 | cd->angle=normalize(cd->angle); |
||
299 | } |
||
300 | } |
||
301 | if(stato==INC1) { |
||
302 | tl=find_tl(cd->angle,cd->xp,cd->yp); |
||
303 | if(!dir_flag) |
||
304 | cd->dir=rand()%3-1; |
||
305 | if(find_col(cd->angle,tl)=='g') { |
||
306 | switch(cd->dir) { |
||
307 | case STEER_RIGHT: |
||
308 | stato=DX1; |
||
309 | break; |
||
310 | case STRAIGHT: |
||
311 | case STEER_LEFT: |
||
312 | stato=STR1; |
||
313 | break; |
||
314 | default: |
||
315 | stato=DX1; |
||
316 | } |
||
317 | } else { |
||
318 | cd->speed=0.0; |
||
319 | dir_flag=1; |
||
320 | } |
||
321 | } |
||
322 | if(stato==DX1) { |
||
323 | dir_flag=0; |
||
324 | cd->speed=4.0; |
||
325 | cd->angle=sensore(cd); |
||
326 | if(ob_type!=STOP) |
||
327 | stato=NORM; |
||
328 | else { |
||
329 | ob_type=collision_sensor(cd); |
||
330 | if(ob_type==CAR) |
||
331 | ch_spd(cd,ob_type); |
||
332 | } |
||
333 | } |
||
334 | if(stato==STR1) { |
||
335 | dir_flag=0; |
||
336 | cd->angle=allinea(cd->angle); |
||
337 | sensore(cd); |
||
338 | cd->speed=4.0; |
||
339 | ob_type=collision_sensor(cd); |
||
340 | if(ob_type==CAR) |
||
341 | ch_spd(cd,ob_type); |
||
342 | if(cd->middle==MAX_DIST) { |
||
343 | if(cd->dir==STRAIGHT) |
||
344 | stato=STR2; |
||
345 | else { |
||
346 | sat_angle=cd->angle+90; |
||
347 | stato=SX1; |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | if(stato==STR2) { |
||
352 | cd->speed=4.0; |
||
353 | sensore(cd); |
||
354 | ob_type=collision_sensor(cd); |
||
355 | if(ob_type==CAR) |
||
356 | ch_spd(cd,ob_type); |
||
357 | if(abs(cd->front-cd->rear)<0.1 && ((cd->middle-NORM_DIST)<7.0)) |
||
358 | stato=NORM; |
||
359 | } |
||
360 | if(stato==SX1) { |
||
361 | cd->speed=4.0; |
||
362 | sensore(cd); |
||
363 | ob_type=collision_sensor(cd); |
||
364 | if(ob_type==CAR) |
||
365 | ch_spd(cd,ob_type); |
||
366 | else { |
||
367 | cd->angle+=7; |
||
368 | if(cd->angle>=sat_angle) { |
||
369 | cd->angle=sat_angle; |
||
370 | } |
||
371 | } |
||
372 | if(abs(cd->front-cd->rear)<0.1 && ((cd->middle-NORM_DIST)<7.0)) |
||
373 | stato=NORM; |
||
374 | } |
||
375 | } |
||
376 | if (cd->dist_obs<COLL_DIST && ob_type==CAR) { |
||
377 | cd->boom=1; |
||
378 | cd->speed=0.0; |
||
379 | } |
||
380 | sem_wait(&mutex); |
||
381 | grx_getimage(cd->xp-15,cd->yp-15,cd->xp+14,cd->yp+14,vbuf[cd->number]); |
||
382 | if (cd->boom==0) { |
||
383 | drawCar(cd->xp,cd->yp,cd->angle,DRAW); |
||
384 | } |
||
385 | if (cd->boom==1) { |
||
386 | drawSprites(cd->xp,cd->yp,0,DRAW); |
||
387 | } |
||
388 | sem_post(&mutex); |
||
389 | task_endcycle(); |
||
390 | sem_wait(&mutex); |
||
391 | if (cd->boom==0) { |
||
392 | grx_putimage(cd->xp-15,cd->yp-15,cd->xp+14,cd->yp+14,vbuf[cd->number]); |
||
393 | } else { |
||
394 | drawSprites(cd->xp,cd->yp,0,CANCEL); |
||
395 | } |
||
396 | sem_post(&mutex); |
||
397 | sem_wait(&(kill_mutex[cd->number])); |
||
398 | while(kill_flag[cd->number]){ |
||
399 | cd->running=0; |
||
400 | sem_post(&(kill_mutex[cd->number])); |
||
401 | return 0; |
||
402 | } |
||
403 | sem_post(&(kill_mutex[cd->number])); |
||
404 | cd->xpos+=cd->speed*cos(DEG_TO_RAD(cd->angle)); |
||
405 | cd->ypos-=cd->speed*sin(DEG_TO_RAD(cd->angle)); |
||
406 | cd->xp=my_rint(cd->xpos); |
||
407 | cd->yp=my_rint(cd->ypos); |
||
408 | } |
||
1109 | pj | 409 | } |