Rev 1468 | 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 | |||
1590 | tullio | 21 | /* CVS : $Id: cannone.c,v 1.4 2006-07-03 15:10:15 tullio 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 | |||
1590 | tullio | 42 | #include "bca.h" |
1090 | pj | 43 | |
44 | TASK cannone_creator(void * arg) |
||
45 | { int i, index, last; |
||
46 | WORD count; |
||
47 | PID pid=-1; |
||
48 | |||
49 | while(1){ |
||
50 | |||
51 | count=0; |
||
52 | index=-1; |
||
53 | last=-1; |
||
54 | |||
55 | for (i=0; i<CANNONE_N_MAX; i++) // Conteggio aerei attivi |
||
56 | if (cannone_table[i].status) { |
||
57 | count++; |
||
58 | last=i; //...ultima posizione occupata in tabella... |
||
59 | } |
||
60 | else { |
||
61 | if (index==-1) index=i; //...prima posizione libera in tabella... |
||
62 | } |
||
63 | |||
64 | if (count<cannone_count && index!=-1) { // Richiesta nuovo cannone |
||
65 | cannone_table[index].status = 1; //...occupa posizione... |
||
66 | cannone_table[index].killing = 0; //...disattiva richiesta di kill... |
||
67 | if (index==0) pid = crea_hard_cannone(index); // sempre il 1ø hard |
||
68 | else pid = crea_soft_cannone(index); // tutti gli altri soft |
||
69 | cannone_table[index].pid = pid; |
||
70 | if (pid!=NIL) task_activate(pid); |
||
71 | else { //ripristino il posto libero in tabella; |
||
72 | cannone_table[index].status = -2; |
||
73 | cannone_table[index].killing = -2; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | else if (count>cannone_count) { // Richiesta cancellazione cannone |
||
78 | cannone_table[last].killing = 1; |
||
79 | } |
||
80 | |||
81 | task_endcycle(); |
||
82 | } |
||
83 | return NULL; |
||
84 | } |
||
85 | |||
1448 | giacomo | 86 | PID crea_soft_cannone_creator(); |
87 | PID crea_hard_cannone(int index); |
||
88 | PID crea_hard_cannone(int index); |
||
89 | |||
90 | PID crea_hard_missile(int index); |
||
91 | PID crea_soft_missile(int index); |
||
92 | |||
1090 | pj | 93 | TASK cannone(void *arg) |
94 | { |
||
95 | PID missile_pid=-2; |
||
96 | int i, index; // indice nella aereo_table e cannone_table |
||
97 | int d, dir; |
||
98 | int xx0,yy0,xx1,yy1; |
||
99 | int first_time; |
||
100 | |||
101 | index = (int)arg; |
||
102 | first_time = 1; |
||
103 | if (index%2) { |
||
104 | //indice dispari |
||
105 | xx0 = X0 + 500/2 - CANNONE_LX/2 + 500/(CANNONE_N_MAX+1)*(index+1)/2; |
||
106 | } |
||
107 | else { |
||
108 | //indice pari |
||
109 | xx0 = X0 + 500/2 - CANNONE_LX/2 - 500/(CANNONE_N_MAX+1)*(index/2); |
||
110 | } |
||
111 | xx1 = xx0 + CANNONE_LX; |
||
112 | yy0= Y1 - CANNONE_LY; |
||
113 | yy1= Y1; |
||
114 | |||
115 | cannone_table[index].x = xx0 + CANNONE_LX/2; |
||
116 | |||
117 | while(1){ |
||
118 | |||
119 | if (cannone_table[index].killing) { |
||
120 | if (!first_time) { |
||
121 | mutex_lock(&grx_mutex); |
||
122 | FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,yy1); |
||
123 | mutex_unlock(&grx_mutex); |
||
124 | } |
||
125 | cannone_table[index].status=0; |
||
126 | cannone_table[index].pid=-2; |
||
127 | cannone_table[index].killing=0; |
||
128 | //il task aereo_creator si accorge che c'Š un aereo in meno |
||
129 | // quindi ne ricreer… uno nuovo!!! |
||
130 | return NULL; |
||
131 | } |
||
132 | |||
133 | if (first_time) { |
||
134 | mutex_lock(&grx_mutex); //...aggiorna disegno... |
||
135 | if (index==0) FAB_image_put(image_cannone[1], xx0, yy0); |
||
136 | else FAB_image_put(image_cannone[0], xx0, yy0); |
||
137 | mutex_unlock(&grx_mutex); |
||
138 | first_time=0; |
||
139 | } |
||
140 | |||
141 | if (!cannone_table[index].fire) { |
||
142 | for (i=0; i<AEREO_N_MAX; i++) { |
||
143 | if (aereo_table[i].status) { |
||
144 | d = aereo_table[i].x - (xx0+CANNONE_LX/2); |
||
145 | dir = aereo_table[i].dir; |
||
146 | if (d*dir<0 && FAB_ABS(d)<=CANNONE_SENSIBILITA/1000.0*500/BASE_L) { |
||
147 | // FA F U O C O |
||
148 | if (index==0) missile_pid=crea_hard_missile(index); |
||
149 | else missile_pid=crea_soft_missile(index); |
||
150 | if (missile_pid!=NIL) { |
||
151 | cannone_table[index].fire = 1; |
||
152 | task_activate(missile_pid); |
||
153 | break; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | task_endcycle(); |
||
161 | } |
||
162 | |||
163 | return NULL; |
||
164 | } |
||
165 | |||
166 | |||
167 | |||
168 | PID crea_soft_cannone_creator() |
||
169 | { |
||
170 | SOFT_TASK_MODEL m; |
||
171 | PID pid; |
||
172 | |||
173 | soft_task_default_model(m); |
||
174 | soft_task_def_periodic(m); |
||
175 | soft_task_def_period(m,CANNONE_CREATOR_PERIOD); |
||
176 | soft_task_def_wcet(m,CANNONE_CREATOR_WCET); |
||
177 | soft_task_def_met(m,CANNONE_CREATOR_MET); |
||
178 | |||
179 | pid = task_create("cannone_creator", cannone_creator, &m, NULL); |
||
180 | return pid; |
||
181 | } |
||
182 | |||
183 | PID crea_hard_cannone(int index) |
||
184 | { |
||
185 | HARD_TASK_MODEL m; |
||
186 | PID pid; |
||
187 | |||
188 | hard_task_default_model(m); |
||
1468 | giacomo | 189 | hard_task_def_level(m,1); |
1090 | pj | 190 | hard_task_def_arg(m,(void*)index); |
191 | hard_task_def_periodic(m); |
||
192 | hard_task_def_wcet(m, CANNONE_WCET); |
||
193 | hard_task_def_mit(m,CANNONE_PERIOD); |
||
194 | |||
195 | pid = task_create("hard_cannone", cannone, &m, NULL); |
||
196 | return pid; |
||
197 | } |
||
198 | |||
199 | PID crea_soft_cannone(int index) |
||
200 | { |
||
201 | SOFT_TASK_MODEL m; |
||
202 | PID pid; |
||
203 | |||
204 | soft_task_default_model(m); |
||
1468 | giacomo | 205 | soft_task_def_level(m,2); |
1090 | pj | 206 | soft_task_def_arg(m,(void*)index); |
207 | soft_task_def_periodic(m); |
||
208 | soft_task_def_period(m,CANNONE_PERIOD); |
||
209 | soft_task_def_wcet(m, CANNONE_WCET); |
||
210 | soft_task_def_met(m,CANNONE_MET); |
||
211 | |||
212 | pid = task_create("soft_cannone", cannone, &m, NULL); |
||
213 | return pid; |
||
214 | } |
||
215 |