Rev 1086 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | ------------ |
||
1123 | pj | 21 | CVS : $Id: soccer.c,v 1.2 2003-01-07 17:10:18 pj Exp $ |
1085 | pj | 22 | |
23 | File: $File$ |
||
1123 | pj | 24 | Revision: $Revision: 1.2 $ |
25 | Last update: $Date: 2003-01-07 17:10:18 $ |
||
1085 | pj | 26 | ------------ |
27 | */ |
||
28 | |||
29 | /* |
||
30 | * Copyright (C) 2000 Merli Andrea and Zucchetti Alessandro |
||
31 | * |
||
32 | * This program is free software; you can redistribute it and/or modify |
||
33 | * it under the terms of the GNU General Public License as published by |
||
34 | * the Free Software Foundation; either version 2 of the License, or |
||
35 | * (at your option) any later version. |
||
36 | * |
||
37 | * This program is distributed in the hope that it will be useful, |
||
38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
40 | * GNU General Public License for more details. |
||
41 | * |
||
42 | * You should have received a copy of the GNU General Public License |
||
43 | * along with this program; if not, write to the Free Software |
||
44 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
45 | * |
||
46 | */ |
||
47 | |||
48 | |||
49 | /*--------------------------------------------------------------*/ |
||
50 | /* */ |
||
51 | /* S.Ha.R.K. SOCCER SIMULATOR 2001 */ |
||
52 | /* */ |
||
53 | /* */ |
||
54 | /*Autori: Merli Andrea */ |
||
55 | /* Zucchetti Alessandro */ |
||
56 | /*--------------------------------------------------------------*/ |
||
57 | |||
58 | #include <kernel/kern.h> |
||
59 | #include <drivers/glib.h> |
||
60 | #include <drivers/keyb.h> |
||
61 | #include <semaphore.h> |
||
62 | #include <stdlib.h> |
||
63 | #include <math.h> |
||
64 | #include <modules/cabs.h> |
||
65 | #include <ll/sys/types.h> |
||
66 | #include "images.h" |
||
67 | #include "position.h" |
||
68 | #include "iniziali.h" |
||
69 | #include "calc.h" |
||
70 | #include "calc2.h" |
||
71 | #include "stadio.h" |
||
72 | |||
73 | struct target{ |
||
74 | int x; |
||
75 | int y; |
||
76 | }; |
||
77 | |||
78 | void draw_calc(int x, int y, int c,int f); /*disegna i calciatori*/ |
||
79 | void draw_ball(int c); /*disegna la palla*/ |
||
80 | void draw_port(int c,int p,int yp,int xp); /*disegna i portieri*/ |
||
81 | void init_calc(); /*carica i task calc*/ |
||
82 | void init_position(int num,int *ox,int *oy); /*inizializza la posizione dei calciatori*/ |
||
83 | void init_calc_area(int i,int *x_min,int *x_max,int *y_min,int *y_max); /*inizializza la zona di campo die calciatori*/ |
||
84 | void get_target(int modo,int i,struct target * obj,int x_max,int x_min,int y_max,int y_min);/*seleziona l'obietivo dei calciatori*/ |
||
85 | void collision_detection(int i);/*gestisce le collisioni tra i calciatori*/ |
||
86 | int get_direction(int x,int y,int x2, int y2); |
||
87 | void init_portiere(); /*carica i task portiere*/ |
||
88 | void init_ball(); /*carica il task ball*/ |
||
89 | void act_goal(); /*carica il task gol*/ |
||
90 | void draw_goal(); /*gestisce il tabellone facendo apparire la scritta GOAL!!*/ |
||
91 | void disegna_campo(); /*disegna il campo di gioco*/ |
||
92 | int yportiere(int y1, int y2); |
||
93 | int xportiere(int x1, int x2); |
||
94 | int goal_behaviour(int f); /*gestisce gli eventi in occasione di un gol*/ |
||
95 | int rigore=0; |
||
96 | float t; |
||
97 | double tick = 1.0; /* system tick = 1 ms */ |
||
98 | int xb=300,yb=300,xob=300,yob=300; /* coordinate globali della palla*/ |
||
99 | struct position pos_calc[MAX_P+2]; |
||
100 | PID pid_calc,pid_calc2,pid_ball,pid_port,pid_gol; |
||
101 | sem_t mutex,mutex2; |
||
102 | CAB cbi[4]; |
||
103 | |||
104 | |||
105 | /*--------------------------------------------------------------*/ |
||
106 | TASK ball(void *arg) |
||
107 | { |
||
108 | float dx=0, dy=0;//,df=0,dg=0; |
||
109 | float acc =0.0; /*accelerazione*/ |
||
110 | float x; /*spostamento*/ |
||
111 | int g=10; /*accelerazione di gravit…*/ |
||
112 | float cattr=0.05; /*coeff di attrito tra palla e campo*/ |
||
113 | int col=1000; |
||
114 | int ij=0; |
||
115 | int r1=0,r2=0; /*gol segnati*/ |
||
116 | char tetaa[15]; |
||
117 | double r; /*angolo in radianti*/ |
||
118 | int flag =0; /*variabile generatrice di eventi*/ |
||
119 | int outy,outx; /*variabile per rilevare il rimbalzo della palla*/ |
||
120 | float vel1=0.0,vel2=0.0; /*variabili di velocita*/ |
||
121 | float tau = 1.0; /*tempo base*/ |
||
122 | char *m; /*modo*/ |
||
123 | char *a; /*angolo*/ |
||
124 | char *s; /*speed*/ |
||
125 | char modo; |
||
126 | char *o; /*azione*/ |
||
127 | char azione; |
||
128 | int teta=0; |
||
129 | char velocita; |
||
130 | char mode; |
||
131 | ij=0; |
||
132 | m = cab_getmes(cbi[0]); |
||
133 | mode = *m; |
||
134 | cab_unget(cbi[0],m); |
||
135 | if(mode==NO_BALL_MODE){ |
||
136 | xb = xob =(XMIN+XMAX)/2; |
||
137 | yb = yob =(YMIN+YMAX)/2; |
||
138 | } |
||
139 | if(mode ==PENALTY_MODE_BLUE){ |
||
140 | |||
141 | xb=700; |
||
142 | yb=(YMIN+YMAX)/2; |
||
143 | } |
||
144 | if(mode == PENALTY_MODE_WHITE){ |
||
145 | xb=100; |
||
146 | yb=(YMIN+YMAX)/2; |
||
147 | } |
||
148 | |||
149 | sprintf(tetaa,"BLUE FC %3d AC WHITE %3d",(r1),(r2)); |
||
150 | grx_text(tetaa,XMIN+270 ,YMENU+60, rgb16(255,255,255), rgb16(0,0,0)); |
||
151 | /***************************************************************************/ |
||
152 | while (1) { //WHILE(1); |
||
153 | m = cab_getmes(cbi[0]); |
||
154 | mode = *m; |
||
155 | cab_unget(cbi[0],m); |
||
156 | /*posiziono la palla sul dischetto del rigore*/ |
||
157 | if(mode ==PENALTY_MODE_BLUE){ |
||
158 | xb=700; |
||
159 | yb=(YMIN+YMAX)/2; |
||
160 | } |
||
161 | if(mode == PENALTY_MODE_WHITE){ |
||
162 | xb=100; |
||
163 | yb=(YMIN+YMAX)/2; |
||
164 | } |
||
165 | /*se rigore=NO_PENALTY si calcolano le coordinate della palla |
||
166 | in base alla velocit… e alla direzione del tiro*/ |
||
167 | if(rigore ==NO_PENALTY){ |
||
168 | a = cab_getmes(cbi[1]); /*direzione*/ |
||
169 | cab_unget(cbi[1],a); |
||
170 | s = cab_getmes(cbi[2]); /*velocit…*/ |
||
171 | velocita = *s; |
||
172 | cab_unget(cbi[2],s); |
||
173 | acc = (float)cattr * g; |
||
174 | r = (double)(36*(*a)/12) * PI / 180.; |
||
175 | if(mode==PASS_MODE){ |
||
176 | vel1= velocita; |
||
177 | } |
||
178 | vel2 = vel1 - acc*tau; |
||
179 | if(vel2>0){ |
||
180 | x = vel1 *tau -0.5*acc*tau*tau; |
||
181 | dx = (float)(x * cos(r)); |
||
182 | dy = (float)(x * sin(r)); |
||
183 | xb += dx; |
||
184 | yb += dy; |
||
185 | ij++; |
||
186 | outx = (xb > XMAX-2) || (xb < XMIN+2); |
||
187 | outy = (yb > YMAX-6) || (yb < YMIN+6); |
||
188 | |||
189 | if (outx || outy) { |
||
190 | if ((yb>=(YMIN+YMAX)/2-50) &&(yb<=(YMIN+YMAX)/2+50)){ |
||
191 | dx=0; |
||
192 | dy=0; |
||
193 | } |
||
194 | xb = xb - dx; |
||
195 | yb = yb - dy; |
||
196 | if (outx) teta = 60 - (*a); |
||
197 | if (outy) teta = 120-(*a); |
||
198 | if (teta > 120) teta -= 120; |
||
199 | if (teta < 0) teta += 120; |
||
200 | *a =(char)teta; |
||
201 | r = (double)(36*(*a)/12) * PI / 180.; |
||
202 | dx = (float)(x * cos(r)); |
||
203 | dy = (float)(x * sin(r)); |
||
204 | if ((yb>=(YMIN+YMAX)/2-50) &&(yb<=(YMIN+YMAX)/2+50)){ |
||
205 | dx=0; |
||
206 | dy=0; |
||
207 | } |
||
208 | xb += dx; |
||
209 | yb += dy; |
||
210 | } |
||
211 | modo = NO_BALL_MODE; |
||
212 | m = cab_reserve(cbi[0]); |
||
213 | *m = modo; |
||
214 | cab_putmes(cbi[0],m); |
||
215 | |||
216 | vel1 = vel2; |
||
217 | } |
||
218 | if (yb< YMIN+5) yb = yb+10; |
||
219 | if(xb< XMIN+5) xb = xb +10; |
||
220 | /*se c'Š un gol*/ |
||
221 | if ((yb>=(YMIN+YMAX)/2-50) &&(yb<=(YMIN+YMAX)/2+50)&&((xb<=XMIN+7)|| |
||
222 | (xb >=XMAX-7))){ |
||
223 | /*aggiorno i risultati*/ |
||
224 | if(xb<XMIN+50) r2++; |
||
225 | else r1++; |
||
226 | /*assegno alle variabili i valori di riposo*/ |
||
227 | yb =(YMAX+YMIN)/2; |
||
228 | xb =(XMIN+XMAX)/2; |
||
229 | teta=0; |
||
230 | velocita=0; |
||
231 | flag =1; |
||
232 | } |
||
233 | /* in base al valore di flag modifico il tabellone*/ |
||
234 | flag = goal_behaviour(flag); |
||
235 | /*scrivo il nuovo risultato nel tabellone*/ |
||
236 | if (flag == 80){ |
||
237 | sprintf(tetaa,"BLUE FC %3d AC WHITE %3d",(r1),(r2)); |
||
238 | grx_text(tetaa,XMIN+270 ,YMENU+60, rgb16(255,255,255), rgb16(0,0,0)); |
||
239 | flag =0; |
||
240 | o = cab_reserve(cbi[3]); |
||
241 | azione = NO_ACT; /*nessuna azione*/ |
||
242 | *o = azione; |
||
243 | cab_putmes(cbi[3],o); |
||
244 | modo = NO_BALL_MODE; |
||
245 | m = cab_reserve(cbi[0]); |
||
246 | *m = modo; |
||
247 | cab_putmes(cbi[0],m); |
||
248 | } |
||
249 | |||
250 | }//fine if(rigore==NO_PENALTY) |
||
251 | draw_ball(0); |
||
252 | draw_ball(col); |
||
253 | xob = xb; yob = yb; |
||
254 | |||
255 | task_endcycle(); |
||
256 | } |
||
257 | |||
258 | } |
||
259 | |||
260 | |||
261 | /*--------------------------------------------------------------*/ |
||
262 | |||
263 | |||
264 | |||
265 | /******************************************************************/ |
||
266 | |||
267 | TASK calc(void *arg){ |
||
268 | |||
269 | struct target obj; |
||
270 | int ox=200, oy=200; |
||
271 | int alpha=0; |
||
272 | int teta; |
||
273 | int col=0; |
||
274 | char speed; |
||
275 | char *s; |
||
276 | char modo; |
||
277 | char *m; |
||
278 | char angolo; |
||
279 | char *a; |
||
280 | int i = (int)arg; |
||
281 | int x_min=0,y_min=0; |
||
282 | int x_max=0,y_max=0; |
||
283 | /*Posiziono i calciatori nel punto iniziale*/ |
||
284 | |||
285 | init_position(i,&ox,&oy); |
||
286 | |||
287 | |||
288 | /*Definisco l'area di gioco del calciatore basandomi sul suo PID*/ |
||
289 | |||
290 | init_calc_area(i,&x_min,&x_max,&y_min,&y_max); |
||
291 | |||
292 | while (1) { |
||
293 | m = cab_getmes(cbi[0]); |
||
294 | modo = *m; |
||
295 | cab_unget(cbi[0],m); |
||
296 | |||
297 | /*Individua l'obiettivo del calciatore a seconda del modo e del */ |
||
298 | /*ruolo in PENALTY_MODE un calciatore va sul pallone gli altri */ |
||
299 | /*ai lati del campo */ |
||
300 | /*in NO_BALL_MODE l'obiettivo Š la palla se essa Š */ |
||
301 | /*nell'area definita dalla funzione init_calc_area(..) */ |
||
302 | /*altrimenti Š la posizione iniziale definita con init_position(.)*/ |
||
303 | |||
304 | |||
305 | get_target(modo,i,&obj,x_max,x_min,y_max,y_min); |
||
306 | |||
307 | /*se raggiungo l'obbiettivo mi fermo*/ |
||
308 | if(obj.x==pos_calc[i].x) pos_calc[i].dx=0; |
||
309 | if(obj.y==pos_calc[i].y) pos_calc[i].dy=0; |
||
310 | |||
311 | /*direzione tra il calciatore e l'obiettivo*/ |
||
312 | |||
313 | alpha= get_direction(pos_calc[i].x,pos_calc[i].y,obj.x,obj.y); |
||
314 | |||
315 | /*direzione di corsa iniziale*/ |
||
316 | |||
317 | pos_calc[i].dy = -VEL*sin((alpha*PI)/180); |
||
318 | pos_calc[i].dx = VEL*cos((alpha*PI)/180); |
||
319 | |||
320 | |||
321 | /*Per evitare che il calciatore corra prima dell'inizio della |
||
322 | partita gli impongo di non muoversi in posizione iniziale*/ |
||
323 | |||
324 | if((obj.x==300)&&(obj.y==300)) { |
||
325 | pos_calc[i].dx=0; |
||
326 | pos_calc[i].dy=0; |
||
327 | } |
||
328 | /*velocità di corsa*/ |
||
329 | |||
330 | pos_calc[i].x+=pos_calc[i].dx; |
||
331 | pos_calc[i].y+=pos_calc[i].dy; |
||
332 | |||
333 | /*verifico se mi trovo in una situazione di collisione con */ |
||
334 | /*un altro calciatore */ |
||
335 | |||
336 | collision_detection(i); |
||
337 | |||
338 | |||
339 | |||
340 | /*se il calciatore arriva sulla palla tira*/ |
||
341 | if((modo==NO_BALL_MODE)||(i==7)||(i==2)) |
||
342 | if(((pos_calc[i].x>=xb-27)&&(pos_calc[i].x<=xb+27))&&((pos_calc[i].y>=yb-27)&&(pos_calc[i].y<=yb+27))) |
||
343 | { |
||
344 | |||
345 | if((i==7)||(i==2)) rigore =NO_PENALTY; |
||
346 | modo = PASS_MODE; |
||
347 | m = cab_reserve(cbi[0]); |
||
348 | *m = modo; |
||
349 | cab_putmes(cbi[0],m); |
||
350 | if(i<5){ |
||
351 | a = cab_reserve(cbi[1]); |
||
352 | teta =(rand()%(90) + 135 ); |
||
353 | if(teta >360) teta -= 360; |
||
354 | if (teta<0) teta += 360; |
||
355 | angolo = (char)(teta/3); |
||
356 | *a = angolo; |
||
357 | cab_putmes(cbi[1],a); |
||
358 | } |
||
359 | if(i>4){ |
||
360 | a = cab_reserve(cbi[1]); |
||
361 | teta =(rand()%(90) -45); |
||
362 | if(teta >360) teta -= 360; |
||
363 | if (teta<0) teta += 360; |
||
364 | teta = (teta/3); |
||
365 | *a = teta;//angolo; |
||
366 | cab_putmes(cbi[1],a); |
||
367 | } |
||
368 | s = cab_reserve(cbi[2]); |
||
369 | speed=16; |
||
370 | *s =speed; |
||
371 | cab_putmes(cbi[2],s); |
||
372 | |||
373 | } |
||
374 | |||
375 | |||
376 | |||
377 | draw_calc(ox, oy, CANC,0); |
||
378 | |||
379 | /*Disegna l'omino voltato nella direzione della palla*/ |
||
380 | if(i<5){ |
||
381 | DIREZ(alpha,col,158,203, RLEFT,pos_calc[i].x,pos_calc[i].y);/*runleft*/ |
||
382 | DIREZ(alpha,col,249,292, RDOWN,pos_calc[i].x,pos_calc[i].y);/*rundown*/ |
||
383 | DIREZ(alpha,col, 68,113, RUP,pos_calc[i].x,pos_calc[i].y);/*runup*/ |
||
384 | DIREZ(alpha,col, 0, 23,RRIGHT,pos_calc[i].x,pos_calc[i].y);/*runright*/ |
||
385 | DIREZ(alpha,col,337,360,RRIGHT,pos_calc[i].x,pos_calc[i].y); |
||
386 | DIREZ(alpha,col, 23, 68, RRUP,pos_calc[i].x,pos_calc[i].y);/*runrightup*/ |
||
387 | DIREZ(alpha,col,113,158, RLUP,pos_calc[i].x,pos_calc[i].y);/*runleftup*/ |
||
388 | DIREZ(alpha,col,203,249, RLDW,pos_calc[i].x,pos_calc[i].y);/*runleftdown*/ |
||
389 | DIREZ(alpha,col,292,337, RRDW,pos_calc[i].x,pos_calc[i].y);/*runrightdown*/ |
||
390 | } |
||
391 | |||
392 | if(i>4){ |
||
393 | |||
394 | DIREZ(alpha,col,158,203, RLEFT2,pos_calc[i].x,pos_calc[i].y);/*runleft*/ |
||
395 | DIREZ(alpha,col,249,292, RDOWN2,pos_calc[i].x,pos_calc[i].y);/*rundown*/ |
||
396 | DIREZ(alpha,col, 68,113, RUP2,pos_calc[i].x,pos_calc[i].y);/*runup*/ |
||
397 | DIREZ(alpha,col, 0, 23,RRIGHT2,pos_calc[i].x,pos_calc[i].y);/*runright*/ |
||
398 | DIREZ(alpha,col,337,360,RRIGHT2,pos_calc[i].x,pos_calc[i].y); |
||
399 | DIREZ(alpha,col, 23, 68, RRUP2,pos_calc[i].x,pos_calc[i].y);/*runrightup*/ |
||
400 | DIREZ(alpha,col,113,158, RLUP2,pos_calc[i].x,pos_calc[i].y);/*runleftup*/ |
||
401 | DIREZ(alpha,col,203,249, RLDW2,pos_calc[i].x,pos_calc[i].y);/*runleftdown*/ |
||
402 | DIREZ(alpha,col,292,337, RRDW2,pos_calc[i].x,pos_calc[i].y);/*runrightdown*/ |
||
403 | } |
||
404 | |||
405 | |||
406 | ox =pos_calc[i].x; oy = pos_calc[i].y; |
||
407 | |||
408 | task_endcycle(); |
||
409 | } |
||
410 | } |
||
411 | /****************************************************************/ |
||
412 | TASK gol(void *arg) |
||
413 | { |
||
414 | while(1){ |
||
415 | draw_goal(); |
||
416 | task_endcycle(); |
||
417 | } |
||
418 | } |
||
419 | /****************************************************************/ |
||
420 | TASK portiere(void *arg) |
||
421 | { |
||
422 | int yp,yp1,yp0,ypo_0=(YMAX+YMIN)/2,ypo_1=(YMAX+YMIN)/2; |
||
423 | int xp0,xp1,xpo_0,xpo_1,xo0,xo1; |
||
424 | int np= (int)arg; |
||
425 | char speed; |
||
426 | char *s; |
||
427 | char modo; |
||
428 | char *m; |
||
429 | char angolo; |
||
430 | char *a; |
||
431 | |||
432 | yp = (YMIN+YMAX)/2; /* y di attesa*/ |
||
433 | yp0 = (YMIN+YMAX)/2; |
||
434 | yp1 = (YMIN+YMAX)/2; |
||
435 | xp0 = xpo_0 = XMIN+3; /* xp ascissa attuale portiere, xpo ascissa vecchia posizione portiere*/ |
||
436 | xp1 = xpo_1 = XMAX-30; |
||
437 | xo0 = XMIN+3; /* x di attesa*/ |
||
438 | xo1 = XMAX-30; /* x di attesa*/ |
||
439 | draw_port(1,np,yp,xo0); |
||
440 | draw_port(1,np,yp,xo1); |
||
441 | |||
442 | while(1){ |
||
443 | m = cab_getmes(cbi[0]); |
||
444 | modo = *m; |
||
445 | cab_unget(cbi[0],m); |
||
446 | /*in base alla posizione della palla si calcola la posizione del portiere*/ |
||
447 | if ((np==0) && (xb < (XMIN+XMAX)/2) &&(xb > XMIN+150)){ |
||
448 | yp0 = yportiere(yb,yp0); |
||
449 | xp0 = xportiere(xo0,xp0); |
||
450 | } |
||
451 | if ((np==0) &&(xb <= XMIN+150)){ |
||
452 | yp0 = yportiere(yb,yp0); |
||
453 | xp0= xportiere(xb,xp0); |
||
454 | } |
||
455 | if ((np==0) && (xb > (XMIN+XMAX)/2)){ |
||
456 | yp0 = yportiere(yp,yp0); |
||
457 | xp0 = xportiere(xo0,xp0); |
||
458 | } |
||
459 | /*il portiere non esce oltre questi limiti*/ |
||
460 | if ((yp0< (YMIN+YMAX)/2-100)|| (yp0>(YMIN+YMAX)/2+100)) |
||
461 | yp0 = ypo_0; |
||
462 | if(xp0> XMIN+150) |
||
463 | xp0 =xpo_0; |
||
464 | |||
465 | pos_calc[10].x = xp0; |
||
466 | pos_calc[10].y = yp0; |
||
467 | |||
468 | /*in base alla posizione della palla si calcola la posizione del portiere*/ |
||
469 | if ((np==1) && (xb > (XMIN+XMAX)/2) && (xb< XMAX-150)){ |
||
470 | yp1 = yportiere(yb,yp1); |
||
471 | xp1 = xportiere(xo1,xp1); |
||
472 | } |
||
473 | if ((np==1) && (xb >= XMAX-150)){ |
||
474 | yp1 = yportiere(yb,yp1); |
||
475 | xp1 = xportiere(xb,xp1); |
||
476 | } |
||
477 | if ((np==1) && (xb < (XMIN+XMAX)/2)){ |
||
478 | yp1 = yportiere(yp,yp1); |
||
479 | xp1 = xportiere(xo1,xp1); |
||
480 | } |
||
481 | /*il portiere non esce oltre questi limiti*/ |
||
482 | if ((yp1< (YMIN+YMAX)/2-100)|| (yp1>(YMIN+YMAX)/2+100)) |
||
483 | yp1 =ypo_1; |
||
484 | if(xp1 < XMAX-150) |
||
485 | xp1 =xpo_1; |
||
486 | |||
487 | pos_calc[11].x = xp1; |
||
488 | pos_calc[11].y = yp1; |
||
489 | |||
490 | /*in caso di rigore il portiere aspetta il tiro sulla linea della porta*/ |
||
491 | if (rigore ==PENALTY) xp0= XMIN+3; |
||
492 | |||
493 | if ((np==0)){ |
||
494 | draw_port(0,np,ypo_0,xpo_0); |
||
495 | draw_port(1,np,yp0,xp0); |
||
496 | ypo_0 = yp0; |
||
497 | xpo_0 =xp0; |
||
498 | } |
||
499 | |||
500 | /*in caso di rigore il portiere aspetta il tiro sulla linea della porta*/ |
||
501 | if(rigore ==PENALTY) xp1= XMAX-30; |
||
502 | |||
503 | if((np==1)){ |
||
504 | draw_port(0,np,ypo_1,xpo_1); |
||
505 | draw_port(1,np,yp1,xp1); |
||
506 | ypo_1 = yp1; |
||
507 | xpo_1 = xp1; |
||
508 | } |
||
509 | |||
510 | /*se para rinvia immediatamente*/ |
||
511 | if( (yp0 >=yb-5) && (yp0<yb+20) && ((xb<=xp0+20)&&(xb>=xp0-30))){ |
||
512 | m = cab_reserve(cbi[0]); |
||
513 | modo = PASS_MODE; |
||
514 | *m = modo; |
||
515 | cab_putmes(cbi[0],m); |
||
516 | a = cab_reserve(cbi[1]); |
||
517 | angolo =0; |
||
518 | *a = angolo; |
||
519 | cab_putmes(cbi[1],a); |
||
520 | s = cab_reserve(cbi[2]); |
||
521 | speed=20; |
||
522 | *s =speed; |
||
523 | cab_putmes(cbi[2],s); |
||
524 | } |
||
525 | /*se para rinvia immediatamente*/ |
||
526 | if((yp1 >=yb-5) && (yp1<yb+20) && (xb<=xp1+20)&&(xb>= xp1-30) ){ |
||
527 | m = cab_reserve(cbi[0]); |
||
528 | modo = PASS_MODE; |
||
529 | *m = modo; |
||
530 | cab_putmes(cbi[0],m); |
||
531 | a = cab_reserve(cbi[1]); |
||
532 | angolo =60; |
||
533 | *a = angolo; |
||
534 | cab_putmes(cbi[1],a); |
||
535 | s = cab_reserve(cbi[2]); |
||
536 | speed=20; |
||
537 | *s =speed; |
||
538 | cab_putmes(cbi[2],s); |
||
539 | } |
||
540 | task_endcycle(); |
||
541 | } |
||
542 | } |
||
543 | |||
544 | /****************************************************************/ |
||
545 | /* This function is called when the system exits */ |
||
546 | void byebye(void *arg) |
||
547 | { |
||
548 | grx_close(); |
||
549 | kern_printf("Bye Bye!\n"); |
||
550 | } |
||
551 | |||
552 | /****************************** MAIN ******************************/ |
||
553 | |||
554 | int main(int argc, char **argv) |
||
555 | { |
||
556 | |||
557 | |||
558 | char c; /* character from keyboard */ |
||
559 | int z=0; |
||
560 | int p=0; |
||
561 | int ij=0; |
||
562 | char modo; |
||
563 | char *m; |
||
564 | char *s; |
||
565 | char speed; |
||
566 | char *a; |
||
567 | char angolo; |
||
568 | char *o; |
||
569 | char azione; |
||
570 | |||
571 | |||
572 | TIME seme; /* used to init the random seed */ |
||
573 | |||
574 | /* Set the closing function */ |
||
575 | sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT); |
||
576 | |||
577 | /* graphic card Initialization */ |
||
578 | if (grx_init() < 1) { |
||
579 | sys_abort(1); |
||
580 | } |
||
581 | |||
582 | if (grx_open(ORI_RES,VER_RES,COL_DEP) < 0) { |
||
583 | kern_printf("GRX Err\n"); |
||
584 | sys_abort(1); |
||
585 | } |
||
586 | kern_printf("Video card ok!\n"); |
||
587 | cbi[1] = cab_create("direzione",10,15); |
||
588 | cbi[0] = cab_create("modo",4,15); |
||
589 | cbi[2] = cab_create("velocita",10,15); |
||
590 | cbi[3] = cab_create("azione",3,3); |
||
591 | m = cab_reserve(cbi[0]); |
||
592 | modo = NO_BALL_MODE; |
||
593 | *m = modo; |
||
594 | cab_putmes(cbi[0],m); |
||
595 | a = cab_reserve(cbi[1]); |
||
596 | angolo =0; |
||
597 | *a = angolo; |
||
598 | cab_putmes(cbi[1],a); |
||
599 | s = cab_reserve(cbi[2]); |
||
600 | speed=0; |
||
601 | *s =speed; |
||
602 | cab_putmes(cbi[2],s); |
||
603 | o = cab_reserve(cbi[3]); |
||
604 | azione = NO_ACT; |
||
605 | *o = azione; |
||
606 | cab_putmes(cbi[3],o); |
||
607 | |||
608 | /* The scenario */ |
||
609 | |||
610 | grx_box(0,0,ORI_RES,VER_RES,0); |
||
611 | disegna_campo(); |
||
612 | for(p=0;p<scale_width;p++) |
||
613 | for(z=0;z<scale_height;z++){ |
||
614 | grx_plot(100+p,0+z,rgb16(stadio_cmap[(unsigned char)scale_data[z*150+p]][0],stadio_cmap[(unsigned char)scale_data[z*150+p]][1],stadio_cmap[(unsigned char)scale_data[z*150+p]][2])); |
||
615 | grx_plot(550+p,0+z,rgb16(stadio_cmap[(unsigned char)scale_data[z*150+p]][0],stadio_cmap[(unsigned char)scale_data[z*150+p]][1],stadio_cmap[(unsigned char)scale_data[z*150+p]][2])); |
||
616 | } |
||
617 | for(p=0;p<faro_width;p++) |
||
618 | for(z=0;z<faro_height;z++){ |
||
619 | grx_plot(0+p,0+z,rgb16(stadio_cmap[(unsigned char)faros_data[z*100+p]][0],stadio_cmap[(unsigned char)faros_data[z*100+p]][1],stadio_cmap[(unsigned char)faros_data[z*100+p]][2])); |
||
620 | grx_plot(700+p,0+z,rgb16(stadio_cmap[(unsigned char)farod_data[z*100+p]][0],stadio_cmap[(unsigned char)farod_data[z*100+p]][1],stadio_cmap[(unsigned char)farod_data[z*100+p]][2])); |
||
621 | } |
||
622 | for(p=0;p<schermo_width;p++) |
||
623 | for(z=0;z<schermo_height;z++){ |
||
624 | grx_plot(250+p,0+z,rgb16(stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][0],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][1],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][2])); |
||
625 | } |
||
626 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
627 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
628 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
629 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
630 | /* The program waits a space to create a calc */ |
||
631 | c = keyb_getch(BLOCK); |
||
632 | |||
633 | /* randomize!!!!*/ |
||
634 | seme = sys_gettime(NULL); |
||
635 | srand(seme); |
||
636 | |||
637 | do { |
||
638 | if ((c == ' ') &&(ij==0)) { |
||
639 | init_ball(); |
||
640 | init_calc(); |
||
641 | init_portiere(); |
||
642 | ij++; |
||
643 | } |
||
644 | if ((c== 'w')){m = cab_reserve(cbi[0]); |
||
645 | modo = PENALTY_MODE_WHITE; |
||
646 | *m = modo; |
||
647 | cab_putmes(cbi[0],m); |
||
648 | rigore =PENALTY; } |
||
649 | if ((c== 'e')){m = cab_reserve(cbi[0]); |
||
650 | modo = PENALTY_MODE_BLUE; |
||
651 | *m = modo; |
||
652 | cab_putmes(cbi[0],m); |
||
653 | rigore =PENALTY; } |
||
654 | if ((c == 'k')) { |
||
655 | group_kill(GROUP_PLAY); |
||
656 | group_kill(GROUP_SOFT); |
||
657 | group_kill(GROUP_BALL); |
||
658 | ij=0; |
||
659 | m = cab_reserve(cbi[0]); |
||
660 | modo = NO_BALL_MODE; |
||
661 | *m = modo; |
||
662 | cab_putmes(cbi[0],m); |
||
663 | disegna_campo(); |
||
664 | for(p=0;p<scale_width;p++) |
||
665 | for(z=0;z<scale_height;z++){ |
||
666 | grx_plot(100+p,0+z,rgb16(stadio_cmap[(unsigned char)scale_data[z*150+p]][0],stadio_cmap[(unsigned char)scale_data[z*150+p]][1],stadio_cmap[(unsigned char)scale_data[z*150+p]][2])); |
||
667 | grx_plot(550+p,0+z,rgb16(stadio_cmap[(unsigned char)scale_data[z*150+p]][0],stadio_cmap[(unsigned char)scale_data[z*150+p]][1],stadio_cmap[(unsigned char)scale_data[z*150+p]][2])); |
||
668 | } |
||
669 | for(p=0;p<faro_width;p++) |
||
670 | for(z=0;z<faro_height;z++){ |
||
671 | grx_plot(0+p,0+z,rgb16(stadio_cmap[(unsigned char)faros_data[z*100+p]][0],stadio_cmap[(unsigned char)faros_data[z*100+p]][1],stadio_cmap[(unsigned char)faros_data[z*100+p]][2])); |
||
672 | grx_plot(700+p,0+z,rgb16(stadio_cmap[(unsigned char)farod_data[z*100+p]][0],stadio_cmap[(unsigned char)farod_data[z*100+p]][1],stadio_cmap[(unsigned char)farod_data[z*100+p]][2])); |
||
673 | } |
||
674 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
675 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
676 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
677 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
678 | xb=(int)(XMAX-XMIN)/2; |
||
679 | yb=(int)(YMIN+YMAX)/2; |
||
680 | } |
||
681 | c = keyb_getch(BLOCK); |
||
682 | } while (c != ESC); |
||
683 | |||
684 | |||
685 | |||
686 | sys_end(); |
||
687 | |||
688 | return 0; |
||
689 | } |
||
690 | |||
691 | /*--------------------------------------------------------------*/ |
||
692 | void draw_calc(int x, int y, int c,int f) |
||
693 | { |
||
694 | int i,j;/*needed in the macro*/ |
||
695 | sem_wait(&mutex); |
||
696 | if(c==CANC){ |
||
697 | |||
698 | grx_box(x, y,x+27,y+27,SFONDO); |
||
699 | if((x>(XMIN+XMAX)/2-27) && (x<(XMIN+XMAX)/2+27)){ |
||
700 | grx_line(XMIN+1,YMIN+1,XMAX-1,YMIN+1,rgb16(255,255,255)); |
||
701 | grx_line(XMIN+1,YMAX-1,XMAX-1,YMAX-1,rgb16(255,255,255)); |
||
702 | grx_line(XMIN+1,YMIN+1,XMIN+1,(YMIN+YMAX)/2-50,rgb16(255,255,255)); |
||
703 | grx_line(XMIN+1,(YMIN+YMAX)/2+50,XMIN+1,YMAX-1,rgb16(255,255,255)); |
||
704 | grx_line(XMAX-1,YMIN+1,XMAX-1,(YMIN+YMAX)/2-50,rgb16(255,255,255)); |
||
705 | grx_line(XMAX-1,(YMIN+YMAX)/2+50,XMAX-1,YMAX-1,rgb16(255,255,255)); |
||
706 | grx_line(((XMIN+XMAX)/2),YMIN,((XMIN+XMAX)/2),YMAX-1,rgb16(255,255,255)); |
||
707 | grx_circle((XMIN+XMAX)/2,(YMIN+YMAX)/2,54,rgb16(255,255,255)); |
||
708 | grx_line(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
709 | grx_line((XMIN+XMAX)/5,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
710 | grx_line(XMIN,3*(YMIN+YMAX)/4-30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
711 | grx_line(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
712 | grx_line((XMIN+XMAX)/10,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
713 | grx_line(XMIN,(YMIN+YMAX)/2+70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
714 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
715 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
716 | grx_line(4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
717 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
718 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
719 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
720 | grx_line(XMIN+1,(YMIN+YMAX)/2-50,XMIN+1,(YMIN+YMAX)/2+50,rgb16(100,100,100)); |
||
721 | grx_line(XMAX-1,(YMIN+YMAX)/2-50,XMAX-1,(YMIN+YMAX)/2+50,rgb16(100,100,100)); |
||
722 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
723 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
724 | } |
||
725 | } |
||
726 | else |
||
727 | if(c==RLEFT) |
||
728 | DIRDRAW(x,y,((unsigned char)runup_data[i*27+j])); |
||
729 | if(c==RLEFT+1) |
||
730 | DIRDRAW(x,y,((unsigned char)runup2_data[i*27+j])); |
||
731 | if(c==RDOWN) |
||
732 | DIRDRAW(x,y,((unsigned char)rundes2_data[i*27+j])); |
||
733 | if(c==RDOWN+1) |
||
734 | DIRDRAW(x,y,((unsigned char)rundes_data[i*27+j])); |
||
735 | if(c==RUP) |
||
736 | DIRDRAW(x,y,((unsigned char)runleft2_data[i*27+j])); |
||
737 | if(c==RUP+1) |
||
738 | DIRDRAW(x,y,((unsigned char)runleft_data[i*27+j])); |
||
739 | if(c==RRIGHT) |
||
740 | DIRDRAW(x,y,((unsigned char)rundown2_data[i*27+j])); |
||
741 | if(c==RRIGHT+1) |
||
742 | DIRDRAW(x,y,((unsigned char)rundown_data[i*27+j])); |
||
743 | if(c==RRUP) |
||
744 | DIRDRAW(x,y,((unsigned char)runld2_data[i*27+j])); |
||
745 | if(c==RRUP+1) |
||
746 | DIRDRAW(x,y,((unsigned char)runld_data[i*27+j])); |
||
747 | if(c==RLUP) |
||
748 | DIRDRAW(x,y,((unsigned char)runlu2_data[i*27+j])); |
||
749 | if(c==RLUP+1) |
||
750 | DIRDRAW(x,y,((unsigned char)runlu_data[i*27+j])); |
||
751 | if(c==RLDW) |
||
752 | DIRDRAW(x,y,((unsigned char)runru2_data[i*27+j])); |
||
753 | if(c==RLDW+1) |
||
754 | DIRDRAW(x,y,((unsigned char)runru_data[i*27+j])); |
||
755 | if(c==RRDW) |
||
756 | DIRDRAW(x,y,((unsigned char)runrd2_data[i*27+j])); |
||
757 | if(c==RRDW+1) |
||
758 | DIRDRAW(x,y,((unsigned char)runrd_data[i*27+j])); |
||
759 | /**********************************************************************/ |
||
760 | if(c==RLEFT2) |
||
761 | DIRDRAW(x,y,((unsigned char)B_runup_data[i*27+j])); |
||
762 | if(c==RLEFT2+1) |
||
763 | DIRDRAW(x,y,((unsigned char)B_runup2_data[i*27+j])); |
||
764 | if(c==RDOWN2) |
||
765 | DIRDRAW(x,y,((unsigned char)B_rundes2_data[i*27+j])); |
||
766 | if(c==RDOWN2+1) |
||
767 | DIRDRAW(x,y,((unsigned char)B_rundes_data[i*27+j])); |
||
768 | if(c==RUP2) |
||
769 | DIRDRAW(x,y,((unsigned char)B_runleft2_data[i*27+j])); |
||
770 | if(c==RUP2+1) |
||
771 | DIRDRAW(x,y,((unsigned char)B_runleft_data[i*27+j])); |
||
772 | if(c==RRIGHT2) |
||
773 | DIRDRAW(x,y,((unsigned char)B_rundown2_data[i*27+j])); |
||
774 | if(c==RRIGHT2+1) |
||
775 | DIRDRAW(x,y,((unsigned char)B_rundown_data[i*27+j])); |
||
776 | if(c==RRUP2) |
||
777 | DIRDRAW(x,y,((unsigned char)B_runld2_data[i*27+j])); |
||
778 | if(c==RRUP2+1) |
||
779 | DIRDRAW(x,y,((unsigned char)B_runld_data[i*27+j])); |
||
780 | if(c==RLUP2) |
||
781 | DIRDRAW(x,y,((unsigned char)B_runlu2_data[i*27+j])); |
||
782 | if(c==RLUP2+1) |
||
783 | DIRDRAW(x,y,((unsigned char)B_runlu_data[i*27+j])); |
||
784 | if(c==RLDW2) |
||
785 | DIRDRAW(x,y,((unsigned char)B_runru2_data[i*27+j])); |
||
786 | if(c==RLDW2+1) |
||
787 | DIRDRAW(x,y,((unsigned char)B_runru_data[i*27+j])); |
||
788 | if(c==RRDW2) |
||
789 | DIRDRAW(x,y,((unsigned char)B_runrd2_data[i*27+j])); |
||
790 | if(c==RRDW2+1) |
||
791 | DIRDRAW(x,y,((unsigned char)B_runrd_data[i*27+j])); |
||
792 | sem_post(&mutex); |
||
793 | } |
||
794 | /*--------------------------------------------------------------*/ |
||
795 | void draw_ball(int c) |
||
796 | { |
||
797 | sem_wait(&mutex); |
||
798 | if(c==0) |
||
799 | grx_box(xob, yob,xob+5,yob+5,SFONDO); |
||
800 | else grx_putimage(xb, yb,xb+5,yb+5,pallone); |
||
801 | sem_post(&mutex); |
||
802 | } |
||
803 | void draw_port(int c,int p,int yp,int xp) |
||
804 | { |
||
805 | int i,j; |
||
806 | sem_wait(&mutex); |
||
807 | if (c==0){ |
||
808 | if(p==0){ |
||
809 | /*ridisegna l'area*/ |
||
810 | grx_box(xp,yp-15,xp+30,yp+25,SFONDO); |
||
811 | grx_line(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
812 | grx_line((XMIN+XMAX)/5,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
813 | grx_line(XMIN,3*(YMIN+YMAX)/4-30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
814 | grx_line(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
815 | grx_line((XMIN+XMAX)/10,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
816 | grx_line(XMIN,(YMIN+YMAX)/2+70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
817 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
818 | } |
||
819 | if(p==1){ |
||
820 | /*ridisegna l'area*/ |
||
821 | grx_box(xp,yp-15,xp+27,yp+25,SFONDO); |
||
822 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
823 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
824 | grx_line(4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
825 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
826 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
827 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
828 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
829 | } |
||
830 | } |
||
831 | else{ |
||
832 | if(p==0) |
||
833 | DIRDRAW(xp,yp,((unsigned char)portsx_data[i*27+j])); |
||
834 | if(p==1) |
||
835 | DIRDRAW(xp,yp,((unsigned char)portdx_data[i*27+j])); |
||
836 | } |
||
837 | sem_post(&mutex); |
||
838 | } |
||
839 | void init_ball(){ |
||
840 | HARD_TASK_MODEL m_ball; |
||
841 | hard_task_default_model(m_ball); |
||
842 | hard_task_def_ctrl_jet (m_ball); |
||
843 | hard_task_def_arg (m_ball, (void *)0); |
||
844 | hard_task_def_wcet (m_ball, BALL_WCET); |
||
845 | hard_task_def_mit (m_ball, PERIOD_BALL); |
||
846 | hard_task_def_group (m_ball, GROUP_BALL); |
||
847 | hard_task_def_usemath (m_ball); |
||
848 | pid_ball = task_create("ball", ball, &m_ball, NULL); |
||
849 | if (pid_ball == NIL) { |
||
850 | grx_close(); |
||
851 | perror("Could not create task <ball>"); |
||
852 | sys_abort(1); |
||
853 | } |
||
854 | |||
855 | task_activate(pid_ball); |
||
856 | } |
||
857 | void init_calc(){ |
||
858 | HARD_TASK_MODEL m_calc; |
||
859 | int i; |
||
860 | disegna_campo(); |
||
861 | for(i=0;i<MAX_P;i++){ |
||
862 | hard_task_default_model(m_calc); |
||
863 | hard_task_def_ctrl_jet (m_calc); |
||
864 | hard_task_def_arg (m_calc, (void *)i); |
||
865 | hard_task_def_wcet (m_calc, CALC_WCET); |
||
866 | hard_task_def_mit (m_calc, PERIOD_CALC); |
||
867 | hard_task_def_group (m_calc, GROUP_PLAY); |
||
868 | hard_task_def_usemath (m_calc); |
||
869 | pid_calc = task_create("calc", calc, &m_calc, NULL); |
||
870 | if (pid_calc == NIL) { |
||
871 | grx_close(); |
||
872 | perror("Could not create task <calc>"); |
||
873 | sys_abort(1); |
||
874 | } |
||
875 | task_activate(pid_calc); |
||
876 | } |
||
877 | } |
||
878 | void act_goal(){ |
||
879 | |||
880 | |||
881 | SOFT_TASK_MODEL m_goal; |
||
882 | soft_task_default_model(m_goal); |
||
883 | soft_task_def_arg (m_goal,(void*)0); |
||
884 | soft_task_def_met (m_goal, 1000); |
||
885 | soft_task_def_level (m_goal,1); |
||
886 | soft_task_def_ctrl_jet (m_goal); |
||
887 | soft_task_def_period (m_goal, 1200000); |
||
888 | soft_task_def_group (m_goal, GROUP_SOFT); |
||
889 | soft_task_def_usemath (m_goal); |
||
890 | soft_task_def_aperiodic(m_goal); |
||
891 | |||
892 | pid_gol = task_create("gol", gol, &m_goal, NULL); |
||
893 | if (pid_gol == NIL) { |
||
894 | grx_close(); |
||
895 | perror("Could not create task <port>"); |
||
896 | sys_abort(1); |
||
897 | } |
||
898 | task_activate(pid_gol); |
||
899 | } |
||
900 | void init_portiere(){ |
||
901 | HARD_TASK_MODEL m_port; |
||
902 | int g; |
||
903 | for(g=0;g<2;g++){ |
||
904 | hard_task_default_model(m_port); |
||
905 | hard_task_def_arg (m_port, (void *)g); |
||
906 | hard_task_def_wcet (m_port, PORT_WCET); |
||
907 | hard_task_def_mit (m_port, PERIOD_PORT); |
||
908 | hard_task_def_group (m_port, GROUP_SOFT); |
||
909 | hard_task_def_usemath (m_port); |
||
910 | pid_port = task_create("portiere", portiere, &m_port, NULL); |
||
911 | if (pid_port == NIL) { |
||
912 | grx_close(); |
||
913 | perror("Could not create task <port>"); |
||
914 | sys_abort(1); |
||
915 | } |
||
916 | task_activate(pid_port); |
||
917 | } |
||
918 | |||
919 | } |
||
920 | void draw_goal(){ |
||
921 | int p,z; |
||
922 | char *m; |
||
923 | char modo; |
||
924 | char *o; |
||
925 | char azione; |
||
926 | m = cab_reserve(cbi[0]); |
||
927 | modo = NO_BALL_MODE; |
||
928 | *m = modo; |
||
929 | cab_putmes(cbi[0],m); |
||
930 | o = cab_getmes(cbi[3]); |
||
931 | azione = *o; |
||
932 | cab_unget(cbi[3],o); |
||
933 | |||
934 | /*scritta GOAL!!*/ |
||
935 | if(azione == GOAL_ACT){ |
||
936 | for(p=0;p<schermo_width;p++) |
||
937 | for(z=0;z<schermo_height;z++){ |
||
938 | grx_plot(250+p,0+z,rgb16(stadio_cmap[(unsigned char)goal1_data[z*schermo_width+p]][0],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][1],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][2])); |
||
939 | } |
||
940 | o = cab_reserve(cbi[3]); |
||
941 | azione = 0; |
||
942 | *o = azione; |
||
943 | cab_putmes(cbi[3],o); |
||
944 | } |
||
945 | |||
946 | /*risistema il menu sul tabellone*/ |
||
947 | if (azione == MENU_ACT){ |
||
948 | for(p=0;p<schermo_width;p++) |
||
949 | for(z=0;z<schermo_height;z++){ |
||
950 | grx_plot(250+p,0+z,rgb16(stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][0],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][1],stadio_cmap[(unsigned char)schermo_data[z*schermo_width+p]][2])); |
||
951 | } |
||
952 | o = cab_reserve(cbi[3]); |
||
953 | azione = NO_ACT; |
||
954 | *o = azione; |
||
955 | cab_putmes(cbi[3],o); |
||
956 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
957 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
958 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
959 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
960 | } |
||
961 | } |
||
962 | |||
963 | /*calcola la y del portiere in base alla yi in ingresso*/ |
||
964 | int yportiere(int yi,int yf){ |
||
965 | int speedy =3; |
||
966 | int dy=0; |
||
967 | int yp; |
||
968 | /*Se sto per raggiungere la palla rallento*/ |
||
969 | if(abs(yi-yf)<VEL) speedy=1; |
||
970 | /*se raggiungo la palla mi fermo*/ |
||
971 | if(yi==yf)dy=0; |
||
972 | /*se palla in basso*/ |
||
973 | if(yi>yf) dy= speedy; |
||
974 | /*se palla in alto*/ |
||
975 | if(yi<yf) dy=-speedy; |
||
976 | yp =yf+dy; |
||
977 | return(yp); |
||
978 | } |
||
979 | /*calcola la x del portiere in base alla xi in ingresso*/ |
||
980 | int xportiere(int xi, int xf){ |
||
981 | int speedx=3; |
||
982 | int dx=0; |
||
983 | int xp; |
||
984 | if(abs(xi-xf)<VEL) speedx=1; |
||
985 | if(xi==xf)dx=0; |
||
986 | if(xi>xf) dx= speedx; |
||
987 | if(xi<xf) dx=-speedx; |
||
988 | xp = xf +dx; |
||
989 | return(xp); |
||
990 | } |
||
991 | /*disegna il campo di gioco*/ |
||
992 | void disegna_campo(){ |
||
993 | //grx_box(0,0,ORI_RES,VER_RES,0); |
||
994 | grx_box(XMIN, YMIN, XMAX, YMAX, SFONDO); |
||
995 | grx_rect(XMIN+1, YMIN+1, XMAX-1, YMAX-1, rgb16(255,255,255)); |
||
996 | grx_line(((XMIN+XMAX)/2),YMIN,((XMIN+XMAX)/2),YMAX-1,rgb16(255,255,255)); |
||
997 | grx_circle((XMIN+XMAX)/2,(YMIN+YMAX)/2,54,rgb16(255,255,255)); |
||
998 | grx_rect(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
999 | grx_rect(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
1000 | grx_rect(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
1001 | grx_rect(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
1002 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
1003 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
1004 | } |
||
1005 | void init_position(int num,int *ox,int *oy) |
||
1006 | { |
||
1007 | if(num<5){ |
||
1008 | pos_calc[num].x = *ox = initial_calc_position[num].x+35; |
||
1009 | pos_calc[num].y = *oy = initial_calc_position[num].y; |
||
1010 | } |
||
1011 | if(num==2){ |
||
1012 | pos_calc[num].x = *ox = initial_calc_position[num].x+100; |
||
1013 | pos_calc[num].y = *oy = initial_calc_position[num].y; |
||
1014 | } |
||
1015 | if(num>4){ |
||
1016 | pos_calc[num].x = *ox = initial_calc_position[num-5].x; |
||
1017 | pos_calc[num].y = *oy = initial_calc_position[num-5].y+35; |
||
1018 | } |
||
1019 | if(num==7){ |
||
1020 | pos_calc[num].x = *ox = initial_calc_position[num-5].x-100; |
||
1021 | pos_calc[num].y = *oy = initial_calc_position[num-5].y; |
||
1022 | } |
||
1023 | |||
1024 | } |
||
1025 | void init_calc_area(int num,int *xmin,int *xmax,int *ymin,int *ymax){ |
||
1026 | |||
1027 | |||
1028 | switch(num) |
||
1029 | { |
||
1030 | case 0: /*attaccante bianchi*/ |
||
1031 | case 5: /*difensore blu*/ |
||
1032 | { |
||
1033 | *xmin = XMIN; |
||
1034 | *ymin = YMIN; |
||
1035 | *xmax = 255; |
||
1036 | *ymax = 305; |
||
1037 | } |
||
1038 | break; |
||
1039 | case 1: /*attaccante bianchi*/ |
||
1040 | case 6: /*difensore blu*/ |
||
1041 | { |
||
1042 | *xmin = XMIN; |
||
1043 | *ymin = 295; |
||
1044 | *xmax = 255; |
||
1045 | *ymax = YMAX; |
||
1046 | } |
||
1047 | break; |
||
1048 | case 2: /*centrocampisti*/ |
||
1049 | case 7: |
||
1050 | { |
||
1051 | *xmin = 255; |
||
1052 | *ymin = YMIN; |
||
1053 | *xmax = 525; |
||
1054 | *ymax = YMAX; |
||
1055 | } break; |
||
1056 | case 3: /*attaccante blu*/ |
||
1057 | case 8: /*difensore bianco*/ |
||
1058 | { |
||
1059 | *xmin = 525; |
||
1060 | *ymin = YMIN; |
||
1061 | *xmax = XMAX; |
||
1062 | *ymax = 305; |
||
1063 | } |
||
1064 | break; |
||
1065 | case 4: /*attaccante blu*/ |
||
1066 | case 9: /*difensore bianco*/ |
||
1067 | { |
||
1068 | *xmin = 505; |
||
1069 | *ymin = 305; |
||
1070 | *xmax = XMAX; |
||
1071 | *ymax = YMAX; |
||
1072 | } |
||
1073 | break; |
||
1074 | default: |
||
1075 | break; |
||
1076 | } |
||
1077 | } |
||
1078 | void get_target(int modo,int i,struct target * obj,int x_max,int x_min,int y_max,int y_min){ |
||
1079 | |||
1080 | |||
1081 | |||
1082 | if((modo==PENALTY_MODE_WHITE)||(modo==PENALTY_MODE_BLUE)){ |
||
1083 | |||
1084 | |||
1085 | |||
1086 | if((i<5)){ |
||
1087 | obj->x = (XMAX)-100-50*i; |
||
1088 | if(pos_calc[i].y>(YMAX+YMIN)/2) |
||
1089 | obj->y = YMAX-60; |
||
1090 | if(pos_calc[i].y<(YMAX+YMIN)/2) |
||
1091 | obj->y = YMIN+60; |
||
1092 | if(modo==PENALTY_MODE_WHITE){ |
||
1093 | if((i==2)){ |
||
1094 | obj->x = xb; |
||
1095 | obj->y = yb; |
||
1096 | } |
||
1097 | } |
||
1098 | } |
||
1099 | if((i>4)){ |
||
1100 | obj->x = (XMIN)+100+50*i; |
||
1101 | if(pos_calc[i].y>(YMAX+YMIN)/2) |
||
1102 | obj->y = YMAX-60; |
||
1103 | if(pos_calc[i].y<(YMAX+YMIN)/2) |
||
1104 | obj->y = YMIN+60; |
||
1105 | if(modo==PENALTY_MODE_BLUE){ |
||
1106 | if((i==7)){ |
||
1107 | obj->x = xb; |
||
1108 | obj->y = yb; |
||
1109 | } |
||
1110 | } |
||
1111 | |||
1112 | } |
||
1113 | } |
||
1114 | |||
1115 | if(modo==NO_BALL_MODE) |
||
1116 | { |
||
1117 | |||
1118 | /*Valutazione se la palla sia in una zona di interesse per il */ |
||
1119 | /*calciatore */ |
||
1120 | |||
1121 | if((xb>x_max)||(xb<x_min)||(yb>y_max)||(yb<y_min)) |
||
1122 | { |
||
1123 | if(i<5) |
||
1124 | { |
||
1125 | obj->x=initial_calc_position[i].x; |
||
1126 | obj->y=initial_calc_position[i].y; |
||
1127 | } |
||
1128 | if(i>4) |
||
1129 | { |
||
1130 | obj->x=initial_calc_position[i-5].x; |
||
1131 | obj->y=initial_calc_position[i-5].y; |
||
1132 | } |
||
1133 | } |
||
1134 | else |
||
1135 | { |
||
1136 | obj->x=xb; |
||
1137 | obj->y=yb; |
||
1138 | } |
||
1139 | } |
||
1140 | |||
1141 | |||
1142 | |||
1143 | |||
1144 | } |
||
1145 | int get_direction(int x,int y,int x2,int y2){ |
||
1146 | |||
1147 | int ang=0; |
||
1148 | if((abs(x-x2) !=0)){ |
||
1149 | if((x>x2)&&(y<=y2)) |
||
1150 | ang = 180+ |
||
1151 | atan((float)abs(y-y2)/ |
||
1152 | (float)abs(x-x2))* |
||
1153 | 180/PI; |
||
1154 | if((x<x2)&&(y<y2)) |
||
1155 | ang = 360- |
||
1156 | atan((float)abs(y-y2)/ |
||
1157 | (float)abs(x-x2))* |
||
1158 | 180/PI; |
||
1159 | if((x<x2)&&(y>y2)) |
||
1160 | ang = |
||
1161 | atan((float)abs(y-y2)/ |
||
1162 | (float)abs(x-x2))* |
||
1163 | 180/PI; |
||
1164 | if((x>x2)&&(y>y2)) |
||
1165 | ang = 180- |
||
1166 | atan((float)abs(y-y2)/ |
||
1167 | (float)abs(x-x2))* |
||
1168 | 180/PI; |
||
1169 | } |
||
1170 | |||
1171 | return ang; |
||
1172 | |||
1173 | } |
||
1174 | |||
1175 | void collision_detection(int i){ |
||
1176 | |||
1177 | int j; |
||
1178 | |||
1179 | |||
1180 | for(j=0;j<MAX_P+2;j++){ |
||
1181 | |||
1182 | if(i!=j){ |
||
1183 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x-27 |
||
1184 | && pos_calc[i].y<pos_calc[j].y && pos_calc[i].y>pos_calc[j].y-27) |
||
1185 | { |
||
1186 | pos_calc[i].y=pos_calc[j].y-27; |
||
1187 | } |
||
1188 | if(pos_calc[i].x<pos_calc[j].x && pos_calc[i].x>pos_calc[j].x-27 |
||
1189 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y-27) |
||
1190 | { |
||
1191 | pos_calc[i].x=pos_calc[j].x-27; |
||
1192 | } |
||
1193 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x-27 |
||
1194 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y) |
||
1195 | { |
||
1196 | pos_calc[i].y=pos_calc[j].y+27; |
||
1197 | } |
||
1198 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x |
||
1199 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y) |
||
1200 | { |
||
1201 | pos_calc[i].x=pos_calc[j].x+27; |
||
1202 | } |
||
1203 | } |
||
1204 | } |
||
1205 | |||
1206 | /*Per impedire un calciatore esca dal campo*/ |
||
1207 | |||
1208 | if(pos_calc[i].x>=XMAX-(pos_calc[i].dx+1)) pos_calc[i].x-=(pos_calc[i].dx+1); |
||
1209 | if(pos_calc[i].x<=XMIN+(pos_calc[i].dx+1)) pos_calc[i].x+=(pos_calc[i].dx+1); |
||
1210 | if(pos_calc[i].y>=YMAX-(pos_calc[i].dy+1)) pos_calc[i].y-=(pos_calc[i].dy+1); |
||
1211 | if(pos_calc[i].y<=YMIN+(pos_calc[i].dy+1)) pos_calc[i].y+=(pos_calc[i].dy+1); |
||
1212 | } |
||
1213 | |||
1214 | int goal_behaviour(int flag){ |
||
1215 | char *o; |
||
1216 | char azione; |
||
1217 | |||
1218 | if (flag !=0) { |
||
1219 | flag++; |
||
1220 | yb =(YMAX+YMIN)/2; |
||
1221 | xb =(XMIN+XMAX)/2; |
||
1222 | } |
||
1223 | if (flag==20){ |
||
1224 | yb =(YMAX+YMIN)/2; |
||
1225 | xb =(XMIN+XMAX)/2; |
||
1226 | /*seleziono l'azione da compiere sul tabellone*/ |
||
1227 | o = cab_reserve(cbi[3]); |
||
1228 | azione = GOAL_ACT; /*scritta GOAL!!*/ |
||
1229 | *o = azione; |
||
1230 | cab_putmes(cbi[3],o); |
||
1231 | /*carico il task gol*/ |
||
1232 | act_goal(); |
||
1233 | } |
||
1234 | if (flag==65){ |
||
1235 | o = cab_reserve(cbi[3]); |
||
1236 | azione = MENU_ACT; /*sistema il menu su tabellone*/ |
||
1237 | *o = azione; |
||
1238 | cab_putmes(cbi[3],o); |
||
1239 | /*carico il task gol*/ |
||
1240 | act_goal(); |
||
1241 | } |
||
1242 | |||
1243 | return(flag); |
||
1244 | } |
||
1245 |