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);
31 //void (*touch)(struct localentity_s *self, struct localentity_s *other);
32 void (*touchnetwork)(struct localentity_s *self);
37 #define MAX_LOCALENTITIES 1024
38 static localentity_t *localentity;
40 static cgphysentity_t *phys_entity;
41 static int phys_entities;
43 static float cg_gravity;
45 static void readvector(vec3_t v)
47 v[0] = CGVM_MSG_ReadFloat();
48 v[1] = CGVM_MSG_ReadFloat();
49 v[2] = CGVM_MSG_ReadFloat();
52 static localentity_t *entspawn(void)
56 for (i = 0;i < MAX_LOCALENTITIES;i++)
59 if (!l->active && l->freetime < gametime)
61 memset(l, 0, sizeof(*l));
66 for (i = 0;i < MAX_LOCALENTITIES;i++)
71 memset(l, 0, sizeof(*l));
79 static void entremove(localentity_t *e)
81 memset(e, 0, sizeof(*e));
82 e->freetime = (float)gametime + 1.0f;
85 static void phys_setupphysentities(void)
89 for (i = 0;i < MAX_LOCALENTITIES;i++)
91 l = localentities + i;
92 if (l->active && l->solid)
99 static void phys_moveentities(void)
103 for (i = 0;i < MAX_LOCALENTITIES;i++)
110 if (l->active && l->draw.model)
111 CGVM_Draw_Entity(&l->draw);
116 static void phys_updateentities(void)
118 phys_setupphysentities();
122 static void phys_update(localentity_t *e)
124 vec3_t impactpos, impactnormal, end;
126 float t, f, frac, bounce;
127 t = (float)frametime;
130 VectorMA(e->draw.angles, t, e->avelocity, e->draw.angles);
131 VectorMA(e->draw.origin, t, e->velocity, end);
132 frac = CGVM_TracePhysics(e->draw.origin, end, e->worldmins, e->worldmaxs, e->entitymins, e->entitymaxs, phys_entity, phys_entities, impactpos, impactnormal, &impactentnum);
133 VectorCopy(impactpos, e->draw.origin);
136 bounce = DotProduct(e->velocity, impactnormal) * -e->bouncescale;
137 VectorMA(e->velocity, bounce, impactnormal, e->velocity);
138 if (impactnormal[2] >= 0.7 && DotProduct(e->velocity, e->velocity) < 100*100)
140 VectorClear(e->velocity);
141 VectorClear(e->avelocity);
146 // FIXME: do some kind of touch code here if physentities get implemented
148 VectorCopy(impactpos, e->lastimpactorigin);
151 if (e->airfrictionscale)
153 if (DotProduct(e->velocity, e->velocity) < 10*10)
155 VectorClear(e->velocity);
156 VectorClear(e->avelocity);
160 f = 1 - (t * e->airfrictionscale);
163 VectorScale(e->velocity, f, e->velocity);
164 if (DotProduct(e->avelocity, e->avelocity) < 10*10)
166 VectorClear(e->avelocity);
170 VectorScale(e->avelocity, f, e->avelocity);
175 VectorClear(e->velocity);
176 VectorClear(e->avelocity);
181 e->velocity[2] += cg_gravity * e->gravityscale * t;
184 static void explosiondebris_framethink(localentity_t *self)
186 if (gametime > self->dietime)
188 self->draw.scale -= frametime * 3;
189 if (self->draw.scale < 0.05f)
198 static void gib_framethink(localentity_t *self)
200 if (gametime > self->dietime)
202 self->draw.scale -= (float)frametime * 3.0f;
203 if (self->draw.scale < 0.05f)
210 if (gametime > self->trailnexttime)
212 self->trailnexttime = gametime + 0.1f;
213 CGVM_BloodParticle(self->draw.origin, self->velocity);
219 static void gib_touchnetwork(localentity_t *self)
221 if (VectorDistance2(self->draw.origin, self->lastimpactorigin) >= 5*5)
222 CGVM_Stain(self->draw.origin, 64, 64, 24, 24, 48, 192, 48, 48, 48);
225 static void net_explosion(unsigned char num)
232 // need the time to know when the rubble should fade
234 // read the network data
237 for (i = 0;i < 40;i++)
243 VectorCopy(org, e->draw.origin);
244 e->draw.angles[0] = CGVM_RandomRange(0, 360);
245 e->draw.angles[1] = CGVM_RandomRange(0, 360);
246 e->draw.angles[2] = CGVM_RandomRange(0, 360);
247 VectorRandom(e->velocity);
248 VectorScale(e->velocity, 300, e->velocity);
249 e->velocity[2] -= (float)cg_gravity * 0.1f;
250 e->avelocity[0] = CGVM_RandomRange(0, 1440);
251 e->avelocity[1] = CGVM_RandomRange(0, 1440);
252 e->avelocity[2] = CGVM_RandomRange(0, 1440);
253 r = CGVM_RandomRange(0, 3);
255 e->draw.model = CGVM_Model("progs/rubble1.mdl");
257 e->draw.model = CGVM_Model("progs/rubble2.mdl");
259 e->draw.model = CGVM_Model("progs/rubble3.mdl");
264 e->draw.framelerp = 0;
266 VectorSet(e->worldmins, 0, 0, -8);
267 VectorSet(e->worldmaxs, 0, 0, -8);
268 VectorSet(e->entitymins, -8, -8, -8);
269 VectorSet(e->entitymaxs, 8, 8, 8);
270 e->bouncescale = 1.4f;
272 e->airfrictionscale = 1;
273 e->framethink = explosiondebris_framethink;
274 e->dietime = (float)time + 5.0f;
278 static void net_gibshower(unsigned char num)
281 float r, velocityscale;
285 // need the time to know when the gibs should fade
287 // read the network data
288 count = CGVM_MSG_ReadByte();
289 velocityscale = CGVM_MSG_ReadByte() * 100;
292 for (i = 0;i < count;i++)
298 VectorCopy(org, e->draw.origin);
299 e->draw.angles[0] = CGVM_RandomRange(0, 360);
300 e->draw.angles[1] = CGVM_RandomRange(0, 360);
301 e->draw.angles[2] = CGVM_RandomRange(0, 360);
302 VectorRandom(e->velocity);
303 VectorScale(e->velocity, velocityscale, e->velocity);
304 e->velocity[2] -= cg_gravity * 0.1;
305 e->avelocity[0] = CGVM_RandomRange(0, 1440);
306 e->avelocity[1] = CGVM_RandomRange(0, 1440);
307 e->avelocity[2] = CGVM_RandomRange(0, 1440);
308 r = CGVM_RandomRange(0, 3);
310 e->draw.model = CGVM_Model("progs/gib1.mdl");
312 e->draw.model = CGVM_Model("progs/gib2.mdl");
314 e->draw.model = CGVM_Model("progs/gib3.mdl");
319 e->draw.framelerp = 0;
321 VectorSet(e->worldmins, 0, 0, -8);
322 VectorSet(e->worldmaxs, 0, 0, -8);
323 VectorSet(e->entitymins, -8, -8, -8);
324 VectorSet(e->entitymaxs, 8, 8, 8);
325 e->bouncescale = 1.5;
327 e->airfrictionscale = 1;
328 e->framethink = gib_framethink;
329 e->touchnetwork = gib_touchnetwork;
330 e->dietime = (float)time + CGVM_RandomRange(3.0f, 5.0f);
337 localentity = CGVM_Malloc(sizeof(localentity_t) * MAX_LOCALENTITIES);
338 phys_entity = CGVM_Malloc(sizeof(cgphysentity_t) * MAX_LOCALENTITIES);
339 CGVM_RegisterNetworkCode(1, net_explosion);
340 CGVM_RegisterNetworkCode(2, net_gibshower);
345 void CG_Frame(double time)
347 cg_gravity = -CGVM_GetCvarFloat("sv_gravity");
348 frametime = time - gametime;
350 phys_updateentities();