source: golgotha/src/golg/objs/jet.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 8.2 KB
RevLine 
[80]1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "sound_man.hh"
10#include "objs/model_id.hh"
11#include "objs/model_draw.hh"
12
13#include "input.hh"
14#include "math/pi.hh"
15#include "math/trig.hh"
16#include "math/angle.hh"
17#include "resources.hh"
18#include "saver.hh"
19#include "map_cell.hh"
20#include "map.hh"
21#include "map_man.hh"
22#include "sound/sfx_id.hh"
23#include "objs/vehic_sounds.hh"
24#include "objs/jet.hh"
25#include "objs/fire.hh"
26#include "object_definer.hh"
27#include "objs/path_object.hh"
28
29#include "image_man.hh"
30static g1_team_icon_ref radar_im("bitmaps/radar/plane.tga");
31
32
33const i4_float FLY_HEIGHT = 1.5f;
34
35enum
36{
37  TAKE_OFF=0,
38  TAKE_OFF2,
39  FLYING,
40  DYING
41};
42
43static g1_object_type bomb_type;
44void g1_jet_init()
45{
46  bomb_type = g1_get_object_type("dropped_bomb");
47}
48
49g1_object_definer<g1_jet_class>
50g1_jet_def("jet",
51           g1_object_definition_class::TO_MAP_PIECE |
52           g1_object_definition_class::EDITOR_SELECTABLE |
53           g1_object_definition_class::MOVABLE,
54           g1_jet_init);
55
56
57g1_jet_class::g1_jet_class(g1_object_type id,
58                           g1_loader_class *fp)
59  : g1_map_piece_class(id, fp)
60
61  allocate_mini_objects(1,"Jet Mini-Objects");
62  engines = &mini_objects[0];
63
64  engines->h = engines->lh = 0.05;
65  engines->x = engines->lx = 0.01;
66  engines->y = engines->ly = 0;
67 
68  w16 ver,data_size;
69  if (fp)
70    fp->get_version(ver,data_size);
71  else
72    ver =0;
73  switch (ver)
74  {
75    case DATA_VERSION:
76      fp->read_format("1ffff4",
77                      &mode,
78                      &engines->rotation.x, &engines->rotation.y, &engines->rotation.z,
79                      &vspeed,
80                      &sway);
81      engines->grab_old();
82      break;
83    case 2:
84      mode = fp->read_8();
85
86      engines->rotation.x = fp->read_float();
87      engines->rotation.y = fp->read_float();
88      engines->rotation.z = fp->read_float();
89      engines->lrotation.x = fp->read_float();
90      engines->lrotation.y = fp->read_float();
91      engines->lrotation.z = fp->read_float();
92       
93      vspeed  = fp->read_float();
94      sway = fp->read_32();
95      break;
96    case 1:
97      mode = 0;
98      if (fp->read_8()) mode=TAKE_OFF;  // take_off
99      if (fp->read_8()) mode=TAKE_OFF;  // taking_off
100      if (fp->read_8()) mode=FLYING;    // flying
101      fp->read_8();
102      fp->read_8();
103
104      engines->rotation.x = fp->read_float();
105      engines->rotation.y = fp->read_float();
106      engines->rotation.z = fp->read_float();
107      engines->lrotation.x = fp->read_float();
108      engines->lrotation.y = fp->read_float();
109      engines->lrotation.z = fp->read_float();
110
111      vspeed  = fp->read_float();
112      fp->read_float();
113      sway = 0;
114      break;
115    default:
116      if (fp) fp->seek(fp->tell() + data_size);
117      mode = 0;
118      engines->rotation.x = engines->lrotation.x = 0;
119      engines->rotation.y = engines->lrotation.y = 0;
120      engines->rotation.z = engines->lrotation.z = 0;
121      vspeed = 0;
122      sway = 0;
123      break;
124  }
125     
126  if (fp)
127    fp->end_version(I4_LF);
128
129  engines->defmodeltype = g1_model_list_man.find_handle("jet_engines"); 
130 
131  draw_params.setup("jet_body","jet_shadow","jet_lod");
132
133  init_rumble_sound(G1_RUMBLE_JET);
134
135  damping_fraction = 0.02;
136
137  radar_image=&radar_im;
138  radar_type=G1_RADAR_VEHICLE; 
139  set_flag(BLOCKING      |
140           TARGETABLE    |
141           AERIAL        |
142           HIT_AERIAL    |
143           DANGEROUS,   1);
144
145}
146   
147void g1_jet_class::save(g1_saver_class *fp)
148{
149  g1_map_piece_class::save(fp);
150
151  fp->start_version(DATA_VERSION);
152 
153  fp->write_format("1ffff4",
154                   &mode,
155                   &engines->rotation.x, &engines->rotation.y, &engines->rotation.z,
156                   &vspeed,
157                   &sway);
158 
159  fp->end_version();
160}
161
162void g1_jet_class::fire()
163{
164  fire_delay = g1_jet_def.defaults->fire_delay;
165
166  i4_3d_vector p(x,y,h), dir(attack_target->x, attack_target->y, attack_target->h);
167
168  dir -= p;
169
170  g1_fire(defaults->fire_type,
171          this, attack_target.get(), p, dir);         
172}
173
174i4_bool g1_jet_class::move(i4_float x_amount, i4_float y_amount)
175{
176  unoccupy_location();
177  x+=x_amount;
178  y+=y_amount;
179
180  if (occupy_location())
181  {
182    g1_add_to_sound_average(rumble_type, i4_3d_vector(x,y,h));   
183    return i4_T;
184  }
185  return i4_F;
186}
187
188
189/* DLL stuff */
190
191void g1_jet_class::think()
192{
193  if (!check_life())
194    return;
195
196  find_target();
197
198  if (fire_delay>0)
199    fire_delay--;
200
201  h      += vspeed;
202       
203  switch (mode)
204  {
205    case TAKE_OFF:
206    {
207      // rotate engines
208      if (i4_rotate_to(engines->rotation.y,3*i4_pi()/2,0.1)==0.0)
209        mode = TAKE_OFF2;
210    } break;
211
212    case TAKE_OFF2:
213    {
214      if (next_path.valid())
215      {
216        dest_x = next_path->x - path_cos*path_len;
217        dest_y = next_path->y - path_sin*path_len;
218        dest_z = next_path->h - path_tan_phi*path_len;
219      }
220
221      //then raise the jet
222      i4_float dist_to_go = dest_z - h;
223      if (dist_to_go>0.05)
224      {
225        //if he is more than halfway away, accelerate down
226        //otherwise accelerate up       
227        if (dist_to_go > (FLY_HEIGHT * 0.5))
228          vspeed = (vspeed<0.05) ? vspeed+0.005 : vspeed;
229      }
230      else
231      {
232        //lock these in case there were any small errors
233        h = dest_z;
234        vspeed = 0;
235       
236        //think one more time so the l* variables catch up
237        //(otherwise he'll bob up and down)
238        if (h == lh)
239          mode = FLYING;
240      }
241    } break;
242
243    case FLYING:
244    {
245      i4_3d_vector d;
246      i4_float angle,t;
247
248      // sway over terrain
249      sway++;
250
251      if (attack_target.valid())
252        if (fire_delay==0)
253          fire();
254
255      if (next_path.valid())
256      {
257        dest_x = next_path->x;
258        dest_y = next_path->y;
259        dest_z = next_path->h;
260      }
261
262      i4_float roll_to  = 0.02*sin(i4_pi()*sway/17.0);
263      i4_float pitch_to = 0.02*sin(i4_pi()*sway/13.0);
264     
265      i4_float dist, dtheta;
266      suggest_air_move(dist, dtheta, d);
267      move(d.x,d.y);
268      h += d.z + 0.02*sin(i4_pi()*sway/15.0);
269       
270      if (dist<speed)
271        advance_path();
272     
273      i4_normalize_angle(roll_to);
274       
275      if (speed>0.001 || dtheta!=0.0)
276      {
277        roll_to = -i4_pi()/4 * dtheta;
278        pitch_to = -i4_pi()/8 * speed;
279      }
280
281      i4_rotate_to(roll,roll_to,defaults->turn_speed/4); 
282      i4_rotate_to(pitch,pitch_to,defaults->turn_speed/4); 
283     
284      groundpitch = 0; //no ground in the air (duh)
285      groundroll  = 0;
286    } break;
287
288    case DYING:
289    {
290      i4_float &roll_speed  = dest_x;
291      i4_float &pitch_speed = dest_y;
292     
293      pitch_speed += 0.004;
294      pitch += pitch_speed;
295
296      roll_speed -= 0.012;
297      roll += roll_speed;
298
299      vspeed -= (g1_resources.gravity * 0.1);
300
301      i4_float dx,dy;
302      dx = speed*cos(theta);
303      dy = speed*sin(theta);
304      move(dx,dy);
305
306      if (h<=terrain_height)
307        g1_map_piece_class::damage(0,health,i4_3d_vector(0,0,1));               // die somehow!!!
308    } break;
309  }
310
311  // have to keep thinking to sway
312  request_think(); 
313}
314
315void g1_jet_class::damage(g1_object_class *obj, int hp, i4_3d_vector _damage_dir)
316{
317  //we dont want to explode if ppl shoot us while we're dying.. we want to
318  //smash into the ground and create a nice explosion
319  if (mode != DYING)
320  {
321    g1_map_piece_class::damage(obj,hp,_damage_dir);
322    if (health<20)
323    {
324      i4_float &roll_speed  = dest_x;
325      i4_float &pitch_speed = dest_y;
326     
327      health = 20;
328      set_flag(DANGEROUS,0);
329      roll_speed  = 0;
330      pitch_speed = 0;
331      mode = DYING;
332    }
333  }
334}
Note: See TracBrowser for help on using the repository browser.