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>
15 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
19 makevectors(this.v_angle);
20 d = (stepnormal * v_forward) + 0.5;
24 i = (stepnormal * this.velocity);
25 into = i * stepnormal;
26 side = this.velocity - into;
27 this.velocity_x = side.x * (1 * d);
28 this.velocity_y = side.y * (1 * d);
32 vector planes[MAX_CLIP_PLANES];
33 int _Movetype_FlyMove(entity this, float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
35 int blocked = 0, bumpcount;
36 int i, j, numplanes = 0;
37 float time_left = dt, grav = 0;
39 vector primal_velocity, original_velocity, restore_velocity;
41 for(i = 0; i < MAX_CLIP_PLANES; ++i)
46 this.move_didgravity = 1;
47 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
49 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
51 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
52 this.velocity_z -= grav * 0.5;
54 this.velocity_z -= grav;
58 original_velocity = primal_velocity = restore_velocity = this.velocity;
60 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
62 if(this.velocity == '0 0 0')
65 push = this.velocity * time_left;
66 _Movetype_PushEntity(this, push, true);
69 // we got teleported by a touch function
70 // let's abort the move
75 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
76 // abort move if we're stuck in the world (and didn't make it out)
77 if(trace_startsolid && trace_allsolid)
79 this.velocity = restore_velocity;
83 if(trace_fraction == 1)
86 float my_trace_fraction = trace_fraction;
87 vector my_trace_plane_normal = trace_plane_normal;
89 if(trace_plane_normal.z)
91 if(trace_plane_normal.z > 0.7)
98 //dprint("_Movetype_FlyMove: !trace_ent\n");
103 this.move_groundentity = trace_ent;
108 // step - handle it immediately
109 vector org = this.origin;
110 vector steppush = '0 0 1' * stepheight;
112 _Movetype_PushEntity(this, steppush, true);
118 _Movetype_PushEntity(this, push, true);
124 float trace2_fraction = trace_fraction;
125 steppush = '0 0 1' * (org_z - this.origin_z);
126 _Movetype_PushEntity(this, steppush, true);
133 // accept the new position if it made some progress...
134 if(fabs(this.origin_x - org_x) >= 0.03125 || fabs(this.origin_y - org_y) >= 0.03125)
136 trace_endpos = this.origin;
137 time_left *= 1 - trace2_fraction;
146 // step - return it to caller
148 // save the trace for player extrafriction
150 stepnormal = trace_plane_normal;
153 if(my_trace_fraction >= 0.001)
155 // actually covered some distance
156 original_velocity = this.velocity;
160 time_left *= 1 - my_trace_fraction;
162 // clipped to another plane
163 if(numplanes >= MAX_CLIP_PLANES)
165 // this shouldn't really happen
166 this.velocity = '0 0 0';
171 planes[numplanes] = my_trace_plane_normal;
174 // modify original_velocity so it parallels all of the clip planes
175 vector new_velocity = '0 0 0';
176 for (i = 0;i < numplanes;i++)
178 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
179 for (j = 0;j < numplanes;j++)
184 if((new_velocity * planes[j]) < 0)
194 // go along this plane
195 this.velocity = new_velocity;
199 // go along the crease
202 this.velocity = '0 0 0';
206 vector dir = cross(planes[0], planes[1]);
207 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
208 float ilength = sqrt((dir * dir));
210 ilength = 1.0 / ilength;
214 float d = (dir * this.velocity);
215 this.velocity = dir * d;
218 // if current velocity is against the original velocity,
219 // stop dead to avoid tiny occilations in sloping corners
220 if((this.velocity * primal_velocity) <= 0)
222 this.velocity = '0 0 0';
227 // LordHavoc: this came from QW and allows you to get out of water more easily
228 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
229 this.velocity = primal_velocity;
233 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
235 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
236 this.velocity_z -= grav * 0.5f;
243 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
245 // if(vlen(this.velocity) < 0.0001)
246 // this.velocity = '0 0 0';
249 bool _Movetype_CheckWater(entity this) // SV_CheckWater
251 vector point = this.origin;
252 point.z += this.mins.z + 1;
254 int nativecontents = pointcontents(point);
255 if(this.watertype && this.watertype != nativecontents)
257 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
258 if(this.contentstransition)
259 this.contentstransition(this.watertype, nativecontents);
262 this.waterlevel = WATERLEVEL_NONE;
263 this.watertype = CONTENT_EMPTY;
265 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
266 if(supercontents & DPCONTENTS_LIQUIDSMASK)
268 this.watertype = nativecontents;
269 this.waterlevel = WATERLEVEL_WETFEET;
270 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
271 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
273 this.waterlevel = WATERLEVEL_SWIMMING;
274 point.z = this.origin.z + this.view_ofs.z;
275 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
276 this.waterlevel = WATERLEVEL_SUBMERGED;
280 return this.waterlevel > 1;
283 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
285 int contents = pointcontents(ent.origin);
290 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
292 ent.watertype = contents;
297 else if(ent.watertype != contents)
299 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
300 if(ent.contentstransition)
301 ent.contentstransition(ent.watertype, contents);
304 if(contents <= CONTENT_WATER)
306 ent.watertype = contents;
311 ent.watertype = CONTENT_EMPTY;
312 ent.waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
316 void _Movetype_Impact(entity this, entity oth) // SV_Impact
319 gettouch(this)(this, oth);
322 gettouch(oth)(oth, this);
325 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
327 if(this.solid == SOLID_NOT)
330 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
331 if (it.solid == SOLID_TRIGGER && it != this)
332 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
333 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
335 trace_allsolid = false;
336 trace_startsolid = false;
338 trace_inwater = false;
340 trace_endpos = it.origin;
341 trace_plane_normal = '0 0 1';
342 trace_plane_dist = 0;
345 gettouch(it)(it, this);
350 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
353 if(this.solid == SOLID_BSP)
355 // TODO set the absolute bbox
367 if(this.flags & FL_ITEM)
390 _Movetype_LinkEdict_TouchAreaGrid(this);
393 entity _Movetype_TestEntityPosition_ent;
394 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
396 entity this = _Movetype_TestEntityPosition_ent;
397 // vector org = this.origin + ofs;
399 int cont = this.dphitcontentsmask;
400 this.dphitcontentsmask = DPCONTENTS_SOLID;
401 tracebox(this.origin, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
402 this.dphitcontentsmask = cont;
407 if(vdist(trace_endpos - this.origin, >, 0.0001))
408 this.origin = trace_endpos;
412 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
414 _Movetype_TestEntityPosition_ent = this;
415 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
418 #define X(v) if (_Movetype_TestEntityPosition(v))
419 X('-1 0 0') X(' 1 0 0')
420 X(' 0 -1 0') X(' 0 1 0')
421 X('-1 -1 0') X(' 1 -1 0')
422 X('-1 1 0') X(' 1 1 0')
426 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
427 if (_Movetype_TestEntityPosition('0 0 1' * i))
428 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
429 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
433 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
434 etof(this), this.classname, vtos(this.origin));
438 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
439 etof(this), this.classname, vtos(this.origin));
440 _Movetype_LinkEdict(this, true);
444 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
446 vel -= ((vel * norm) * norm) * f;
448 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
449 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
450 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
455 void _Movetype_PushEntityTrace(entity this, vector push)
457 vector end = this.origin + push;
459 if(this.move_nomonsters)
460 type = max(0, this.move_nomonsters);
461 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
463 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
464 type = MOVE_WORLDONLY;
465 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
466 type = MOVE_NOMONSTERS;
470 tracebox(this.origin, this.mins, this.maxs, end, type, this);
473 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
475 _Movetype_PushEntityTrace(this, push);
477 if(trace_startsolid && failonstartsolid)
478 return trace_fraction;
480 this.origin = trace_endpos;
482 if(trace_fraction < 1)
483 if(this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || (this.move_groundentity != trace_ent)))
484 _Movetype_Impact(this, trace_ent);
486 return trace_fraction;
492 // matrix version of makevectors, sets v_forward, v_right and v_up
493 void makevectors_matrix(vector myangles) // AngleVectorsFLU
495 v_forward = v_right = v_up = '0 0 0';
497 float y = myangles.y * (M_PI * 2 / 360);
500 float p = myangles.x * (M_PI * 2 / 360);
505 v_forward.x = cp * cy;
506 v_forward.y = cp * sy;
513 float r = myangles.z * (M_PI * 2 / 360);
518 v_right.x = sr * sp * cy + cr * -sy;
519 v_right.y = sr * sp * sy + cr * cy;
524 v_up.x = cr * sp * cy + -sr * -sy;
525 v_up.y = cr * sp * sy + -sr * cy;
547 void _Movetype_Physics_Frame(entity this, float movedt)
549 this.move_didgravity = -1;
550 switch (this.move_movetype)
553 case MOVETYPE_FAKEPUSH:
554 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!\n");
558 case MOVETYPE_FOLLOW:
559 _Movetype_Physics_Follow(this);
561 case MOVETYPE_NOCLIP:
562 _Movetype_CheckWater(this);
563 this.origin = this.origin + movedt * this.velocity;
564 this.angles = this.angles + movedt * this.avelocity;
565 _Movetype_LinkEdict(this, false);
568 _Movetype_Physics_Step(this, movedt);
571 _Movetype_Physics_Walk(this, movedt);
574 case MOVETYPE_BOUNCE:
575 case MOVETYPE_BOUNCEMISSILE:
576 case MOVETYPE_FLYMISSILE:
578 case MOVETYPE_FLY_WORLDONLY:
579 _Movetype_Physics_Toss(this, movedt);
580 _Movetype_LinkEdict(this, true);
582 case MOVETYPE_PHYSICS:
587 void _Movetype_Physics_ClientFrame(entity this, float movedt)
589 this.move_didgravity = -1;
590 switch (this.move_movetype)
593 case MOVETYPE_FAKEPUSH:
594 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!\n");
598 case MOVETYPE_FOLLOW:
599 _Movetype_Physics_Follow(this);
601 case MOVETYPE_NOCLIP:
602 _Movetype_CheckWater(this);
603 this.origin = this.origin + movedt * this.velocity;
604 this.angles = this.angles + movedt * this.avelocity;
605 _Movetype_LinkEdict(this, false);
608 _Movetype_Physics_Step(this, movedt);
612 case MOVETYPE_FLY_WORLDONLY:
613 _Movetype_Physics_Walk(this, movedt);
616 case MOVETYPE_BOUNCE:
617 case MOVETYPE_BOUNCEMISSILE:
618 case MOVETYPE_FLYMISSILE:
619 _Movetype_Physics_Toss(this, movedt);
621 case MOVETYPE_PHYSICS:
626 void Movetype_Physics_NoMatchServer(entity this) // optimized
628 float movedt = time - this.move_time;
629 this.move_time = time;
631 _Movetype_Physics_Frame(this, movedt);
635 setorigin(this, this.origin);
638 void Movetype_Physics_MatchServer(entity this, bool sloppy)
640 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
644 .vector tic_velocity;
646 .vector tic_avelocity;
649 .vector tic_saved_origin;
650 .vector tic_saved_velocity;
651 .int tic_saved_flags;
652 .vector tic_saved_avelocity;
653 .vector tic_saved_angles;
654 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
656 // this will also detect new updates
658 if(this.(s) != this.tic_saved_##s) \
659 this.tic_##s = this.(s)
670 this.flags = this.tic_flags;
671 this.velocity = this.tic_velocity;
672 this.origin = this.tic_origin;
673 this.avelocity = this.tic_avelocity;
674 this.angles = this.tic_angles;
675 Movetype_Physics_NoMatchServer(this);
676 this.tic_origin = this.origin;
677 this.tic_velocity = this.velocity;
678 this.tic_avelocity = this.avelocity;
679 this.tic_angles = this.angles;
680 this.tic_flags = this.flags;
684 float dt = time - this.move_time;
686 int n = max(0, floor(dt / tr));
688 this.move_time += n * tr;
690 if(!this.move_didgravity)
691 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
693 for (int i = 0; i < n; ++i)
695 this.flags = this.tic_flags;
696 this.velocity = this.tic_velocity;
697 this.origin = this.tic_origin;
698 this.avelocity = this.tic_avelocity;
699 this.angles = this.tic_angles;
700 _Movetype_Physics_Frame(this, tr);
701 this.tic_origin = this.origin;
702 this.tic_velocity = this.velocity;
703 this.tic_avelocity = this.avelocity;
704 this.tic_angles = this.angles;
705 this.tic_flags = this.flags;
710 this.avelocity = this.tic_avelocity;
712 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
714 // now continue the move from move_time to time
715 this.velocity = this.tic_velocity;
717 if(this.move_didgravity > 0)
719 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
721 * (this.gravity ? this.gravity : 1)
722 * PHYS_GRAVITY(this);
725 this.angles = this.tic_angles + dt * this.avelocity;
727 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
729 setorigin(this, this.tic_origin + dt * this.velocity);
733 _Movetype_PushEntityTrace(this, dt * this.velocity);
734 if(!trace_startsolid)
735 setorigin(this, trace_endpos);
738 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
739 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
743 this.velocity = this.tic_velocity;
744 this.angles = this.tic_angles;
745 setorigin(this, this.tic_origin);
748 this.tic_saved_flags = this.flags;
749 this.tic_saved_velocity = this.velocity;
750 this.tic_saved_origin = this.origin;
751 this.tic_saved_avelocity = this.avelocity;
752 this.tic_saved_angles = this.angles;