Subversion Repositories shark

Rev

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