1 #include "../player.qh"
4 #include <client/defs.qh>
5 #include <common/stats.qh>
6 #include <common/util.qh>
7 #include "movetypes.qh"
8 #include <lib/csqcmodel/common.qh>
9 #include <common/t_items.qh>
12 #include <server/autocvars.qh>
16 void set_movetype(entity this, int mt)
18 if(mt == MOVETYPE_PHYSICS || mt == MOVETYPE_PUSH || mt == MOVETYPE_FAKEPUSH)
20 this.movetype = this.move_movetype = mt; // we still set move_movetype, for other code that checks movetype
21 this.move_qcphysics = false;
25 this.move_movetype = mt;
27 if(!this.move_qcphysics)
31 void set_movetype(entity this, int mt)
33 this.move_movetype = mt;
37 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
41 makevectors(this.v_angle);
42 d = (stepnormal * v_forward) + 0.5;
46 i = (stepnormal * this.velocity);
47 into = i * stepnormal;
48 side = this.velocity - into;
49 this.velocity_x = side.x * (1 * d);
50 this.velocity_y = side.y * (1 * d);
54 vector planes[MAX_CLIP_PLANES];
55 int _Movetype_FlyMove(entity this, float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
57 int blocked = 0, bumpcount;
58 int i, j, numplanes = 0;
59 float time_left = dt, grav = 0;
61 vector primal_velocity, original_velocity, restore_velocity;
63 for(i = 0; i < MAX_CLIP_PLANES; ++i)
68 this.move_didgravity = 1;
69 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
71 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
73 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
74 this.velocity_z -= grav * 0.5;
76 this.velocity_z -= grav;
80 original_velocity = primal_velocity = restore_velocity = this.velocity;
82 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
84 if(this.velocity == '0 0 0')
87 push = this.velocity * time_left;
88 _Movetype_PushEntity(this, push, true);
91 // we got teleported by a touch function
92 // let's abort the move
97 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
98 // abort move if we're stuck in the world (and didn't make it out)
99 if(trace_startsolid && trace_allsolid)
101 this.velocity = restore_velocity;
105 if(trace_fraction == 1)
108 float my_trace_fraction = trace_fraction;
109 vector my_trace_plane_normal = trace_plane_normal;
111 if(trace_plane_normal.z)
113 if(trace_plane_normal.z > 0.7)
120 //dprint("_Movetype_FlyMove: !trace_ent\n");
125 this.move_groundentity = trace_ent;
130 // step - handle it immediately
131 vector org = this.origin;
132 vector steppush = '0 0 1' * stepheight;
134 _Movetype_PushEntity(this, steppush, true);
140 _Movetype_PushEntity(this, push, true);
146 float trace2_fraction = trace_fraction;
147 steppush = '0 0 1' * (org_z - this.origin_z);
148 _Movetype_PushEntity(this, steppush, true);
155 // accept the new position if it made some progress...
156 if(fabs(this.origin_x - org_x) >= 0.03125 || fabs(this.origin_y - org_y) >= 0.03125)
158 trace_endpos = this.origin;
159 time_left *= 1 - trace2_fraction;
168 // step - return it to caller
170 // save the trace for player extrafriction
172 stepnormal = trace_plane_normal;
175 if(my_trace_fraction >= 0.001)
177 // actually covered some distance
178 original_velocity = this.velocity;
182 time_left *= 1 - my_trace_fraction;
184 // clipped to another plane
185 if(numplanes >= MAX_CLIP_PLANES)
187 // this shouldn't really happen
188 this.velocity = '0 0 0';
193 planes[numplanes] = my_trace_plane_normal;
196 // modify original_velocity so it parallels all of the clip planes
197 vector new_velocity = '0 0 0';
198 for (i = 0;i < numplanes;i++)
200 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
201 for (j = 0;j < numplanes;j++)
206 if((new_velocity * planes[j]) < 0)
216 // go along this plane
217 this.velocity = new_velocity;
221 // go along the crease
224 this.velocity = '0 0 0';
228 vector dir = cross(planes[0], planes[1]);
229 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
230 float ilength = sqrt((dir * dir));
232 ilength = 1.0 / ilength;
236 float d = (dir * this.velocity);
237 this.velocity = dir * d;
240 // if current velocity is against the original velocity,
241 // stop dead to avoid tiny occilations in sloping corners
242 if((this.velocity * primal_velocity) <= 0)
244 this.velocity = '0 0 0';
249 // LordHavoc: this came from QW and allows you to get out of water more easily
250 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
251 this.velocity = primal_velocity;
255 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
257 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
258 this.velocity_z -= grav * 0.5f;
265 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
267 // if(vlen(this.velocity) < 0.0001)
268 // this.velocity = '0 0 0';
271 bool _Movetype_CheckWater(entity this) // SV_CheckWater
273 vector point = this.origin;
274 point.z += this.mins.z + 1;
276 int nativecontents = pointcontents(point);
277 if(this.watertype && this.watertype != nativecontents)
279 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
280 if(this.contentstransition)
281 this.contentstransition(this.watertype, nativecontents);
284 this.waterlevel = WATERLEVEL_NONE;
285 this.watertype = CONTENT_EMPTY;
287 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
288 if(supercontents & DPCONTENTS_LIQUIDSMASK)
290 this.watertype = nativecontents;
291 this.waterlevel = WATERLEVEL_WETFEET;
292 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
293 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
295 this.waterlevel = WATERLEVEL_SWIMMING;
296 point.z = this.origin.z + this.view_ofs.z;
297 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
298 this.waterlevel = WATERLEVEL_SUBMERGED;
302 return this.waterlevel > 1;
305 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
307 int contents = pointcontents(ent.origin);
312 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
314 ent.watertype = contents;
319 else if(ent.watertype != contents)
321 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
322 if(ent.contentstransition)
323 ent.contentstransition(ent.watertype, contents);
326 if(contents <= CONTENT_WATER)
328 ent.watertype = contents;
333 ent.watertype = CONTENT_EMPTY;
334 ent.waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
338 void _Movetype_Impact(entity this, entity oth) // SV_Impact
341 gettouch(this)(this, oth);
344 gettouch(oth)(oth, this);
347 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
349 if(this.solid == SOLID_NOT)
352 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
353 if (it.solid == SOLID_TRIGGER && it != this)
354 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
355 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
357 trace_allsolid = false;
358 trace_startsolid = false;
360 trace_inwater = false;
362 trace_endpos = it.origin;
363 trace_plane_normal = '0 0 1';
364 trace_plane_dist = 0;
367 gettouch(it)(it, this);
372 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
375 if(this.solid == SOLID_BSP)
377 // TODO set the absolute bbox
389 if(this.flags & FL_ITEM)
412 _Movetype_LinkEdict_TouchAreaGrid(this);
415 entity _Movetype_TestEntityPosition_ent;
416 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
418 entity this = _Movetype_TestEntityPosition_ent;
419 // vector org = this.origin + ofs;
421 int cont = this.dphitcontentsmask;
422 this.dphitcontentsmask = DPCONTENTS_SOLID;
423 tracebox(this.origin, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
424 this.dphitcontentsmask = cont;
429 if(vdist(trace_endpos - this.origin, >, 0.0001))
430 this.origin = trace_endpos;
434 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
436 _Movetype_TestEntityPosition_ent = this;
437 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
440 #define X(v) if (_Movetype_TestEntityPosition(v))
441 X('-1 0 0') X(' 1 0 0')
442 X(' 0 -1 0') X(' 0 1 0')
443 X('-1 -1 0') X(' 1 -1 0')
444 X('-1 1 0') X(' 1 1 0')
448 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
449 if (_Movetype_TestEntityPosition('0 0 1' * i))
450 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
451 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
455 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
456 etof(this), this.classname, vtos(this.origin));
460 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
461 etof(this), this.classname, vtos(this.origin));
462 _Movetype_LinkEdict(this, true);
466 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
468 vel -= ((vel * norm) * norm) * f;
470 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
471 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
472 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
477 void _Movetype_PushEntityTrace(entity this, vector push)
479 vector end = this.origin + push;
481 if(this.move_nomonsters)
482 type = max(0, this.move_nomonsters);
483 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
485 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
486 type = MOVE_WORLDONLY;
487 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
488 type = MOVE_NOMONSTERS;
492 tracebox(this.origin, this.mins, this.maxs, end, type, this);
495 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
497 _Movetype_PushEntityTrace(this, push);
499 if(trace_startsolid && failonstartsolid)
500 return trace_fraction;
502 this.origin = trace_endpos;
504 if(trace_fraction < 1)
505 if(this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || (this.move_groundentity != trace_ent)))
506 _Movetype_Impact(this, trace_ent);
508 return trace_fraction;
514 // matrix version of makevectors, sets v_forward, v_right and v_up
515 void makevectors_matrix(vector myangles) // AngleVectorsFLU
517 v_forward = v_right = v_up = '0 0 0';
519 float y = myangles.y * (M_PI * 2 / 360);
522 float p = myangles.x * (M_PI * 2 / 360);
527 v_forward.x = cp * cy;
528 v_forward.y = cp * sy;
535 float r = myangles.z * (M_PI * 2 / 360);
540 v_right.x = sr * sp * cy + cr * -sy;
541 v_right.y = sr * sp * sy + cr * cy;
546 v_up.x = cr * sp * cy + -sr * -sy;
547 v_up.y = cr * sp * sy + -sr * cy;
569 void _Movetype_Physics_Frame(entity this, float movedt)
571 this.move_didgravity = -1;
572 switch (this.move_movetype)
575 case MOVETYPE_FAKEPUSH:
576 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!\n");
580 case MOVETYPE_FOLLOW:
581 _Movetype_Physics_Follow(this);
583 case MOVETYPE_NOCLIP:
584 _Movetype_CheckWater(this);
585 this.origin = this.origin + movedt * this.velocity;
586 this.angles = this.angles + movedt * this.avelocity;
587 _Movetype_LinkEdict(this, false);
590 _Movetype_Physics_Step(this, movedt);
593 _Movetype_Physics_Walk(this, movedt);
596 case MOVETYPE_BOUNCE:
597 case MOVETYPE_BOUNCEMISSILE:
598 case MOVETYPE_FLYMISSILE:
600 case MOVETYPE_FLY_WORLDONLY:
601 _Movetype_Physics_Toss(this, movedt);
602 _Movetype_LinkEdict(this, true);
604 case MOVETYPE_PHYSICS:
609 void _Movetype_Physics_ClientFrame(entity this, float movedt)
611 this.move_didgravity = -1;
612 switch (this.move_movetype)
615 case MOVETYPE_FAKEPUSH:
616 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!\n");
620 case MOVETYPE_FOLLOW:
621 _Movetype_Physics_Follow(this);
623 case MOVETYPE_NOCLIP:
624 _Movetype_CheckWater(this);
625 this.origin = this.origin + movedt * this.velocity;
626 this.angles = this.angles + movedt * this.avelocity;
627 _Movetype_LinkEdict(this, false);
630 _Movetype_Physics_Step(this, movedt);
634 case MOVETYPE_FLY_WORLDONLY:
635 _Movetype_Physics_Walk(this, movedt);
638 case MOVETYPE_BOUNCE:
639 case MOVETYPE_BOUNCEMISSILE:
640 case MOVETYPE_FLYMISSILE:
641 _Movetype_Physics_Toss(this, movedt);
643 case MOVETYPE_PHYSICS:
648 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
650 this.move_time = time;
653 _Movetype_Physics_ClientFrame(this, movedt);
655 _Movetype_Physics_Frame(this, movedt);
659 setorigin(this, this.origin);
662 void Movetype_Physics_NoMatchServer(entity this) // optimized
664 float movedt = time - this.move_time;
665 this.move_time = time;
667 _Movetype_Physics_Frame(this, movedt);
671 setorigin(this, this.origin);
674 void Movetype_Physics_MatchServer(entity this, bool sloppy)
676 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
680 .vector tic_velocity;
682 .vector tic_avelocity;
685 .vector tic_saved_origin;
686 .vector tic_saved_velocity;
687 .int tic_saved_flags;
688 .vector tic_saved_avelocity;
689 .vector tic_saved_angles;
690 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
693 if(this.(s) != this.tic_saved_##s) \
694 this.tic_##s = this.(s)
705 this.flags = this.tic_flags;
706 this.velocity = this.tic_velocity;
707 this.origin = this.tic_origin;
708 this.avelocity = this.tic_avelocity;
709 this.angles = this.tic_angles;
710 Movetype_Physics_NoMatchServer(this);
711 this.tic_origin = this.origin;
712 this.tic_velocity = this.velocity;
713 this.tic_avelocity = this.avelocity;
714 this.tic_angles = this.angles;
715 this.tic_flags = this.flags;
717 this.tic_saved_flags = this.flags;
718 this.tic_saved_velocity = this.velocity;
719 this.tic_saved_origin = this.origin;
720 this.tic_saved_avelocity = this.avelocity;
721 this.tic_saved_angles = this.angles;
725 float dt = time - this.move_time;
727 int n = max(0, floor(dt / tr));
729 this.move_time += n * tr;
731 if(!this.move_didgravity)
732 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
734 for (int i = 0; i < n; ++i)
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;
741 _Movetype_Physics_Frame(this, tr);
742 this.tic_origin = this.origin;
743 this.tic_velocity = this.velocity;
744 this.tic_avelocity = this.avelocity;
745 this.tic_angles = this.angles;
746 this.tic_flags = this.flags;
751 this.avelocity = this.tic_avelocity;
753 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
755 // now continue the move from move_time to time
756 this.velocity = this.tic_velocity;
758 if(this.move_didgravity > 0)
760 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
762 * (this.gravity ? this.gravity : 1)
763 * PHYS_GRAVITY(this);
766 this.angles = this.tic_angles + dt * this.avelocity;
768 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
770 setorigin(this, this.tic_origin + dt * this.velocity);
774 vector oldorg = this.origin;
775 this.origin = this.tic_origin;
776 _Movetype_PushEntityTrace(this, dt * this.velocity);
777 this.origin = oldorg;
778 if(!trace_startsolid)
779 setorigin(this, trace_endpos);
782 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
783 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
787 this.velocity = this.tic_velocity;
788 this.angles = this.tic_angles;
789 setorigin(this, this.tic_origin);
792 this.tic_saved_flags = this.flags;
793 this.tic_saved_velocity = this.velocity;
794 this.tic_saved_origin = this.origin;
795 this.tic_saved_avelocity = this.avelocity;
796 this.tic_saved_angles = this.angles;