1 #include "movetypes.qh"
4 void set_movetype(entity this, int mt)
6 this.move_movetype = mt;
7 if (mt == MOVETYPE_PHYSICS /*|| mt == MOVETYPE_PUSH || mt == MOVETYPE_FAKEPUSH*/) {
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) // NOTE: this checks for bmodelstartsolid in the engine
58 // BAD BAD, can't fix that
62 if(trace_fraction >= 1)
67 // BAD BAD, can't fix that
71 // we hit something... let's move out of it
72 vector move = trace_endpos - testorigin;
73 float nudge = (trace_plane_normal * move) + 0.03125f; // FIXME cvar this constant
74 stuckorigin = stuckorigin + nudge * trace_plane_normal;
82 case 0: goodmaxs.x = this.maxs_x; break;
83 case 1: goodmaxs.y = this.maxs_y; break;
84 case 2: goodmaxs.z = this.maxs_z; break;
92 case 0: goodmins.x = this.mins_x; break;
93 case 1: goodmins.y = this.mins_y; break;
94 case 2: goodmins.z = this.mins_z; break;
100 this.origin = stuckorigin;
105 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
109 makevectors(this.v_angle);
110 d = (stepnormal * v_forward) + 0.5;
114 i = (stepnormal * this.velocity);
115 into = i * stepnormal;
116 side = this.velocity - into;
117 this.velocity_x = side.x * (1 * d);
118 this.velocity_y = side.y * (1 * d);
122 vector planes[MAX_CLIP_PLANES];
123 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
125 move_stepnormal = '0 0 0';
131 int i, j, numplanes = 0;
132 float time_left = dt, grav = 0;
134 vector primal_velocity, original_velocity;
135 vector restore_velocity = this.velocity;
137 for(i = 0; i < MAX_CLIP_PLANES; ++i)
142 this.move_didgravity = 1;
143 grav = dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
145 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
147 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
148 this.velocity_z -= grav * 0.5;
150 this.velocity_z -= grav;
154 original_velocity = primal_velocity = this.velocity;
156 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
158 if(this.velocity == '0 0 0')
161 push = this.velocity * time_left;
162 if(!_Movetype_PushEntity(this, push, true, false))
164 // we got teleported by a touch function
165 // let's abort the move
170 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
171 // abort move if we're stuck in the world (and didn't make it out)
172 if(trace_startsolid && trace_allsolid)
174 this.velocity = restore_velocity;
178 if(trace_fraction == 1)
181 float my_trace_fraction = trace_fraction;
182 vector my_trace_plane_normal = trace_plane_normal;
184 if(trace_plane_normal.z)
186 if(trace_plane_normal.z > 0.7)
193 //dprint("_Movetype_FlyMove: !trace_ent\n");
198 this.groundentity = trace_ent;
203 // step - handle it immediately
204 vector org = this.origin;
205 vector steppush = '0 0 1' * stepheight;
207 if(!_Movetype_PushEntity(this, steppush, true, false))
212 if(!_Movetype_PushEntity(this, push, true, false))
217 float trace2_fraction = trace_fraction;
218 steppush = vec3(0, 0, org.z - this.origin_z);
219 if(!_Movetype_PushEntity(this, steppush, true, false))
225 // accept the new position if it made some progress...
226 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
228 trace_endpos = this.origin;
229 time_left *= 1 - trace2_fraction;
238 // step - return it to caller
240 // save the trace for player extrafriction
242 move_stepnormal = trace_plane_normal;
245 if(my_trace_fraction >= 0.001)
247 // actually covered some distance
248 original_velocity = this.velocity;
252 time_left *= 1 - my_trace_fraction;
254 // clipped to another plane
255 if(numplanes >= MAX_CLIP_PLANES)
257 // this shouldn't really happen
258 this.velocity = '0 0 0';
263 planes[numplanes] = my_trace_plane_normal;
266 // modify original_velocity so it parallels all of the clip planes
267 vector new_velocity = '0 0 0';
268 for (i = 0;i < numplanes;i++)
270 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
271 for (j = 0;j < numplanes;j++)
276 if((new_velocity * planes[j]) < 0)
286 // go along this plane
287 this.velocity = new_velocity;
291 // go along the crease
294 this.velocity = '0 0 0';
298 vector dir = cross(planes[0], planes[1]);
299 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
300 float ilength = sqrt((dir * dir));
302 ilength = 1.0 / ilength;
306 float d = (dir * this.velocity);
307 this.velocity = dir * d;
310 // if current velocity is against the original velocity,
311 // stop dead to avoid tiny occilations in sloping corners
312 if((this.velocity * primal_velocity) <= 0)
314 this.velocity = '0 0 0';
319 // LordHavoc: this came from QW and allows you to get out of water more easily
320 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
321 this.velocity = primal_velocity;
323 if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blocked & 8))
324 this.velocity = primal_velocity;
328 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
330 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
331 this.velocity_z -= grav * 0.5f;
338 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
340 // if(vlen(this.velocity) < 0.0001)
341 // this.velocity = '0 0 0';
344 bool _Movetype_CheckWater(entity this) // SV_CheckWater
346 vector point = this.origin;
347 point.z += this.mins.z + 1;
349 int nativecontents = pointcontents(point);
350 if(this.watertype && this.watertype != nativecontents)
352 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
353 if(this.contentstransition)
354 this.contentstransition(this.watertype, nativecontents);
357 this.waterlevel = WATERLEVEL_NONE;
358 this.watertype = CONTENT_EMPTY;
360 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
361 if(supercontents & DPCONTENTS_LIQUIDSMASK)
363 this.watertype = nativecontents;
364 this.waterlevel = WATERLEVEL_WETFEET;
365 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
366 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
368 this.waterlevel = WATERLEVEL_SWIMMING;
369 point.z = this.origin.z + this.view_ofs.z;
370 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
371 this.waterlevel = WATERLEVEL_SUBMERGED;
375 return this.waterlevel > 1;
378 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
380 int contents = pointcontents(ent.origin);
385 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
387 ent.watertype = contents;
392 else if(ent.watertype != contents)
394 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
395 if(ent.contentstransition)
396 ent.contentstransition(ent.watertype, contents);
399 if(contents <= CONTENT_WATER)
401 ent.watertype = contents;
406 ent.watertype = CONTENT_EMPTY;
407 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
411 void _Movetype_Impact(entity this, entity oth) // SV_Impact
416 if(this.solid != SOLID_NOT && gettouch(this))
417 gettouch(this)(this, oth);
419 if(oth.solid != SOLID_NOT && gettouch(oth))
420 gettouch(oth)(oth, this);
423 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
425 if(this.solid == SOLID_NOT)
428 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
429 if (it.solid == SOLID_TRIGGER && it != this)
430 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
431 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
433 trace_allsolid = false;
434 trace_startsolid = false;
436 trace_inwater = false;
438 trace_endpos = it.origin;
439 trace_plane_normal = '0 0 1';
440 trace_plane_dist = 0;
442 trace_dpstartcontents = 0;
443 trace_dphitcontents = 0;
444 trace_dphitq3surfaceflags = 0;
445 trace_dphittexturename = string_null;
447 gettouch(it)(it, this);
452 bool autocvar__movetype_debug = false;
453 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
455 if(autocvar__movetype_debug)
458 if(this.solid == SOLID_BSP)
460 // TODO set the absolute bbox
472 if(this.flags & FL_ITEM)
488 setorigin(this, this.origin); // calls SV_LinkEdict
490 // NOTE: CSQC's version of setorigin doesn't expand
491 this.absmin -= '1 1 1';
492 this.absmax += '1 1 1';
497 _Movetype_LinkEdict_TouchAreaGrid(this);
500 int _Movetype_ContentsMask(entity this) // SV_GenericHitSuperContentsMask
504 if(this.dphitcontentsmask)
505 return this.dphitcontentsmask;
506 else if(this.solid == SOLID_SLIDEBOX)
508 if(this.flags & 32) // TODO: FL_MONSTER
509 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
511 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
513 else if(this.solid == SOLID_CORPSE)
514 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
515 else if(this.solid == SOLID_TRIGGER)
516 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
518 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
521 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
524 entity _Movetype_TestEntityPosition_ent;
525 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
527 entity this = _Movetype_TestEntityPosition_ent;
528 vector org = this.origin + ofs;
530 //int cont = this.dphitcontentsmask;
531 //this.dphitcontentsmask = DPCONTENTS_SOLID;
532 tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
533 //this.dphitcontentsmask = cont;
534 if(trace_dpstartcontents & _Movetype_ContentsMask(this))
537 if(vlen2(trace_endpos - this.origin) >= 0.0001)
539 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
540 if(!trace_startsolid)
541 this.origin = trace_endpos;
546 bool _Movetype_TestEntityPosition_Offset(int offset)
548 // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
549 // returns true if stuck
551 // start at 2, since the first position has already been checked
552 for(int j = 2; j <= offset; ++j)
554 if(!_Movetype_TestEntityPosition('0 0 -1' * j))
556 if(!_Movetype_TestEntityPosition('0 0 1' * j))
563 int _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
565 _Movetype_TestEntityPosition_ent = this;
566 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
569 #define X(v) if (_Movetype_TestEntityPosition(v))
570 X('0 0 -1') X(' 0 0 1')
571 X('-1 0 0') X(' 1 0 0')
572 X(' 0 -1 0') X(' 0 1 0')
573 X('-1 -1 0') X(' 1 -1 0')
574 X('-1 1 0') X(' 1 1 0')
577 if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
579 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
580 etof(this), this.classname, vtos(this.origin));
581 return UNSTICK_STUCK;
584 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
585 etof(this), this.classname, vtos(this.origin));
586 _Movetype_LinkEdict(this, false);
587 return UNSTICK_FIXED;
590 void _Movetype_CheckStuck(entity this) // SV_CheckStuck
592 int unstick = _Movetype_UnstickEntity(this); // sets test position entity
596 this.oldorigin = this.origin;
599 break; // already sorted
601 vector offset = this.oldorigin - this.origin;
602 if(!_Movetype_TestEntityPosition(offset))
603 _Movetype_LinkEdict(this, false);
604 // couldn't unstick, should we warn about this?
609 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
611 vel -= ((vel * norm) * norm) * f;
613 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
614 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
615 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
620 void _Movetype_PushEntityTrace(entity this, vector push)
622 vector end = this.origin + push;
624 if(this.move_nomonsters)
625 type = max(0, this.move_nomonsters);
626 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
628 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
629 type = MOVE_WORLDONLY;
630 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
631 type = MOVE_NOMONSTERS;
635 tracebox(this.origin, this.mins, this.maxs, end, type, this);
638 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink) // SV_PushEntity
640 _Movetype_PushEntityTrace(this, push);
642 // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
643 if(trace_startsolid && failonstartsolid)
645 int oldtype = this.move_nomonsters;
646 this.move_nomonsters = MOVE_WORLDONLY;
647 _Movetype_PushEntityTrace(this, push);
648 this.move_nomonsters = oldtype;
653 this.origin = trace_endpos;
655 vector last_origin = this.origin;
657 _Movetype_LinkEdict(this, dolink);
659 if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
660 _Movetype_Impact(this, trace_ent);
662 return (this.origin == last_origin); // false if teleported by touch
669 void _Movetype_Physics_Frame(entity this, float movedt)
671 this.move_didgravity = -1;
672 switch (this.move_movetype)
675 case MOVETYPE_FAKEPUSH:
676 _Movetype_Physics_Push(this, movedt);
680 case MOVETYPE_FOLLOW:
681 _Movetype_Physics_Follow(this);
683 case MOVETYPE_NOCLIP:
684 _Movetype_CheckWater(this);
685 this.origin = this.origin + movedt * this.velocity;
686 this.angles = this.angles + movedt * this.avelocity;
687 _Movetype_LinkEdict(this, false);
690 _Movetype_Physics_Step(this, movedt);
693 _Movetype_Physics_Walk(this, movedt);
696 case MOVETYPE_BOUNCE:
697 case MOVETYPE_BOUNCEMISSILE:
698 case MOVETYPE_FLYMISSILE:
700 case MOVETYPE_FLY_WORLDONLY:
701 _Movetype_Physics_Toss(this, movedt);
704 _Movetype_LinkEdict(this, true);
706 case MOVETYPE_PHYSICS:
711 void _Movetype_Physics_ClientFrame(entity this, float movedt)
713 this.move_didgravity = -1;
714 switch (this.move_movetype)
717 case MOVETYPE_FAKEPUSH:
718 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
722 case MOVETYPE_FOLLOW:
723 _Movetype_Physics_Follow(this);
725 case MOVETYPE_NOCLIP:
726 _Movetype_CheckWater(this);
727 this.origin = this.origin + movedt * this.velocity;
728 this.angles = this.angles + movedt * this.avelocity;
731 _Movetype_Physics_Step(this, movedt);
735 case MOVETYPE_FLY_WORLDONLY:
736 _Movetype_Physics_Walk(this, movedt);
739 case MOVETYPE_BOUNCE:
740 case MOVETYPE_BOUNCEMISSILE:
741 case MOVETYPE_FLYMISSILE:
742 _Movetype_Physics_Toss(this, movedt);
744 case MOVETYPE_PHYSICS:
748 //_Movetype_CheckVelocity(this);
750 _Movetype_LinkEdict(this, true);
752 //_Movetype_CheckVelocity(this);
755 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
757 this.move_time = time;
760 _Movetype_Physics_ClientFrame(this, movedt);
762 _Movetype_Physics_Frame(this, movedt);
766 setorigin(this, this.origin);
769 void Movetype_Physics_NoMatchServer(entity this) // optimized
771 float movedt = time - this.move_time;
772 this.move_time = time;
774 _Movetype_Physics_Frame(this, movedt);
778 setorigin(this, this.origin);
781 void Movetype_Physics_MatchServer(entity this, bool sloppy)
783 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
788 .vector tic_velocity;
790 .vector tic_avelocity;
794 .vector tic_saved_origin;
795 .vector tic_saved_velocity;
796 .int tic_saved_flags;
797 .vector tic_saved_avelocity;
798 .vector tic_saved_angles;
799 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
801 // this hack exists to contain the physics feature
802 // (so entities can place themselves in the world and not need to update .tic_* themselves)
804 if(this.(s) != this.tic_saved_##s) \
805 this.tic_##s = this.(s)
814 this.flags = this.tic_flags;
815 this.velocity = this.tic_velocity;
816 setorigin(this, this.tic_origin);
817 this.avelocity = this.tic_avelocity;
818 this.angles = this.tic_angles;
822 Movetype_Physics_NoMatchServer(this);
824 this.tic_saved_flags = this.tic_flags = this.flags;
825 this.tic_saved_velocity = this.tic_velocity = this.velocity;
826 this.tic_saved_origin = this.tic_origin = this.origin;
827 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
828 this.tic_saved_angles = this.tic_angles = this.angles;
832 float dt = time - this.move_time;
834 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)
836 this.move_time += n * tr;
838 if(!this.move_didgravity)
839 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
841 for (int j = 0; j < n; ++j)
843 _Movetype_Physics_Frame(this, tr);
848 // update the physics fields
849 this.tic_origin = this.origin;
850 this.tic_velocity = this.velocity;
851 this.tic_avelocity = this.avelocity;
852 this.tic_angles = this.angles;
853 this.tic_flags = this.flags;
855 // restore their actual values
856 this.flags = this.tic_saved_flags;
857 this.velocity = this.tic_saved_velocity;
858 setorigin(this, this.tic_saved_origin);
859 //this.avelocity = this.tic_saved_avelocity;
860 this.angles = this.tic_saved_angles;
862 this.avelocity = this.tic_avelocity;
864 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
866 // now continue the move from move_time to time
867 this.velocity = this.tic_velocity;
869 if(this.move_didgravity > 0)
871 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
873 * ((this.gravity) ? this.gravity : 1)
874 * PHYS_GRAVITY(this);
877 this.angles = this.tic_angles + dt * this.avelocity;
879 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
881 setorigin(this, this.tic_origin + dt * this.velocity);
885 setorigin(this, this.tic_origin);
886 _Movetype_PushEntityTrace(this, dt * this.velocity);
887 if(!trace_startsolid)
888 setorigin(this, trace_endpos);
890 setorigin(this, this.tic_saved_origin);
893 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
894 this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
898 this.velocity = this.tic_velocity;
899 this.angles = this.tic_angles;
900 setorigin(this, this.tic_origin);
903 this.flags = this.tic_flags;
905 this.tic_saved_flags = this.flags;
906 this.tic_saved_velocity = this.velocity;
907 this.tic_saved_origin = this.origin;
908 this.tic_saved_avelocity = this.avelocity;
909 this.tic_saved_angles = this.angles;