Rev 1448 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1090 | 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 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
1468 | giacomo | 21 | /* CVS : $Id: cannone.c,v 1.3 2004-05-25 18:24:59 giacomo Exp $ */ |
1090 | pj | 22 | |
23 | /* |
||
24 | * Copyright (C) 2000 Fabio Calabrese <fabiocalabrese77@yahoo.it> |
||
25 | * |
||
26 | * This program is free software; you can redistribute it and/or modify |
||
27 | * it under the terms of the GNU General Public License as published by |
||
28 | * the Free Software Foundation; either version 2 of the License, or |
||
29 | * (at your option) any later version. |
||
30 | * |
||
31 | * This program is distributed in the hope that it will be useful, |
||
32 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
33 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
34 | * GNU General Public License for more details. |
||
35 | * |
||
36 | * You should have received a copy of the GNU General Public License |
||
37 | * along with this program; if not, write to the Free Software |
||
38 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
39 | * |
||
40 | */ |
||
41 | |||
42 | //************************** |
||
43 | //* file: cannone.c * |
||
44 | //* included by: bca.c * |
||
45 | //************************** |
||
46 | |||
47 | TASK cannone_creator(void * arg) |
||
48 | { int i, index, last; |
||
49 | WORD count; |
||
50 | PID pid=-1; |
||
51 | |||
52 | while(1){ |
||
53 | |||
54 | count=0; |
||
55 | index=-1; |
||
56 | last=-1; |
||
57 | |||
58 | for (i=0; i<CANNONE_N_MAX; i++) // Conteggio aerei attivi |
||
59 | if (cannone_table[i].status) { |
||
60 | count++; |
||
61 | last=i; //...ultima posizione occupata in tabella... |
||
62 | } |
||
63 | else { |
||
64 | if (index==-1) index=i; //...prima posizione libera in tabella... |
||
65 | } |
||
66 | |||
67 | if (count<cannone_count && index!=-1) { // Richiesta nuovo cannone |
||
68 | cannone_table[index].status = 1; //...occupa posizione... |
||
69 | cannone_table[index].killing = 0; //...disattiva richiesta di kill... |
||
70 | if (index==0) pid = crea_hard_cannone(index); // sempre il 1ø hard |
||
71 | else pid = crea_soft_cannone(index); // tutti gli altri soft |
||
72 | cannone_table[index].pid = pid; |
||
73 | if (pid!=NIL) task_activate(pid); |
||
74 | else { //ripristino il posto libero in tabella; |
||
75 | cannone_table[index].status = -2; |
||
76 | cannone_table[index].killing = -2; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | else if (count>cannone_count) { // Richiesta cancellazione cannone |
||
81 | cannone_table[last].killing = 1; |
||
82 | } |
||
83 | |||
84 | task_endcycle(); |
||
85 | } |
||
86 | return NULL; |
||
87 | } |
||
88 | |||
1448 | giacomo | 89 | PID crea_soft_cannone_creator(); |
90 | PID crea_hard_cannone(int index); |
||
91 | PID crea_hard_cannone(int index); |
||
92 | |||
93 | PID crea_hard_missile(int index); |
||
94 | PID crea_soft_missile(int index); |
||
95 | |||
1090 | pj | 96 | TASK cannone(void *arg) |
97 | { |
||
98 | PID missile_pid=-2; |
||
99 | int i, index; // indice nella aereo_table e cannone_table |
||
100 | int d, dir; |
||
101 | int xx0,yy0,xx1,yy1; |
||
102 | int first_time; |
||
103 | |||
104 | index = (int)arg; |
||
105 | first_time = 1; |
||
106 | if (index%2) { |
||
107 | //indice dispari |
||
108 | xx0 = X0 + 500/2 - CANNONE_LX/2 + 500/(CANNONE_N_MAX+1)*(index+1)/2; |
||
109 | } |
||
110 | else { |
||
111 | //indice pari |
||
112 | xx0 = X0 + 500/2 - CANNONE_LX/2 - 500/(CANNONE_N_MAX+1)*(index/2); |
||
113 | } |
||
114 | xx1 = xx0 + CANNONE_LX; |
||
115 | yy0= Y1 - CANNONE_LY; |
||
116 | yy1= Y1; |
||
117 | |||
118 | cannone_table[index].x = xx0 + CANNONE_LX/2; |
||
119 | |||
120 | while(1){ |
||
121 | |||
122 | if (cannone_table[index].killing) { |
||
123 | if (!first_time) { |
||
124 | mutex_lock(&grx_mutex); |
||
125 | FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,yy1); |
||
126 | mutex_unlock(&grx_mutex); |
||
127 | } |
||
128 | cannone_table[index].status=0; |
||
129 | cannone_table[index].pid=-2; |
||
130 | cannone_table[index].killing=0; |
||
131 | //il task aereo_creator si accorge che c'Š un aereo in meno |
||
132 | // quindi ne ricreer… uno nuovo!!! |
||
133 | return NULL; |
||
134 | } |
||
135 | |||
136 | if (first_time) { |
||
137 | mutex_lock(&grx_mutex); //...aggiorna disegno... |
||
138 | if (index==0) FAB_image_put(image_cannone[1], xx0, yy0); |
||
139 | else FAB_image_put(image_cannone[0], xx0, yy0); |
||
140 | mutex_unlock(&grx_mutex); |
||
141 | first_time=0; |
||
142 | } |
||
143 | |||
144 | if (!cannone_table[index].fire) { |
||
145 | for (i=0; i<AEREO_N_MAX; i++) { |
||
146 | if (aereo_table[i].status) { |
||
147 | d = aereo_table[i].x - (xx0+CANNONE_LX/2); |
||
148 | dir = aereo_table[i].dir; |
||
149 | if (d*dir<0 && FAB_ABS(d)<=CANNONE_SENSIBILITA/1000.0*500/BASE_L) { |
||
150 | // FA F U O C O |
||
151 | if (index==0) missile_pid=crea_hard_missile(index); |
||
152 | else missile_pid=crea_soft_missile(index); |
||
153 | if (missile_pid!=NIL) { |
||
154 | cannone_table[index].fire = 1; |
||
155 | task_activate(missile_pid); |
||
156 | break; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | task_endcycle(); |
||
164 | } |
||
165 | |||
166 | return NULL; |
||
167 | } |
||
168 | |||
169 | |||
170 | |||
171 | PID crea_soft_cannone_creator() |
||
172 | { |
||
173 | SOFT_TASK_MODEL m; |
||
174 | PID pid; |
||
175 | |||
176 | soft_task_default_model(m); |
||
177 | soft_task_def_periodic(m); |
||
178 | soft_task_def_period(m,CANNONE_CREATOR_PERIOD); |
||
179 | soft_task_def_wcet(m,CANNONE_CREATOR_WCET); |
||
180 | soft_task_def_met(m,CANNONE_CREATOR_MET); |
||
181 | |||
182 | pid = task_create("cannone_creator", cannone_creator, &m, NULL); |
||
183 | return pid; |
||
184 | } |
||
185 | |||
186 | PID crea_hard_cannone(int index) |
||
187 | { |
||
188 | HARD_TASK_MODEL m; |
||
189 | PID pid; |
||
190 | |||
191 | hard_task_default_model(m); |
||
1468 | giacomo | 192 | hard_task_def_level(m,1); |
1090 | pj | 193 | hard_task_def_arg(m,(void*)index); |
194 | hard_task_def_periodic(m); |
||
195 | hard_task_def_wcet(m, CANNONE_WCET); |
||
196 | hard_task_def_mit(m,CANNONE_PERIOD); |
||
197 | |||
198 | pid = task_create("hard_cannone", cannone, &m, NULL); |
||
199 | return pid; |
||
200 | } |
||
201 | |||
202 | PID crea_soft_cannone(int index) |
||
203 | { |
||
204 | SOFT_TASK_MODEL m; |
||
205 | PID pid; |
||
206 | |||
207 | soft_task_default_model(m); |
||
1468 | giacomo | 208 | soft_task_def_level(m,2); |
1090 | pj | 209 | soft_task_def_arg(m,(void*)index); |
210 | soft_task_def_periodic(m); |
||
211 | soft_task_def_period(m,CANNONE_PERIOD); |
||
212 | soft_task_def_wcet(m, CANNONE_WCET); |
||
213 | soft_task_def_met(m,CANNONE_MET); |
||
214 | |||
215 | pid = task_create("soft_cannone", cannone, &m, NULL); |
||
216 | return pid; |
||
217 | } |
||
218 |