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 _COLLIDESTUFF_H
13 #define _COLLIDESTUFF_H
14 
15 #include "globalincs/pstypes.h"
16 
17 class object;
18 struct CFILE;
19 struct mc_info;
20 
21 // used for ship:ship and ship:debris
22 struct collision_info_struct {
23 	object	*heavy;
24 	object	*light;
25 	vec3d	light_collision_cm_pos;	// relative cm collision pos
26 	vec3d	r_heavy;						// relative to A
27 	vec3d	r_light;						// relative to B
28 	vec3d	hit_pos;					// relative hit position in A's rf (r_heavy)
29 	vec3d	collision_normal;		// normal outward from heavy
30 	float		hit_time;				// time normalized [0,1] when sphere hits model
31 	float		impulse;					// damage scales according to impulse
32 	vec3d	light_rel_vel;			// velocity of light relative to heavy before collison
33 	int		collide_rotate;		// if collision is detected purely from rotation
34 	int		submodel_num;			// submodel of heavy object that is hit
35 	int		edge_hit;				// if edge is hit, need to change collision normal
36 	int		submodel_rot_hit;		// if collision is against rotating submodel
37 	bool	is_landing;			//SUSHI: Maybe treat current collision as a landing
38 };
39 
40 //Collision physics constants
41 #define COLLISION_FRICTION_FACTOR		0.0f	//Default value if not set in ships.tbl
42 #define COLLISION_ROTATION_FACTOR		0.2f	//Default value if not set in ships.tbl
43 #define MIN_LANDING_SOUND_VEL			2.0f
44 #define LANDING_POS_OFFSET				0.05f
45 
46 //===============================================================================
47 // GENERAL COLLISION DETECTION HELPER FUNCTIONS
48 // These are in CollideGeneral.cpp and are used by one or more of the collision-
49 // type specific collision modules.
50 //===============================================================================
51 
52 // Keeps track of pairs of objects for collision detection
53 struct obj_pair	{
54 	object *a;
55 	object *b;
56 	int	next_check_time;	// a timestamp that when elapsed means to check for a collision
57 	struct obj_pair *next;
58 };
59 
60 extern SCP_vector<int> Collision_sort_list;
61 
62 #define COLLISION_OF(a,b) (((a)<<8)|(b))
63 
64 #define SUBMODEL_NO_ROT_HIT	0
65 #define SUBMODEL_ROT_HIT		1
66 void set_hit_struct_info(collision_info_struct *hit, mc_info *mc, int submodel_rot_hit);
67 
68 void obj_add_collider(int obj_index);
69 void obj_remove_collider(int obj_index);
70 void obj_reset_colliders();
71 void obj_sort_and_collide(SCP_vector<int>* Collision_list = nullptr);
72 
73 // retimes all collision pairs to be checked immediately
74 void obj_collide_retime_cached_pairs();
75 
76 // Returns TRUE if the weapon will never hit the other object.
77 // If it can it predicts how long until these two objects need
78 // to be checked and fills the time in in current_pair.
79 // CODE is locatated in CollideGeneral.cpp
80 int weapon_will_never_hit( object *weapon, object *other, obj_pair * current_pair );
81 
82 
83 //	See if two lines intersect by doing recursive subdivision.
84 //	Bails out if larger distance traveled is less than sum of radii + 1.0f.
85 // CODE is locatated in CollideGeneral.cpp
86 int collide_subdivide(vec3d *p0, vec3d *p1, float prad, vec3d *q0, vec3d *q1, float qrad);
87 
88 
89 //===============================================================================
90 // SPECIFIC COLLISION DETECTION FUNCTIONS
91 //===============================================================================
92 
93 // Checks weapon-weapon collisions.  pair->a and pair->b are weapons.
94 // Returns 1 if all future collisions between these can be ignored
95 // CODE is locatated in CollideWeaponWeapon.cpp
96 int collide_weapon_weapon( obj_pair * pair );
97 
98 // Checks ship-weapon collisions.  pair->a is ship and pair->b is weapon.
99 // Returns 1 if all future collisions between these can be ignored
100 // CODE is locatated in CollideShipWeapon.cpp
101 int collide_ship_weapon( obj_pair * pair );
102 
103 // Checks debris-weapon collisions.  pair->a is debris and pair->b is weapon.
104 // Returns 1 if all future collisions between these can be ignored
105 // CODE is locatated in CollideDebrisWeapon.cpp
106 int collide_debris_weapon( obj_pair * pair );
107 
108 // Checks debris-ship collisions.  pair->a is debris and pair->b is ship.
109 // Returns 1 if all future collisions between these can be ignored
110 // CODE is locatated in CollideDebrisShip.cpp
111 int collide_debris_ship( obj_pair * pair );
112 
113 int collide_asteroid_ship(obj_pair *pair);
114 int collide_asteroid_weapon(obj_pair *pair);
115 
116 // Checks ship-ship collisions.  pair->a and pair->b are ships.
117 // Returns 1 if all future collisions between these can be ignored
118 // CODE is locatated in CollideShipShip.cpp
119 int collide_ship_ship( obj_pair * pair );
120 
121 //	Predictive functions.
122 //	Returns true if vector from curpos to goalpos with radius radius will collide with object goalobjp
123 int pp_collide(vec3d *curpos, vec3d *goalpos, object *goalobjp, float radius);
124 
125 //	Return true if objp will collide with some large ship if it moves distance distance.
126 int collide_predict_large_ship(object *objp, float distance);
127 
128 // function to remove old weapons when no more weapon slots available.
129 int collide_remove_weapons();
130 
131 void collide_ship_ship_do_sound(vec3d *world_hit_pos, object *A, object *B, int player_involved);
132 void collide_ship_ship_sounds_init();
133 
134 int get_ship_quadrant_from_global(vec3d *global_pos, object *objp);
135 
136 int reject_due_collision_groups(object *A, object *B);
137 
138 void init_collision_info_struct(collision_info_struct *cis);
139 #endif
140