source: golgotha/src/golg/objs/turret.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: 5.3 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/turret.hh"
13#include "input.hh"
14#include "math/pi.hh"
15#include "math/angle.hh"
16#include "math/trig.hh"
17#include "g1_rand.hh"
18#include "resources.hh"
19#include "saver.hh"
20#include "map_cell.hh"
21#include "map.hh"
22#include "map_man.hh"
23#include "object_definer.hh"
24#include "lisp/lisp.hh"
25#include "objs/fire.hh"
26
27enum {DATA_VERSION=1};
28 
29static g1_model_ref model_ref("aagun_base"),
30  turret_ref("aagun_turret"),
31  barrels_ref("aagun_barrels");
32
33static g1_object_type bullet;
34static i4_3d_vector turret_attach, muzzle_attach;
35
36void g1_turret_init()
37{
38  bullet = g1_get_object_type("bullet");
39
40  turret_attach.set(0,0,0);
41  model_ref()->get_mount_point("Turret", turret_attach);
42  muzzle_attach.set(0,0,0.15);
43  model_ref()->get_mount_point("Muzzle", muzzle_attach);
44}
45
46g1_object_definer<g1_turret_class>
47g1_turret_def("turret", g1_object_definition_class::EDITOR_SELECTABLE, g1_turret_init);
48
49void g1_turret_class::setup(i4_float _x, i4_float _y, g1_object_class *creator)
50{
51  x = x;
52  y = y;
53  player_num = creator->player_num;
54  occupy_location();
55  request_think();
56  grab_old();
57}
58
59g1_turret_class::g1_turret_class(g1_object_type id,
60                                 g1_loader_class *fp)
61  : g1_map_piece_class(id,fp)
62
63  defaults = g1_turret_def.defaults;
64  draw_params.setup(model_ref.id());
65 
66  allocate_mini_objects(2,"turret mini objects");
67
68  muzzle = &mini_objects[0];
69  muzzle->defmodeltype = barrels_ref.id();
70  muzzle->position(muzzle_attach);
71  muzzle->rotation.set(0,0,0);
72 
73  top = &mini_objects[1];
74  top->defmodeltype = turret_ref.id();
75  top->position(turret_attach);
76  top->rotation.set(0,0,0);
77  top->grab_old();
78 
79  if (fp && fp->check_version(DATA_VERSION))
80  {
81    fp->end_version(I4_LF);
82  }
83  else
84  {
85    health = defaults->health;
86  }
87
88  guard_angle = 0;
89  guard_time = 0;
90
91  radar_type=G1_RADAR_BUILDING;
92  set_flag(BLOCKING      |
93           SELECTABLE    |
94           TARGETABLE    |
95           GROUND        |
96           HIT_GROUND    |
97           HIT_AERIAL    |
98           DANGEROUS     |
99           SHADOWED, 1);
100}
101
102void g1_turret_class::save(g1_saver_class *fp)
103{
104  g1_map_piece_class::save(fp);
105 
106  fp->start_version(DATA_VERSION);
107
108  fp->end_version();
109}
110
111
112void g1_turret_class::fire()
113{
114  fire_delay = defaults->fire_delay;
115 
116  //recoil
117  if (muzzle->offset.x != -0.2)
118    muzzle->offset.x = -0.2;
119
120  i4_3d_point_class tpos, bpos, bvel;
121  i4_transform_class btrans;
122 
123  btrans.rotate_x_y_z(muzzle->rotation.x,muzzle->rotation.y,muzzle->rotation.z,i4_T);
124  btrans.t = i4_3d_vector(muzzle->x,muzzle->y,muzzle->h);
125  btrans.t += i4_3d_vector(x,y,h);
126
127  btrans.transform(i4_3d_point_class(1.0,order?-0.15:0.15,0),bpos);
128  btrans.transform_3x3(i4_3d_point_class(g1_resources.bullet_speed,0,0),bvel);
129  order = !order;
130
131  g1_fire(defaults->fire_type, this, attack_target.get(), bpos, bvel);
132}
133
134
135void g1_turret_class::think()
136
137  //undo the recoil
138  if (muzzle->offset.x < 0)
139    muzzle->offset.x += 0.01;
140 
141  if (muzzle->offset.x > 0)
142    muzzle->offset.x = 0;
143
144  find_target();
145
146  if (health < defaults->health)
147    health++;
148
149  if (fire_delay>0)
150    fire_delay--;
151
152  pitch = 0;//groundpitch*cos(theta) - groundroll *sin(theta);
153  roll  = 0;//groundroll *cos(theta) + groundpitch*sin(theta);
154  h = terrain_height;
155
156  //aim the turet
157  if (attack_target.valid())
158  {
159    request_think();
160
161    i4_3d_point_class d,pos;
162
163    lead_target(d);
164
165    pos.set(x,y,h);
166    d -= pos;
167
168    //aim the turet
169   
170    guard_angle = i4_atan2(d.y,d.x);
171    guard_pitch = i4_atan2(-d.z,sqrt(d.x*d.x+d.y*d.y));
172   
173    i4_normalize_angle(guard_angle);
174    i4_normalize_angle(guard_pitch);
175
176    int aimed=i4_F;
177    i4_float dangle;
178
179    dangle = i4_rotate_to(muzzle->rotation.z,guard_angle,defaults->turn_speed);
180    aimed = (dangle<defaults->turn_speed && dangle>-defaults->turn_speed);
181    dangle = i4_rotate_to(muzzle->rotation.y,guard_pitch,defaults->turn_speed);
182    aimed &= (dangle<defaults->turn_speed && dangle>-defaults->turn_speed);
183    top->rotation.z = muzzle->rotation.z;
184    if (aimed)
185      if (!fire_delay)
186        fire();
187    guard_time = 30;
188  }
189  else
190  {
191    request_think();            // move this to draw function
192
193    if (guard_time<=0)
194    {
195      guard_angle = g1_float_rand(4)*i4_2pi();
196      guard_pitch = -g1_float_rand(5)*i4_pi()*0.5;
197      guard_time = 20;
198    }
199
200    int aimed;
201
202    aimed = (i4_rotate_to(muzzle->rotation.z,guard_angle,defaults->turn_speed)==0.0);
203    aimed &= (i4_rotate_to(muzzle->rotation.y,guard_pitch,defaults->turn_speed)==0.0);
204    top->rotation.z = muzzle->rotation.z;
205    if (aimed)
206      if (guard_time>0)
207        guard_time--;
208  }
209}
Note: See TracBrowser for help on using the repository browser.