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_NONE : mt;
15 void set_movetype(entity this, int mt)
17 this.move_movetype = mt;
21 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
25 makevectors(this.v_angle);
26 d = (stepnormal * v_forward) + 0.5;
30 i = (stepnormal * this.velocity);
31 into = i * stepnormal;
32 side = this.velocity - into;
33 this.velocity_x = side.x * (1 * d);
34 this.velocity_y = side.y * (1 * d);
38 vector planes[MAX_CLIP_PLANES];
39 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
41 move_stepnormal = '0 0 0';
47 int i, j, numplanes = 0;
48 float time_left = dt, grav = 0;
50 vector primal_velocity, original_velocity;
51 vector restore_velocity = this.velocity;
53 for(i = 0; i < MAX_CLIP_PLANES; ++i)
58 this.move_didgravity = 1;
59 grav = dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
61 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
63 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
64 this.velocity_z -= grav * 0.5;
66 this.velocity_z -= grav;
70 original_velocity = primal_velocity = this.velocity;
72 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
74 if(this.velocity == '0 0 0')
77 push = this.velocity * time_left;
78 if(!_Movetype_PushEntity(this, push, true, false))
80 // we got teleported by a touch function
81 // let's abort the move
86 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
87 // abort move if we're stuck in the world (and didn't make it out)
88 if(trace_startsolid && trace_allsolid)
90 this.velocity = restore_velocity;
94 if(trace_fraction == 1)
97 float my_trace_fraction = trace_fraction;
98 vector my_trace_plane_normal = trace_plane_normal;
100 if(trace_plane_normal.z)
102 if(trace_plane_normal.z > 0.7)
109 //dprint("_Movetype_FlyMove: !trace_ent\n");
114 this.groundentity = trace_ent;
119 // step - handle it immediately
120 vector org = this.origin;
121 vector steppush = '0 0 1' * stepheight;
123 if(!_Movetype_PushEntity(this, steppush, true, false))
128 if(!_Movetype_PushEntity(this, push, true, false))
133 float trace2_fraction = trace_fraction;
134 steppush = vec3(0, 0, org.z - this.origin_z);
135 if(!_Movetype_PushEntity(this, steppush, true, false))
141 // accept the new position if it made some progress...
142 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
144 trace_endpos = this.origin;
145 time_left *= 1 - trace2_fraction;
154 // step - return it to caller
156 // save the trace for player extrafriction
158 move_stepnormal = trace_plane_normal;
161 if(my_trace_fraction >= 0.001)
163 // actually covered some distance
164 original_velocity = this.velocity;
168 time_left *= 1 - my_trace_fraction;
170 // clipped to another plane
171 if(numplanes >= MAX_CLIP_PLANES)
173 // this shouldn't really happen
174 this.velocity = '0 0 0';
179 planes[numplanes] = my_trace_plane_normal;
182 // modify original_velocity so it parallels all of the clip planes
183 vector new_velocity = '0 0 0';
184 for (i = 0;i < numplanes;i++)
186 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
187 for (j = 0;j < numplanes;j++)
192 if((new_velocity * planes[j]) < 0)
202 // go along this plane
203 this.velocity = new_velocity;
207 // go along the crease
210 this.velocity = '0 0 0';
214 vector dir = cross(planes[0], planes[1]);
215 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
216 float ilength = sqrt((dir * dir));
218 ilength = 1.0 / ilength;
222 float d = (dir * this.velocity);
223 this.velocity = dir * d;
226 // if current velocity is against the original velocity,
227 // stop dead to avoid tiny occilations in sloping corners
228 if((this.velocity * primal_velocity) <= 0)
230 this.velocity = '0 0 0';
235 // LordHavoc: this came from QW and allows you to get out of water more easily
236 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
237 this.velocity = primal_velocity;
239 if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blocked & 8))
240 this.velocity = primal_velocity;
244 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
246 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
247 this.velocity_z -= grav * 0.5f;
254 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
256 // if(vlen(this.velocity) < 0.0001)
257 // this.velocity = '0 0 0';
260 bool _Movetype_CheckWater(entity this) // SV_CheckWater
262 vector point = this.origin;
263 point.z += this.mins.z + 1;
265 int nativecontents = pointcontents(point);
266 if(this.watertype && this.watertype != nativecontents)
268 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
269 if(this.contentstransition)
270 this.contentstransition(this.watertype, nativecontents);
273 this.waterlevel = WATERLEVEL_NONE;
274 this.watertype = CONTENT_EMPTY;
276 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
277 if(supercontents & DPCONTENTS_LIQUIDSMASK)
279 this.watertype = nativecontents;
280 this.waterlevel = WATERLEVEL_WETFEET;
281 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
282 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
284 this.waterlevel = WATERLEVEL_SWIMMING;
285 point.z = this.origin.z + this.view_ofs.z;
286 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
287 this.waterlevel = WATERLEVEL_SUBMERGED;
291 return this.waterlevel > 1;
294 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
296 int contents = pointcontents(ent.origin);
301 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
303 ent.watertype = contents;
308 else if(ent.watertype != contents)
310 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
311 if(ent.contentstransition)
312 ent.contentstransition(ent.watertype, contents);
315 if(contents <= CONTENT_WATER)
317 ent.watertype = contents;
322 ent.watertype = CONTENT_EMPTY;
323 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
327 void _Movetype_Impact(entity this, entity oth) // SV_Impact
332 if(this.solid != SOLID_NOT && gettouch(this))
333 gettouch(this)(this, oth);
335 if(oth.solid != SOLID_NOT && gettouch(oth))
336 gettouch(oth)(oth, this);
339 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
341 if(this.solid == SOLID_NOT)
344 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
345 if (it.solid == SOLID_TRIGGER && it != this)
346 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
347 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
349 trace_allsolid = false;
350 trace_startsolid = false;
352 trace_inwater = false;
354 trace_endpos = it.origin;
355 trace_plane_normal = '0 0 1';
356 trace_plane_dist = 0;
358 trace_dpstartcontents = 0;
359 trace_dphitcontents = 0;
360 trace_dphitq3surfaceflags = 0;
361 trace_dphittexturename = string_null;
363 gettouch(it)(it, this);
368 bool autocvar__movetype_debug = false;
369 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
371 if(autocvar__movetype_debug)
374 if(this.solid == SOLID_BSP)
376 // TODO set the absolute bbox
388 if(this.flags & FL_ITEM)
404 setorigin(this, this.origin); // calls SV_LinkEdict
406 // NOTE: CSQC's version of setorigin doesn't expand
407 this.absmin -= '1 1 1';
408 this.absmax += '1 1 1';
413 _Movetype_LinkEdict_TouchAreaGrid(this);
416 int _Movetype_ContentsMask(entity this) // SV_GenericHitSuperContentsMask
420 if(this.dphitcontentsmask)
421 return this.dphitcontentsmask;
422 else if(this.solid == SOLID_SLIDEBOX)
424 if(this.flags & 32) // TODO: FL_MONSTER
425 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
427 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
429 else if(this.solid == SOLID_CORPSE)
430 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
431 else if(this.solid == SOLID_TRIGGER)
432 return DPCONTENTS_SOLID | DPCONTENTS_BODY;
434 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
437 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
440 entity _Movetype_TestEntityPosition_ent;
441 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
443 entity this = _Movetype_TestEntityPosition_ent;
444 vector org = this.origin + ofs;
446 //int cont = this.dphitcontentsmask;
447 //this.dphitcontentsmask = DPCONTENTS_SOLID;
448 tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
449 //this.dphitcontentsmask = cont;
450 if(trace_dpstartcontents & _Movetype_ContentsMask(this))
453 if(vlen2(trace_endpos - this.origin) >= 0.0001)
455 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
456 if(!trace_startsolid)
457 this.origin = trace_endpos;
462 bool _Movetype_TestEntityPosition_Offset(int offset)
464 // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
465 // returns true if stuck
467 // start at 2, since the first position has already been checked
468 for(int j = 2; j <= offset; ++j)
470 if(!_Movetype_TestEntityPosition('0 0 -1' * j))
472 if(!_Movetype_TestEntityPosition('0 0 1' * j))
479 int _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
481 _Movetype_TestEntityPosition_ent = this;
482 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
485 #define X(v) if (_Movetype_TestEntityPosition(v))
486 X('0 0 -1') X(' 0 0 1')
487 X('-1 0 0') X(' 1 0 0')
488 X(' 0 -1 0') X(' 0 1 0')
489 X('-1 -1 0') X(' 1 -1 0')
490 X('-1 1 0') X(' 1 1 0')
493 if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
495 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
496 etof(this), this.classname, vtos(this.origin));
497 return UNSTICK_STUCK;
500 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
501 etof(this), this.classname, vtos(this.origin));
502 _Movetype_LinkEdict(this, false);
503 return UNSTICK_FIXED;
506 void _Movetype_CheckStuck(entity this) // SV_CheckStuck
508 int unstick = _Movetype_UnstickEntity(this); // sets test position entity
512 this.oldorigin = this.origin;
515 break; // already sorted
517 vector offset = this.oldorigin - this.origin;
518 if(!_Movetype_TestEntityPosition(offset))
519 _Movetype_LinkEdict(this, false);
520 // couldn't unstick, should we warn about this?
525 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
527 vel -= ((vel * norm) * norm) * f;
529 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
530 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
531 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
536 void _Movetype_PushEntityTrace(entity this, vector push)
538 vector end = this.origin + push;
540 if(this.move_nomonsters)
541 type = max(0, this.move_nomonsters);
542 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
544 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
545 type = MOVE_WORLDONLY;
546 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
547 type = MOVE_NOMONSTERS;
551 tracebox(this.origin, this.mins, this.maxs, end, type, this);
554 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink) // SV_PushEntity
556 _Movetype_PushEntityTrace(this, push);
558 // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
559 if(trace_startsolid && failonstartsolid)
561 int oldtype = this.move_nomonsters;
562 this.move_nomonsters = MOVE_WORLDONLY;
563 _Movetype_PushEntityTrace(this, push);
564 this.move_nomonsters = oldtype;
569 this.origin = trace_endpos;
571 vector last_origin = this.origin;
573 _Movetype_LinkEdict(this, dolink);
575 if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
576 _Movetype_Impact(this, trace_ent);
578 return (this.origin == last_origin); // false if teleported by touch
585 void _Movetype_Physics_Frame(entity this, float movedt)
587 this.move_didgravity = -1;
588 switch (this.move_movetype)
591 case MOVETYPE_FAKEPUSH:
592 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
596 case MOVETYPE_FOLLOW:
597 _Movetype_Physics_Follow(this);
599 case MOVETYPE_NOCLIP:
600 _Movetype_CheckWater(this);
601 this.origin = this.origin + movedt * this.velocity;
602 this.angles = this.angles + movedt * this.avelocity;
603 _Movetype_LinkEdict(this, false);
606 _Movetype_Physics_Step(this, movedt);
609 _Movetype_Physics_Walk(this, movedt);
612 case MOVETYPE_BOUNCE:
613 case MOVETYPE_BOUNCEMISSILE:
614 case MOVETYPE_FLYMISSILE:
616 case MOVETYPE_FLY_WORLDONLY:
617 _Movetype_Physics_Toss(this, movedt);
620 _Movetype_LinkEdict(this, true);
622 case MOVETYPE_PHYSICS:
627 void _Movetype_Physics_ClientFrame(entity this, float movedt)
629 this.move_didgravity = -1;
630 switch (this.move_movetype)
633 case MOVETYPE_FAKEPUSH:
634 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
638 case MOVETYPE_FOLLOW:
639 _Movetype_Physics_Follow(this);
641 case MOVETYPE_NOCLIP:
642 _Movetype_CheckWater(this);
643 this.origin = this.origin + movedt * this.velocity;
644 this.angles = this.angles + movedt * this.avelocity;
647 if (GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
648 _Movetype_CheckStuck(this);
649 _Movetype_Physics_Step(this, movedt);
653 case MOVETYPE_FLY_WORLDONLY:
654 if (movedt > 0 && GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
655 _Movetype_CheckStuck(this);
656 _Movetype_Physics_Walk(this, movedt);
659 case MOVETYPE_BOUNCE:
660 case MOVETYPE_BOUNCEMISSILE:
661 case MOVETYPE_FLYMISSILE:
662 if (GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
663 _Movetype_CheckStuck(this);
664 _Movetype_Physics_Toss(this, movedt);
666 case MOVETYPE_PHYSICS:
670 //_Movetype_CheckVelocity(this);
672 _Movetype_LinkEdict(this, true);
674 //_Movetype_CheckVelocity(this);
677 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
679 this.move_time = time;
682 _Movetype_Physics_ClientFrame(this, movedt);
684 _Movetype_Physics_Frame(this, movedt);
688 setorigin(this, this.origin);
691 void Movetype_Physics_NoMatchServer(entity this) // optimized
693 float movedt = time - this.move_time;
694 this.move_time = time;
696 _Movetype_Physics_Frame(this, movedt);
700 setorigin(this, this.origin);
703 void Movetype_Physics_MatchServer(entity this, bool sloppy)
705 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
710 .vector tic_velocity;
712 .vector tic_avelocity;
716 .vector tic_saved_origin;
717 .vector tic_saved_velocity;
718 .int tic_saved_flags;
719 .vector tic_saved_avelocity;
720 .vector tic_saved_angles;
721 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
723 // this hack exists to contain the physics feature
724 // (so entities can place themselves in the world and not need to update .tic_* themselves)
726 if(this.(s) != this.tic_saved_##s) \
727 this.tic_##s = this.(s)
736 this.flags = this.tic_flags;
737 this.velocity = this.tic_velocity;
738 setorigin(this, this.tic_origin);
739 this.avelocity = this.tic_avelocity;
740 this.angles = this.tic_angles;
744 Movetype_Physics_NoMatchServer(this);
746 this.tic_saved_flags = this.tic_flags = this.flags;
747 this.tic_saved_velocity = this.tic_velocity = this.velocity;
748 this.tic_saved_origin = this.tic_origin = this.origin;
749 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
750 this.tic_saved_angles = this.tic_angles = this.angles;
754 float dt = time - this.move_time;
756 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)
758 this.move_time += n * tr;
760 if(!this.move_didgravity)
761 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
763 for (int j = 0; j < n; ++j)
765 _Movetype_Physics_Frame(this, tr);
770 // update the physics fields
771 this.tic_origin = this.origin;
772 this.tic_velocity = this.velocity;
773 this.tic_avelocity = this.avelocity;
774 this.tic_angles = this.angles;
775 this.tic_flags = this.flags;
777 // restore their actual values
778 this.flags = this.tic_saved_flags;
779 this.velocity = this.tic_saved_velocity;
780 setorigin(this, this.tic_saved_origin);
781 //this.avelocity = this.tic_saved_avelocity;
782 this.angles = this.tic_saved_angles;
784 this.avelocity = this.tic_avelocity;
786 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
788 // now continue the move from move_time to time
789 this.velocity = this.tic_velocity;
791 if(this.move_didgravity > 0)
793 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
795 * ((this.gravity) ? this.gravity : 1)
796 * PHYS_GRAVITY(this);
799 this.angles = this.tic_angles + dt * this.avelocity;
801 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
803 setorigin(this, this.tic_origin + dt * this.velocity);
807 setorigin(this, this.tic_origin);
808 _Movetype_PushEntityTrace(this, dt * this.velocity);
809 if(!trace_startsolid)
810 setorigin(this, trace_endpos);
812 setorigin(this, this.tic_saved_origin);
815 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
816 this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
820 this.velocity = this.tic_velocity;
821 this.angles = this.tic_angles;
822 setorigin(this, this.tic_origin);
825 this.flags = this.tic_flags;
827 this.tic_saved_flags = this.flags;
828 this.tic_saved_velocity = this.velocity;
829 this.tic_saved_origin = this.origin;
830 this.tic_saved_avelocity = this.avelocity;
831 this.tic_saved_angles = this.angles;