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, vector stepnormal, float stepheight) // SV_FlyMove
43 int i, j, numplanes = 0;
44 float time_left = dt, grav = 0;
46 vector primal_velocity, original_velocity;
47 vector restore_velocity = this.velocity;
49 for(i = 0; i < MAX_CLIP_PLANES; ++i)
54 this.move_didgravity = 1;
55 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
57 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
59 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
60 this.velocity_z -= grav * 0.5;
62 this.velocity_z -= grav;
66 original_velocity = primal_velocity = this.velocity;
68 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
70 if(this.velocity == '0 0 0')
73 push = this.velocity * time_left;
74 if(!_Movetype_PushEntity(this, push, true, false))
76 // we got teleported by a touch function
77 // let's abort the move
82 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
83 // abort move if we're stuck in the world (and didn't make it out)
84 if(trace_startsolid && trace_allsolid)
86 this.velocity = restore_velocity;
90 if(trace_fraction == 1)
93 float my_trace_fraction = trace_fraction;
94 vector my_trace_plane_normal = trace_plane_normal;
96 if(trace_plane_normal.z)
98 if(trace_plane_normal.z > 0.7)
105 //dprint("_Movetype_FlyMove: !trace_ent\n");
110 this.groundentity = trace_ent;
115 // step - handle it immediately
116 vector org = this.origin;
117 vector steppush = '0 0 1' * stepheight;
119 if(!_Movetype_PushEntity(this, steppush, true, false))
124 if(!_Movetype_PushEntity(this, push, true, false))
129 float trace2_fraction = trace_fraction;
130 steppush = vec3(0, 0, org.z - this.origin_z);
131 if(!_Movetype_PushEntity(this, steppush, true, false))
137 // accept the new position if it made some progress...
138 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
140 trace_endpos = this.origin;
141 time_left *= 1 - trace2_fraction;
150 // step - return it to caller
152 // save the trace for player extrafriction
154 stepnormal = trace_plane_normal;
157 if(my_trace_fraction >= 0.001)
159 // actually covered some distance
160 original_velocity = this.velocity;
164 time_left *= 1 - my_trace_fraction;
166 // clipped to another plane
167 if(numplanes >= MAX_CLIP_PLANES)
169 // this shouldn't really happen
170 this.velocity = '0 0 0';
175 planes[numplanes] = my_trace_plane_normal;
178 // modify original_velocity so it parallels all of the clip planes
179 vector new_velocity = '0 0 0';
180 for (i = 0;i < numplanes;i++)
182 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
183 for (j = 0;j < numplanes;j++)
188 if((new_velocity * planes[j]) < 0)
198 // go along this plane
199 this.velocity = new_velocity;
203 // go along the crease
206 this.velocity = '0 0 0';
210 vector dir = cross(planes[0], planes[1]);
211 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
212 float ilength = sqrt((dir * dir));
214 ilength = 1.0 / ilength;
218 float d = (dir * this.velocity);
219 this.velocity = dir * d;
222 // if current velocity is against the original velocity,
223 // stop dead to avoid tiny occilations in sloping corners
224 if((this.velocity * primal_velocity) <= 0)
226 this.velocity = '0 0 0';
231 // LordHavoc: this came from QW and allows you to get out of water more easily
232 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
233 this.velocity = primal_velocity;
235 if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blocked & 8))
236 this.velocity = primal_velocity;
240 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
242 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
243 this.velocity_z -= grav * 0.5f;
250 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
252 // if(vlen(this.velocity) < 0.0001)
253 // this.velocity = '0 0 0';
256 bool _Movetype_CheckWater(entity this) // SV_CheckWater
258 vector point = this.origin;
259 point.z += this.mins.z + 1;
261 int nativecontents = pointcontents(point);
262 if(this.watertype && this.watertype != nativecontents)
264 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
265 if(this.contentstransition)
266 this.contentstransition(this.watertype, nativecontents);
269 this.waterlevel = WATERLEVEL_NONE;
270 this.watertype = CONTENT_EMPTY;
272 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
273 if(supercontents & DPCONTENTS_LIQUIDSMASK)
275 this.watertype = nativecontents;
276 this.waterlevel = WATERLEVEL_WETFEET;
277 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
278 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
280 this.waterlevel = WATERLEVEL_SWIMMING;
281 point.z = this.origin.z + this.view_ofs.z;
282 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
283 this.waterlevel = WATERLEVEL_SUBMERGED;
287 return this.waterlevel > 1;
290 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
292 int contents = pointcontents(ent.origin);
297 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
299 ent.watertype = contents;
304 else if(ent.watertype != contents)
306 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
307 if(ent.contentstransition)
308 ent.contentstransition(ent.watertype, contents);
311 if(contents <= CONTENT_WATER)
313 ent.watertype = contents;
318 ent.watertype = CONTENT_EMPTY;
319 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
323 void _Movetype_Impact(entity this, entity oth) // SV_Impact
326 gettouch(this)(this, oth);
329 gettouch(oth)(oth, this);
332 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
334 if(this.solid == SOLID_NOT)
337 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
338 if (it.solid == SOLID_TRIGGER && it != this)
339 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
340 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
342 trace_allsolid = false;
343 trace_startsolid = false;
345 trace_inwater = false;
347 trace_endpos = it.origin;
348 trace_plane_normal = '0 0 1';
349 trace_plane_dist = 0;
351 trace_dpstartcontents = 0;
352 trace_dphitcontents = 0;
353 trace_dphitq3surfaceflags = 0;
354 trace_dphittexturename = string_null;
356 gettouch(it)(it, this);
361 bool autocvar__movetype_debug = false;
362 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
364 if(autocvar__movetype_debug)
367 if(this.solid == SOLID_BSP)
369 // TODO set the absolute bbox
381 if(this.flags & FL_ITEM)
397 setorigin(this, this.origin); // calls SV_LinkEdict
399 // NOTE: CSQC's version of setorigin doesn't expand
400 this.absmin -= '1 1 1';
401 this.absmax += '1 1 1';
406 _Movetype_LinkEdict_TouchAreaGrid(this);
409 entity _Movetype_TestEntityPosition_ent;
410 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
412 entity this = _Movetype_TestEntityPosition_ent;
413 vector org = this.origin + ofs;
415 int cont = this.dphitcontentsmask;
416 this.dphitcontentsmask = DPCONTENTS_SOLID;
417 tracebox(org, this.mins, this.maxs, org, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
418 this.dphitcontentsmask = cont;
423 if(vdist(trace_endpos - this.origin, >, 0.0001))
424 this.origin = trace_endpos;
428 int _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
430 _Movetype_TestEntityPosition_ent = this;
431 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
434 #define X(v) if (_Movetype_TestEntityPosition(v))
435 X('-1 0 0') X(' 1 0 0')
436 X(' 0 -1 0') X(' 0 1 0')
437 X('-1 -1 0') X(' 1 -1 0')
438 X('-1 1 0') X(' 1 1 0')
442 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
443 if (_Movetype_TestEntityPosition('0 0 1' * i))
444 X(1) X(2) X(3) X(4) X(5) X(6) X(7) X(8)
445 X(9) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
449 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
450 etof(this), this.classname, vtos(this.origin));
451 return UNSTICK_STUCK;
454 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
455 etof(this), this.classname, vtos(this.origin));
456 _Movetype_LinkEdict(this, true);
457 return UNSTICK_FIXED;
460 void _Movetype_CheckStuck(entity this) // SV_CheckStuck
462 int unstick = _Movetype_UnstickEntity(this); // sets test position entity
466 this.oldorigin = this.origin;
469 break; // already sorted
471 vector offset = this.oldorigin - this.origin;
472 if(!_Movetype_TestEntityPosition(offset))
473 _Movetype_LinkEdict(this, false);
474 // couldn't unstick, should we warn about this?
479 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
481 vel -= ((vel * norm) * norm) * f;
483 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
484 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
485 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
490 void _Movetype_PushEntityTrace(entity this, vector push)
492 vector end = this.origin + push;
494 if(this.move_nomonsters)
495 type = max(0, this.move_nomonsters);
496 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
498 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
499 type = MOVE_WORLDONLY;
500 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
501 type = MOVE_NOMONSTERS;
505 tracebox(this.origin, this.mins, this.maxs, end, type, this);
508 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink) // SV_PushEntity
510 _Movetype_PushEntityTrace(this, push);
512 // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
513 if(trace_startsolid && failonstartsolid)
515 int oldtype = this.move_nomonsters;
516 this.move_nomonsters = MOVE_WORLDONLY;
517 _Movetype_PushEntityTrace(this, push);
518 this.move_nomonsters = oldtype;
523 this.origin = trace_endpos;
525 vector last_origin = this.origin;
528 _Movetype_LinkEdict(this, true);
530 if(trace_fraction < 1)
531 if(this.solid >= SOLID_TRIGGER && trace_ent && (!IS_ONGROUND(this) || (this.groundentity != trace_ent)))
532 _Movetype_Impact(this, trace_ent);
534 return (this.origin == last_origin); // false if teleported by touch
541 void _Movetype_Physics_Frame(entity this, float movedt)
543 this.move_didgravity = -1;
544 switch (this.move_movetype)
547 case MOVETYPE_FAKEPUSH:
548 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
552 case MOVETYPE_FOLLOW:
553 _Movetype_Physics_Follow(this);
555 case MOVETYPE_NOCLIP:
556 _Movetype_CheckWater(this);
557 this.origin = this.origin + movedt * this.velocity;
558 this.angles = this.angles + movedt * this.avelocity;
559 _Movetype_LinkEdict(this, false);
562 _Movetype_Physics_Step(this, movedt);
565 _Movetype_Physics_Walk(this, movedt);
568 case MOVETYPE_BOUNCE:
569 case MOVETYPE_BOUNCEMISSILE:
570 case MOVETYPE_FLYMISSILE:
572 case MOVETYPE_FLY_WORLDONLY:
573 _Movetype_Physics_Toss(this, movedt);
576 _Movetype_LinkEdict(this, true);
578 case MOVETYPE_PHYSICS:
583 void _Movetype_Physics_ClientFrame(entity this, float movedt)
585 this.move_didgravity = -1;
586 switch (this.move_movetype)
589 case MOVETYPE_FAKEPUSH:
590 LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
594 case MOVETYPE_FOLLOW:
595 _Movetype_Physics_Follow(this);
597 case MOVETYPE_NOCLIP:
598 _Movetype_CheckWater(this);
599 this.origin = this.origin + movedt * this.velocity;
600 this.angles = this.angles + movedt * this.avelocity;
603 _Movetype_Physics_Step(this, movedt);
607 case MOVETYPE_FLY_WORLDONLY:
608 _Movetype_Physics_Walk(this, movedt);
611 case MOVETYPE_BOUNCE:
612 case MOVETYPE_BOUNCEMISSILE:
613 case MOVETYPE_FLYMISSILE:
614 _Movetype_Physics_Toss(this, movedt);
616 case MOVETYPE_PHYSICS:
620 //_Movetype_CheckVelocity(this);
622 _Movetype_LinkEdict(this, true);
624 //_Movetype_CheckVelocity(this);
627 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
629 this.move_time = time;
632 _Movetype_Physics_ClientFrame(this, movedt);
634 _Movetype_Physics_Frame(this, movedt);
638 setorigin(this, this.origin);
641 void Movetype_Physics_NoMatchServer(entity this) // optimized
643 float movedt = time - this.move_time;
644 this.move_time = time;
646 _Movetype_Physics_Frame(this, movedt);
650 setorigin(this, this.origin);
653 void Movetype_Physics_MatchServer(entity this, bool sloppy)
655 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
659 .vector tic_velocity;
661 .vector tic_avelocity;
664 .vector tic_saved_origin;
665 .vector tic_saved_velocity;
666 .int tic_saved_flags;
667 .vector tic_saved_avelocity;
668 .vector tic_saved_angles;
669 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
672 if(this.(s) != this.tic_saved_##s) \
673 this.tic_##s = this.(s)
684 this.flags = this.tic_flags;
685 this.velocity = this.tic_velocity;
686 this.origin = this.tic_origin;
687 this.avelocity = this.tic_avelocity;
688 this.angles = this.tic_angles;
689 Movetype_Physics_NoMatchServer(this);
690 this.tic_origin = this.origin;
691 this.tic_velocity = this.velocity;
692 this.tic_avelocity = this.avelocity;
693 this.tic_angles = this.angles;
694 this.tic_flags = this.flags;
696 this.tic_saved_flags = this.flags;
697 this.tic_saved_velocity = this.velocity;
698 this.tic_saved_origin = this.origin;
699 this.tic_saved_avelocity = this.avelocity;
700 this.tic_saved_angles = this.angles;
704 float dt = time - this.move_time;
706 int n = max(0, floor(dt / tr));
708 this.move_time += n * tr;
710 if(!this.move_didgravity)
711 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
713 for (int i = 0; i < n; ++i)
715 this.flags = this.tic_flags;
716 this.velocity = this.tic_velocity;
717 setorigin(this, this.tic_origin);
718 this.avelocity = this.tic_avelocity;
719 this.angles = this.tic_angles;
720 _Movetype_Physics_Frame(this, tr);
721 this.tic_origin = this.origin;
722 this.tic_velocity = this.velocity;
723 this.tic_avelocity = this.avelocity;
724 this.tic_angles = this.angles;
725 this.tic_flags = this.flags;
730 this.avelocity = this.tic_avelocity;
732 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
734 // now continue the move from move_time to time
735 this.velocity = this.tic_velocity;
737 if(this.move_didgravity > 0)
739 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
741 * (this.gravity ? this.gravity : 1)
742 * PHYS_GRAVITY(this);
745 this.angles = this.tic_angles + dt * this.avelocity;
747 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
749 setorigin(this, this.tic_origin + dt * this.velocity);
753 vector oldorg = this.origin;
754 this.origin = this.tic_origin;
755 _Movetype_PushEntityTrace(this, dt * this.velocity);
756 this.origin = oldorg;
757 if(!trace_startsolid)
758 setorigin(this, trace_endpos);
761 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
762 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
766 this.velocity = this.tic_velocity;
767 this.angles = this.tic_angles;
768 setorigin(this, this.tic_origin);
771 this.tic_saved_flags = this.flags;
772 this.tic_saved_velocity = this.velocity;
773 this.tic_saved_origin = this.origin;
774 this.tic_saved_avelocity = this.avelocity;
775 this.tic_saved_angles = this.angles;