Subversion Repositories shark

Rev

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