Rev 1448 | Go to most recent revision | Details | 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 | |||
21 | /* CVS : $Id: cannone.c,v 1.1 2002-10-01 10:25:00 pj Exp $ */ |
||
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 | |||
89 | TASK cannone(void *arg) |
||
90 | { |
||
91 | PID missile_pid=-2; |
||
92 | int i, index; // indice nella aereo_table e cannone_table |
||
93 | int d, dir; |
||
94 | int xx0,yy0,xx1,yy1; |
||
95 | int first_time; |
||
96 | |||
97 | index = (int)arg; |
||
98 | first_time = 1; |
||
99 | if (index%2) { |
||
100 | //indice dispari |
||
101 | xx0 = X0 + 500/2 - CANNONE_LX/2 + 500/(CANNONE_N_MAX+1)*(index+1)/2; |
||
102 | } |
||
103 | else { |
||
104 | //indice pari |
||
105 | xx0 = X0 + 500/2 - CANNONE_LX/2 - 500/(CANNONE_N_MAX+1)*(index/2); |
||
106 | } |
||
107 | xx1 = xx0 + CANNONE_LX; |
||
108 | yy0= Y1 - CANNONE_LY; |
||
109 | yy1= Y1; |
||
110 | |||
111 | cannone_table[index].x = xx0 + CANNONE_LX/2; |
||
112 | |||
113 | while(1){ |
||
114 | |||
115 | if (cannone_table[index].killing) { |
||
116 | if (!first_time) { |
||
117 | mutex_lock(&grx_mutex); |
||
118 | FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,yy1); |
||
119 | mutex_unlock(&grx_mutex); |
||
120 | } |
||
121 | cannone_table[index].status=0; |
||
122 | cannone_table[index].pid=-2; |
||
123 | cannone_table[index].killing=0; |
||
124 | //il task aereo_creator si accorge che c'Š un aereo in meno |
||
125 | // quindi ne ricreer… uno nuovo!!! |
||
126 | return NULL; |
||
127 | } |
||
128 | |||
129 | if (first_time) { |
||
130 | mutex_lock(&grx_mutex); //...aggiorna disegno... |
||
131 | if (index==0) FAB_image_put(image_cannone[1], xx0, yy0); |
||
132 | else FAB_image_put(image_cannone[0], xx0, yy0); |
||
133 | mutex_unlock(&grx_mutex); |
||
134 | first_time=0; |
||
135 | } |
||
136 | |||
137 | if (!cannone_table[index].fire) { |
||
138 | for (i=0; i<AEREO_N_MAX; i++) { |
||
139 | if (aereo_table[i].status) { |
||
140 | d = aereo_table[i].x - (xx0+CANNONE_LX/2); |
||
141 | dir = aereo_table[i].dir; |
||
142 | if (d*dir<0 && FAB_ABS(d)<=CANNONE_SENSIBILITA/1000.0*500/BASE_L) { |
||
143 | // FA F U O C O |
||
144 | if (index==0) missile_pid=crea_hard_missile(index); |
||
145 | else missile_pid=crea_soft_missile(index); |
||
146 | if (missile_pid!=NIL) { |
||
147 | cannone_table[index].fire = 1; |
||
148 | task_activate(missile_pid); |
||
149 | break; |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | task_endcycle(); |
||
157 | } |
||
158 | |||
159 | return NULL; |
||
160 | } |
||
161 | |||
162 | |||
163 | |||
164 | PID crea_soft_cannone_creator() |
||
165 | { |
||
166 | SOFT_TASK_MODEL m; |
||
167 | PID pid; |
||
168 | |||
169 | soft_task_default_model(m); |
||
170 | soft_task_def_periodic(m); |
||
171 | soft_task_def_period(m,CANNONE_CREATOR_PERIOD); |
||
172 | soft_task_def_wcet(m,CANNONE_CREATOR_WCET); |
||
173 | soft_task_def_met(m,CANNONE_CREATOR_MET); |
||
174 | |||
175 | pid = task_create("cannone_creator", cannone_creator, &m, NULL); |
||
176 | return pid; |
||
177 | } |
||
178 | |||
179 | PID crea_hard_cannone(int index) |
||
180 | { |
||
181 | HARD_TASK_MODEL m; |
||
182 | PID pid; |
||
183 | |||
184 | hard_task_default_model(m); |
||
185 | hard_task_def_level(m,0); |
||
186 | hard_task_def_arg(m,(void*)index); |
||
187 | hard_task_def_periodic(m); |
||
188 | hard_task_def_wcet(m, CANNONE_WCET); |
||
189 | hard_task_def_mit(m,CANNONE_PERIOD); |
||
190 | |||
191 | pid = task_create("hard_cannone", cannone, &m, NULL); |
||
192 | return pid; |
||
193 | } |
||
194 | |||
195 | PID crea_soft_cannone(int index) |
||
196 | { |
||
197 | SOFT_TASK_MODEL m; |
||
198 | PID pid; |
||
199 | |||
200 | soft_task_default_model(m); |
||
201 | soft_task_def_level(m,0); |
||
202 | soft_task_def_arg(m,(void*)index); |
||
203 | soft_task_def_periodic(m); |
||
204 | soft_task_def_period(m,CANNONE_PERIOD); |
||
205 | soft_task_def_wcet(m, CANNONE_WCET); |
||
206 | soft_task_def_met(m,CANNONE_MET); |
||
207 | |||
208 | pid = task_create("soft_cannone", cannone, &m, NULL); |
||
209 | return pid; |
||
210 | } |
||
211 |