Subversion Repositories shark

Rev

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: missile.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:        missile.c *
44
//* included by: bca.c     *
45
//**************************
46
 
1448 giacomo 47
PID crea_hard_missile(int index);
48
PID crea_soft_missile(int index);
49
PID crea_soft_esplo(int index);
50
 
1090 pj 51
TASK missile(void *arg)
52
{
53
  PID        esplo_pid;
54
  int        img;
55
  int        i, index;    // indice nella cannone_table
56
  int        distx, disty;
57
  int        xx0,yy0,xx1,yy1;
58
  double     vel, acc;
59
  double     a;
60
  double     y;
61
  double     x;
62
  double     old_x;    // [pixel]: conserva valore x prima di aggiornare
63
  double     old_y;
64
  double     dx;       // [pixel/us]: spostamento periodico
65
  double     dy;
66
  int        first_time;
67
  int killing;
68
 
69
  index = (int)arg;
70
  img=0;
71
  y = MISSILE_Y_MAX;
72
  x = cannone_table[index].x;
73
  a = FAB_rad(270);
74
  vel = MISSILE_V_MIN;
75
  acc = MISSILE_ACC_MIN;
76
  dx = 0;
77
  dy = 0;
78
 
79
  first_time = 1;
80
  killing = 0;
81
 
82
  while(1){
83
 
84
   old_x = x;
85
   old_y = y;
86
 
87
   if (vel<MISSILE_V_MAX) {
88
      vel += acc*MISSILE_PERIOD/(60*60*1000000.0);
89
      if (vel>MISSILE_V_MAX) vel = MISSILE_V_MAX;
90
      }
91
   if (acc<MISSILE_ACC_MAX) {
92
      acc += MISSILE_ACC_MIN;
93
      if (acc>MISSILE_ACC_MAX) acc = MISSILE_ACC_MAX;
94
      }
95
 
96
   cannone_table[index].missile_vel=vel;
97
 
98
   dx = cos(a)*vel * (X1-X0) / (double)BASE_L
99
        * (MISSILE_PERIOD / (60*60*1000000.0));
100
   dy = sin(a)*vel * (Y1-Y0) / ((double)BASE_H/1000)
101
        * (MISSILE_PERIOD / (60*60*1000000.0));
102
 
103
   x += dx;
104
   y += dy;
105
 
106
   xx0 = old_x - MISSILE_LX/2;
107
   yy0 = old_y - MISSILE_LY/2;
108
   xx1 = xx0   + MISSILE_LX-1;
109
   yy1 = yy0   + MISSILE_LY-1;
110
 
111
   if (x<MISSILE_X_MIN || x>MISSILE_X_MAX
112
     || y<MISSILE_Y_MIN || y>MISSILE_Y_MAX) {
113
      killing = 1;
114
      }
115
 
116
   if (killing) {
117
      if (!first_time) {
118
          mutex_lock(&grx_mutex);
119
          FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,yy1);
120
          mutex_unlock(&grx_mutex);
121
          }
122
      cannone_table[index].fire = 0;
123
      //il task cannone si accorge che il suo missile ha finito
124
      // quindi ne ricreer… uno nuovo in caso di bersaglio!!!
125
      return NULL;
126
      }
127
 
128
   if ( (int)x != (int)old_x
129
      ||(int)y != (int)old_y ) {//...se c'Š lo spostamento reale
130
                                //        di almeno un pixel...
131
     mutex_lock(&grx_mutex);   //...aggiorna disegno...
132
     if (first_time) {
133
         first_time=0;
134
         }
135
     else {
136
         if (yy1<Y1-CANNONE_LY) FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,yy1);
137
         else                   FAB_image_put_within(image_bca,X0,Y0,xx0,yy0,xx1,Y1-CANNONE_LY);
138
         }
139
 
140
     FAB_image_put_within(image_missile[img++], x - MISSILE_LX/2, y - MISSILE_LY/2,
141
                                              X0, Y0,
142
                                              X1, Y1-CANNONE_LY);
143
     mutex_unlock(&grx_mutex);
144
     if (img==2) img = 0;
145
     }
146
 
147
   //Ha colpito un aereo?
148
   for (i=0; i<AEREO_N_MAX; i++)
149
       if (aereo_table[i].status){
150
          distx = aereo_table[i].x-x;
151
          disty = aereo_table[i].y-y;
152
          if (distx<0) distx *= -1;
153
          if (disty<0) disty *= -1;
154
          if ( distx<(AEREO_LX+MISSILE_LX)/2-1
155
             &&disty<(AEREO_LY+MISSILE_LY)/2-1 ) { // BERSAGLIO COLPITO!
156
               esplo_pid = crea_soft_esplo(i);
157
               task_activate(esplo_pid);
158
               killing=1;
159
               aereo_table[i].killing=1;
160
               break;
161
               }
162
          }
163
 
164
   task_endcycle();
165
 }
166
 
167
return NULL;
168
}
169
 
170
 
171
PID crea_hard_missile(int index)
172
{
173
  HARD_TASK_MODEL m;
174
  PID pid;
175
 
176
  hard_task_default_model(m);
1468 giacomo 177
  hard_task_def_level(m,1);
1090 pj 178
  hard_task_def_arg(m,(void*)index);
179
  hard_task_def_periodic(m);
180
  hard_task_def_wcet(m, MISSILE_WCET);
181
  hard_task_def_mit(m,MISSILE_PERIOD);
182
 
183
  pid = task_create("hard_missile", missile, &m, NULL);
184
  return pid;
185
}
186
 
187
PID crea_soft_missile(int index)
188
{
189
  SOFT_TASK_MODEL m;
190
  PID pid;
191
 
192
  soft_task_default_model(m);
1468 giacomo 193
  soft_task_def_level(m,2);
1090 pj 194
  soft_task_def_arg(m,(void*)index);
195
  soft_task_def_periodic(m);
196
  soft_task_def_period(m,MISSILE_PERIOD);
197
  soft_task_def_wcet(m, MISSILE_WCET);
198
  soft_task_def_met(m,MISSILE_MET);
199
 
200
  pid = task_create("soft_missile", missile, &m, NULL);
201
  return pid;
202
}