FS2_Open
Open source remastering of the Freespace 2 engine
object.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _OBJECT_H
13 #define _OBJECT_H
14 
15 #include "globalincs/globals.h"
16 #include "globalincs/pstypes.h"
17 #include "math/vecmat.h"
18 #include "physics/physics.h"
19 
20 /*
21  * CONSTANTS
22  */
23 
24 #define DEFAULT_SHIELD_SECTIONS 4 // Number of sections in standard shields.
25 
26 #ifndef NDEBUG
27 #define OBJECT_CHECK
28 #endif
29 
30 //Object types
31 #define OBJ_NONE 0 //unused object
32 #define OBJ_SHIP 1 //a ship
33 #define OBJ_WEAPON 2 //a laser, missile, etc
34 #define OBJ_FIREBALL 3 //an explosion
35 #define OBJ_START 4 //a starting point marker (player start, etc)
36 #define OBJ_WAYPOINT 5 //a waypoint object, maybe only ever used by Fred
37 #define OBJ_DEBRIS 6 //a flying piece of ship debris
38 //#define OBJ_CMEASURE 7 //a countermeasure, such as chaff
39 #define OBJ_GHOST 8 //so far, just a placeholder for when a player dies.
40 #define OBJ_POINT 9 //generic object type to display a point in Fred.
41 #define OBJ_SHOCKWAVE 10 // a shockwave
42 #define OBJ_WING 11 // not really a type used anywhere, but I need it for Fred.
43 #define OBJ_OBSERVER 12 // used for multiplayer observers (possibly single player later)
44 #define OBJ_ASTEROID 13 // An asteroid, you know, a big rock, like debris, sort of.
45 #define OBJ_JUMP_NODE 14 // A jump node object, used only in Fred.
46 #define OBJ_BEAM 15 // beam weapons. we have to roll them into the object system to get the benefits of the collision pairs
47 
48 //Make sure to change Object_type_names in Object.c when adding another type!
49 #define MAX_OBJECT_TYPES 16
50 
51 #define UNUSED_OBJNUM (-MAX_OBJECTS*2) // Newer systems use this instead of -1 for invalid object.
52 
54 
55 // each object type should have these functions: (I will use weapon as example)
56 //
57 // int weapon_create( weapon specific parameters )
58 // {
59 // ...
60 // objnum = obj_create();
61 // ... Do some check to correctly handle obj_create returning which
62 // means that that object couldn't be created
63 // ... Initialize the weapon-specific info in Objects[objnum]
64 // return objnum;
65 // }
66 //
67 // void weapon_delete( object * obj )
68 // {
69 // {Put a call to this in OBJECT.C, function obj_delete_all_that_should_be_dead }
70 // WARNING: To kill an object, set it's OF_SHOULD_BE_DEAD flag. Then,
71 // this function will get called when it's time to clean up the data.
72 // Assert( obj->flags & OF_SHOULD_BE_DEAD );
73 // ...
74 // ... Free up all weapon-specfic data
75 // obj_delete(objnum);
76 // }
77 //
78 // void weapon_move( object * obj )
79 // {
80 // {Put a call to this in ??? }
81 // ... Do whatever needs to be done each frame. Usually this amounts
82 // to setting the thrust, seeing if we've died, etc.
83 // }
84 //
85 // int weapon_check_collision( object * obj, object * other_obj, vec3d * hitpos )
86 // {
87 // this should check if a vector from
88 // other_obj->last_pos to other_obj->pos with a radius of other_obj->radius
89 // collides with object obj. If it does, then fill in hitpos with the point
90 // of impact and return non-zero, otherwise return 0 if no impact. Note that
91 // this shouldn't take any action... that happens in weapon_hit.
92 // }
93 
94 //
95 // void weapon_hit( object * obj, object * other_obj, vec3d * hitpos )
96 // {
97 // {Put a call to this in COLLIDE.C}
98 // ... Do what needs to be done when this object gets hit
99 // ... Reducing shields, etc
100 // }
101 
102 //Misc object flags
103 #define OF_RENDERS (1<<0) // It renders as something ( objtype_render gets called)
104 #define OF_COLLIDES (1<<1) // It collides with stuff (objtype_check_impact & objtype_hit gets called)
105 #define OF_PHYSICS (1<<2) // It moves with standard physics.
106 #define OF_SHOULD_BE_DEAD (1<<3) // this object should be dead, so next time we can, we should delete this object.
107 #define OF_INVULNERABLE (1<<4) // invulnerable
108 #define OF_PROTECTED (1<<5) // Don't kill this object, probably mission-critical.
109 #define OF_PLAYER_SHIP (1<<6) // this object under control of some player -- don't do ai stuff on it!!!
110 #define OF_NO_SHIELDS (1<<7) // object has no shield generator system (i.e. no shields)
111 #define OF_JUST_UPDATED (1<<8) // for multiplayer -- indicates that we received object update this frame
112 #define OF_COULD_BE_PLAYER (1<<9) // for multiplayer -- indicates that it is selectable ingame joiners as their ship
113 #define OF_WAS_RENDERED (1<<10) // Set if this object was rendered this frame. Only gets set if OF_RENDERS set. Gets cleared or set in obj_render_all().
114 #define OF_NOT_IN_COLL (1<<11) // object has not been added to collision list
115 #define OF_BEAM_PROTECTED (1<<12) // don't fire beam weapons at this type of object, probably mission critical.
116 #define OF_SPECIAL_WARPIN (1<<13) // Object has special warp-in enabled.
117 #define OF_DOCKED_ALREADY_HANDLED (1<<14) // Goober5000 - a docked object that we already moved
118 #define OF_TARGETABLE_AS_BOMB (1<<15)
119 #define OF_FLAK_PROTECTED (1<<16) // Goober5000 - protected from flak turrets
120 #define OF_LASER_PROTECTED (1<<17) // Goober5000 - protected from laser turrets
121 #define OF_MISSILE_PROTECTED (1<<18) // Goober5000 - protected from missile turrets
122 #define OF_IMMOBILE (1<<19) // Goober5000 - doesn't move, no matter what
123 
124 // Flags used by Fred
125 #define OF_MARKED (1<<29) // Object is marked (Fred). Can be reused in FreeSpace for anything that won't be used by Fred.
126 #define OF_TEMP_MARKED (1<<30) // Temporarily marked (Fred).
127 #define OF_HIDDEN (1<<31) // Object is hidden (not shown) and can't be manipulated
128 
129 typedef struct obj_flag_name {
130  int flag;
133 } obj_flag_name;
134 
135 #define MAX_OBJECT_FLAG_NAMES 10
137 
138 struct dock_instance;
139 class draw_list;
140 
141 class object
142 {
143 public:
144  class object *next, *prev; // for linked lists of objects
145  int signature; // Every object ever has a unique signature...
146  char type; // what type of object this is... robot, weapon, hostage, powerup, fireball
147  int parent; // This object's parent.
148  int parent_sig; // This object's parent's signature
149  char parent_type; // This object's parent's type
150  int instance; // which instance. ie.. if type is Robot, then this indexes into the Robots array
151  uint flags; // misc flags. Call obj_set_flags to change this.
152  vec3d pos; // absolute x,y,z coordinate of center of object
153  matrix orient; // orientation of object in world
154  float radius; // 3d size of object - for collision detection
155  vec3d last_pos; // where object was last frame
156  matrix last_orient; // how the object was oriented last frame
157  physics_info phys_info; // a physics object
158  int n_quadrants; // how many shield quadrants the ship has
159  SCP_vector<float> shield_quadrant; // Shield is broken into components, quadrants by default.
160  float hull_strength; // Remaining hull strength.
161  float sim_hull_strength; // Simulated hull strength - used with training weapons.
162  SCP_vector<int> objsnd_num; // Index of persistant sound struct.
164  int num_pairs; // How many object pairs this is associated with. When 0 then there are no more.
165 
166  dock_instance *dock_list; // Goober5000 - objects this object is docked to
167  dock_instance *dead_dock_list; // Goober5000 - objects this object was docked to when destroyed; replaces dock_objnum_when_dead
168 
169  int collision_group_id; // This is a bitfield. Collision checks will be skipped if A->collision_group_id & B->collision_group_id returns nonzero
170 
171  object();
172  ~object();
173  void clear();
174 
175 private:
176  // An object should never be copied; there are allocated pointers, and linked list shenanigans.
177  object(const object& other); // no implementation
178  object& operator= (const object & other); // no implementation
179 };
180 
181 struct object_h {
182  object *objp;
183  int sig;
184 
185  bool IsValid(){return (objp != NULL && objp->signature == sig);}
186  object_h(object *in){objp=in; if(objp!=NULL){sig=in->signature;}}
187  object_h(){objp=NULL;sig=-1;}
188 };
189 
190 // object backup struct used by Fred.
191 typedef struct object_orient_pos {
195 
196 #ifdef OBJECT_CHECK
197 typedef struct checkobject
198 {
199  int type;
204 } checkobject;
205 #endif
206 
207 /*
208  * VARIABLES
209  */
210 
211 extern int Object_inited;
212 extern int Show_waypoints;
213 
214 // The next signature for the next newly created object. Zero is bogus
215 extern int Object_next_signature;
216 extern int Num_objects;
217 
218 extern object Objects[];
219 extern int Highest_object_index; //highest objnum
220 extern int Highest_ever_object_index;
221 extern object obj_free_list;
222 extern object obj_used_list;
223 extern object obj_create_list;
224 
225 extern int render_total;
226 extern int render_order[MAX_OBJECTS];
227 
228 extern object *Viewer_obj; // Which object is the viewer. Can be NULL.
229 extern object *Player_obj; // Which object is the player. Has to be valid.
230 
231 // Use this instead of "objp - Objects" to get an object number
232 // given it's pointer. This way, we can replace it with a macro
233 // to check that the pointer is valid for debugging.
234 // https://stackoverflow.com/questions/7954439/c-which-character-should-be-used-for-ptrdiff-t-in-printf
235 #define OBJ_INDEX(objp) (ptrdiff_t)(objp-Objects)
236 
237 /*
238  * FUNCTIONS
239  */
240 
241 //do whatever setup needs to be done
242 void obj_init();
243 
244 //initialize a new object. adds to the list for the given segment.
245 //returns the object number. The object will be a non-rendering, non-physics
246 //object. Returns 0 if failed, otherwise object index.
247 //You can pass 0 for parent if you don't care about that.
248 //You can pass null for orient and/or pos if you don't care.
249 int obj_create(ubyte type,int parent_obj, int instance, matrix * orient, vec3d * pos, float radius, uint flags );
250 
251 //Render an object. Calls one of several routines based on type
252 void obj_render_DEPRECATED(object *obj);
253 
254 void obj_render(object* obj);
255 
256 void obj_queue_render(object* obj, draw_list* scene);
257 
258 //Sorts and renders all the ojbects
259 void obj_render_all(void (*render_function)(object *objp), bool* render_viewer_last );
260 
261 //move all objects for the current frame
262 void obj_move_all(float frametime); // moves all objects
263 
264 //move an object for the current frame
265 void obj_move_one(object * obj, float frametime);
266 
267 // function to delete an object -- should probably only be called directly from editor code
268 void obj_delete(int objnum);
269 
270 // should only be used by the editor!
271 void obj_merge_created_list(void);
272 
273 // recalculate object pairs for an object
274 #define OBJ_RECALC_PAIRS(obj_to_reset) do { obj_set_flags(obj_to_reset, obj_to_reset->flags & ~(OF_COLLIDES)); obj_set_flags(obj_to_reset, obj_to_reset->flags | OF_COLLIDES); } while(0);
275 
276 // Removes any occurances of object 'a' from the pairs list.
277 void obj_remove_pairs( object * a );
278 
279 // add an object to the pairs list
280 void obj_add_pairs(int objnum);
281 
282 // Returns true if objects A and B are expected to collide in next duration seconds.
283 // For purposes of this check, the first object moves from current location to predicted
284 // location. The second object is assumed to be where it will be at time duration, NOT
285 // where it currently is.
286 // radius_scale: 0.0f means use polygon models, else scale sphere size by radius_scale
287 // radius_scale == 1.0f means Descent style collisions.
288 int objects_will_collide(object *A, object *B, float duration, float radius_scale);
289 
290 // Used for pausing. Seems hacked. Was in PHYSICS, but that broke the TestCode program,
291 // so I moved it into the object lib. -John
293 
294 // Goober5000
295 float get_hull_pct(object *objp);
296 float get_sim_hull_pct(object *objp);
297 float get_shield_pct(object *objp);
298 float get_max_shield_quad(object *objp);
299 
300 // returns the average 3-space position of all ships. useful to find "center" of battle (sort of)
302 
303 // function to deal with firing player things like lasers, missiles, etc.
304 // separated out because of multiplayer issues.
305 void obj_player_fire_stuff( object *objp, control_info ci );
306 
307 // Call this if you want to change an object flag so that the
308 // object code knows what's going on. For instance if you turn
309 // off OF_COLLIDES, the object code needs to know this in order to
310 // actually turn the object collision detection off. By calling
311 // this you shouldn't get Int3's in the checkobject code. If you
312 // do, then put code in here to correctly handle the case.
313 void obj_set_flags(object *obj, uint new_flags);
314 
315 // get the Ship_info flags for a given ship (if you have the object)
316 int obj_get_SIF(object *objp);
317 int obj_get_SIF(int obj);
318 
319 // get the team for any object
320 int obj_team(object *objp);
321 
322 void obj_move_all_pre(object *objp, float frametime);
323 void obj_move_all_post(object *objp, float frametime);
324 
325 void obj_move_call_physics(object *objp, float frametime);
326 
327 // multiplayer object update stuff begins -------------------------------------------
328 
329 // do client-side pre-interpolation object movement
331 
332 // do client-side post-interpolation object movement
334 
335 // move an observer object in multiplayer
336 void obj_observer_move(float frame_time);
337 
338 // Goober5000
339 int object_is_docked(object *objp);
340 int object_is_dead_docked(object *objp);
341 void obj_move_one_docked_object(object *objp, object *parent_objp);
342 
343 //WMC
344 void object_set_gliding(object *objp, bool enable=true, bool force = false);
345 bool object_get_gliding(object *objp);
346 bool object_glide_forced(object* objp);
347 int obj_get_by_signature(int sig);
348 int object_get_model(object *objp);
349 
350 void obj_render_queue_all();
351 
352 #endif
#define MAX_OBJECT_TYPES
Definition: object.h:49
float sim_hull_strength
Definition: object.h:161
int Show_waypoints
Definition: object.cpp:73
void obj_queue_render(object *obj, draw_list *scene)
Definition: object.cpp:1625
object * objp
Definition: object.h:182
void obj_move_all(float frametime)
Definition: object.cpp:1383
int render_order[MAX_OBJECTS]
void obj_observer_move(float frame_time)
Definition: object.cpp:1772
int object_get_model(object *objp)
Definition: object.cpp:2092
int Object_inited
Definition: object.cpp:72
object * Player_obj
Definition: object.cpp:56
char parent_type
Definition: object.h:149
void obj_render_DEPRECATED(object *obj)
Definition: object.cpp:1537
void obj_client_pre_interpolate()
Definition: object.cpp:1703
physics_info phys_info
Definition: object.h:157
void obj_merge_created_list(void)
Definition: object.cpp:651
int render_total
void obj_get_average_ship_pos(vec3d *pos)
Definition: object.cpp:1796
Definition: pstypes.h:88
ushort net_signature
Definition: object.h:163
matrix orient
Definition: object.h:193
int Highest_object_index
Definition: object.cpp:69
object * Viewer_obj
Definition: object.cpp:57
void obj_render_queue_all()
Definition: objectsort.cpp:347
int parent_type
Definition: object.h:203
#define MAX_OBJECTS
Definition: globals.h:83
object()
Definition: object.cpp:111
char flag_name[TOKEN_LENGTH]
Definition: object.h:131
void object_set_gliding(object *objp, bool enable=true, bool force=false)
Definition: object.cpp:2036
int n_quadrants
Definition: object.h:158
matrix * B
Definition: lua.cpp:445
hull_check orient
Definition: lua.cpp:5049
object obj_create_list
Definition: object.cpp:54
object * objp
Definition: lua.cpp:3105
struct obj_flag_name obj_flag_name
void obj_client_post_interpolate()
Definition: object.cpp:1748
GLuint in
Definition: Glext.h:9087
uint flags
Definition: object.h:201
vec3d pos
Definition: object.h:152
int signature
Definition: object.h:145
GLenum type
Definition: Gl.h:1492
class object * prev
Definition: object.h:144
object obj_free_list
Definition: object.cpp:52
void obj_move_call_physics(object *objp, float frametime)
Definition: object.cpp:781
void obj_init_all_ships_physics()
Definition: object.cpp:1689
int instance
Definition: object.h:150
void obj_delete(int objnum)
Definition: object.cpp:522
void obj_move_one(object *obj, float frametime)
void obj_render(object *obj)
Definition: object.cpp:1604
vec3d last_pos
Definition: object.h:155
dock_instance * dock_list
Definition: object.h:166
unsigned int uint
Definition: pstypes.h:64
void obj_remove_pairs(object *a)
Definition: object.cpp:1938
void obj_move_all_post(object *objp, float frametime)
Definition: object.cpp:1167
GLboolean GLboolean GLboolean GLboolean a
Definition: Glext.h:5781
void obj_player_fire_stuff(object *objp, control_info ci)
Definition: object.cpp:720
int objects_will_collide(object *A, object *B, float duration, float radius_scale)
Definition: objcollide.cpp:672
float get_shield_pct(object *objp)
Definition: object.cpp:308
int type
Definition: object.h:199
float hull_strength
Definition: object.h:160
float get_max_shield_quad(object *objp)
Definition: object.cpp:260
struct checkobject checkobject
int parent
Definition: object.h:147
bool object_glide_forced(object *objp)
Definition: object.cpp:2066
bool object_get_gliding(object *objp)
Definition: object.cpp:2059
object Objects[]
Definition: object.cpp:62
class object * next
Definition: object.h:144
unsigned char ubyte
Definition: pstypes.h:62
void obj_move_all_pre(object *objp, float frametime)
Definition: object.cpp:1103
Definition: object.h:141
matrix orient
Definition: object.h:153
int parent_sig
Definition: object.h:202
void clear()
Definition: object.cpp:127
GLbitfield flags
Definition: Glext.h:6722
GLboolean enable
Definition: Glext.h:10591
int Highest_ever_object_index
Definition: object.cpp:70
#define TOKEN_LENGTH
Definition: globals.h:41
int Object_next_signature
Definition: object.cpp:71
void obj_render_all(void(*render_function)(object *objp), bool *render_viewer_last)
Definition: objectsort.cpp:172
float frame_time
Definition: multi.cpp:1426
int num_pairs
Definition: object.h:164
obj_flag_name Object_flag_names[]
Definition: object.cpp:97
int signature
Definition: object.h:200
bool IsValid()
Definition: object.h:185
int Num_objects
Definition: object.cpp:68
unsigned short ushort
Definition: pstypes.h:63
int object_is_dead_docked(object *objp)
Definition: object.cpp:2025
int obj_team(object *objp)
Definition: object.cpp:1843
SCP_vector< float > shield_quadrant
Definition: object.h:159
int obj_get_SIF(object *objp)
Definition: object.cpp:1817
dock_instance * dead_dock_list
Definition: object.h:167
hull_check pos
Definition: lua.cpp:5050
GLsizei GLsizei GLuint * obj
Definition: Glext.h:5619
int obj_get_by_signature(int sig)
Definition: object.cpp:2074
void obj_init()
Definition: object.cpp:327
matrix * A
Definition: lua.cpp:444
struct object_orient_pos object_orient_pos
int flag_list
Definition: object.h:132
int collision_group_id
Definition: object.h:169
void obj_move_one_docked_object(object *objp, object *parent_objp)
Definition: object.cpp:682
uint flags
Definition: object.h:151
float radius
Definition: object.h:154
matrix last_orient
Definition: object.h:156
void obj_add_pairs(int objnum)
Definition: object.cpp:1901
float get_hull_pct(object *objp)
Definition: object.cpp:271
char type
Definition: object.h:146
object_h(object *in)
Definition: object.h:186
SCP_vector< int > objsnd_num
Definition: object.h:162
object_h()
Definition: object.h:187
~object()
Definition: object.cpp:118
int object_is_docked(object *objp)
Definition: object.cpp:2019
int parent_sig
Definition: object.h:148
char * Object_type_names[MAX_OBJECT_TYPES]
Definition: object.cpp:76
void obj_set_flags(object *obj, uint new_flags)
Definition: object.cpp:1000
int sig
Definition: object.h:183
float get_sim_hull_pct(object *objp)
Definition: object.cpp:289
int obj_create(ubyte type, int parent_obj, int instance, matrix *orient, vec3d *pos, float radius, uint flags)
Definition: object.cpp:467
object obj_used_list
Definition: object.cpp:53