1 /*
2 Copyright (C) 2010 COR Entertainment, LLC.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include <ode/ode.h>
22 
23 #define MAX_RAGDOLLS 64
24 #define MAX_RAGDOLL_OBJECTS 15
25 #define MAX_RAGDOLL_JOINTS 15
26 #define MAX_CONTACTS 32
27 #define MAX_FORCES 4
28 #define MAX_ODESTEPS 120
29 #define MIN_ODESTEPS 20
30 #define RAGDOLL_DURATION 10000 //10 seconds
31 
32 //body id's
33 #define CHEST 0
34 #define PELVIS 1
35 #define HEAD 2
36 #define RIGHTUPPERLEG 3
37 #define LEFTUPPERLEG 4
38 #define RIGHTLOWERLEG 5
39 #define LEFTLOWERLEG 6
40 #define RIGHTFOOT 7
41 #define LEFTFOOT 8
42 #define RIGHTUPPERARM 9
43 #define LEFTUPPERARM 10
44 #define RIGHTFOREARM 11
45 #define LEFTFOREARM 12
46 #define RIGHTHAND 13
47 #define LEFTHAND 14
48 
49 //joint id's
50 #define MIDSPINE 0
51 #define LOWSPINE 1
52 #define NECK 2
53 #define RIGHTHIP 3
54 #define LEFTHIP 4
55 #define RIGHTKNEE 5
56 #define LEFTKNEE 6
57 #define RIGHTANKLE 7
58 #define LEFTANKLE 8
59 #define RIGHTSHOULDER 9
60 #define LEFTSHOULDER 10
61 #define RIGHTELBOW 11
62 #define LEFTELBOW 12
63 #define RIGHTWRIST 13
64 #define LEFTWRIST 14
65 
66 //ragdoll dimensions
67 #define RAGDOLL_DIMS 56
68 #define ELBOW_X_OFF 0 //note - we likely want to do something similar for knees and ankles
69 #define ELBOW_Y_OFF 1
70 #define ELBOW_Z_OFF 2
71 #define WRIST_X_OFF 3
72 #define WRIST_Y_OFF 4
73 #define WRIST_Z_OFF 5
74 #define FINGERS_X_OFF 6
75 #define FINGERS_Y_OFF 7
76 #define FINGERS_Z_OFF 8
77 #define FOOT_LEN 9 // ankles to base of ball of foot only
78 #define HEEL_LEN 10
79 
80 #define HEAD_H 11
81 #define NECK_H 12
82 #define SHOULDER_H 13
83 #define CHEST_H 14
84 #define HIP_H 15
85 
86 #define HEAD_W 16
87 #define SHOULDER_W 17
88 #define CHEST_W 18 // actually wider, but we want narrower than shoulders (esp. with large radius)
89 #define BICEP_W 19 //thickness of bicep
90 #define FOREARM_W 20 //thickness of forearm
91 #define HAND_W 21 //width of hand
92 #define LEG_W 22 // between middles of upper legs
93 #define PELVIS_W 23 // actually wider, but we want smaller than hip width
94 #define THIGH_W 24 //thickness of thigh
95 #define SHIN_W 25 //thickness of shin
96 #define FOOT_W 26 //width of foot
97 
98 #define KNEE_X_OFF 27
99 #define KNEE_Y_OFF 28
100 #define KNEE_Z_OFF 29
101 #define ANKLE_X_OFF 30
102 #define ANKLE_Y_OFF 31
103 #define ANKLE_Z_OFF 32
104 
105 #define GLOBAL_X_OFF 33
106 #define GLOBAL_Y_OFF 34
107 #define GLOBAL_Z_OFF 35
108 
109 //contraint section
110 #define HIP_LOSTOP1 36
111 #define HIP_HISTOP1 37
112 #define HIP_LOSTOP2 38
113 #define HIP_HISTOP2 39
114 #define KNEE_LOSTOP 40
115 #define KNEE_HISTOP 41
116 #define ANKLE_LOSTOP 42
117 #define ANKLE_HISTOP 43
118 #define SHOULDER_LOSTOP1 44
119 #define SHOULDER_HISTOP1 45
120 #define SHOULDER_LOSTOP2 46
121 #define SHOULDER_HISTOP2 47
122 #define ELBOW_LOSTOP 48
123 #define ELBOW_HISTOP 49
124 #define WRIST_LOSTOP 50
125 #define WRIST_HISTOP 51
126 #define HEAD_LOSTOP1 52
127 #define HEAD_HISTOP1 53
128 #define HEAD_LOSTOP2 54
129 #define HEAD_HISTOP2 55
130 
131 dWorldID RagDollWorld;
132 dSpaceID RagDollSpace;
133 
134 dJointGroupID contactGroup;
135 
136 int lastODEUpdate;
137 
138 dQuaternion initialQuaternion;
139 
140 typedef struct RagDollBind_s {
141 
142     const char *name;
143     int object;
144 
145 } RagDollBind_t;
146 
147 extern RagDollBind_t RagDollBinds[];
148 extern int RagDollBindsCount;
149 
150 typedef struct RagDollObject_s {
151 
152 	dBodyID body;
153 	dMass mass;
154 	dGeomID geom;
155     matrix3x4_t initmat;
156 
157 } RagDollObject_t;
158 
159 typedef struct RagDollForce_s {
160 
161 	vec3_t org;
162 	vec3_t dir;
163 	float force;
164 
165 	int spawnTime;
166 
167 	int destroyed;
168 
169 } RagDollForce_t;
170 
171 #define GROW_ODE_VERTS 16384
172 #define GROW_ODE_TRIS 16384
173 typedef struct RagDollWorld_s {
174 
175 	dVector3	*ODEVerts;
176 	dTriIndex	*ODETris;
177     int numODEVerts, maxODEVerts;
178     int numODETris, maxODETris;
179 	dTriMeshDataID triMesh;
180 	dGeomID geom;
181 
182 } RagDollWorld_t;
183 
184 typedef struct RagDoll_s {
185 
186 	char	name[MAX_QPATH];
187 
188 	RagDollObject_t RagDollObject[MAX_RAGDOLL_OBJECTS];
189 	dJointID RagDollJoint[MAX_RAGDOLL_JOINTS];
190 
191 	RagDollForce_t RagDollForces[MAX_FORCES];
192 
193 	//mesh information
194 	model_t *ragDollMesh;
195     matrix3x4_t *initframe;
196 	int		texnum;
197 	int		flags;
198 	struct	rscript_s *script;
199 	float	angles[3];
200 	vec3_t	origin;
201 	vec3_t	curPos;
202 
203 	int spawnTime;
204 
205 	int destroyed;
206 
207 } RagDoll_t;
208 
209 RagDoll_t RagDoll[MAX_RAGDOLLS];
210 
211 // A few values will be copied into this. It's a bit wasteful, but it allows
212 // us to reuse code that expects them to be in an entity_t struct.
213 entity_t RagDollEntity;
214 
215 //surface for ragdoll to collide
216 RagDollWorld_t RagDollTriWorld;
217 
218 //Funcs
219 extern void R_RenderAllRagdolls ( void );
220 extern void R_ClearAllRagdolls( void );
221 extern void RGD_CreateWorldObject( void );
222 extern void RGD_DestroyWorldObject( void );
223 extern void RGD_AddNewRagdoll( vec3_t origin, char name[MAX_QPATH] );
224 extern void RGD_DestroyWorldTrimesh();
225 extern void RGD_BuildWorldTrimesh ();
226 extern qboolean RGD_CullRagDolls( int RagDollID );
227