source: golgotha/src/golg/objs/beam_weapon.cc

Last change on this file 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: 6.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 "objs/beam_weapon.hh"
10#include "objs/shrapnel.hh"
11#include "sound/sfx_id.hh"
12#include "map.hh"
13#include "map_man.hh"
14#include "objs/defaults.hh"
15#include "lisp/lisp.hh"
16#include "math/random.hh"
17#include "g1_render.hh"
18#include "object_definer.hh"
19#include "g1_texture_id.hh"
20#include "math/pi.hh"
21#include "draw_context.hh"
22#include "r1_clip.hh"
23#include "tick_count.hh"
24
25li_symbol_ref li_shrapnel("shrapnel"), beam("beam"), plasma("plasma");
26
27g1_beam_weapon_class::g1_beam_weapon_class(g1_object_type id,
28                                       g1_loader_class *fp)
29  : g1_object_class(id,fp)
30{   
31  set_flag(AERIAL | HIT_GROUND, 1);
32  range=0;
33  idle_ticks=0;
34  should_draw=0;
35}
36
37void g1_beam_weapon_class::setup(const i4_3d_vector &pos,
38                               const i4_3d_vector &direction,
39                               g1_object_class *this_guy_fired_me,
40                               float range)
41{
42  x=pos.x;
43  y=pos.y;
44  h=pos.z;
45
46  who_fired_me=this_guy_fired_me;
47
48  if (this_guy_fired_me)
49  {
50    lx=pos.x + (this_guy_fired_me->lx - this_guy_fired_me->x);
51    ly=pos.y + (this_guy_fired_me->ly - this_guy_fired_me->y);
52    lh=pos.z + (this_guy_fired_me->lh - this_guy_fired_me->h);
53  }
54  else
55  {
56    lx=x;
57    ly=y;
58    lh=h;
59  }
60
61  if (!get_flag(MAP_OCCUPIED))
62    occupy_location();
63 
64  i4_3d_vector dir=direction;
65  dir.normalize();
66
67  // sfxfix 
68//   if (!fire_sound)
69//   {
70//     fire_sound=g1_sound_man.alloc_dynamic_3d_sound(g1_sfx_fire_supertank_auto1);
71//     g1_sound_man.update_dynamic_3d_sound(fire_sound, pos, dir, 1.0);
72//   }
73
74  idle_ticks=0;
75  should_draw=1;
76
77  dir*=range;
78 
79  g1_object_class *hit=0;
80 
81  if (g1_get_map()->check_non_player_collision(player_num,pos,dir,hit))
82  {
83    if (hit)
84      g1_apply_damage(this, this_guy_fired_me, hit, dir);
85       
86    g1_shrapnel_class *shrapnel = NULL;
87    shrapnel = (g1_shrapnel_class *)g1_create_object(g1_get_object_type(li_shrapnel.get()));
88    i4_3d_vector pos=i4_3d_vector(x,y,h);
89    pos+=dir;
90   
91    if (shrapnel)
92    {
93      i4_float rx,ry,rh;
94      rx = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2;
95      ry = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2;
96      rh = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2;
97      shrapnel->setup(pos.x+rx, pos.y+ry, pos.z + rh,1, i4_F);
98    }
99  }
100
101  end_point=pos;
102  end_point+=dir;
103  request_think();
104}
105 
106void g1_beam_weapon_class::think()
107{
108  if (idle_ticks>5)
109  {
110    unoccupy_location();
111    request_remove();
112  }
113  else
114  {
115    request_think();
116    idle_ticks++;
117  }
118}
119
120void g1_beam_weapon_class::unoccupy_location()
121{
122//   if (fire_sound) sfxfix
123//   {
124//     g1_sound_man.free_dynamic_3d_sound(fire_sound);
125//     fire_sound=0;
126//   }
127
128  g1_object_class::unoccupy_location();
129}
130
131
132i4_bool g1_beam_weapon_class::occupy_location()
133{
134  float w=g1_get_map()->width(), h=g1_get_map()->height();
135 
136  if (!(x>=0 && x<w && y>=0 && y<h))
137  {
138    request_remove();
139    return i4_F;
140  }
141
142  if (!(end_point.x>=0 && end_point.y>=0 && end_point.x<w && end_point.y<h))
143  {
144    request_remove();
145    return i4_F;
146  }
147
148  g1_map_class *map=g1_get_map();
149  map->add_object(*new_occupied_square(), i4_f_to_i(x), i4_f_to_i(y));
150  map->add_object(*new_occupied_square(), i4_f_to_i(end_point.x), i4_f_to_i(end_point.y)); 
151
152  set_flag(MAP_OCCUPIED,1);
153 
154  return i4_T;
155}
156
157
158
159static void setup_p(r1_vert &rv, const i4_3d_vector &p,  float a, float r, float g, float b)
160{
161  rv.v.x=p.x;
162  rv.v.y=p.y;
163  rv.v.z=p.z;
164  rv.a=a;
165  rv.r=r;
166  rv.g=g;
167  rv.b=b;
168}
169                   
170
171static void draw_sprite(const i4_3d_vector &p, r1_texture_handle tex, float sprite_scale)
172{
173
174  if (p.z > r1_near_clip_z)
175  {
176    i4_float ooz = 1 / p.z;
177    i4_float xs = g1_render.center_x * ooz * g1_render.scale_x;
178    i4_float ys = g1_render.center_y * ooz * g1_render.scale_y;
179
180    float cx=g1_render.center_x + p.x*xs;
181    float cy=g1_render.center_y + p.y*ys;
182    float w=sprite_scale * g1_render.center_x * 2 * ooz;
183       
184    r1_clip_render_textured_rect(i4_f_to_i(cx-w/2), i4_f_to_i(cy-w/2),
185                                 i4_f_to_i(cx+w/2), i4_f_to_i(cy+w/2), p.z,
186                                 1.0,
187                                 i4_f_to_i(g1_render.center_x*2),
188                                 i4_f_to_i(g1_render.center_y*2),
189                                 tex, 0, g1_render.r_api);       
190  }
191}
192
193void g1_beam_weapon_class::draw(g1_draw_context_class *context)
194{
195  if (should_draw)
196  {
197    r1_vert      points[4];
198    i4_3d_vector norm;
199
200    float fr=g1_render.frame_ratio;
201    i4_3d_vector start, step, t_start, up, side;
202    start.interpolate(i4_3d_vector(x,y,h), i4_3d_vector(lx,ly,lh), fr);
203    up=i4_3d_vector(0,0,1);
204   
205     
206    step=end_point;
207    step-=start;
208    side.cross(up, step);
209    side.normalize();
210    side*=0.2;
211
212    r1_render_api_class *api=g1_render.r_api;
213   
214    float cx=g1_render.center_x, cy=g1_render.center_y;
215
216    r1_texture_handle texture=g1_get_texture("red_flare");
217    float theta=g1_tick_counter*3.0 + fr;
218    float theta_step=2*i4_pi()/10.0;
219
220   
221    for (float t=0; t<1; t+=(1.1-t)*0.3)
222    {
223      i4_3d_vector s=i4_3d_vector(start.x + step.x*t + side.x*cos(theta),
224                                  start.y + step.y*t + side.y*sin(theta),
225                                  start.z + step.z*t + side.z*cos(theta+0.5)*0.5);
226   
227      theta+=theta_step;
228     
229      context->transform->transform(s, t_start);           
230      draw_sprite(t_start, texture, 0.1);
231    }
232  }
233}
234
235     
236
237g1_object_definer<g1_beam_weapon_class>
238  g1_beam_def("beam", g1_object_definition_class::EDITOR_SELECTABLE);
239
240g1_object_definer<g1_beam_weapon_class>
241  g1_plasma_def("plasma", g1_object_definition_class::EDITOR_SELECTABLE);
242
Note: See TracBrowser for help on using the repository browser.