source: golgotha/src/golg/objs/miscobjs.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: 4.9 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 "g1_object.hh"
10#include "saver.hh"
11#include "objs/model_draw.hh"
12#include "objs/model_collide.hh"
13#include "map.hh"
14#include "map_man.hh"
15#include "objs/shrapnel.hh"
16#include "math/random.hh"
17
18#include "objs/miscobjs.hh"
19
20class g1_deco_definition_class;
21
22g1_deco_type_manager_class g1_deco_type_manager;
23
24static g1_object_type shrapnel_type=0;
25
26g1_deco_object_class::g1_deco_object_class(g1_object_type id, g1_loader_class *fp)
27: g1_object_class(id, fp), death(this)
28{
29  model_name=0;
30  health=100;
31  if (fp)
32  {
33    w16 ver,data_size;
34    fp->get_version(ver,data_size);
35    switch (ver)
36    {
37      case 2 :
38        health=fp->read_16();
39      case 1 :
40        char buf[1024];
41        int len=fp->read_16();
42        fp->read(buf, len);
43
44        model_name=g1_deco_type_manager.find_name(buf);
45        if (model_name)
46        {
47          char lod_name[256];
48          sprintf(lod_name, "%s_lod", model_name);
49
50          draw_params.setup(model_name, 0, lod_name);
51        }
52    }
53    fp->end_version(I4_LF);
54  }
55
56  collision_type = 1;
57
58  player_num=G1_MAX_PLAYERS-1;
59  set_flag(BLOCKING      |
60           SELECTABLE    |
61           CAN_DRIVE_ON  |
62           TARGETABLE    |
63           GROUND        |
64           SHADOWED,
65           1);
66}
67
68void g1_deco_object_class::save(g1_saver_class *fp)
69{
70  g1_object_class::save(fp);
71
72  fp->start_version(DATA_VERSION);
73  fp->write_16(health);
74
75  if (model_name)
76  {
77    int len=strlen(model_name)+1;
78    fp->write_16(len);
79    fp->write((void*)model_name, len);
80  }
81  else fp->write_16(0);
82
83  fp->end_version();
84}
85
86i4_bool g1_deco_object_class::check_collision(const i4_3d_vector &start, i4_3d_vector &ray)
87{
88  i4_3d_vector normal;
89  switch (collision_type)
90  {
91    case 0: return g1_model_collide_radial(this, draw_params, start, ray);
92    case 1: return g1_model_collide_polygonal(this, draw_params, start, ray, normal);
93    default: return i4_F;
94  }
95
96  i4_error("case not handled");
97  return i4_F;
98}
99
100
101
102void g1_deco_object_class::think()
103{
104  if (occupancy_radius()>0.8 && !death.think())
105    return;
106
107  if (model_name==0)
108  {
109    unoccupy_location();
110    request_remove();
111  }
112
113  if (health<0)
114  {
115    g1_shrapnel_class *shrapnel = NULL;
116    if (!shrapnel_type)
117      shrapnel_type = g1_get_object_type("shrapnel");
118    shrapnel = (g1_shrapnel_class *)g1_create_object(shrapnel_type);
119    if (shrapnel)
120    {
121      i4_float rh = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2; 
122      shrapnel->setup(x, y, h + rh, 10, i4_T);
123    }
124
125    unoccupy_location();
126    request_remove();
127  }
128}
129
130void g1_deco_object_class::damage(g1_object_class *who_is_hurting,
131                                  int how_much_hurt, i4_3d_vector damage_dir)
132{
133  if (occupancy_radius()>0.8)
134    death.damage(who_is_hurting, how_much_hurt, damage_dir);
135  else
136    g1_object_class::damage(who_is_hurting,how_much_hurt,damage_dir);
137}
138
139class g1_deco_definition_class : public g1_object_definition_class
140{
141public:
142  char deco_name[40];
143
144  g1_object_class *create_object(g1_object_type type,
145                                 g1_loader_class *fp)
146  {
147    g1_deco_object_class *o=new g1_deco_object_class(type, fp);
148
149    if (!fp)
150    {
151      o->model_name=_name;
152      if (o->model_name)
153      {
154        char lod_name[256];
155        sprintf(lod_name, "%s_lod", o->model_name);
156
157        o->draw_params.setup(o->model_name, 0, lod_name);
158        o->set_flag(g1_object_class::SHADOWED,1);
159      }
160    }
161
162    o->init();
163
164    return o;
165  }
166
167  g1_deco_definition_class(char *__name)
168    : g1_object_definition_class(strcpy(deco_name, __name))
169  {
170    set_flag(MOVABLE,0);
171  }
172
173  ~g1_deco_definition_class()
174  {
175    g1_deco_type_manager.remove_type(this);
176  }
177 
178};
179
180g1_object_type g1_create_deco_object(char *name)
181{
182  g1_deco_definition_class *def=new g1_deco_definition_class(name);
183  g1_object_type t=def->type;
184  def->flags|=g1_object_definition_class::TO_DECO_OBJECT |
185              g1_object_definition_class::DELETE_WITH_LEVEL;
186 
187  g1_deco_type_manager.add_type(def);
188
189  return t;
190}
191
192
193const char *g1_deco_type_manager_class::find_name(const char *name)
194{
195  for (int i=0; i<deco_objs.size(); i++)
196  {
197    if (deco_objs[i])
198      if (strcmp(name, deco_objs[i]->name())==0)
199        return deco_objs[i]->name();
200  }
201   
202  return 0;  // this object name doesn't exsist in the game anymore
203}
204
205void g1_deco_type_manager_class::uninit()
206{
207  deco_objs.uninit();
208}
Note: See TracBrowser for help on using the repository browser.