source: golgotha/src/golg/objs/peontank.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: 6.1 KB
Line 
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#include "objs/rocktank.hh"
13#include "objs/peontank.hh"
14#include "input.hh"
15#include "math/pi.hh"
16#include "math/trig.hh"
17#include "math/angle.hh"
18#include "g1_rand.hh"
19#include "resources.hh"
20#include "saver.hh"
21#include "objs/vehic_sounds.hh"
22#include "sound/sfx_id.hh"
23#include "object_definer.hh"
24#include "g1_render.hh"
25#include "objs/fire.hh"
26#include "lisp/lisp.hh"
27
28#include "image_man.hh"
29static g1_team_icon_ref radar_im("bitmaps/radar/peon_tank.tga");
30
31// Constants
32enum { DATA_VERSION=2 };
33enum { ORIGNAL_RUMBLE_VOLUME=I4_SOUND_VOLUME_LEVELS/8 };
34 
35static g1_model_ref model_ref("peon_body"),
36  shadow_ref("peon_body_shadow"),
37  turret_ref("peon_turret"),
38  lod_bot("peon_body_lod"),
39  lod_top("peon_turret_lod");
40
41static g1_object_type bullet;
42static i4_3d_vector turret_attach;
43
44void g1_peon_tank_init()
45{
46  bullet = g1_get_object_type("b90mm");
47
48  turret_attach.set(0,0,0);
49  model_ref()->get_mount_point("Turret", turret_attach);
50}
51
52g1_object_definer<g1_peon_tank_class>
53g1_peon_tank_def("peon_tank",
54                 g1_object_definition_class::EDITOR_SELECTABLE |
55                 g1_object_definition_class::MOVABLE |
56                 g1_object_definition_class::TO_MAP_PIECE,
57                 g1_peon_tank_init);
58
59
60static char *chunk_list[3]={"chunk_peon_barrel", "chunk_peon_body", "chunk_peon_turret"};
61                             
62                             
63int g1_peon_tank_class::get_chunk_names(char **&list) { list=chunk_list; return 3; }
64
65
66g1_peon_tank_class::g1_peon_tank_class(g1_object_type id,
67                                       g1_loader_class *fp)
68  : g1_map_piece_class(id, fp)
69
70
71  radar_type=G1_RADAR_VEHICLE; 
72  radar_image=&radar_im;
73  set_flag(BLOCKING      |
74           TARGETABLE    |
75           GROUND        |
76           HIT_GROUND    |
77           DANGEROUS,1);
78
79  draw_params.setup(model_ref.id(),turret_ref.id(), lod_bot.id());
80
81  allocate_mini_objects(2,"Peon Tank Mini-objects");
82  turret = &mini_objects[0];
83
84  turret->defmodeltype = turret_ref.id();
85  turret->lod_model = lod_top.id();
86  turret->position(turret_attach);
87
88  w16 ver=0,data_size;
89
90  if (fp)
91    fp->get_version(ver,data_size);
92
93  switch (ver)
94  {
95    case DATA_VERSION:
96      fp->read_format("fff",
97                      &turret->rotation.x,
98                      &turret->rotation.y,
99                      &turret->rotation.z);
100      turret->grab_old();
101      break;
102
103    case 1:
104      turret->rotation.x = fp->read_float();
105      turret->rotation.y = fp->read_float();
106      turret->rotation.z = fp->read_float();   
107       
108      turret->lrotation.x = fp->read_float();
109      turret->lrotation.y = fp->read_float();
110      turret->lrotation.z = fp->read_float();   
111      break;
112
113    default:
114      if (fp)
115        fp->seek(fp->tell() + data_size);
116      turret->rotation.x = 0;
117      turret->rotation.y = 0;
118      turret->rotation.z = 0;
119      turret->lrotation = turret->rotation;
120      fire_delay = 0;
121      break;
122  }
123
124  if (fp)
125    fp->end_version(I4_LF);
126
127
128  init_rumble_sound(G1_RUMBLE_GROUND); 
129
130  guard_angle = 0;
131  guard_time = 0;
132  tread_pan=0.0;
133
134  turret_kick = lturret_kick = 0;
135}
136
137
138
139void g1_peon_tank_class::save(g1_saver_class *fp)
140{
141  g1_map_piece_class::save(fp);
142
143  fp->start_version(DATA_VERSION);
144
145  fp->write_format("fff",
146                   &turret->rotation.x,
147                   &turret->rotation.y,
148                   &turret->rotation.z);
149 
150  fp->end_version();
151 
152}
153
154   
155void g1_peon_tank_class::fire()
156{
157  g1_object_class *target=attack_target.get();
158  if (target)
159  {   
160    fire_delay = defaults->fire_delay;
161    i4_3d_vector pos, dir;
162   
163    i4_transform_class t, main, l2w;
164    turret->calc_transform(1.0, &t);   
165    calc_world_transform(1.0, &main);
166    l2w.multiply(main, t);
167   
168    l2w.transform(i4_3d_point_class(0.4, 0, 0.11), pos);
169    dir=l2w.x;
170   
171    g1_fire(defaults->fire_type, this, target, pos, dir);
172  }
173}
174
175void g1_peon_tank_class::think()
176
177  g1_map_piece_class::think();
178
179  if (!alive())
180    return;
181
182  //turret_kick thing
183  lturret_kick = turret_kick;
184
185  if (turret_kick < 0.f)
186  {
187    turret_kick += 0.01;
188   
189    if (turret_kick >= 0)
190      turret_kick = 0;
191   
192    request_think();
193  }
194 
195  //aim the turret
196  if (attack_target.valid())
197  {
198    request_think();
199           
200    i4_float dx,dy,dangle;
201
202    i4_3d_point_class pos;
203
204    lead_target(pos);
205
206    //this will obviously only be true if attack_target.ref != NULL   
207    dx = ((x+turret->x) - pos.x);
208    dy = ((y+turret->y) - pos.y);
209
210    //aim the turret
211   
212    guard_angle = i4_atan2(dy,dx) + i4_pi() - theta;
213    i4_normalize_angle(guard_angle);
214   
215    dangle = i4_rotate_to(turret->rotation.z, guard_angle, defaults->turn_speed);
216    if (dangle<defaults->turn_speed && dangle>-defaults->turn_speed)
217      if (!fire_delay)
218        fire();
219    guard_time = 30;
220  }
221  else
222  {
223    request_think();            // move this to draw function
224
225    if (moved())
226    {
227      guard_angle = 0;
228      guard_time = 30;
229    }
230    else if (guard_time<=0)
231    {
232      guard_angle = g1_float_rand(83)*i4_2pi();
233      guard_time = 20;
234    }
235
236    if (i4_rotate_to(turret->rotation.z, guard_angle, defaults->turn_speed)==0)
237      if (guard_time>0)
238        guard_time--;
239  }
240}
241
242void g1_peon_tank_class::draw(g1_draw_context_class *context)
243{
244  turret->offset.x = i4_interpolate(lturret_kick, turret_kick, g1_render.frame_ratio);
245
246  g1_map_piece_class::draw(context);
247}
Note: See TracBrowser for help on using the repository browser.