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

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 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.2 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/repairer.hh"
13#include "input.hh"
14#include "math/pi.hh"
15#include "math/angle.hh"
16#include "math/trig.hh"
17#include "math/random.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
25enum {DATA_VERSION=1};
26
27g1_object_definer<g1_repairer_class>
28g1_repairer_def("repairer", g1_object_definition_class::EDITOR_SELECTABLE);
29
30g1_repairer_class::g1_repairer_class(g1_object_type id,
31                                     g1_loader_class *fp)
32  : g1_map_piece_class(id,fp)
33
34  draw_params.setup("repair_base");
35  defaults = g1_repairer_def.defaults;
36 
37  allocate_mini_objects(3,"repair_objects");
38
39  turret = &mini_objects[0];
40  turret->x = turret->lx = 0;
41  turret->y = turret->ly = 0;
42  turret->h = turret->lh = 0;
43  turret->rotation.set(0,0,0);
44  turret->defmodeltype = g1_model_list_man.find_handle("repair_turret");
45  turret->grab_old();
46 
47  boom = &mini_objects[1];
48  boom->x = turret->lx = turret->x;
49  boom->y = turret->ly = turret->y;
50  boom->h = turret->lh = turret->h;
51  boom->rotation.set(0,0,0);
52  boom->defmodeltype = g1_model_list_man.find_handle("repair_boom");
53  boom->grab_old();
54 
55  tip = &mini_objects[2];
56  tip->x = tip->lx = g1_resources.repairer_tip_attach.x;
57  tip->y = tip->ly = g1_resources.repairer_tip_attach.y;
58  tip->h = tip->lh = g1_resources.repairer_tip_attach.z;
59  tip->rotation.set(0,-i4_pi()/2.0,0);
60  tip->defmodeltype = g1_model_list_man.find_handle("repair_tip");
61  tip->grab_old();
62 
63  w16 ver,data_size;
64  if (fp)
65    fp->get_version(ver,data_size);
66  else
67    ver =0;
68  switch (ver)
69  {
70    case DATA_VERSION:
71      break;
72    default:
73      if (fp) fp->seek(fp->tell() + data_size);
74      health = defaults->health;
75      break;
76  }
77
78  if (fp)
79    fp->end_version(I4_LF);
80
81  length = g1_resources.repairer_boom_offset;
82
83  set_flag(BLOCKING      |
84           SHADOWED,
85           1);
86}
87
88void g1_repairer_class::save(g1_saver_class *fp)
89{
90  g1_map_piece_class::save(fp);
91 
92  fp->start_version(DATA_VERSION);
93
94  fp->end_version();
95}
96
97i4_bool g1_repairer_class::can_attack(g1_object_class *who) const
98{
99  if (who->player_num == player_num)
100  {
101    g1_map_piece_class *mp = g1_map_piece_class::cast(who);
102    return (mp && mp->get_flag(GROUND) && mp->health>0 && mp->health<mp->defaults->health);
103  }
104  return i4_F;
105}
106
107void g1_repairer_class::think()
108
109  find_target();
110
111  if (health < defaults->health)
112    health++;
113
114  if (fire_delay>0)
115    fire_delay--;
116
117  pitch = 0;
118  roll  = 0;
119  h = terrain_height;
120
121  if (attack_target.valid())
122  {
123    i4_float dx,dy;
124
125    //this will obviously only be true if attack_target.ref != NULL   
126    dx = (attack_target->x - x);
127    dy = (attack_target->y - y);
128
129    //aim the turet
130   
131    repair_angle = i4_atan2(dy,dx);
132    i4_normalize_angle(repair_angle);
133    repair_length = sqrt(dx*dx + dy*dy);
134  }
135  else
136  {
137    repair_angle = 0;
138    repair_length = 1.5;
139  }
140
141  int aimed;
142  i4_float dangle;
143 
144  aimed = (i4_rotate_to(turret->rotation.z,repair_angle,defaults->turn_speed)==0.0);
145  if (length<repair_length)
146  {
147    length += speed;
148    if (length>repair_length) length=repair_length;
149  }
150  else
151  {
152    length -= speed;
153    if (length<repair_length) length=repair_length;
154  }
155  i4_float
156    cs = cos(turret->rotation.z),
157    sn = sin(turret->rotation.z),
158    ex = length - g1_resources.repairer_boom_offset;
159  boom->rotation.z = turret->rotation.z;
160  tip->rotation.z = turret->rotation.z;
161  boom->x = cs*ex;
162  boom->y = sn*ex;
163  ex += g1_resources.repairer_tip_attach.x;
164  tip->x = cs*ex;
165  tip->y = sn*ex;
166
167  if (attack_target.valid() && aimed)
168  {
169    if (i4_rotate_to(tip->rotation.y,-i4_pi()/6.0,0.1)==0.0 && fire_delay==0)
170    {
171      g1_map_piece_class *mp = g1_map_piece_class::cast(attack_target.get());
172     
173      if (mp->health<mp->defaults->health && mp->health>0)
174      {
175        fire_delay = defaults->fire_delay;
176        mp->health+=get_type()->get_damage_map()->get_damage_for(mp->id);
177       
178        if (mp->health>mp->defaults->health)
179          mp->health = mp->defaults->health;
180      }
181      else
182        attack_target = 0;
183    }
184  }
185  else
186    // return to stopped position
187    i4_rotate_to(tip->rotation.y,-i4_pi()/2.0,0.1);
188
189  // don't rethink if i'm not healing or moving
190  if (attack_target.valid() ||
191      tip->rotation.y != tip->lrotation.y ||
192      tip->rotation.z != tip->lrotation.z ||
193      tip->x != tip->lx ||
194      tip->y != tip->ly)
195    request_think();
196}
197
198void g1_repairer_class::draw(g1_draw_context_class *context)
199{
200  g1_model_draw(this, draw_params, context);
201}
Note: See TracBrowser for help on using the repository browser.