2 #include "../dpdefs/csprogsdefs.qh"
4 #include "../common/stats.qh"
5 #include "../common/util.qh"
6 #include "movetypes.qh"
7 #include "../csqcmodellib/common.qh"
8 #include "../server/t_items.qh"
14 const int MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE = 4;
15 #define GRAVITY_UNAFFECTED_BY_TICRATE (getstati(STAT_MOVEFLAGS) & MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE)
17 .entity move_groundentity; // FIXME add move_groundnetworkentity?
18 .float move_suspendedinair;
19 .float move_didgravity;
21 void _Movetype_CheckVelocity() // SV_CheckVelocity
25 float _Movetype_CheckWater(entity ent) // SV_CheckWater
31 point = ent.move_origin;
32 point_z += (ent.mins.z + 1);
34 nativecontents = pointcontents(point);
36 if(ent.move_watertype)
37 if(ent.move_watertype != nativecontents)
39 //print(sprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents));
40 if(ent.contentstransition)
41 ent.contentstransition(ent.move_watertype, nativecontents);
44 ent.move_waterlevel = 0;
45 ent.move_watertype = CONTENT_EMPTY;
47 supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
48 if(supercontents & DPCONTENTS_LIQUIDSMASK)
50 ent.move_watertype = nativecontents;
51 ent.move_waterlevel = 1;
52 point_y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
53 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
55 ent.move_waterlevel = 2;
56 point_y = ent.origin.y + ent.view_ofs.y;
57 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
58 ent.move_waterlevel = 3;
62 return (ent.move_waterlevel > 1);
65 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
67 float contents = pointcontents(ent.move_origin);
69 if(!ent.move_watertype)
72 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
74 ent.move_watertype = contents;
75 ent.move_waterlevel = 1;
79 else if(ent.move_watertype != contents)
81 //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));
82 if(ent.contentstransition)
83 ent.contentstransition(ent.move_watertype, contents);
86 if(contents <= CONTENT_WATER)
88 ent.move_watertype = contents;
89 ent.move_waterlevel = 1;
93 ent.move_watertype = CONTENT_EMPTY;
94 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
98 void _Movetype_Impact(entity oth) // SV_Impact
100 entity oldother, oldself;
126 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
128 entity e, oldself, oldother;
133 for(e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
136 if(boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
141 trace_allsolid = false;
142 trace_startsolid = false;
144 trace_inwater = false;
146 trace_endpos = e.origin;
147 trace_plane_normal = '0 0 1';
148 trace_plane_dist = 0;
159 void _Movetype_LinkEdict(float touch_triggers) // SV_LinkEdict
162 if(self.solid == SOLID_BSP)
164 // TODO set the absolute bbox
173 mi = mi + self.origin;
174 ma = ma + self.origin;
176 if(self.move_flags & FL_ITEM)
199 _Movetype_LinkEdict_TouchAreaGrid();
202 float _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
205 org = self.move_origin + ofs;
207 int cont = self.dphitcontentsmask;
208 self.dphitcontentsmask = DPCONTENTS_SOLID;
209 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
210 self.dphitcontentsmask = cont;
215 if(vlen(trace_endpos - self.move_origin) > 0.0001)
216 self.move_origin = trace_endpos;
220 float _Movetype_UnstickEntity() // SV_UnstickEntity
222 if(!_Movetype_TestEntityPosition('0 0 0'))
224 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
225 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
226 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
227 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
228 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
229 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
230 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
231 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
233 for(i = 1; i <= 17; ++i)
235 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
236 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
238 dprintf("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
241 dprintf("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
242 _Movetype_LinkEdict(true);
246 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
248 vel = vel - ((vel * norm) * norm) * f;
250 if(vel.x > -0.1 && vel.x < 0.1) vel_x = 0;
251 if(vel.y > -0.1 && vel.y < 0.1) vel_y = 0;
252 if(vel.z > -0.1 && vel.z < 0.1) vel_z = 0;
257 void _Movetype_PushEntityTrace(vector push)
262 end = self.move_origin + push;
264 if(self.move_nomonsters)
265 type = max(0, self.move_nomonsters);
266 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
268 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
269 type = MOVE_NOMONSTERS;
273 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
276 float _Movetype_PushEntity(vector push, float failonstartsolid) // SV_PushEntity
278 _Movetype_PushEntityTrace(push);
280 if(trace_startsolid && failonstartsolid)
281 return trace_fraction;
283 self.move_origin = trace_endpos;
285 if(trace_fraction < 1)
286 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
287 _Movetype_Impact(trace_ent);
289 return trace_fraction;
292 const float MAX_CLIP_PLANES = 5;
293 void _Movetype_Physics_Toss(float dt) // SV_Physics_Toss
295 if(self.move_flags & FL_ONGROUND)
297 if(self.move_velocity.z >= 1/32)
298 self.move_flags &= ~FL_ONGROUND;
299 else if(!self.move_groundentity)
301 else if(self.move_suspendedinair && wasfreed(self.move_groundentity))
303 self.move_groundentity = world;
308 self.move_suspendedinair = false;
310 _Movetype_CheckVelocity();
312 if(self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS)
314 self.move_didgravity = 1;
315 if(GRAVITY_UNAFFECTED_BY_TICRATE)
318 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
320 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
325 self.move_velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
327 self.move_velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
331 self.move_angles = self.move_angles + self.move_avelocity * dt;
333 float movetime, bump;
335 for(bump = 0; bump < MAX_CLIP_PLANES && movetime > 0; ++bump)
338 move = self.move_velocity * movetime;
339 _Movetype_PushEntity(move, true);
345 _Movetype_UnstickEntity();
346 _Movetype_PushEntity(move, false);
351 if(trace_fraction == 1)
354 movetime *= 1 - min(1, trace_fraction);
356 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
358 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 2.0);
359 self.move_flags &= ~FL_ONGROUND;
361 else if(self.move_movetype == MOVETYPE_BOUNCE)
363 float d, bouncefac, bouncestop;
365 bouncefac = self.move_bounce_factor; if(!bouncefac) bouncefac = 0.5;
366 bouncestop = self.move_bounce_stopspeed; if(!bouncestop) bouncestop = 60 / 800;
368 bouncestop *= self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
370 bouncestop *= getstatf(STAT_MOVEVARS_GRAVITY);
372 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1 + bouncefac);
374 d = trace_plane_normal * self.move_velocity;
375 if(trace_plane_normal.z > 0.7 && d < bouncestop && d > -bouncestop)
377 self.move_flags |= FL_ONGROUND;
378 self.move_groundentity = trace_ent;
379 self.move_velocity = '0 0 0';
380 self.move_avelocity = '0 0 0';
383 self.move_flags &= ~FL_ONGROUND;
387 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1.0);
388 if(trace_plane_normal.z > 0.7)
390 self.move_flags |= FL_ONGROUND;
391 self.move_groundentity = trace_ent;
392 if(trace_ent.solid == SOLID_BSP)
393 self.move_suspendedinair = true;
394 self.move_velocity = '0 0 0';
395 self.move_avelocity = '0 0 0';
398 self.move_flags &= ~FL_ONGROUND;
401 // DP revision 8905 (just, WHY...)
402 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
405 // DP revision 8918 (WHY...)
406 if(self.move_flags & FL_ONGROUND)
410 if(GRAVITY_UNAFFECTED_BY_TICRATE)
411 if(self.move_didgravity > 0)
412 if(!(self.move_flags & FL_ONGROUND))
415 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
417 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
420 _Movetype_CheckWaterTransition(self);
423 void _Movetype_Physics_Frame(float movedt)
425 self.move_didgravity = -1;
426 switch(self.move_movetype)
429 case MOVETYPE_FAKEPUSH:
430 error("SV_Physics_Pusher not implemented");
434 case MOVETYPE_FOLLOW:
435 error("SV_Physics_Follow not implemented");
437 case MOVETYPE_NOCLIP:
438 _Movetype_CheckWater(self);
439 self.move_origin = self.move_origin + ticrate * self.move_velocity;
440 self.move_angles = self.move_angles + ticrate * self.move_avelocity;
441 _Movetype_LinkEdict(false);
444 error("SV_Physics_Step not implemented");
447 error("SV_Physics_Walk not implemented");
450 case MOVETYPE_BOUNCE:
451 case MOVETYPE_BOUNCEMISSILE:
452 case MOVETYPE_FLYMISSILE:
454 _Movetype_Physics_Toss(movedt);
459 void Movetype_Physics_NoMatchServer() // optimized
463 movedt = time - self.move_time;
464 self.move_time = time;
466 _Movetype_Physics_Frame(movedt);
470 self.avelocity = self.move_avelocity;
471 self.velocity = self.move_velocity;
472 self.angles = self.move_angles;
473 setorigin(self, self.move_origin);
476 void Movetype_Physics_MatchServer(bool sloppy)
478 Movetype_Physics_MatchTicrate(ticrate, sloppy);
481 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
483 float n, i, dt, movedt;
487 Movetype_Physics_NoMatchServer();
491 dt = time - self.move_time;
494 n = max(0, floor(dt / tr));
496 self.move_time += n * tr;
498 if(!self.move_didgravity)
499 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
501 for(i = 0; i < n; ++i)
503 _Movetype_Physics_Frame(movedt);
508 self.avelocity = self.move_avelocity;
510 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
512 // now continue the move from move_time to time
513 self.velocity = self.move_velocity;
515 if(self.move_didgravity > 0)
517 if(GRAVITY_UNAFFECTED_BY_TICRATE)
520 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
522 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
527 self.velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
529 self.velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
533 self.angles = self.move_angles + dt * self.avelocity;
535 if(sloppy || self.movetype == MOVETYPE_NOCLIP)
537 setorigin(self, self.move_origin + dt * self.velocity);
541 _Movetype_PushEntityTrace(dt * self.velocity);
542 if(!trace_startsolid)
543 setorigin(self, trace_endpos);
546 if(self.move_didgravity > 0)
548 if(GRAVITY_UNAFFECTED_BY_TICRATE)
551 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
553 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
559 self.velocity = self.move_velocity;
560 self.angles = self.move_angles;
561 setorigin(self, self.move_origin);