1 #include "movetypes.qh"
4 void set_movetype(entity this, int mt)
6 this.move_movetype = mt;
7 if (mt == MOVETYPE_PHYSICS) {
8 this.move_qcphysics = false;
10 if(!IL_CONTAINS(g_moveables, this))
11 IL_PUSH(g_moveables, this); // add it to the moveable entities list (even if it doesn't move!) logic: if an object never sets its movetype, we assume it never does anything notable
12 this.movetype = (this.move_qcphysics) ? MOVETYPE_QCENTITY : mt;
15 void set_movetype(entity this, int mt)
17 this.move_movetype = mt;
21 bool _Movetype_NudgeOutOfSolid_PivotIsKnownGood(entity this, vector pivot) // SV_NudgeOutOfSolid_PivotIsKnownGood
23 vector stuckorigin = this.origin;
24 vector goodmins = pivot, goodmaxs = pivot;
25 for(int bump = 0; bump < 6; bump++)
27 int coord = 2 - (bump >> 1);
30 for(int subbump = 0; ; ++subbump)
32 vector testorigin = stuckorigin;
38 case 0: testorigin.x += this.maxs_x - goodmaxs.x; break;
39 case 1: testorigin.y += this.maxs_y - goodmaxs.y; break;
40 case 2: testorigin.z += this.maxs_z - goodmaxs.z; break;
48 case 0: testorigin.x += this.mins_x - goodmins.x; break;
49 case 1: testorigin.y += this.mins_y - goodmins.y; break;
50 case 2: testorigin.z += this.mins_z - goodmins.z; break;
54 tracebox(stuckorigin, goodmins, goodmaxs, testorigin, MOVE_NOMONSTERS, this);
55 if(trace_startsolid && trace_ent.solid == SOLID_BSP) // NOTE: this checks for bmodelstartsolid in the engine
57 // BAD BAD, can't fix that
61 if(trace_fraction >= 1)
66 // BAD BAD, can't fix that
70 // we hit something... let's move out of it
71 vector move = trace_endpos - testorigin;
72 float nudge = (trace_plane_normal * move) + 0.03125; // FIXME cvar this constant
73 stuckorigin = stuckorigin + nudge * trace_plane_normal;
81 case 0: goodmaxs.x = this.maxs_x; break;
82 case 1: goodmaxs.y = this.maxs_y; break;
83 case 2: goodmaxs.z = this.maxs_z; break;
91 case 0: goodmins.x = this.mins_x; break;
92 case 1: goodmins.y = this.mins_y; break;
93 case 2: goodmins.z = this.mins_z; break;
99 this.origin = stuckorigin;
104 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
108 makevectors(this.v_angle);
109 d = (stepnormal * v_forward) + 0.5;
113 i = (stepnormal * this.velocity);
114 into = i * stepnormal;
115 side = this.velocity - into;
116 this.velocity_x = side.x * (1 * d);
117 this.velocity_y = side.y * (1 * d);
121 vector planes[MAX_CLIP_PLANES];
122 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
124 move_stepnormal = '0 0 0';
130 int i, j, numplanes = 0;
131 float time_left = dt, grav = 0;
133 vector primal_velocity, original_velocity;
134 vector restore_velocity = this.velocity;
136 for(i = 0; i < MAX_CLIP_PLANES; ++i)
141 this.move_didgravity = 1;
142 grav = dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
144 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
146 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
147 this.velocity_z -= grav * 0.5;
149 this.velocity_z -= grav;
153 original_velocity = primal_velocity = this.velocity;
155 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
157 if(this.velocity == '0 0 0')
160 push = this.velocity * time_left;
161 if(!_Movetype_PushEntity(this, push, true, false))
163 // we got teleported by a touch function
164 // let's abort the move
169 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
170 // abort move if we're stuck in the world (and didn't make it out)
171 if(trace_startsolid && trace_allsolid)
173 this.velocity = restore_velocity;
177 if(trace_fraction == 1)
180 float my_trace_fraction = trace_fraction;
181 vector my_trace_plane_normal = trace_plane_normal;
183 if(trace_plane_normal.z)
185 if(trace_plane_normal.z > 0.7)
192 //dprint("_Movetype_FlyMove: !trace_ent\n");
197 this.groundentity = trace_ent;
202 // step - handle it immediately
203 vector org = this.origin;
204 vector steppush = '0 0 1' * stepheight;
206 if(!_Movetype_PushEntity(this, steppush, true, false))
211 if(!_Movetype_PushEntity(this, push, true, false))
216 float trace2_fraction = trace_fraction;
217 steppush = vec3(0, 0, org.z - this.origin_z);
218 if(!_Movetype_PushEntity(this, steppush, true, false))
224 // accept the new position if it made some progress...
225 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
227 trace_endpos = this.origin;
228 time_left *= 1 - trace2_fraction;
237 // step - return it to caller
239 // save the trace for player extrafriction
241 move_stepnormal = trace_plane_normal;
244 if(my_trace_fraction >= 0.001)
246 // actually covered some distance
247 original_velocity = this.velocity;
251 time_left *= 1 - my_trace_fraction;
253 // clipped to another plane
254 if(numplanes >= MAX_CLIP_PLANES)
256 // this shouldn't really happen
257 this.velocity = '0 0 0';
262 planes[numplanes] = my_trace_plane_normal;
265 // modify original_velocity so it parallels all of the clip planes
266 vector new_velocity = '0 0 0';
267 for (i = 0;i < numplanes;i++)
269 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
270 for (j = 0;j < numplanes;j++)
275 if((new_velocity * planes[j]) < 0)
285 // go along this plane
286 this.velocity = new_velocity;
290 // go along the crease
293 this.velocity = '0 0 0';
297 vector dir = cross(planes[0], planes[1]);
298 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
299 float ilength = sqrt((dir * dir));
301 ilength = 1.0 / ilength;
305 float d = (dir * this.velocity);
306 this.velocity = dir * d;
309 // if current velocity is against the original velocity,
310 // stop dead to avoid tiny occilations in sloping corners
311 if((this.velocity * primal_velocity) <= 0)
313 this.velocity = '0 0 0';
318 // LordHavoc: this came from QW and allows you to get out of water more easily
319 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blockedflag & 8))
320 this.velocity = primal_velocity;
322 if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blockedflag & 8))
323 this.velocity = primal_velocity;
327 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
329 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
330 this.velocity_z -= grav * 0.5f;
337 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
339 // if(vlen(this.velocity) < 0.0001)
340 // this.velocity = '0 0 0';
343 bool _Movetype_CheckWater(entity this) // SV_CheckWater
345 vector point = this.origin;
346 point.z += this.mins.z + 1;
348 int nativecontents = pointcontents(point);
349 if(this.watertype && this.watertype != nativecontents)
351 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
352 if(this.contentstransition)
353 this.contentstransition(this.watertype, nativecontents);
356 this.waterlevel = WATERLEVEL_NONE;
357 this.watertype = CONTENT_EMPTY;
359 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
360 if(supercontents & DPCONTENTS_LIQUIDSMASK)
362 this.watertype = nativecontents;
363 this.waterlevel = WATERLEVEL_WETFEET;
364 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
365 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
367 this.waterlevel = WATERLEVEL_SWIMMING;
368 point.z = this.origin.z + this.view_ofs.z;
369 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
370 this.waterlevel = WATERLEVEL_SUBMERGED;
374 return this.waterlevel > 1;
377 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
379 int contents = pointcontents(ent.origin);
384 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
386 ent.watertype = contents;
391 else if(ent.watertype != contents)
393 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
394 if(ent.contentstransition)
395 ent.contentstransition(ent.watertype, contents);
398 if(contents <= CONTENT_WATER)
400 ent.watertype = contents;
405 ent.watertype = CONTENT_EMPTY;
406 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
410 void _Movetype_Impact(entity this, entity oth) // SV_Impact
415 if(this.solid != SOLID_NOT && gettouch(this))
416 gettouch(this)(this, oth);
418 if(oth.solid != SOLID_NOT && gettouch(oth))
419 gettouch(oth)(oth, this);
422 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
424 if(this.solid == SOLID_NOT)
427 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
428 if (it.solid == SOLID_TRIGGER && it != this)
429 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
430 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
432 trace_allsolid = false;
433 trace_startsolid = false;
435 trace_inwater = false;
437 trace_endpos = it.origin;
438 trace_plane_normal = '0 0 1';
439 trace_plane_dist = 0;
441 trace_dpstartcontents = 0;
442 trace_dphitcontents = 0;
443 trace_dphitq3surfaceflags = 0;
444 trace_dphittexturename = string_null;
446 gettouch(it)(it, this);
451 bool autocvar__movetype_debug = false;
452 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
454 if(autocvar__movetype_debug)
457 if(this.solid == SOLID_BSP)
459 // TODO set the absolute bbox
471 if(this.flags & FL_ITEM)
487 setorigin(this, this.origin); // calls SV_LinkEdict
489 // NOTE: CSQC's version of setorigin doesn't expand
490 this.absmin -= '1 1 1';
491 this.absmax += '1 1 1';
496 _Movetype_LinkEdict_TouchAreaGrid(this);
499 int _Movetype_ContentsMask(entity this) // SV_GenericHitSuperContentsMask
503 if(this.dphitcontentsmask)
504 return this.dphitcontentsmask;
505 else if(this.solid == SOLID_SLIDEBOX)
507 if(this.flags & 32) // TODO: FL_MONSTER
508 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
510 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
512 else if(this.solid == SOLID_CORPSE)
513 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
514 else if(this.solid == SOLID_TRIGGER)
515 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
517 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
520 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
523 entity _Movetype_TestEntityPosition_ent;
524 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
526 entity this = _Movetype_TestEntityPosition_ent;
527 vector org = this.origin + ofs;
529 //int cont = this.dphitcontentsmask;
530 //this.dphitcontentsmask = DPCONTENTS_SOLID;
531 tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
532 //this.dphitcontentsmask = cont;
533 if(trace_dpstartcontents & _Movetype_ContentsMask(this))
536 if(vlen2(trace_endpos - this.origin) >= 0.0001)
538 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
539 if(!trace_startsolid)
540 this.origin = trace_endpos;
545 bool _Movetype_TestEntityPosition_Offset(int offset)
547 // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
548 // returns true if stuck
550 // start at 2, since the first position has already been checked
551 for(int j = 2; j <= offset; ++j)
553 if(!_Movetype_TestEntityPosition('0 0 -1' * j))
555 if(!_Movetype_TestEntityPosition('0 0 1' * j))
562 int _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
564 _Movetype_TestEntityPosition_ent = this;
565 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
568 #define X(v) if (_Movetype_TestEntityPosition(v))
569 X('0 0 -1') X(' 0 0 1')
570 X('-1 0 0') X(' 1 0 0')
571 X(' 0 -1 0') X(' 0 1 0')
572 X('-1 -1 0') X(' 1 -1 0')
573 X('-1 1 0') X(' 1 1 0')
576 if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
578 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
579 etof(this), this.classname, vtos(this.origin));
580 return UNSTICK_STUCK;
583 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
584 etof(this), this.classname, vtos(this.origin));
585 _Movetype_LinkEdict(this, false);
586 return UNSTICK_FIXED;
589 void _Movetype_CheckStuck(entity this) // SV_CheckStuck
591 int unstick = _Movetype_UnstickEntity(this); // sets test position entity
595 this.oldorigin = this.origin;
598 break; // already sorted
600 vector offset = this.oldorigin - this.origin;
601 if(!_Movetype_TestEntityPosition(offset))
602 _Movetype_LinkEdict(this, false);
603 // couldn't unstick, should we warn about this?
608 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
610 vel -= ((vel * norm) * norm) * f;
612 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
613 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
614 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
619 void _Movetype_PushEntityTrace(entity this, vector push)
621 vector end = this.origin + push;
623 if(this.move_nomonsters)
624 type = max(0, this.move_nomonsters);
625 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
627 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
628 type = MOVE_WORLDONLY;
629 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
630 type = MOVE_NOMONSTERS;
634 tracebox(this.origin, this.mins, this.maxs, end, type, this);
637 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink) // SV_PushEntity
639 _Movetype_PushEntityTrace(this, push);
641 // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
642 if(trace_startsolid && failonstartsolid)
644 int oldtype = this.move_nomonsters;
645 this.move_nomonsters = MOVE_WORLDONLY;
646 _Movetype_PushEntityTrace(this, push);
647 this.move_nomonsters = oldtype;
652 this.origin = trace_endpos;
654 vector last_origin = this.origin;
656 _Movetype_LinkEdict(this, dolink);
658 if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
659 _Movetype_Impact(this, trace_ent);
661 return (this.origin == last_origin); // false if teleported by touch
664 void _Movetype_Physics_Frame(entity this, float movedt)
666 this.move_didgravity = -1;
667 switch (this.move_movetype)
670 case MOVETYPE_FAKEPUSH:
671 _Movetype_Physics_Push(this, movedt);
675 case MOVETYPE_FOLLOW:
676 _Movetype_Physics_Follow(this);
678 case MOVETYPE_NOCLIP:
679 _Movetype_CheckWater(this);
680 this.origin = this.origin + movedt * this.velocity;
681 this.angles = this.angles + movedt * this.avelocity;
682 _Movetype_LinkEdict(this, false);
685 _Movetype_Physics_Step(this, movedt);
688 _Movetype_Physics_Walk(this, movedt);
691 case MOVETYPE_BOUNCE:
692 case MOVETYPE_BOUNCEMISSILE:
693 case MOVETYPE_FLYMISSILE:
695 case MOVETYPE_FLY_WORLDONLY:
696 _Movetype_Physics_Toss(this, movedt);
699 _Movetype_LinkEdict(this, true);
701 case MOVETYPE_PHYSICS:
706 void _Movetype_Physics_ClientFrame(entity this, float movedt)
708 this.move_didgravity = -1;
709 switch (this.move_movetype)
712 case MOVETYPE_FAKEPUSH:
713 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
717 case MOVETYPE_FOLLOW:
718 _Movetype_Physics_Follow(this);
720 case MOVETYPE_NOCLIP:
721 _Movetype_CheckWater(this);
722 this.origin = this.origin + movedt * this.velocity;
723 this.angles = this.angles + movedt * this.avelocity;
726 _Movetype_Physics_Step(this, movedt);
730 case MOVETYPE_FLY_WORLDONLY:
731 _Movetype_Physics_Walk(this, movedt);
734 case MOVETYPE_BOUNCE:
735 case MOVETYPE_BOUNCEMISSILE:
736 case MOVETYPE_FLYMISSILE:
737 _Movetype_Physics_Toss(this, movedt);
739 case MOVETYPE_PHYSICS:
743 //_Movetype_CheckVelocity(this);
745 _Movetype_LinkEdict(this, true);
747 //_Movetype_CheckVelocity(this);
750 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
752 this.move_time = time;
755 _Movetype_Physics_ClientFrame(this, movedt);
757 _Movetype_Physics_Frame(this, movedt);
761 setorigin(this, this.origin);
764 void Movetype_Physics_NoMatchServer(entity this) // optimized
766 float movedt = time - this.move_time;
767 this.move_time = time;
769 _Movetype_Physics_Frame(this, movedt);
773 setorigin(this, this.origin);
776 void Movetype_Physics_MatchServer(entity this, bool sloppy)
778 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
783 .vector tic_velocity;
785 .vector tic_avelocity;
789 .vector tic_saved_origin;
790 .vector tic_saved_velocity;
791 .int tic_saved_flags;
792 .vector tic_saved_avelocity;
793 .vector tic_saved_angles;
794 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
796 // this hack exists to contain the physics feature
797 // (so entities can place themselves in the world and not need to update .tic_* themselves)
799 if(this.(s) != this.tic_saved_##s) \
800 this.tic_##s = this.(s)
809 this.flags = this.tic_flags;
810 this.velocity = this.tic_velocity;
811 setorigin(this, this.tic_origin);
812 this.avelocity = this.tic_avelocity;
813 this.angles = this.tic_angles;
817 Movetype_Physics_NoMatchServer(this);
819 this.tic_saved_flags = this.tic_flags = this.flags;
820 this.tic_saved_velocity = this.tic_velocity = this.velocity;
821 this.tic_saved_origin = this.tic_origin = this.origin;
822 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
823 this.tic_saved_angles = this.tic_angles = this.angles;
827 float dt = time - this.move_time;
829 int n = bound(0, floor(dt / tr), 32); // limit the number of frames to 32 (CL_MAX_USERCMDS, using DP_SMALLMEMORY value for consideration of QC's limitations)
831 this.move_time += n * tr;
833 if(!this.move_didgravity)
834 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
836 for (int j = 0; j < n; ++j)
838 _Movetype_Physics_Frame(this, tr);
843 // update the physics fields
844 this.tic_origin = this.origin;
845 this.tic_velocity = this.velocity;
846 this.tic_avelocity = this.avelocity;
847 this.tic_angles = this.angles;
848 this.tic_flags = this.flags;
850 // restore their actual values
851 this.flags = this.tic_saved_flags;
852 this.velocity = this.tic_saved_velocity;
853 setorigin(this, this.tic_saved_origin);
854 //this.avelocity = this.tic_saved_avelocity;
855 this.angles = this.tic_saved_angles;
857 this.avelocity = this.tic_avelocity;
859 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
861 // now continue the move from move_time to time
862 this.velocity = this.tic_velocity;
864 if(this.move_didgravity > 0)
866 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
868 * ((this.gravity) ? this.gravity : 1)
869 * PHYS_GRAVITY(this);
872 this.angles = this.tic_angles + dt * this.avelocity;
874 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
876 setorigin(this, this.tic_origin + dt * this.velocity);
880 setorigin(this, this.tic_origin);
881 _Movetype_PushEntityTrace(this, dt * this.velocity);
882 if(!trace_startsolid)
883 setorigin(this, trace_endpos);
885 setorigin(this, this.tic_saved_origin);
888 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
889 this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
893 this.velocity = this.tic_velocity;
894 this.angles = this.tic_angles;
895 setorigin(this, this.tic_origin);
898 this.flags = this.tic_flags;
900 this.tic_saved_flags = this.flags;
901 this.tic_saved_velocity = this.velocity;
902 this.tic_saved_origin = this.origin;
903 this.tic_saved_avelocity = this.avelocity;
904 this.tic_saved_angles = this.angles;