1 const float MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE = 4;
2 #define GRAVITY_UNAFFECTED_BY_TICRATE (getstati(STAT_MOVEFLAGS) & MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE)
4 .entity move_groundentity; // FIXME add move_groundnetworkentity?
5 .float move_suspendedinair;
6 .float move_didgravity;
8 void _Movetype_CheckVelocity() // SV_CheckVelocity
12 float _Movetype_CheckWater(entity ent) // SV_CheckWater
18 point = ent.move_origin;
19 point_z += (ent.mins_z + 1);
21 nativecontents = pointcontents(point);
23 if(ent.move_watertype)
24 if(ent.move_watertype != nativecontents)
26 //print(sprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents));
27 if(ent.contentstransition)
28 ent.contentstransition(ent.move_watertype, nativecontents);
31 ent.move_waterlevel = 0;
32 ent.move_watertype = CONTENT_EMPTY;
34 supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
35 if(supercontents & DPCONTENTS_LIQUIDSMASK)
37 ent.move_watertype = nativecontents;
38 ent.move_waterlevel = 1;
39 point_y = (ent.origin_y + ((ent.mins_z + ent.maxs_y) * 0.5));
40 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
42 ent.move_waterlevel = 2;
43 point_y = ent.origin_y + ent.view_ofs_y;
44 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
45 ent.move_waterlevel = 3;
49 return (ent.move_waterlevel > 1);
52 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
54 float contents = pointcontents(ent.move_origin);
56 if(!ent.move_watertype)
59 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
61 ent.move_watertype = contents;
62 ent.move_waterlevel = 1;
66 else if(ent.move_watertype != contents)
68 //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));
69 if(ent.contentstransition)
70 ent.contentstransition(ent.move_watertype, contents);
73 if(contents <= CONTENT_WATER)
75 ent.move_watertype = contents;
76 ent.move_waterlevel = 1;
80 ent.move_watertype = CONTENT_EMPTY;
81 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
85 void _Movetype_Impact(entity oth) // SV_Impact
87 entity oldother, oldself;
113 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
115 entity e, oldself, oldother;
120 for(e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
123 if(boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
128 trace_allsolid = FALSE;
129 trace_startsolid = FALSE;
131 trace_inwater = FALSE;
133 trace_endpos = e.origin;
134 trace_plane_normal = '0 0 1';
135 trace_plane_dist = 0;
146 void _Movetype_LinkEdict(float touch_triggers) // SV_LinkEdict
149 if(self.solid == SOLID_BSP)
151 // TODO set the absolute bbox
160 mi = mi + self.origin;
161 ma = ma + self.origin;
163 if(self.move_flags & FL_ITEM)
186 _Movetype_LinkEdict_TouchAreaGrid();
189 float _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
193 org = self.move_origin + ofs;
195 cont = self.dphitcontentsmask;
196 self.dphitcontentsmask = DPCONTENTS_SOLID;
197 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
198 self.dphitcontentsmask = cont;
203 if(vlen(trace_endpos - self.move_origin) > 0.0001)
204 self.move_origin = trace_endpos;
208 float _Movetype_UnstickEntity() // SV_UnstickEntity
210 if(!_Movetype_TestEntityPosition('0 0 0'))
212 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
213 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
214 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
215 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
216 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
217 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
218 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
219 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
221 for(i = 1; i <= 17; ++i)
223 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
224 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
226 dprintf("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
229 dprintf("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
230 _Movetype_LinkEdict(TRUE);
234 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
236 vel = vel - ((vel * norm) * norm) * f;
238 if(vel_x > -0.1 && vel_x < 0.1) vel_x = 0;
239 if(vel_y > -0.1 && vel_y < 0.1) vel_y = 0;
240 if(vel_z > -0.1 && vel_z < 0.1) vel_z = 0;
245 void _Movetype_PushEntityTrace(vector push)
250 end = self.move_origin + push;
252 if(self.move_nomonsters)
253 type = max(0, self.move_nomonsters);
254 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
256 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
257 type = MOVE_NOMONSTERS;
261 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
264 float _Movetype_PushEntity(vector push, float failonstartsolid) // SV_PushEntity
266 _Movetype_PushEntityTrace(push);
268 if(trace_startsolid && failonstartsolid)
269 return trace_fraction;
271 self.move_origin = trace_endpos;
273 if(trace_fraction < 1)
274 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
275 _Movetype_Impact(trace_ent);
277 return trace_fraction;
280 const float MAX_CLIP_PLANES = 5;
281 void _Movetype_Physics_Toss(float dt) // SV_Physics_Toss
283 if(self.move_flags & FL_ONGROUND)
285 if(self.move_velocity_z >= 1/32)
286 self.move_flags &= ~FL_ONGROUND;
287 else if(!self.move_groundentity)
289 else if(self.move_suspendedinair && wasfreed(self.move_groundentity))
291 self.move_groundentity = world;
296 self.move_suspendedinair = FALSE;
298 _Movetype_CheckVelocity();
300 if(self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS)
302 self.move_didgravity = 1;
303 if(GRAVITY_UNAFFECTED_BY_TICRATE)
306 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
308 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
313 self.move_velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
315 self.move_velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
319 self.move_angles = self.move_angles + self.move_avelocity * dt;
321 float movetime, bump;
323 for(bump = 0; bump < MAX_CLIP_PLANES && movetime > 0; ++bump)
326 move = self.move_velocity * movetime;
327 _Movetype_PushEntity(move, TRUE);
333 _Movetype_UnstickEntity();
334 _Movetype_PushEntity(move, FALSE);
339 if(trace_fraction == 1)
342 movetime *= 1 - min(1, trace_fraction);
344 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
346 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 2.0);
347 self.move_flags &= ~FL_ONGROUND;
349 else if(self.move_movetype == MOVETYPE_BOUNCE)
351 float d, bouncefac, bouncestop;
353 bouncefac = self.move_bounce_factor; if(!bouncefac) bouncefac = 0.5;
354 bouncestop = self.move_bounce_stopspeed; if(!bouncestop) bouncestop = 60 / 800;
356 bouncestop *= self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
358 bouncestop *= getstatf(STAT_MOVEVARS_GRAVITY);
360 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1 + bouncefac);
362 d = trace_plane_normal * self.move_velocity;
363 if(trace_plane_normal_z > 0.7 && d < bouncestop && d > -bouncestop)
365 self.move_flags |= FL_ONGROUND;
366 self.move_groundentity = trace_ent;
367 self.move_velocity = '0 0 0';
368 self.move_avelocity = '0 0 0';
371 self.move_flags &= ~FL_ONGROUND;
375 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1.0);
376 if(trace_plane_normal_z > 0.7)
378 self.move_flags |= FL_ONGROUND;
379 self.move_groundentity = trace_ent;
380 if(trace_ent.solid == SOLID_BSP)
381 self.move_suspendedinair = TRUE;
382 self.move_velocity = '0 0 0';
383 self.move_avelocity = '0 0 0';
386 self.move_flags &= ~FL_ONGROUND;
389 // DP revision 8905 (just, WHY...)
390 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
393 // DP revision 8918 (WHY...)
394 if(self.move_flags & FL_ONGROUND)
398 if(GRAVITY_UNAFFECTED_BY_TICRATE)
399 if(self.move_didgravity > 0)
400 if(!(self.move_flags & FL_ONGROUND))
403 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
405 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
408 _Movetype_CheckWaterTransition(self);
411 void _Movetype_Physics_Frame(float movedt)
413 self.move_didgravity = -1;
414 switch(self.move_movetype)
417 case MOVETYPE_FAKEPUSH:
418 error("SV_Physics_Pusher not implemented");
422 case MOVETYPE_FOLLOW:
423 error("SV_Physics_Follow not implemented");
425 case MOVETYPE_NOCLIP:
426 _Movetype_CheckWater(self);
427 self.move_origin = self.move_origin + ticrate * self.move_velocity;
428 self.move_angles = self.move_angles + ticrate * self.move_avelocity;
429 _Movetype_LinkEdict(FALSE);
432 error("SV_Physics_Step not implemented");
435 error("SV_Physics_Walk not implemented");
438 case MOVETYPE_BOUNCE:
439 case MOVETYPE_BOUNCEMISSILE:
440 case MOVETYPE_FLYMISSILE:
442 _Movetype_Physics_Toss(movedt);
447 void Movetype_Physics_NoMatchServer() // optimized
451 movedt = time - self.move_time;
452 self.move_time = time;
454 _Movetype_Physics_Frame(movedt);
458 self.avelocity = self.move_avelocity;
459 self.velocity = self.move_velocity;
460 self.angles = self.move_angles;
461 setorigin(self, self.move_origin);
464 void Movetype_Physics_MatchServer(bool sloppy)
466 Movetype_Physics_MatchTicrate(ticrate, sloppy);
469 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
471 float n, i, dt, movedt;
475 Movetype_Physics_NoMatchServer();
479 dt = time - self.move_time;
482 n = max(0, floor(dt / tr));
484 self.move_time += n * tr;
486 if(!self.move_didgravity)
487 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
489 for(i = 0; i < n; ++i)
491 _Movetype_Physics_Frame(movedt);
496 self.avelocity = self.move_avelocity;
498 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
500 // now continue the move from move_time to time
501 self.velocity = self.move_velocity;
503 if(self.move_didgravity > 0)
505 if(GRAVITY_UNAFFECTED_BY_TICRATE)
508 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
510 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
515 self.velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
517 self.velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
521 self.angles = self.move_angles + dt * self.avelocity;
523 if(sloppy || self.movetype == MOVETYPE_NOCLIP)
525 setorigin(self, self.move_origin + dt * self.velocity);
529 _Movetype_PushEntityTrace(dt * self.velocity);
530 if(!trace_startsolid)
531 setorigin(self, trace_endpos);
534 if(self.move_didgravity > 0)
536 if(GRAVITY_UNAFFECTED_BY_TICRATE)
539 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
541 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
547 self.velocity = self.move_velocity;
548 self.angles = self.move_angles;
549 setorigin(self, self.move_origin);