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 this.movetype = (this.move_qcphysics) ? MOVETYPE_NONE : mt;
13 void set_movetype(entity this, int mt)
15 this.move_movetype = mt;
19 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
23 makevectors(this.v_angle);
24 d = (stepnormal * v_forward) + 0.5;
28 i = (stepnormal * this.velocity);
29 into = i * stepnormal;
30 side = this.velocity - into;
31 this.velocity_x = side.x * (1 * d);
32 this.velocity_y = side.y * (1 * d);
36 vector planes[MAX_CLIP_PLANES];
37 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
39 move_stepnormal = '0 0 0';
45 int i, j, numplanes = 0;
46 float time_left = dt, grav = 0;
48 vector primal_velocity, original_velocity;
49 vector restore_velocity = this.velocity;
51 for(i = 0; i < MAX_CLIP_PLANES; ++i)
56 this.move_didgravity = 1;
57 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
59 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
61 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
62 this.velocity_z -= grav * 0.5;
64 this.velocity_z -= grav;
68 original_velocity = primal_velocity = this.velocity;
70 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
72 if(this.velocity == '0 0 0')
75 push = this.velocity * time_left;
76 if(!_Movetype_PushEntity(this, push, true, false))
78 // we got teleported by a touch function
79 // let's abort the move
84 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
85 // abort move if we're stuck in the world (and didn't make it out)
86 if(trace_startsolid && trace_allsolid)
88 this.velocity = restore_velocity;
92 if(trace_fraction == 1)
95 float my_trace_fraction = trace_fraction;
96 vector my_trace_plane_normal = trace_plane_normal;
98 if(trace_plane_normal.z)
100 if(trace_plane_normal.z > 0.7)
107 //dprint("_Movetype_FlyMove: !trace_ent\n");
112 this.groundentity = trace_ent;
117 // step - handle it immediately
118 vector org = this.origin;
119 vector steppush = '0 0 1' * stepheight;
121 if(!_Movetype_PushEntity(this, steppush, true, false))
126 if(!_Movetype_PushEntity(this, push, true, false))
131 float trace2_fraction = trace_fraction;
132 steppush = vec3(0, 0, org.z - this.origin_z);
133 if(!_Movetype_PushEntity(this, steppush, true, false))
139 // accept the new position if it made some progress...
140 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
142 trace_endpos = this.origin;
143 time_left *= 1 - trace2_fraction;
152 // step - return it to caller
154 // save the trace for player extrafriction
156 move_stepnormal = trace_plane_normal;
159 if(my_trace_fraction >= 0.001)
161 // actually covered some distance
162 original_velocity = this.velocity;
166 time_left *= 1 - my_trace_fraction;
168 // clipped to another plane
169 if(numplanes >= MAX_CLIP_PLANES)
171 // this shouldn't really happen
172 this.velocity = '0 0 0';
177 planes[numplanes] = my_trace_plane_normal;
180 // modify original_velocity so it parallels all of the clip planes
181 vector new_velocity = '0 0 0';
182 for (i = 0;i < numplanes;i++)
184 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
185 for (j = 0;j < numplanes;j++)
190 if((new_velocity * planes[j]) < 0)
200 // go along this plane
201 this.velocity = new_velocity;
205 // go along the crease
208 this.velocity = '0 0 0';
212 vector dir = cross(planes[0], planes[1]);
213 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
214 float ilength = sqrt((dir * dir));
216 ilength = 1.0 / ilength;
220 float d = (dir * this.velocity);
221 this.velocity = dir * d;
224 // if current velocity is against the original velocity,
225 // stop dead to avoid tiny occilations in sloping corners
226 if((this.velocity * primal_velocity) <= 0)
228 this.velocity = '0 0 0';
233 // LordHavoc: this came from QW and allows you to get out of water more easily
234 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
235 this.velocity = primal_velocity;
237 if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blocked & 8))
238 this.velocity = primal_velocity;
242 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
244 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
245 this.velocity_z -= grav * 0.5f;
252 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
254 // if(vlen(this.velocity) < 0.0001)
255 // this.velocity = '0 0 0';
258 bool _Movetype_CheckWater(entity this) // SV_CheckWater
260 vector point = this.origin;
261 point.z += this.mins.z + 1;
263 int nativecontents = pointcontents(point);
264 if(this.watertype && this.watertype != nativecontents)
266 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
267 if(this.contentstransition)
268 this.contentstransition(this.watertype, nativecontents);
271 this.waterlevel = WATERLEVEL_NONE;
272 this.watertype = CONTENT_EMPTY;
274 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
275 if(supercontents & DPCONTENTS_LIQUIDSMASK)
277 this.watertype = nativecontents;
278 this.waterlevel = WATERLEVEL_WETFEET;
279 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
280 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
282 this.waterlevel = WATERLEVEL_SWIMMING;
283 point.z = this.origin.z + this.view_ofs.z;
284 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
285 this.waterlevel = WATERLEVEL_SUBMERGED;
289 return this.waterlevel > 1;
292 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
294 int contents = pointcontents(ent.origin);
299 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
301 ent.watertype = contents;
306 else if(ent.watertype != contents)
308 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
309 if(ent.contentstransition)
310 ent.contentstransition(ent.watertype, contents);
313 if(contents <= CONTENT_WATER)
315 ent.watertype = contents;
320 ent.watertype = CONTENT_EMPTY;
321 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
325 void _Movetype_Impact(entity this, entity oth) // SV_Impact
328 gettouch(this)(this, oth);
331 gettouch(oth)(oth, this);
334 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
336 if(this.solid == SOLID_NOT)
339 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
340 if (it.solid == SOLID_TRIGGER && it != this)
341 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
342 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
344 trace_allsolid = false;
345 trace_startsolid = false;
347 trace_inwater = false;
349 trace_endpos = it.origin;
350 trace_plane_normal = '0 0 1';
351 trace_plane_dist = 0;
353 trace_dpstartcontents = 0;
354 trace_dphitcontents = 0;
355 trace_dphitq3surfaceflags = 0;
356 trace_dphittexturename = string_null;
358 gettouch(it)(it, this);
363 bool autocvar__movetype_debug = false;
364 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
366 if(autocvar__movetype_debug)
369 if(this.solid == SOLID_BSP)
371 // TODO set the absolute bbox
383 if(this.flags & FL_ITEM)
399 setorigin(this, this.origin); // calls SV_LinkEdict
401 // NOTE: CSQC's version of setorigin doesn't expand
402 this.absmin -= '1 1 1';
403 this.absmax += '1 1 1';
408 _Movetype_LinkEdict_TouchAreaGrid(this);
411 entity _Movetype_TestEntityPosition_ent;
412 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
414 entity this = _Movetype_TestEntityPosition_ent;
415 vector org = this.origin + ofs;
417 int cont = this.dphitcontentsmask;
418 this.dphitcontentsmask = DPCONTENTS_SOLID;
419 tracebox(org, this.mins, this.maxs, org, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
420 this.dphitcontentsmask = cont;
425 if(vdist(trace_endpos - this.origin, >, 0.0001))
426 this.origin = trace_endpos;
430 int _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
432 _Movetype_TestEntityPosition_ent = this;
433 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
436 #define X(v) if (_Movetype_TestEntityPosition(v))
437 X('-1 0 0') X(' 1 0 0')
438 X(' 0 -1 0') X(' 0 1 0')
439 X('-1 -1 0') X(' 1 -1 0')
440 X('-1 1 0') X(' 1 1 0')
444 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
445 if (_Movetype_TestEntityPosition('0 0 1' * i))
446 X(1) X(2) X(3) X(4) X(5) X(6) X(7) X(8)
447 X(9) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
451 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
452 etof(this), this.classname, vtos(this.origin));
453 return UNSTICK_STUCK;
456 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
457 etof(this), this.classname, vtos(this.origin));
458 _Movetype_LinkEdict(this, true);
459 return UNSTICK_FIXED;
462 void _Movetype_CheckStuck(entity this) // SV_CheckStuck
464 int unstick = _Movetype_UnstickEntity(this); // sets test position entity
468 this.oldorigin = this.origin;
471 break; // already sorted
473 vector offset = this.oldorigin - this.origin;
474 if(!_Movetype_TestEntityPosition(offset))
475 _Movetype_LinkEdict(this, false);
476 // couldn't unstick, should we warn about this?
481 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
483 vel -= ((vel * norm) * norm) * f;
485 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
486 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
487 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
492 void _Movetype_PushEntityTrace(entity this, vector push)
494 vector end = this.origin + push;
496 if(this.move_nomonsters)
497 type = max(0, this.move_nomonsters);
498 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
500 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
501 type = MOVE_WORLDONLY;
502 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
503 type = MOVE_NOMONSTERS;
507 tracebox(this.origin, this.mins, this.maxs, end, type, this);
510 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink) // SV_PushEntity
512 _Movetype_PushEntityTrace(this, push);
514 // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
515 if(trace_startsolid && failonstartsolid)
517 int oldtype = this.move_nomonsters;
518 this.move_nomonsters = MOVE_WORLDONLY;
519 _Movetype_PushEntityTrace(this, push);
520 this.move_nomonsters = oldtype;
525 this.origin = trace_endpos;
527 vector last_origin = this.origin;
530 _Movetype_LinkEdict(this, true);
532 if(trace_fraction < 1)
533 if(this.solid >= SOLID_TRIGGER && trace_ent && (!IS_ONGROUND(this) || (this.groundentity != trace_ent)))
534 _Movetype_Impact(this, trace_ent);
536 return (this.origin == last_origin); // false if teleported by touch
543 void _Movetype_Physics_Frame(entity this, float movedt)
545 this.move_didgravity = -1;
546 switch (this.move_movetype)
549 case MOVETYPE_FAKEPUSH:
550 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
554 case MOVETYPE_FOLLOW:
555 _Movetype_Physics_Follow(this);
557 case MOVETYPE_NOCLIP:
558 _Movetype_CheckWater(this);
559 this.origin = this.origin + movedt * this.velocity;
560 this.angles = this.angles + movedt * this.avelocity;
561 _Movetype_LinkEdict(this, false);
564 _Movetype_Physics_Step(this, movedt);
567 _Movetype_Physics_Walk(this, movedt);
570 case MOVETYPE_BOUNCE:
571 case MOVETYPE_BOUNCEMISSILE:
572 case MOVETYPE_FLYMISSILE:
574 case MOVETYPE_FLY_WORLDONLY:
575 _Movetype_Physics_Toss(this, movedt);
578 _Movetype_LinkEdict(this, true);
580 case MOVETYPE_PHYSICS:
585 void _Movetype_Physics_ClientFrame(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;
605 _Movetype_Physics_Step(this, movedt);
609 case MOVETYPE_FLY_WORLDONLY:
610 _Movetype_Physics_Walk(this, movedt);
613 case MOVETYPE_BOUNCE:
614 case MOVETYPE_BOUNCEMISSILE:
615 case MOVETYPE_FLYMISSILE:
616 _Movetype_Physics_Toss(this, movedt);
618 case MOVETYPE_PHYSICS:
622 //_Movetype_CheckVelocity(this);
624 _Movetype_LinkEdict(this, true);
626 //_Movetype_CheckVelocity(this);
629 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
631 this.move_time = time;
634 _Movetype_Physics_ClientFrame(this, movedt);
636 _Movetype_Physics_Frame(this, movedt);
640 setorigin(this, this.origin);
643 void Movetype_Physics_NoMatchServer(entity this) // optimized
645 float movedt = time - this.move_time;
646 this.move_time = time;
648 _Movetype_Physics_Frame(this, movedt);
652 setorigin(this, this.origin);
655 void Movetype_Physics_MatchServer(entity this, bool sloppy)
657 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
661 .vector tic_velocity;
663 .vector tic_avelocity;
666 .vector tic_saved_origin;
667 .vector tic_saved_velocity;
668 .int tic_saved_flags;
669 .vector tic_saved_avelocity;
670 .vector tic_saved_angles;
671 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
674 if(this.(s) != this.tic_saved_##s) \
675 this.tic_##s = this.(s)
686 this.flags = this.tic_flags;
687 this.velocity = this.tic_velocity;
688 this.origin = this.tic_origin;
689 this.avelocity = this.tic_avelocity;
690 this.angles = this.tic_angles;
691 Movetype_Physics_NoMatchServer(this);
692 this.tic_origin = this.origin;
693 this.tic_velocity = this.velocity;
694 this.tic_avelocity = this.avelocity;
695 this.tic_angles = this.angles;
696 this.tic_flags = this.flags;
698 this.tic_saved_flags = this.flags;
699 this.tic_saved_velocity = this.velocity;
700 this.tic_saved_origin = this.origin;
701 this.tic_saved_avelocity = this.avelocity;
702 this.tic_saved_angles = this.angles;
706 float dt = time - this.move_time;
708 int n = max(0, floor(dt / tr));
710 this.move_time += n * tr;
712 if(!this.move_didgravity)
713 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
715 for (int i = 0; i < n; ++i)
717 this.flags = this.tic_flags;
718 this.velocity = this.tic_velocity;
719 setorigin(this, this.tic_origin);
720 this.avelocity = this.tic_avelocity;
721 this.angles = this.tic_angles;
722 _Movetype_Physics_Frame(this, tr);
723 this.tic_origin = this.origin;
724 this.tic_velocity = this.velocity;
725 this.tic_avelocity = this.avelocity;
726 this.tic_angles = this.angles;
727 this.tic_flags = this.flags;
732 this.avelocity = this.tic_avelocity;
734 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
736 // now continue the move from move_time to time
737 this.velocity = this.tic_velocity;
739 if(this.move_didgravity > 0)
741 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
743 * (this.gravity ? this.gravity : 1)
744 * PHYS_GRAVITY(this);
747 this.angles = this.tic_angles + dt * this.avelocity;
749 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
751 setorigin(this, this.tic_origin + dt * this.velocity);
755 vector oldorg = this.origin;
756 this.origin = this.tic_origin;
757 _Movetype_PushEntityTrace(this, dt * this.velocity);
758 this.origin = oldorg;
759 if(!trace_startsolid)
760 setorigin(this, trace_endpos);
763 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
764 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
768 this.velocity = this.tic_velocity;
769 this.angles = this.tic_angles;
770 setorigin(this, this.tic_origin);
773 this.tic_saved_flags = this.flags;
774 this.tic_saved_velocity = this.velocity;
775 this.tic_saved_origin = this.origin;
776 this.tic_saved_avelocity = this.avelocity;
777 this.tic_saved_angles = this.angles;