source: golgotha/src/golg/objs/crate.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: 10.5 KB
RevLine 
[80]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/crate.hh"
10#include "objs/model_draw.hh"
11#include "lisp/li_class.hh"
12#include "objs/model_id.hh"
13#include "object_definer.hh"
14#include "map.hh"
15#include "map_man.hh"
16#include "g1_render.hh"
17#include "math/pi.hh"
18#include "r1_api.hh"
19#include "objs/stank.hh"
20#include "player.hh"
21#include "resources.hh"
22
23
24static li_symbol_class_member type("type"), amount("amount");
25static li_symbol_ref li_health("health"), li_missile("missile"),
26  li_bullet("bullet"), li_money("money"), li_small("small"), li_large("large"),
27  li_chain("chain_gun");
28
29static g1_model_ref
30  health_model("crate_health"),
31  missile_model("crate_missiles"),
32  money_model("crate_money"),
33  chain_model("crate_minigun"),
34  bullet_model("crate_bullets"),
35  health_flare_model("powerup_flare_blue"),
36  missile_flare_model("powerup_flare_white"),
37  money_flare_model("powerup_flare_green"),
38  bullet_flare_model("powerup_flare_red"),
39  chain_flare_model("powerup_flare_yellow");
40
41static li_int_class_member li_ticks_left("ticks_left");
42static li_float_class_member li_yvel("yvel");
43
44S1_SFX(bullet_sfx, "misc/main_barrel_powerup_22khz.wav", 0, 200);
45S1_SFX(health_sfx, "misc/health_powerup_three_22khz.wav", 0, 200);
46S1_SFX(missile_sfx, "misc/missle_powerup_22khz.wav", 0, 200);
47S1_SFX(chain_sfx, "misc/supertank_chain_gun_refuel.wav", 0, 200);
48S1_SFX(money_sfx, "misc/powerup_money_22khz.wav", 0, 200);
49
50
51float &g1_crate_class::yvel() { return vars->get(li_yvel); }
52int &g1_crate_class::ticks_left() { return vars->get(li_ticks_left); }
53
54
55g1_object_definer<g1_crate_class>
56g1_create_def("crate", g1_object_definition_class::EDITOR_SELECTABLE);
57
58i4_bool g1_crate_class::occupy_location()
59{
60  h=lh=g1_get_map()->terrain_height(x,y)+float_height();
61  return g1_object_class::occupy_location_corners();
62}
63
64
65int g1_crate_class::added_money()
66{
67  return get_amount()==SMALL ?
68    g1_resources.small_money_added :
69    g1_resources.large_money_added;
70}
71
72int g1_crate_class::added_bullets()
73{
74  return get_amount()==SMALL ?
75    g1_resources.small_bullets_added :
76    g1_resources.large_bullets_added;
77}
78
79int g1_crate_class::added_health()
80{
81  return get_amount()==SMALL ?
82    g1_resources.small_health_added :
83    g1_resources.large_health_added;
84}
85
86int g1_crate_class::added_missiles()
87{
88  return get_amount()==SMALL ?
89    g1_resources.small_missiles_added :
90    g1_resources.large_missiles_added;
91}
92
93int g1_crate_class::added_chain()
94{
95  return get_amount()==SMALL ?
96    g1_resources.small_chain_added :
97    g1_resources.large_chain_added;
98}
99
100
101
102g1_crate_class::ctype g1_crate_class::get_type()
103{
104  li_symbol *s=vars->get(type);
105  if (s==li_health.get())
106    return HEALTH;
107  else if (s==li_missile.get())
108    return MISSILE;
109  else if (s==li_bullet.get())
110    return BULLET;
111  else if (s==li_chain.get())
112    return CHAIN;
113  else return MONEY;   
114}
115
116void g1_crate_class::set_type(ctype x)
117{
118  li_symbol *s;
119  int sub_type;
120
121  switch (x)
122  {
123    case HEALTH :
124    {
125      s=li_health.get();
126      draw_params.setup(health_model.get()); 
127      sub_type=health_flare_model.value;
128    } break;
129
130    case MISSILE :
131    {
132      s=li_missile.get();
133      draw_params.setup(missile_model.get()); 
134      sub_type=missile_flare_model.value;
135    } break;
136
137    case BULLET :
138    {
139      s=li_bullet.get();
140      draw_params.setup(bullet_model.get()); 
141      sub_type=bullet_flare_model.value;
142    } break;
143
144    case CHAIN :
145    {
146      s=li_chain.get();
147      draw_params.setup(chain_model.get()); 
148      sub_type=chain_flare_model.value;
149    } break;
150
151    case MONEY :
152    {
153      s=li_money.get();
154      draw_params.setup(money_model.get()); 
155      sub_type=money_flare_model.value;
156    } break;
157  }
158
159
160
161//   if (get_amount()==LARGE)
162//     num_mini_objects=3;
163//   else
164//     num_mini_objects=3;
165
166
167//   for (int i=0; i<num_mini_objects; i++)
168//     mini_objects[i].defmodeltype=sub_type;
169
170
171
172  vars->set(type, s);
173}
174
175g1_crate_class::atype g1_crate_class::get_amount()
176{
177  if (vars->get(amount)==li_small.get())
178    return SMALL;
179  else
180    return LARGE;
181}
182
183
184void g1_crate_class::set_amount(atype x)
185{
186  switch (x)
187  {
188    case SMALL : 
189      vars->set(amount, li_small.get());
190      break;
191    case LARGE : 
192      vars->set(amount, li_large.get());
193      break;
194  }
195
196}
197
198
199void g1_crate_class::think()
200{
201  if (ticks_to_think || ticks_left())
202  {
203    if (ticks_left()>0)
204    {
205      ticks_left()--;
206      if (ticks_left()==0)
207      {
208        unoccupy_location();
209        request_remove();
210        return ;
211      }
212    }
213    else
214      ticks_to_think--;
215
216    request_think();
217
218    theta+=0.1;
219    pitch+=0.01;
220   
221//     mini_objects[0].rotation.x-=0.2;
222//     mini_objects[0].rotation.y-=0;
223//     mini_objects[0].rotation.z-=0;
224
225//     mini_objects[1].rotation.x-=0;
226//     mini_objects[1].rotation.y-=0.2;
227//     mini_objects[1].rotation.z-=0;
228
229//     mini_objects[2].rotation.x-=0.2;
230//     mini_objects[2].rotation.y-=0.15;
231//     mini_objects[2].rotation.z-=0.0;
232
233//     float mh=g1_get_map()->map_height(x,y,h);
234//     if (mh!=h+float_height())
235//     {
236//       h+=yvel();
237//       yvel()-=g1_resources.gravity;
238//       if (h+float_height()<mh)
239//       {
240//         h=mh+float_height();
241//         yvel()=0;
242//         request_remove();
243//       }
244//     }   
245  }
246}
247
248void g1_crate_class::go_away()
249{
250  unoccupy_location();
251  request_remove();
252}
253
254void g1_crate_class::note_stank_near(g1_player_piece_class *s)
255{
256  float dist_sqrd=(s->x-x)*(s->x-x)+(s->y-y)*(s->y-y)+(s->h-h)*(s->h-h);
257
258  if (dist_sqrd<0.5*0.5)
259  {   
260    char msg[100];
261    switch (get_type())
262    {
263      case HEALTH :
264        if (s->health!=s->ammo[3].amount)
265        {
266          int old=s->health;
267
268          s->health+=added_health();
269          if (s->health>s->ammo[3].amount)
270            s->health=s->ammo[3].amount;
271          go_away();
272
273          sprintf(msg, "+%d Armor", s->health-old);
274          health_sfx.play();
275        }
276        break;
277       
278      case CHAIN :
279        if (s->ammo[2].amount!=s->ammo[2].ammo_type->max_amount)
280        {
281          int old=s->ammo[2].amount;
282          s->ammo[2].amount+=added_bullets();
283          if (s->ammo[2].amount>s->ammo[2].ammo_type->max_amount)
284            s->ammo[2].amount=s->ammo[2].ammo_type->max_amount;
285          go_away();
286
287          sprintf(msg, "+%d Mini Gun", s->ammo[2].amount-old);       
288        }
289        break;
290
291      case BULLET :
292        if (s->ammo[0].amount!=s->ammo[0].ammo_type->max_amount)
293        {         
294          int old=s->ammo[0].amount;
295          s->ammo[0].amount+=added_chain();
296          if (s->ammo[0].amount>s->ammo[0].ammo_type->max_amount)
297            s->ammo[0].amount=s->ammo[0].ammo_type->max_amount;
298          go_away();
299
300          sprintf(msg, "+%d Main Rounds", s->ammo[0].amount-old);
301          chain_sfx.play();
302        }
303        break;
304
305      case MISSILE :
306        if (s->ammo[1].amount!=s->ammo[1].ammo_type->max_amount)
307        {         
308          int old=s->ammo[1].amount;
309          s->ammo[1].amount+=added_missiles();
310          if (s->ammo[1].amount>s->ammo[1].ammo_type->max_amount)
311            s->ammo[1].amount=s->ammo[1].ammo_type->max_amount;
312          go_away();
313
314          sprintf(msg, "+%d Missiles", s->ammo[1].amount-old);
315          missile_sfx.play();
316        }
317        break;
318
319      case MONEY :       
320        g1_player_man.get(s->player_num)->money()+=added_money();
321        go_away();
322        sprintf(msg, "+ $%d ", added_money());
323        money_sfx.play();
324        break;
325    }
326
327    g1_player_man.show_message(msg, 0x00ff00, s->player_num);
328
329  }   
330}
331
332void g1_crate_class::draw(g1_draw_context_class *context)
333{
334  i4_transform_class *old = context->transform; 
335  i4_transform_class view_transform;
336  context->transform = &view_transform;
337 
338  i4_3d_vector my_interp_position = world_transform->t;
339  view_transform.multiply(*old,*(world_transform));
340
341  // draw without lighting
342  g1_render.render_object(draw_params.model,
343                          &view_transform,
344                          0,
345                          1,
346                          player_num,
347                          0,
348                          0,
349                          0);
350
351
352   
353//   for (int i=0; i<num_mini_objects; i++)
354//   {
355//     g1_quad_object_class *model = g1_model_list_man.get_model(mini_objects[i].defmodeltype);
356
357//     i4_transform_class l2w, w2v, tmp, l2v;
358//     mini_objects[i].calc_transform(g1_render.frame_ratio, &l2w);
359//     tmp.translate(x,y,h);
360//     tmp.multiply(l2w);
361
362
363//     l2v.multiply(*old, tmp);
364//     g1_render.render_object(model,
365//                             &l2v,
366//                             0,
367//                             1,
368//                             player_num,
369//                             0,
370//                             0,
371//                             0);
372
373//   }
374
375
376  ticks_to_think=10;
377  request_think();
378
379  context->transform=old;
380}
381
382g1_crate_class::g1_crate_class(g1_object_type id, g1_loader_class *fp)
383  : g1_object_class(id, fp)
384{
385  ticks_to_think=0;
386
387  //  allocate_mini_objects(3,"crate mini objects");
388
389//   g1_mini_object *a=mini_objects;
390//   int i;
391//   for (i=0; i<3; i++)
392//   {
393//     a->x=0; a->y=0; a->h=0;
394//     a->offset = i4_3d_vector(0,0,0);
395//     a++;
396//   }
397
398  set_type(get_type());     // to setup the draw params
399
400//   mini_objects[1].rotation.y=i4_pi()/2;
401//   mini_objects[2].rotation.z=i4_pi()/2;
402 
403//   for (i=0; i<3; i++)
404//     mini_objects[i].grab_old();
405}
406
407
408void g1_crate_class::setup(const i4_3d_vector &pos, ctype t, atype amount, int ticks)
409{
410  ticks_left()=ticks;
411
412  lx=x=pos.x;
413  ly=y=pos.y;
414  lh=h=pos.z;
415  set_type(t);
416  set_amount(amount);
417
418  if (!get_flag(MAP_OCCUPIED))
419  {
420    if (!occupy_location())
421      return;
422  }
423}
424
425void g1_crate_class::object_changed_by_editor(g1_object_class *who, li_class *old_values)
426{
427  if (who==this)
428    set_type(get_type());
429}
Note: See TracBrowser for help on using the repository browser.