Details | 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 | ------------ |
||
21 | CVS : $Id: soccer.c,v 1.1.1.1 2002-09-02 09:37:44 pj Exp $ |
||
22 | |||
23 | File: $File$ |
||
24 | Revision: $Revision: 1.1.1.1 $ |
||
25 | Last update: $Date: 2002-09-02 09:37:44 $ |
||
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 exception handler */ |
||
575 | set_exchandler_grx(); |
||
576 | |||
577 | /* Set the closing function */ |
||
578 | sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT); |
||
579 | |||
580 | /* graphic card Initialization */ |
||
581 | if (grx_init() < 1) { |
||
582 | sys_abort(1); |
||
583 | } |
||
584 | |||
585 | if (grx_open(ORI_RES,VER_RES,COL_DEP) < 0) { |
||
586 | kern_printf("GRX Err\n"); |
||
587 | sys_abort(1); |
||
588 | } |
||
589 | kern_printf("Video card ok!\n"); |
||
590 | cbi[1] = cab_create("direzione",10,15); |
||
591 | cbi[0] = cab_create("modo",4,15); |
||
592 | cbi[2] = cab_create("velocita",10,15); |
||
593 | cbi[3] = cab_create("azione",3,3); |
||
594 | m = cab_reserve(cbi[0]); |
||
595 | modo = NO_BALL_MODE; |
||
596 | *m = modo; |
||
597 | cab_putmes(cbi[0],m); |
||
598 | a = cab_reserve(cbi[1]); |
||
599 | angolo =0; |
||
600 | *a = angolo; |
||
601 | cab_putmes(cbi[1],a); |
||
602 | s = cab_reserve(cbi[2]); |
||
603 | speed=0; |
||
604 | *s =speed; |
||
605 | cab_putmes(cbi[2],s); |
||
606 | o = cab_reserve(cbi[3]); |
||
607 | azione = NO_ACT; |
||
608 | *o = azione; |
||
609 | cab_putmes(cbi[3],o); |
||
610 | |||
611 | /* The scenario */ |
||
612 | |||
613 | grx_box(0,0,ORI_RES,VER_RES,0); |
||
614 | disegna_campo(); |
||
615 | for(p=0;p<scale_width;p++) |
||
616 | for(z=0;z<scale_height;z++){ |
||
617 | 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])); |
||
618 | 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])); |
||
619 | } |
||
620 | for(p=0;p<faro_width;p++) |
||
621 | for(z=0;z<faro_height;z++){ |
||
622 | 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])); |
||
623 | 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])); |
||
624 | } |
||
625 | for(p=0;p<schermo_width;p++) |
||
626 | for(z=0;z<schermo_height;z++){ |
||
627 | 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])); |
||
628 | } |
||
629 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
630 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
631 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
632 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
633 | /* The program waits a space to create a calc */ |
||
634 | c = keyb_getch(BLOCK); |
||
635 | |||
636 | /* randomize!!!!*/ |
||
637 | seme = sys_gettime(NULL); |
||
638 | srand(seme); |
||
639 | |||
640 | do { |
||
641 | if ((c == ' ') &&(ij==0)) { |
||
642 | init_ball(); |
||
643 | init_calc(); |
||
644 | init_portiere(); |
||
645 | ij++; |
||
646 | } |
||
647 | if ((c== 'w')){m = cab_reserve(cbi[0]); |
||
648 | modo = PENALTY_MODE_WHITE; |
||
649 | *m = modo; |
||
650 | cab_putmes(cbi[0],m); |
||
651 | rigore =PENALTY; } |
||
652 | if ((c== 'e')){m = cab_reserve(cbi[0]); |
||
653 | modo = PENALTY_MODE_BLUE; |
||
654 | *m = modo; |
||
655 | cab_putmes(cbi[0],m); |
||
656 | rigore =PENALTY; } |
||
657 | if ((c == 'k')) { |
||
658 | group_kill(GROUP_PLAY); |
||
659 | group_kill(GROUP_SOFT); |
||
660 | group_kill(GROUP_BALL); |
||
661 | ij=0; |
||
662 | m = cab_reserve(cbi[0]); |
||
663 | modo = NO_BALL_MODE; |
||
664 | *m = modo; |
||
665 | cab_putmes(cbi[0],m); |
||
666 | disegna_campo(); |
||
667 | for(p=0;p<scale_width;p++) |
||
668 | for(z=0;z<scale_height;z++){ |
||
669 | 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])); |
||
670 | 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])); |
||
671 | } |
||
672 | for(p=0;p<faro_width;p++) |
||
673 | for(z=0;z<faro_height;z++){ |
||
674 | 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])); |
||
675 | 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])); |
||
676 | } |
||
677 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
678 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
679 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
680 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
681 | xb=(int)(XMAX-XMIN)/2; |
||
682 | yb=(int)(YMIN+YMAX)/2; |
||
683 | } |
||
684 | c = keyb_getch(BLOCK); |
||
685 | } while (c != ESC); |
||
686 | |||
687 | |||
688 | |||
689 | sys_end(); |
||
690 | |||
691 | return 0; |
||
692 | } |
||
693 | |||
694 | /*--------------------------------------------------------------*/ |
||
695 | void draw_calc(int x, int y, int c,int f) |
||
696 | { |
||
697 | int i,j;/*needed in the macro*/ |
||
698 | sem_wait(&mutex); |
||
699 | if(c==CANC){ |
||
700 | |||
701 | grx_box(x, y,x+27,y+27,SFONDO); |
||
702 | if((x>(XMIN+XMAX)/2-27) && (x<(XMIN+XMAX)/2+27)){ |
||
703 | grx_line(XMIN+1,YMIN+1,XMAX-1,YMIN+1,rgb16(255,255,255)); |
||
704 | grx_line(XMIN+1,YMAX-1,XMAX-1,YMAX-1,rgb16(255,255,255)); |
||
705 | grx_line(XMIN+1,YMIN+1,XMIN+1,(YMIN+YMAX)/2-50,rgb16(255,255,255)); |
||
706 | grx_line(XMIN+1,(YMIN+YMAX)/2+50,XMIN+1,YMAX-1,rgb16(255,255,255)); |
||
707 | grx_line(XMAX-1,YMIN+1,XMAX-1,(YMIN+YMAX)/2-50,rgb16(255,255,255)); |
||
708 | grx_line(XMAX-1,(YMIN+YMAX)/2+50,XMAX-1,YMAX-1,rgb16(255,255,255)); |
||
709 | grx_line(((XMIN+XMAX)/2),YMIN,((XMIN+XMAX)/2),YMAX-1,rgb16(255,255,255)); |
||
710 | grx_circle((XMIN+XMAX)/2,(YMIN+YMAX)/2,54,rgb16(255,255,255)); |
||
711 | grx_line(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
712 | grx_line((XMIN+XMAX)/5,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
713 | grx_line(XMIN,3*(YMIN+YMAX)/4-30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
714 | grx_line(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
715 | grx_line((XMIN+XMAX)/10,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
716 | grx_line(XMIN,(YMIN+YMAX)/2+70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
717 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
718 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
719 | grx_line(4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
720 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
721 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
722 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
723 | grx_line(XMIN+1,(YMIN+YMAX)/2-50,XMIN+1,(YMIN+YMAX)/2+50,rgb16(100,100,100)); |
||
724 | grx_line(XMAX-1,(YMIN+YMAX)/2-50,XMAX-1,(YMIN+YMAX)/2+50,rgb16(100,100,100)); |
||
725 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
726 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
727 | } |
||
728 | } |
||
729 | else |
||
730 | if(c==RLEFT) |
||
731 | DIRDRAW(x,y,((unsigned char)runup_data[i*27+j])); |
||
732 | if(c==RLEFT+1) |
||
733 | DIRDRAW(x,y,((unsigned char)runup2_data[i*27+j])); |
||
734 | if(c==RDOWN) |
||
735 | DIRDRAW(x,y,((unsigned char)rundes2_data[i*27+j])); |
||
736 | if(c==RDOWN+1) |
||
737 | DIRDRAW(x,y,((unsigned char)rundes_data[i*27+j])); |
||
738 | if(c==RUP) |
||
739 | DIRDRAW(x,y,((unsigned char)runleft2_data[i*27+j])); |
||
740 | if(c==RUP+1) |
||
741 | DIRDRAW(x,y,((unsigned char)runleft_data[i*27+j])); |
||
742 | if(c==RRIGHT) |
||
743 | DIRDRAW(x,y,((unsigned char)rundown2_data[i*27+j])); |
||
744 | if(c==RRIGHT+1) |
||
745 | DIRDRAW(x,y,((unsigned char)rundown_data[i*27+j])); |
||
746 | if(c==RRUP) |
||
747 | DIRDRAW(x,y,((unsigned char)runld2_data[i*27+j])); |
||
748 | if(c==RRUP+1) |
||
749 | DIRDRAW(x,y,((unsigned char)runld_data[i*27+j])); |
||
750 | if(c==RLUP) |
||
751 | DIRDRAW(x,y,((unsigned char)runlu2_data[i*27+j])); |
||
752 | if(c==RLUP+1) |
||
753 | DIRDRAW(x,y,((unsigned char)runlu_data[i*27+j])); |
||
754 | if(c==RLDW) |
||
755 | DIRDRAW(x,y,((unsigned char)runru2_data[i*27+j])); |
||
756 | if(c==RLDW+1) |
||
757 | DIRDRAW(x,y,((unsigned char)runru_data[i*27+j])); |
||
758 | if(c==RRDW) |
||
759 | DIRDRAW(x,y,((unsigned char)runrd2_data[i*27+j])); |
||
760 | if(c==RRDW+1) |
||
761 | DIRDRAW(x,y,((unsigned char)runrd_data[i*27+j])); |
||
762 | /**********************************************************************/ |
||
763 | if(c==RLEFT2) |
||
764 | DIRDRAW(x,y,((unsigned char)B_runup_data[i*27+j])); |
||
765 | if(c==RLEFT2+1) |
||
766 | DIRDRAW(x,y,((unsigned char)B_runup2_data[i*27+j])); |
||
767 | if(c==RDOWN2) |
||
768 | DIRDRAW(x,y,((unsigned char)B_rundes2_data[i*27+j])); |
||
769 | if(c==RDOWN2+1) |
||
770 | DIRDRAW(x,y,((unsigned char)B_rundes_data[i*27+j])); |
||
771 | if(c==RUP2) |
||
772 | DIRDRAW(x,y,((unsigned char)B_runleft2_data[i*27+j])); |
||
773 | if(c==RUP2+1) |
||
774 | DIRDRAW(x,y,((unsigned char)B_runleft_data[i*27+j])); |
||
775 | if(c==RRIGHT2) |
||
776 | DIRDRAW(x,y,((unsigned char)B_rundown2_data[i*27+j])); |
||
777 | if(c==RRIGHT2+1) |
||
778 | DIRDRAW(x,y,((unsigned char)B_rundown_data[i*27+j])); |
||
779 | if(c==RRUP2) |
||
780 | DIRDRAW(x,y,((unsigned char)B_runld2_data[i*27+j])); |
||
781 | if(c==RRUP2+1) |
||
782 | DIRDRAW(x,y,((unsigned char)B_runld_data[i*27+j])); |
||
783 | if(c==RLUP2) |
||
784 | DIRDRAW(x,y,((unsigned char)B_runlu2_data[i*27+j])); |
||
785 | if(c==RLUP2+1) |
||
786 | DIRDRAW(x,y,((unsigned char)B_runlu_data[i*27+j])); |
||
787 | if(c==RLDW2) |
||
788 | DIRDRAW(x,y,((unsigned char)B_runru2_data[i*27+j])); |
||
789 | if(c==RLDW2+1) |
||
790 | DIRDRAW(x,y,((unsigned char)B_runru_data[i*27+j])); |
||
791 | if(c==RRDW2) |
||
792 | DIRDRAW(x,y,((unsigned char)B_runrd2_data[i*27+j])); |
||
793 | if(c==RRDW2+1) |
||
794 | DIRDRAW(x,y,((unsigned char)B_runrd_data[i*27+j])); |
||
795 | sem_post(&mutex); |
||
796 | } |
||
797 | /*--------------------------------------------------------------*/ |
||
798 | void draw_ball(int c) |
||
799 | { |
||
800 | sem_wait(&mutex); |
||
801 | if(c==0) |
||
802 | grx_box(xob, yob,xob+5,yob+5,SFONDO); |
||
803 | else grx_putimage(xb, yb,xb+5,yb+5,pallone); |
||
804 | sem_post(&mutex); |
||
805 | } |
||
806 | void draw_port(int c,int p,int yp,int xp) |
||
807 | { |
||
808 | int i,j; |
||
809 | sem_wait(&mutex); |
||
810 | if (c==0){ |
||
811 | if(p==0){ |
||
812 | /*ridisegna l'area*/ |
||
813 | grx_box(xp,yp-15,xp+30,yp+25,SFONDO); |
||
814 | grx_line(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
815 | grx_line((XMIN+XMAX)/5,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
816 | grx_line(XMIN,3*(YMIN+YMAX)/4-30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
817 | grx_line(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
818 | grx_line((XMIN+XMAX)/10,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
819 | grx_line(XMIN,(YMIN+YMAX)/2+70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
820 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
821 | } |
||
822 | if(p==1){ |
||
823 | /*ridisegna l'area*/ |
||
824 | grx_box(xp,yp-15,xp+27,yp+25,SFONDO); |
||
825 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,(YMIN+YMAX)/4+30,rgb16(255,255,255)); |
||
826 | grx_line(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
827 | grx_line(4*(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
828 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2-70,rgb16(255,255,255)); |
||
829 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
830 | grx_line(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
831 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
832 | } |
||
833 | } |
||
834 | else{ |
||
835 | if(p==0) |
||
836 | DIRDRAW(xp,yp,((unsigned char)portsx_data[i*27+j])); |
||
837 | if(p==1) |
||
838 | DIRDRAW(xp,yp,((unsigned char)portdx_data[i*27+j])); |
||
839 | } |
||
840 | sem_post(&mutex); |
||
841 | } |
||
842 | void init_ball(){ |
||
843 | HARD_TASK_MODEL m_ball; |
||
844 | hard_task_default_model(m_ball); |
||
845 | hard_task_def_ctrl_jet (m_ball); |
||
846 | hard_task_def_arg (m_ball, (void *)0); |
||
847 | hard_task_def_wcet (m_ball, BALL_WCET); |
||
848 | hard_task_def_mit (m_ball, PERIOD_BALL); |
||
849 | hard_task_def_group (m_ball, GROUP_BALL); |
||
850 | hard_task_def_usemath (m_ball); |
||
851 | pid_ball = task_create("ball", ball, &m_ball, NULL); |
||
852 | if (pid_ball == NIL) { |
||
853 | grx_close(); |
||
854 | perror("Could not create task <ball>"); |
||
855 | sys_abort(1); |
||
856 | } |
||
857 | |||
858 | task_activate(pid_ball); |
||
859 | } |
||
860 | void init_calc(){ |
||
861 | HARD_TASK_MODEL m_calc; |
||
862 | int i; |
||
863 | disegna_campo(); |
||
864 | for(i=0;i<MAX_P;i++){ |
||
865 | hard_task_default_model(m_calc); |
||
866 | hard_task_def_ctrl_jet (m_calc); |
||
867 | hard_task_def_arg (m_calc, (void *)i); |
||
868 | hard_task_def_wcet (m_calc, CALC_WCET); |
||
869 | hard_task_def_mit (m_calc, PERIOD_CALC); |
||
870 | hard_task_def_group (m_calc, GROUP_PLAY); |
||
871 | hard_task_def_usemath (m_calc); |
||
872 | pid_calc = task_create("calc", calc, &m_calc, NULL); |
||
873 | if (pid_calc == NIL) { |
||
874 | grx_close(); |
||
875 | perror("Could not create task <calc>"); |
||
876 | sys_abort(1); |
||
877 | } |
||
878 | task_activate(pid_calc); |
||
879 | } |
||
880 | } |
||
881 | void act_goal(){ |
||
882 | |||
883 | |||
884 | SOFT_TASK_MODEL m_goal; |
||
885 | soft_task_default_model(m_goal); |
||
886 | soft_task_def_arg (m_goal,(void*)0); |
||
887 | soft_task_def_met (m_goal, 1000); |
||
888 | soft_task_def_level (m_goal,1); |
||
889 | soft_task_def_ctrl_jet (m_goal); |
||
890 | soft_task_def_period (m_goal, 1200000); |
||
891 | soft_task_def_group (m_goal, GROUP_SOFT); |
||
892 | soft_task_def_usemath (m_goal); |
||
893 | soft_task_def_aperiodic(m_goal); |
||
894 | |||
895 | pid_gol = task_create("gol", gol, &m_goal, NULL); |
||
896 | if (pid_gol == NIL) { |
||
897 | grx_close(); |
||
898 | perror("Could not create task <port>"); |
||
899 | sys_abort(1); |
||
900 | } |
||
901 | task_activate(pid_gol); |
||
902 | } |
||
903 | void init_portiere(){ |
||
904 | HARD_TASK_MODEL m_port; |
||
905 | int g; |
||
906 | for(g=0;g<2;g++){ |
||
907 | hard_task_default_model(m_port); |
||
908 | hard_task_def_arg (m_port, (void *)g); |
||
909 | hard_task_def_wcet (m_port, PORT_WCET); |
||
910 | hard_task_def_mit (m_port, PERIOD_PORT); |
||
911 | hard_task_def_group (m_port, GROUP_SOFT); |
||
912 | hard_task_def_usemath (m_port); |
||
913 | pid_port = task_create("portiere", portiere, &m_port, NULL); |
||
914 | if (pid_port == NIL) { |
||
915 | grx_close(); |
||
916 | perror("Could not create task <port>"); |
||
917 | sys_abort(1); |
||
918 | } |
||
919 | task_activate(pid_port); |
||
920 | } |
||
921 | |||
922 | } |
||
923 | void draw_goal(){ |
||
924 | int p,z; |
||
925 | char *m; |
||
926 | char modo; |
||
927 | char *o; |
||
928 | char azione; |
||
929 | m = cab_reserve(cbi[0]); |
||
930 | modo = NO_BALL_MODE; |
||
931 | *m = modo; |
||
932 | cab_putmes(cbi[0],m); |
||
933 | o = cab_getmes(cbi[3]); |
||
934 | azione = *o; |
||
935 | cab_unget(cbi[3],o); |
||
936 | |||
937 | /*scritta GOAL!!*/ |
||
938 | if(azione == GOAL_ACT){ |
||
939 | for(p=0;p<schermo_width;p++) |
||
940 | for(z=0;z<schermo_height;z++){ |
||
941 | 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])); |
||
942 | } |
||
943 | o = cab_reserve(cbi[3]); |
||
944 | azione = 0; |
||
945 | *o = azione; |
||
946 | cab_putmes(cbi[3],o); |
||
947 | } |
||
948 | |||
949 | /*risistema il menu sul tabellone*/ |
||
950 | if (azione == MENU_ACT){ |
||
951 | for(p=0;p<schermo_width;p++) |
||
952 | for(z=0;z<schermo_height;z++){ |
||
953 | 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])); |
||
954 | } |
||
955 | o = cab_reserve(cbi[3]); |
||
956 | azione = NO_ACT; |
||
957 | *o = azione; |
||
958 | cab_putmes(cbi[3],o); |
||
959 | grx_text(" Simulation of Soccer", XMIN+260, YMENU+10, rgb16(255,255,255), rgb16(0,0,0)); |
||
960 | grx_text(" W rigore bianchi, E rigore blue", XMIN+260, YMENU+20, rgb16(255,255,255), rgb16(0,0,0)); |
||
961 | grx_text(" SPACE crea squadre, K kill all ", XMIN+260, YMENU+30, rgb16(255,255,255), rgb16(0,0,0)); |
||
962 | grx_text(" ESC Return to MS-DOS ", XMIN+260, YMENU+40, 12, rgb16(255,255,255)); |
||
963 | } |
||
964 | } |
||
965 | |||
966 | /*calcola la y del portiere in base alla yi in ingresso*/ |
||
967 | int yportiere(int yi,int yf){ |
||
968 | int speedy =3; |
||
969 | int dy=0; |
||
970 | int yp; |
||
971 | /*Se sto per raggiungere la palla rallento*/ |
||
972 | if(abs(yi-yf)<VEL) speedy=1; |
||
973 | /*se raggiungo la palla mi fermo*/ |
||
974 | if(yi==yf)dy=0; |
||
975 | /*se palla in basso*/ |
||
976 | if(yi>yf) dy= speedy; |
||
977 | /*se palla in alto*/ |
||
978 | if(yi<yf) dy=-speedy; |
||
979 | yp =yf+dy; |
||
980 | return(yp); |
||
981 | } |
||
982 | /*calcola la x del portiere in base alla xi in ingresso*/ |
||
983 | int xportiere(int xi, int xf){ |
||
984 | int speedx=3; |
||
985 | int dx=0; |
||
986 | int xp; |
||
987 | if(abs(xi-xf)<VEL) speedx=1; |
||
988 | if(xi==xf)dx=0; |
||
989 | if(xi>xf) dx= speedx; |
||
990 | if(xi<xf) dx=-speedx; |
||
991 | xp = xf +dx; |
||
992 | return(xp); |
||
993 | } |
||
994 | /*disegna il campo di gioco*/ |
||
995 | void disegna_campo(){ |
||
996 | //grx_box(0,0,ORI_RES,VER_RES,0); |
||
997 | grx_box(XMIN, YMIN, XMAX, YMAX, SFONDO); |
||
998 | grx_rect(XMIN+1, YMIN+1, XMAX-1, YMAX-1, rgb16(255,255,255)); |
||
999 | grx_line(((XMIN+XMAX)/2),YMIN,((XMIN+XMAX)/2),YMAX-1,rgb16(255,255,255)); |
||
1000 | grx_circle((XMIN+XMAX)/2,(YMIN+YMAX)/2,54,rgb16(255,255,255)); |
||
1001 | grx_rect(XMIN,(YMIN+YMAX)/4+30,(XMIN+XMAX)/5,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
1002 | grx_rect(4*(XMIN+XMAX)/5,(YMIN+YMAX)/4+30,XMAX,3*(YMIN+YMAX)/4-30,rgb16(255,255,255)); |
||
1003 | grx_rect(XMIN,(YMIN+YMAX)/2-70,(XMIN+XMAX)/10,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
1004 | grx_rect(9*(XMIN+XMAX)/10,(YMIN+YMAX)/2-70,XMAX,(YMIN+YMAX)/2+70,rgb16(255,255,255)); |
||
1005 | grx_box(XMIN,(YMIN+YMAX)/2-50,XMIN+2,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
1006 | grx_box(XMAX-2,(YMIN+YMAX)/2-50,XMAX,(YMIN+YMAX)/2+50,rgb16(50,0,50)); |
||
1007 | } |
||
1008 | void init_position(int num,int *ox,int *oy) |
||
1009 | { |
||
1010 | if(num<5){ |
||
1011 | pos_calc[num].x = *ox = initial_calc_position[num].x+35; |
||
1012 | pos_calc[num].y = *oy = initial_calc_position[num].y; |
||
1013 | } |
||
1014 | if(num==2){ |
||
1015 | pos_calc[num].x = *ox = initial_calc_position[num].x+100; |
||
1016 | pos_calc[num].y = *oy = initial_calc_position[num].y; |
||
1017 | } |
||
1018 | if(num>4){ |
||
1019 | pos_calc[num].x = *ox = initial_calc_position[num-5].x; |
||
1020 | pos_calc[num].y = *oy = initial_calc_position[num-5].y+35; |
||
1021 | } |
||
1022 | if(num==7){ |
||
1023 | pos_calc[num].x = *ox = initial_calc_position[num-5].x-100; |
||
1024 | pos_calc[num].y = *oy = initial_calc_position[num-5].y; |
||
1025 | } |
||
1026 | |||
1027 | } |
||
1028 | void init_calc_area(int num,int *xmin,int *xmax,int *ymin,int *ymax){ |
||
1029 | |||
1030 | |||
1031 | switch(num) |
||
1032 | { |
||
1033 | case 0: /*attaccante bianchi*/ |
||
1034 | case 5: /*difensore blu*/ |
||
1035 | { |
||
1036 | *xmin = XMIN; |
||
1037 | *ymin = YMIN; |
||
1038 | *xmax = 255; |
||
1039 | *ymax = 305; |
||
1040 | } |
||
1041 | break; |
||
1042 | case 1: /*attaccante bianchi*/ |
||
1043 | case 6: /*difensore blu*/ |
||
1044 | { |
||
1045 | *xmin = XMIN; |
||
1046 | *ymin = 295; |
||
1047 | *xmax = 255; |
||
1048 | *ymax = YMAX; |
||
1049 | } |
||
1050 | break; |
||
1051 | case 2: /*centrocampisti*/ |
||
1052 | case 7: |
||
1053 | { |
||
1054 | *xmin = 255; |
||
1055 | *ymin = YMIN; |
||
1056 | *xmax = 525; |
||
1057 | *ymax = YMAX; |
||
1058 | } break; |
||
1059 | case 3: /*attaccante blu*/ |
||
1060 | case 8: /*difensore bianco*/ |
||
1061 | { |
||
1062 | *xmin = 525; |
||
1063 | *ymin = YMIN; |
||
1064 | *xmax = XMAX; |
||
1065 | *ymax = 305; |
||
1066 | } |
||
1067 | break; |
||
1068 | case 4: /*attaccante blu*/ |
||
1069 | case 9: /*difensore bianco*/ |
||
1070 | { |
||
1071 | *xmin = 505; |
||
1072 | *ymin = 305; |
||
1073 | *xmax = XMAX; |
||
1074 | *ymax = YMAX; |
||
1075 | } |
||
1076 | break; |
||
1077 | default: |
||
1078 | break; |
||
1079 | } |
||
1080 | } |
||
1081 | void get_target(int modo,int i,struct target * obj,int x_max,int x_min,int y_max,int y_min){ |
||
1082 | |||
1083 | |||
1084 | |||
1085 | if((modo==PENALTY_MODE_WHITE)||(modo==PENALTY_MODE_BLUE)){ |
||
1086 | |||
1087 | |||
1088 | |||
1089 | if((i<5)){ |
||
1090 | obj->x = (XMAX)-100-50*i; |
||
1091 | if(pos_calc[i].y>(YMAX+YMIN)/2) |
||
1092 | obj->y = YMAX-60; |
||
1093 | if(pos_calc[i].y<(YMAX+YMIN)/2) |
||
1094 | obj->y = YMIN+60; |
||
1095 | if(modo==PENALTY_MODE_WHITE){ |
||
1096 | if((i==2)){ |
||
1097 | obj->x = xb; |
||
1098 | obj->y = yb; |
||
1099 | } |
||
1100 | } |
||
1101 | } |
||
1102 | if((i>4)){ |
||
1103 | obj->x = (XMIN)+100+50*i; |
||
1104 | if(pos_calc[i].y>(YMAX+YMIN)/2) |
||
1105 | obj->y = YMAX-60; |
||
1106 | if(pos_calc[i].y<(YMAX+YMIN)/2) |
||
1107 | obj->y = YMIN+60; |
||
1108 | if(modo==PENALTY_MODE_BLUE){ |
||
1109 | if((i==7)){ |
||
1110 | obj->x = xb; |
||
1111 | obj->y = yb; |
||
1112 | } |
||
1113 | } |
||
1114 | |||
1115 | } |
||
1116 | } |
||
1117 | |||
1118 | if(modo==NO_BALL_MODE) |
||
1119 | { |
||
1120 | |||
1121 | /*Valutazione se la palla sia in una zona di interesse per il */ |
||
1122 | /*calciatore */ |
||
1123 | |||
1124 | if((xb>x_max)||(xb<x_min)||(yb>y_max)||(yb<y_min)) |
||
1125 | { |
||
1126 | if(i<5) |
||
1127 | { |
||
1128 | obj->x=initial_calc_position[i].x; |
||
1129 | obj->y=initial_calc_position[i].y; |
||
1130 | } |
||
1131 | if(i>4) |
||
1132 | { |
||
1133 | obj->x=initial_calc_position[i-5].x; |
||
1134 | obj->y=initial_calc_position[i-5].y; |
||
1135 | } |
||
1136 | } |
||
1137 | else |
||
1138 | { |
||
1139 | obj->x=xb; |
||
1140 | obj->y=yb; |
||
1141 | } |
||
1142 | } |
||
1143 | |||
1144 | |||
1145 | |||
1146 | |||
1147 | } |
||
1148 | int get_direction(int x,int y,int x2,int y2){ |
||
1149 | |||
1150 | int ang=0; |
||
1151 | if((abs(x-x2) !=0)){ |
||
1152 | if((x>x2)&&(y<=y2)) |
||
1153 | ang = 180+ |
||
1154 | atan((float)abs(y-y2)/ |
||
1155 | (float)abs(x-x2))* |
||
1156 | 180/PI; |
||
1157 | if((x<x2)&&(y<y2)) |
||
1158 | ang = 360- |
||
1159 | atan((float)abs(y-y2)/ |
||
1160 | (float)abs(x-x2))* |
||
1161 | 180/PI; |
||
1162 | if((x<x2)&&(y>y2)) |
||
1163 | ang = |
||
1164 | atan((float)abs(y-y2)/ |
||
1165 | (float)abs(x-x2))* |
||
1166 | 180/PI; |
||
1167 | if((x>x2)&&(y>y2)) |
||
1168 | ang = 180- |
||
1169 | atan((float)abs(y-y2)/ |
||
1170 | (float)abs(x-x2))* |
||
1171 | 180/PI; |
||
1172 | } |
||
1173 | |||
1174 | return ang; |
||
1175 | |||
1176 | } |
||
1177 | |||
1178 | void collision_detection(int i){ |
||
1179 | |||
1180 | int j; |
||
1181 | |||
1182 | |||
1183 | for(j=0;j<MAX_P+2;j++){ |
||
1184 | |||
1185 | if(i!=j){ |
||
1186 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x-27 |
||
1187 | && pos_calc[i].y<pos_calc[j].y && pos_calc[i].y>pos_calc[j].y-27) |
||
1188 | { |
||
1189 | pos_calc[i].y=pos_calc[j].y-27; |
||
1190 | } |
||
1191 | if(pos_calc[i].x<pos_calc[j].x && pos_calc[i].x>pos_calc[j].x-27 |
||
1192 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y-27) |
||
1193 | { |
||
1194 | pos_calc[i].x=pos_calc[j].x-27; |
||
1195 | } |
||
1196 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x-27 |
||
1197 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y) |
||
1198 | { |
||
1199 | pos_calc[i].y=pos_calc[j].y+27; |
||
1200 | } |
||
1201 | if(pos_calc[i].x<pos_calc[j].x+27 && pos_calc[i].x>pos_calc[j].x |
||
1202 | && pos_calc[i].y<pos_calc[j].y+27 && pos_calc[i].y>pos_calc[j].y) |
||
1203 | { |
||
1204 | pos_calc[i].x=pos_calc[j].x+27; |
||
1205 | } |
||
1206 | } |
||
1207 | } |
||
1208 | |||
1209 | /*Per impedire un calciatore esca dal campo*/ |
||
1210 | |||
1211 | if(pos_calc[i].x>=XMAX-(pos_calc[i].dx+1)) pos_calc[i].x-=(pos_calc[i].dx+1); |
||
1212 | if(pos_calc[i].x<=XMIN+(pos_calc[i].dx+1)) pos_calc[i].x+=(pos_calc[i].dx+1); |
||
1213 | if(pos_calc[i].y>=YMAX-(pos_calc[i].dy+1)) pos_calc[i].y-=(pos_calc[i].dy+1); |
||
1214 | if(pos_calc[i].y<=YMIN+(pos_calc[i].dy+1)) pos_calc[i].y+=(pos_calc[i].dy+1); |
||
1215 | } |
||
1216 | |||
1217 | int goal_behaviour(int flag){ |
||
1218 | char *o; |
||
1219 | char azione; |
||
1220 | |||
1221 | if (flag !=0) { |
||
1222 | flag++; |
||
1223 | yb =(YMAX+YMIN)/2; |
||
1224 | xb =(XMIN+XMAX)/2; |
||
1225 | } |
||
1226 | if (flag==20){ |
||
1227 | yb =(YMAX+YMIN)/2; |
||
1228 | xb =(XMIN+XMAX)/2; |
||
1229 | /*seleziono l'azione da compiere sul tabellone*/ |
||
1230 | o = cab_reserve(cbi[3]); |
||
1231 | azione = GOAL_ACT; /*scritta GOAL!!*/ |
||
1232 | *o = azione; |
||
1233 | cab_putmes(cbi[3],o); |
||
1234 | /*carico il task gol*/ |
||
1235 | act_goal(); |
||
1236 | } |
||
1237 | if (flag==65){ |
||
1238 | o = cab_reserve(cbi[3]); |
||
1239 | azione = MENU_ACT; /*sistema il menu su tabellone*/ |
||
1240 | *o = azione; |
||
1241 | cab_putmes(cbi[3],o); |
||
1242 | /*carico il task gol*/ |
||
1243 | act_goal(); |
||
1244 | } |
||
1245 | |||
1246 | return(flag); |
||
1247 | } |
||
1248 |