8 #define NULL ((void *)0)
11 static double gametime, frametime;
14 typedef struct localentity_s
16 int active; // true if the entity is alive (not freed)
17 float freetime; // time this entity was freed
25 vec3_t lastimpactorigin; // updated by physics code, used by gib blood stains
27 float airfrictionscale;
29 void (*framethink)(struct localentity_s *e);
30 void (*touchnetwork)(struct localentity_s *self);
35 #define MAX_LOCALENTITIES 1024
36 static localentity_t *localentity;
38 static cgphysentity_t *phys_entity;
39 static int phys_entities;
41 static float cg_gravity;
43 static void readvector(vec3_t v)
45 v[0] = CGVM_MSG_ReadFloat();
46 v[1] = CGVM_MSG_ReadFloat();
47 v[2] = CGVM_MSG_ReadFloat();
50 static localentity_t *entspawn(void)
54 for (i = 0;i < MAX_LOCALENTITIES;i++)
57 if (!l->active && l->freetime < gametime)
59 memset(l, 0, sizeof(*l));
64 for (i = 0;i < MAX_LOCALENTITIES;i++)
69 memset(l, 0, sizeof(*l));
77 static void entremove(localentity_t *e)
79 memset(e, 0, sizeof(*e));
80 e->freetime = (float)gametime + 1.0f;
83 static void phys_setupphysentities(void)
87 for (i = 0;i < MAX_LOCALENTITIES;i++)
89 l = localentities + i;
90 if (l->active && l->solid)
97 static void phys_moveentities(void)
101 for (i = 0;i < MAX_LOCALENTITIES;i++)
108 if (l->active && l->draw.model)
109 CGVM_Draw_Entity(&l->draw);
114 static void phys_updateentities(void)
116 phys_setupphysentities();
120 static void phys_update(localentity_t *e)
122 vec3_t impactpos, impactnormal, end;
124 float t, f, frac, bounce;
125 t = (float)frametime;
128 VectorMA(e->draw.angles, t, e->avelocity, e->draw.angles);
129 VectorMA(e->draw.origin, t, e->velocity, end);
130 frac = CGVM_TracePhysics(e->draw.origin, end, e->worldmins, e->worldmaxs, e->entitymins, e->entitymaxs, phys_entity, phys_entities, impactpos, impactnormal, &impactentnum);
131 VectorCopy(impactpos, e->draw.origin);
134 bounce = DotProduct(e->velocity, impactnormal) * -e->bouncescale;
135 VectorMA(e->velocity, bounce, impactnormal, e->velocity);
136 if (impactnormal[2] >= 0.7 && DotProduct(e->velocity, e->velocity) < 100*100)
138 VectorClear(e->velocity);
139 VectorClear(e->avelocity);
144 // FIXME: do some kind of touch code here if physentities get implemented
146 VectorCopy(impactpos, e->lastimpactorigin);
149 if (e->airfrictionscale)
151 if (DotProduct(e->velocity, e->velocity) < 10*10)
153 VectorClear(e->velocity);
154 VectorClear(e->avelocity);
158 f = 1 - (t * e->airfrictionscale);
161 VectorScale(e->velocity, f, e->velocity);
162 if (DotProduct(e->avelocity, e->avelocity) < 10*10)
164 VectorClear(e->avelocity);
168 VectorScale(e->avelocity, f, e->avelocity);
173 VectorClear(e->velocity);
174 VectorClear(e->avelocity);
179 e->velocity[2] += cg_gravity * e->gravityscale * t;
182 static void explosiondebris_framethink(localentity_t *self)
184 if (gametime > self->dietime)
186 self->draw.scale -= (float)(frametime * 3.0);
187 if (self->draw.scale < 0.05f)
196 static void gib_framethink(localentity_t *self)
198 if (gametime > self->dietime)
200 self->draw.scale -= (float)frametime * 3.0f;
201 if (self->draw.scale < 0.05f)
208 if (gametime > self->trailnexttime)
210 self->trailnexttime = gametime + 0.1f;
211 CGVM_BloodParticle(self->draw.origin, self->velocity);
217 static void gib_touchnetwork(localentity_t *self)
219 if (VectorDistance2(self->draw.origin, self->lastimpactorigin) >= 5*5)
220 CGVM_Stain(self->draw.origin, 64, 64, 24, 24, 48, 192, 48, 48, 48);
223 static void net_explosion(unsigned char num)
230 // need the time to know when the rubble should fade
232 // read the network data
235 for (i = 0;i < 40;i++)
241 VectorCopy(org, e->draw.origin);
242 e->draw.angles[0] = CGVM_RandomRange(0, 360);
243 e->draw.angles[1] = CGVM_RandomRange(0, 360);
244 e->draw.angles[2] = CGVM_RandomRange(0, 360);
245 VectorRandom(e->velocity);
246 VectorScale(e->velocity, 300, e->velocity);
247 e->velocity[2] -= (float)cg_gravity * 0.1f;
248 e->avelocity[0] = CGVM_RandomRange(0, 1440);
249 e->avelocity[1] = CGVM_RandomRange(0, 1440);
250 e->avelocity[2] = CGVM_RandomRange(0, 1440);
251 r = CGVM_RandomRange(0, 3);
253 e->draw.model = CGVM_Model("progs/rubble1.mdl");
255 e->draw.model = CGVM_Model("progs/rubble2.mdl");
257 e->draw.model = CGVM_Model("progs/rubble3.mdl");
262 e->draw.framelerp = 0;
264 VectorSet(e->worldmins, 0, 0, -8);
265 VectorSet(e->worldmaxs, 0, 0, -8);
266 VectorSet(e->entitymins, -8, -8, -8);
267 VectorSet(e->entitymaxs, 8, 8, 8);
268 e->bouncescale = 1.4f;
270 e->airfrictionscale = 1;
271 e->framethink = explosiondebris_framethink;
272 e->dietime = (float)time + 5.0f;
276 static void net_gibshower(unsigned char num)
279 float r, velocityscale;
283 // need the time to know when the gibs should fade
285 // read the network data
286 count = CGVM_MSG_ReadByte();
287 velocityscale = (float)(CGVM_MSG_ReadByte() * 100);
290 for (i = 0;i < count;i++)
296 VectorCopy(org, e->draw.origin);
297 e->draw.angles[0] = CGVM_RandomRange(0, 360);
298 e->draw.angles[1] = CGVM_RandomRange(0, 360);
299 e->draw.angles[2] = CGVM_RandomRange(0, 360);
300 VectorRandom(e->velocity);
301 VectorScale(e->velocity, velocityscale, e->velocity);
302 e->velocity[2] -= (float)(cg_gravity * 0.1);
303 e->avelocity[0] = CGVM_RandomRange(0, 1440);
304 e->avelocity[1] = CGVM_RandomRange(0, 1440);
305 e->avelocity[2] = CGVM_RandomRange(0, 1440);
306 r = CGVM_RandomRange(0, 3);
308 e->draw.model = CGVM_Model("progs/gib1.mdl");
310 e->draw.model = CGVM_Model("progs/gib2.mdl");
312 e->draw.model = CGVM_Model("progs/gib3.mdl");
317 e->draw.framelerp = 0;
319 VectorSet(e->worldmins, 0, 0, -8);
320 VectorSet(e->worldmaxs, 0, 0, -8);
321 VectorSet(e->entitymins, -8, -8, -8);
322 VectorSet(e->entitymaxs, 8, 8, 8);
323 e->bouncescale = 1.5;
325 e->airfrictionscale = 1;
326 e->framethink = gib_framethink;
327 e->touchnetwork = gib_touchnetwork;
328 e->dietime = (float)time + CGVM_RandomRange(3.0f, 5.0f);
335 localentity = CGVM_Malloc(sizeof(localentity_t) * MAX_LOCALENTITIES);
336 phys_entity = CGVM_Malloc(sizeof(cgphysentity_t) * MAX_LOCALENTITIES);
337 CGVM_RegisterNetworkCode(1, net_explosion);
338 CGVM_RegisterNetworkCode(2, net_gibshower);
343 void CG_Frame(double time)
345 cg_gravity = -CGVM_GetCvarFloat("sv_gravity");
346 frametime = time - gametime;
348 phys_updateentities();