source: golgotha/src/golg/objs/bolt.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.8 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 "math/pi.hh"
10#include "math/trig.hh"
11#include "math/angle.hh"
12#include "g1_rand.hh"
13#include "saver.hh"
14#include "map_cell.hh"
15#include "map.hh"
16#include "map_man.hh"
17#include "g1_render.hh"
18#include "object_definer.hh"
19#include "objs/light_o.hh"
20#include "objs/bolt.hh"
21
22#include "time/profile.hh"
23
24#include "camera.hh"
25#include "resources.hh"
26#include "lisp/lisp.hh"
27
28static i4_profile_class pf_bolt("bolt_think");
29
30
31S1_SFX(fire_sfx, "fire/electric_tower_firing_three_22khz_lp.wav", S1_3D, 30);
32
33static li_symbol_ref light_type("lightbulb");
34
35
36g1_object_definer<g1_bolt_class>
37g1_bolt_def("bolt");
38
39g1_bolt_class::g1_bolt_class(g1_object_type id, g1_loader_class *fp)
40  : g1_object_class(id,fp)
41//{{{
42{   
43  memset(arc,0,sizeof(arc_point) * NUM_ARCS);
44  first = 1;
45  size = 0.8;
46  w1 = w2 = 0.02;
47  a1 = a2 = 1.0;
48  c1 = 0xffffff;
49  c2 = 0x8800ff;
50  lit = i4_F;
51}
52//}}}
53
54void g1_bolt_class::setup(const i4_3d_vector &start_pos,
55                          const i4_3d_vector &_target_pos,
56                          g1_object_class *_originator,
57                          g1_object_class *_target)
58//{{{
59{
60  move(start_pos);
61
62  ticks = 1;
63  originator = _originator;
64  target = _target;
65
66  if (target.valid())
67    target_pos.set(target->x,target->y,target->h);
68  else
69    target_pos = _target_pos;
70
71  if (!get_flag(MAP_OCCUPIED))
72  {
73    occupy_location();
74    request_think();
75  }
76
77  if (lit && get_flag(MAP_OCCUPIED))
78    create_light();
79}
80//}}}
81   
82g1_bolt_class::~g1_bolt_class()
83//{{{
84{
85}
86//}}}
87
88void g1_bolt_class::save(g1_saver_class *fp)
89//{{{
90{
91  g1_object_class::save(fp);
92}
93//}}}
94
95void g1_bolt_class::draw(g1_draw_context_class *context)
96//{{{
97
98  int i;
99  i4_3d_point_class pts[g1_bolt_class::NUM_ARCS];
100
101  for (i=0;i<g1_bolt_class::NUM_ARCS;i++)
102    pts[i].interpolate(arc[i].lposition,arc[i].position,g1_render.frame_ratio);
103 
104  g1_render.add_translucent_trail(context->transform,pts,
105                                  g1_bolt_class::NUM_ARCS,
106                                  w1,w2,a1,a2,c1,c2);
107}
108//}}}
109
110void g1_bolt_class::copy_old_points()
111//{{{
112{
113  sw32 i;
114  for (i=0;i<NUM_ARCS;i++)
115    arc[i].lposition = arc[i].position;
116}
117//}}}
118
119void g1_bolt_class::arc_to(const i4_3d_vector &target)
120//{{{
121{
122  i4_3d_vector r,p;
123  i4_float map_point_height;
124
125  i4_3d_vector point, ray;
126 
127  point.set(x,y,h);
128  ray = target;
129  ray -= point;
130  ray /= NUM_ARCS-1;
131
132  sw32 i;
133
134  for (i=0; i<NUM_ARCS; i++)
135  {
136    i4_float sin_factor = sin(i4_pi()*i/(NUM_ARCS-1));
137
138    r.x = ((g1_rand(23)&0xFFFF)/((i4_float)0xffff) - 0.5) * size*sin_factor;
139    r.y = ((g1_rand(32)&0xFFFF)/((i4_float)0xffff) - 0.5) * size*sin_factor;
140    r.z = ((g1_rand(45)&0xFFFF)/((i4_float)0xffff) - 0.5) * size*sin_factor;
141   
142    p.set(point.x + ray.x*i + r.x,
143          point.y + ray.y*i + r.y,
144          point.z + ray.z*i + r.z);
145   
146    map_point_height = g1_get_map()->map_height(p.x,p.y,p.z) + 0.05;
147 
148    if (p.z < map_point_height)
149      p.z = map_point_height;
150 
151    arc[i].position = p;
152  }
153
154  if (end_light.get())
155    end_light->move(target.x, target.y, target.z);
156}
157//}}}
158
159void g1_bolt_class::think()
160//{{{
161{
162  pf_bolt.start();
163  if (target.valid())
164    g1_apply_damage(this, originator.get(), target.get(), i4_3d_vector(0,0,0));
165  pf_bolt.stop();
166}
167//}}}
168
169void g1_bolt_class::request_remove()
170{
171  s1_end_looping(sfx_loop);
172}
173
174void g1_bolt_class::post_think()
175//{{{
176{
177  if (ticks<=0)
178  {
179    unoccupy_location();
180    destroy_light();
181    request_remove();
182    return ;
183  }
184
185  copy_old_points();
186
187  if (target.valid())
188    target_pos.set(target->x,target->y,target->h);
189
190  arc_to(target_pos);
191
192  if (first)
193    copy_old_points();
194
195  first=0;
196
197  ticks--;
198  request_think();
199}
200//}}}
201
202void g1_bolt_class::move(const i4_3d_vector &pos)
203//{{{
204{
205  x = pos.x;
206  y = pos.y;
207  h = pos.z;
208
209  if (light.valid())
210    light->move(x,y,h);
211}
212//}}}
213
214void g1_bolt_class::create_light()
215{
216  sfx_loop.setup(x,y,h,0,0,0,1);
217  fire_sfx.play_looping(sfx_loop);
218
219  if (!light.get())
220  {
221    float r=g1_resources.visual_radius();
222    if (g1_current_view_state()->dist_sqrd(i4_3d_vector(x,y,h))<r*r)
223    {
224      light = (g1_light_object_class *)g1_create_object(g1_get_object_type(light_type.get()));
225      light->setup(x,y,h+0.3,
226                   i4_float(c1>>16&0xff)/256.0,
227                   i4_float(c1>>8&0xff)/256.0,
228                   i4_float(c1&0xff)/256.0,
229                   1);
230      light->occupy_location();
231     
232      end_light = (g1_light_object_class *)g1_create_object(g1_get_object_type(light_type.get()));
233      end_light->setup(target_pos.x, target_pos.y, target_pos.z,
234                       i4_float(c2>>16&0xff)/256.0,
235                       i4_float(c2>>8&0xff)/256.0,
236                       i4_float(c2&0xff)/256.0,
237                       1);
238      end_light->occupy_location();
239    }
240  }
241}
242
243void g1_bolt_class::destroy_light()
244{
245  s1_end_looping(sfx_loop);
246
247  if (light.get())
248  {
249    light->unoccupy_location();
250    light->request_remove();
251    light=0;
252  }
253   
254  if (end_light.get())
255  {
256    end_light->unoccupy_location();
257    end_light->request_remove();
258    end_light=0;
259  }
260}
261
262//{{{ Emacs Locals
263// Local Variables:
264// folded-file: t
265// End:
266//}}}
Note: See TracBrowser for help on using the repository browser.