1 #include "movetypes.qh"
6 #include "../common/stats.qh"
7 #include "../common/util.qh"
9 #include "../csqcmodellib/common.qh"
11 #include "../dpdefs/csprogsdefs.qh"
15 const int MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE = 4;
16 #define GRAVITY_UNAFFECTED_BY_TICRATE (getstati(STAT_MOVEFLAGS) & MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE)
18 .entity move_groundentity; // FIXME add move_groundnetworkentity?
19 .float move_suspendedinair;
20 .float move_didgravity;
22 void _Movetype_CheckVelocity() // SV_CheckVelocity
26 float _Movetype_CheckWater(entity ent) // SV_CheckWater
28 vector point = ent.move_origin;
29 point.z += (ent.mins.z + 1);
31 int nativecontents = pointcontents(point);
33 if(ent.move_watertype)
34 if(ent.move_watertype != nativecontents)
36 //print(sprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents));
37 if(ent.contentstransition)
38 ent.contentstransition(ent.move_watertype, nativecontents);
41 ent.move_waterlevel = 0;
42 ent.move_watertype = CONTENT_EMPTY;
44 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
45 if(supercontents & DPCONTENTS_LIQUIDSMASK)
47 ent.move_watertype = nativecontents;
48 ent.move_waterlevel = 1;
49 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
50 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
52 ent.move_waterlevel = 2;
53 point.y = ent.origin.y + ent.view_ofs.y;
54 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
55 ent.move_waterlevel = 3;
59 return (ent.move_waterlevel > 1);
62 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
64 float contents = pointcontents(ent.move_origin);
66 if(!ent.move_watertype)
69 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
71 ent.move_watertype = contents;
72 ent.move_waterlevel = 1;
76 else if(ent.move_watertype != contents)
78 //print(sprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents));
79 if(ent.contentstransition)
80 ent.contentstransition(ent.move_watertype, contents);
83 if(contents <= CONTENT_WATER)
85 ent.move_watertype = contents;
86 ent.move_waterlevel = 1;
90 ent.move_watertype = CONTENT_EMPTY;
91 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
95 void _Movetype_Impact(entity oth) // SV_Impact
97 entity oldother, oldself;
123 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
125 entity e, oldself, oldother;
130 for(e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
133 if(boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
138 trace_allsolid = false;
139 trace_startsolid = false;
141 trace_inwater = false;
143 trace_endpos = e.origin;
144 trace_plane_normal = '0 0 1';
145 trace_plane_dist = 0;
156 void _Movetype_LinkEdict(float touch_triggers) // SV_LinkEdict
159 if(self.solid == SOLID_BSP)
161 // TODO set the absolute bbox
170 mi = mi + self.origin;
171 ma = ma + self.origin;
173 if(self.move_flags & FL_ITEM)
196 _Movetype_LinkEdict_TouchAreaGrid();
199 float _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
202 org = self.move_origin + ofs;
204 int cont = self.dphitcontentsmask;
205 self.dphitcontentsmask = DPCONTENTS_SOLID;
206 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
207 self.dphitcontentsmask = cont;
212 if(vlen(trace_endpos - self.move_origin) > 0.0001)
213 self.move_origin = trace_endpos;
217 float _Movetype_UnstickEntity() // SV_UnstickEntity
219 if(!_Movetype_TestEntityPosition('0 0 0'))
221 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
222 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
223 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
224 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
225 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
226 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
227 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
228 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
230 for(i = 1; i <= 17; ++i)
232 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
233 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
235 dprintf("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
238 dprintf("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
239 _Movetype_LinkEdict(true);
243 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
245 vel = vel - ((vel * norm) * norm) * f;
247 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
248 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
249 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
254 void _Movetype_PushEntityTrace(vector push)
259 end = self.move_origin + push;
261 if(self.move_nomonsters)
262 type = max(0, self.move_nomonsters);
263 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
265 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
266 type = MOVE_NOMONSTERS;
270 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
273 float _Movetype_PushEntity(vector push, float failonstartsolid) // SV_PushEntity
275 _Movetype_PushEntityTrace(push);
277 if(trace_startsolid && failonstartsolid)
278 return trace_fraction;
280 self.move_origin = trace_endpos;
282 if(trace_fraction < 1)
283 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
284 _Movetype_Impact(trace_ent);
286 return trace_fraction;
289 const float MAX_CLIP_PLANES = 5;
290 void _Movetype_Physics_Toss(float dt) // SV_Physics_Toss
292 if(self.move_flags & FL_ONGROUND)
294 if(self.move_velocity.z >= 1/32)
295 self.move_flags &= ~FL_ONGROUND;
296 else if(!self.move_groundentity)
298 else if(self.move_suspendedinair && wasfreed(self.move_groundentity))
300 self.move_groundentity = world;
305 self.move_suspendedinair = false;
307 _Movetype_CheckVelocity();
309 if(self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS)
311 self.move_didgravity = 1;
312 if(GRAVITY_UNAFFECTED_BY_TICRATE)
315 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
317 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
322 self.move_velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
324 self.move_velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
328 self.move_angles = self.move_angles + self.move_avelocity * dt;
330 float movetime, bump;
332 for(bump = 0; bump < MAX_CLIP_PLANES && movetime > 0; ++bump)
335 move = self.move_velocity * movetime;
336 _Movetype_PushEntity(move, true);
342 _Movetype_UnstickEntity();
343 _Movetype_PushEntity(move, false);
348 if(trace_fraction == 1)
351 movetime *= 1 - min(1, trace_fraction);
353 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
355 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 2.0);
356 self.move_flags &= ~FL_ONGROUND;
358 else if(self.move_movetype == MOVETYPE_BOUNCE)
360 float d, bouncefac, bouncestop;
362 bouncefac = self.move_bounce_factor; if(!bouncefac) bouncefac = 0.5;
363 bouncestop = self.move_bounce_stopspeed; if(!bouncestop) bouncestop = 60 / 800;
365 bouncestop *= self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
367 bouncestop *= getstatf(STAT_MOVEVARS_GRAVITY);
369 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1 + bouncefac);
371 d = trace_plane_normal * self.move_velocity;
372 if(trace_plane_normal.z > 0.7 && d < bouncestop && d > -bouncestop)
374 self.move_flags |= FL_ONGROUND;
375 self.move_groundentity = trace_ent;
376 self.move_velocity = '0 0 0';
377 self.move_avelocity = '0 0 0';
380 self.move_flags &= ~FL_ONGROUND;
384 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1.0);
385 if(trace_plane_normal.z > 0.7)
387 self.move_flags |= FL_ONGROUND;
388 self.move_groundentity = trace_ent;
389 if(trace_ent.solid == SOLID_BSP)
390 self.move_suspendedinair = true;
391 self.move_velocity = '0 0 0';
392 self.move_avelocity = '0 0 0';
395 self.move_flags &= ~FL_ONGROUND;
398 // DP revision 8905 (just, WHY...)
399 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
402 // DP revision 8918 (WHY...)
403 if(self.move_flags & FL_ONGROUND)
407 if(GRAVITY_UNAFFECTED_BY_TICRATE)
408 if(self.move_didgravity > 0)
409 if(!(self.move_flags & FL_ONGROUND))
412 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
414 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
417 _Movetype_CheckWaterTransition(self);
420 void _Movetype_Physics_Frame(float movedt)
422 self.move_didgravity = -1;
423 switch(self.move_movetype)
426 case MOVETYPE_FAKEPUSH:
427 error("SV_Physics_Pusher not implemented");
431 case MOVETYPE_FOLLOW:
432 error("SV_Physics_Follow not implemented");
434 case MOVETYPE_NOCLIP:
435 _Movetype_CheckWater(self);
436 self.move_origin = self.move_origin + ticrate * self.move_velocity;
437 self.move_angles = self.move_angles + ticrate * self.move_avelocity;
438 _Movetype_LinkEdict(false);
441 error("SV_Physics_Step not implemented");
444 error("SV_Physics_Walk not implemented");
447 case MOVETYPE_BOUNCE:
448 case MOVETYPE_BOUNCEMISSILE:
449 case MOVETYPE_FLYMISSILE:
451 _Movetype_Physics_Toss(movedt);
456 void Movetype_Physics_NoMatchServer() // optimized
460 movedt = time - self.move_time;
461 self.move_time = time;
463 _Movetype_Physics_Frame(movedt);
467 self.avelocity = self.move_avelocity;
468 self.velocity = self.move_velocity;
469 self.angles = self.move_angles;
470 setorigin(self, self.move_origin);
473 void Movetype_Physics_MatchServer(bool sloppy)
475 Movetype_Physics_MatchTicrate(ticrate, sloppy);
478 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
480 float n, i, dt, movedt;
484 Movetype_Physics_NoMatchServer();
488 dt = time - self.move_time;
491 n = max(0, floor(dt / tr));
493 self.move_time += n * tr;
495 if(!self.move_didgravity)
496 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
498 for(i = 0; i < n; ++i)
500 _Movetype_Physics_Frame(movedt);
505 self.avelocity = self.move_avelocity;
507 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
509 // now continue the move from move_time to time
510 self.velocity = self.move_velocity;
512 if(self.move_didgravity > 0)
514 if(GRAVITY_UNAFFECTED_BY_TICRATE)
517 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
519 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
524 self.velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
526 self.velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
530 self.angles = self.move_angles + dt * self.avelocity;
532 if(sloppy || self.movetype == MOVETYPE_NOCLIP)
534 setorigin(self, self.move_origin + dt * self.velocity);
538 _Movetype_PushEntityTrace(dt * self.velocity);
539 if(!trace_startsolid)
540 setorigin(self, trace_endpos);
543 if(self.move_didgravity > 0)
545 if(GRAVITY_UNAFFECTED_BY_TICRATE)
548 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
550 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
556 self.velocity = self.move_velocity;
557 self.angles = self.move_angles;
558 setorigin(self, self.move_origin);