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.move_velocity);
25 into = i * stepnormal;
26 side = this.move_velocity - into;
27 this.move_velocity_x = side.x * (1 * d);
28 this.move_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 || !(this.move_flags & FL_ONGROUND))
51 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
52 this.move_velocity_z -= grav * 0.5;
54 this.move_velocity_z -= grav;
58 original_velocity = primal_velocity = restore_velocity = this.move_velocity;
60 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
62 if(this.move_velocity == '0 0 0')
65 push = this.move_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.move_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");
102 this.move_flags |= FL_ONGROUND;
103 this.move_groundentity = trace_ent;
108 // step - handle it immediately
109 vector org = this.move_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.move_origin_z);
126 _Movetype_PushEntity(this, steppush, true);
133 // accept the new position if it made some progress...
134 if(fabs(this.move_origin_x - org_x) >= 0.03125 || fabs(this.move_origin_y - org_y) >= 0.03125)
136 trace_endpos = this.move_origin;
137 time_left *= 1 - trace2_fraction;
142 this.move_origin = org;
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.move_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.move_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.move_velocity = new_velocity;
199 // go along the crease
202 this.move_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.move_velocity);
215 this.move_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.move_velocity * primal_velocity) <= 0)
222 this.move_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.move_flags & FL_WATERJUMP) && !(blocked & 8))
229 this.move_velocity = primal_velocity;
233 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(this.move_flags & FL_ONGROUND))
235 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
236 this.move_velocity_z -= grav * 0.5f;
243 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
245 // if(vlen(this.move_velocity) < 0.0001)
246 // this.move_velocity = '0 0 0';
249 bool _Movetype_CheckWater(entity this) // SV_CheckWater
251 vector point = this.move_origin;
252 point.z += this.mins.z + 1;
254 int nativecontents = pointcontents(point);
255 if(this.move_watertype && this.move_watertype != nativecontents)
257 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.move_watertype, nativecontents);
258 if(this.contentstransition)
259 this.contentstransition(this.move_watertype, nativecontents);
262 this.move_waterlevel = WATERLEVEL_NONE;
263 this.move_watertype = CONTENT_EMPTY;
265 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
266 if(supercontents & DPCONTENTS_LIQUIDSMASK)
268 this.move_watertype = nativecontents;
269 this.move_waterlevel = WATERLEVEL_WETFEET;
270 point.z = this.move_origin.z + (this.mins.z + this.maxs.z) * 0.5;
271 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
273 this.move_waterlevel = WATERLEVEL_SWIMMING;
274 point.z = this.move_origin.z + this.view_ofs.z;
275 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
276 this.move_waterlevel = WATERLEVEL_SUBMERGED;
280 return this.move_waterlevel > 1;
283 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
285 int contents = pointcontents(ent.move_origin);
287 if(!ent.move_watertype)
290 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
292 ent.move_watertype = contents;
293 ent.move_waterlevel = 1;
297 else if(ent.move_watertype != contents)
299 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents);
300 if(ent.contentstransition)
301 ent.contentstransition(ent.move_watertype, contents);
304 if(contents <= CONTENT_WATER)
306 ent.move_watertype = contents;
307 ent.move_waterlevel = 1;
311 ent.move_watertype = CONTENT_EMPTY;
312 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
316 void _Movetype_Impact(entity this, entity oth) // SV_Impact
318 entity oldother = other;
324 gettouch(this)(this);
339 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
341 if(this.solid == SOLID_NOT)
344 entity oldother = other;
346 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
347 if (it.solid == SOLID_TRIGGER && it != this)
348 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
349 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
353 trace_allsolid = false;
354 trace_startsolid = false;
356 trace_inwater = false;
358 trace_endpos = it.move_origin;
359 trace_plane_normal = '0 0 1';
360 trace_plane_dist = 0;
370 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
373 if(this.solid == SOLID_BSP)
375 // TODO set the absolute bbox
384 mi += this.move_origin;
385 ma += this.move_origin;
387 if(this.move_flags & FL_ITEM)
410 _Movetype_LinkEdict_TouchAreaGrid(this);
413 entity _Movetype_TestEntityPosition_ent;
414 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
416 entity this = _Movetype_TestEntityPosition_ent;
417 // vector org = this.move_origin + ofs;
419 int cont = this.dphitcontentsmask;
420 this.dphitcontentsmask = DPCONTENTS_SOLID;
421 tracebox(this.move_origin, this.mins, this.maxs, this.move_origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
422 this.dphitcontentsmask = cont;
427 if(vdist(trace_endpos - this.move_origin, >, 0.0001))
428 this.move_origin = trace_endpos;
432 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
434 _Movetype_TestEntityPosition_ent = this;
435 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
438 #define X(v) if (_Movetype_TestEntityPosition(v))
439 X('-1 0 0') X(' 1 0 0')
440 X(' 0 -1 0') X(' 0 1 0')
441 X('-1 -1 0') X(' 1 -1 0')
442 X('-1 1 0') X(' 1 1 0')
446 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
447 if (_Movetype_TestEntityPosition('0 0 1' * i))
448 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
449 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
453 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
454 etof(this), this.classname, vtos(this.move_origin));
458 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
459 etof(this), this.classname, vtos(this.move_origin));
460 _Movetype_LinkEdict(this, true);
464 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
466 vel -= ((vel * norm) * norm) * f;
468 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
469 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
470 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
475 void _Movetype_PushEntityTrace(entity this, vector push)
477 vector end = this.move_origin + push;
479 if(this.move_nomonsters)
480 type = max(0, this.move_nomonsters);
481 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
483 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
484 type = MOVE_WORLDONLY;
485 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
486 type = MOVE_NOMONSTERS;
490 tracebox(this.move_origin, this.mins, this.maxs, end, type, this);
493 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
495 _Movetype_PushEntityTrace(this, push);
497 if(trace_startsolid && failonstartsolid)
498 return trace_fraction;
500 this.move_origin = trace_endpos;
502 if(trace_fraction < 1)
503 if(this.solid >= SOLID_TRIGGER && (!(this.move_flags & FL_ONGROUND) || (this.move_groundentity != trace_ent)))
504 _Movetype_Impact(this, trace_ent);
506 return trace_fraction;
512 // matrix version of makevectors, sets v_forward, v_right and v_up
513 void makevectors_matrix(vector myangles) // AngleVectorsFLU
515 v_forward = v_right = v_up = '0 0 0';
517 float y = myangles.y * (M_PI * 2 / 360);
520 float p = myangles.x * (M_PI * 2 / 360);
525 v_forward.x = cp * cy;
526 v_forward.y = cp * sy;
533 float r = myangles.z * (M_PI * 2 / 360);
538 v_right.x = sr * sp * cy + cr * -sy;
539 v_right.y = sr * sp * sy + cr * cy;
544 v_up.x = cr * sp * cy + -sr * -sy;
545 v_up.y = cr * sp * sy + -sr * cy;
567 void _Movetype_Physics_Frame(entity this, float movedt)
569 this.move_didgravity = -1;
570 switch (this.move_movetype)
573 case MOVETYPE_FAKEPUSH:
574 _Movetype_Physics_Pusher(this, movedt);
578 case MOVETYPE_FOLLOW:
579 _Movetype_Physics_Follow(this);
581 case MOVETYPE_NOCLIP:
582 _Movetype_CheckWater(this);
583 this.move_origin = this.move_origin + TICRATE * this.move_velocity;
584 this.move_angles = this.move_angles + TICRATE * this.move_avelocity;
585 _Movetype_LinkEdict(this, false);
588 _Movetype_Physics_Step(this, movedt);
591 _Movetype_Physics_Walk(this, movedt);
594 case MOVETYPE_BOUNCE:
595 case MOVETYPE_BOUNCEMISSILE:
596 case MOVETYPE_FLYMISSILE:
598 case MOVETYPE_FLY_WORLDONLY:
599 _Movetype_Physics_Toss(this, movedt);
600 _Movetype_LinkEdict(this, true);
602 case MOVETYPE_PHYSICS:
607 void _Movetype_Physics_ClientFrame(entity this, float movedt)
609 this.move_didgravity = -1;
610 switch (this.move_movetype)
613 case MOVETYPE_FAKEPUSH:
614 _Movetype_Physics_Pusher(this, movedt);
618 case MOVETYPE_FOLLOW:
619 _Movetype_Physics_Follow(this);
621 case MOVETYPE_NOCLIP:
622 _Movetype_CheckWater(this);
623 this.move_origin = this.move_origin + TICRATE * this.move_velocity;
624 this.move_angles = this.move_angles + TICRATE * this.move_avelocity;
625 _Movetype_LinkEdict(this, false);
628 _Movetype_Physics_Step(this, movedt);
632 case MOVETYPE_FLY_WORLDONLY:
633 _Movetype_Physics_Walk(this, movedt);
636 case MOVETYPE_BOUNCE:
637 case MOVETYPE_BOUNCEMISSILE:
638 case MOVETYPE_FLYMISSILE:
639 _Movetype_Physics_Toss(this, movedt);
641 case MOVETYPE_PHYSICS:
646 void Movetype_Physics_NoMatchServer(entity this) // optimized
648 float movedt = time - this.move_time;
649 this.move_time = time;
651 _Movetype_Physics_Frame(this, movedt);
655 this.avelocity = this.move_avelocity;
656 this.velocity = this.move_velocity;
657 this.angles = this.move_angles;
658 setorigin(this, this.move_origin);
661 void Movetype_Physics_MatchServer(entity this, bool sloppy)
663 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
666 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
670 Movetype_Physics_NoMatchServer(this);
674 float dt = time - this.move_time;
676 int n = max(0, floor(dt / tr));
678 this.move_time += n * tr;
680 if(!this.move_didgravity)
681 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.move_flags & FL_ONGROUND));
683 for (int i = 0; i < n; ++i)
685 _Movetype_Physics_Frame(this, tr);
690 this.avelocity = this.move_avelocity;
692 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.move_flags & FL_ONGROUND))
694 // now continue the move from move_time to time
695 this.velocity = this.move_velocity;
697 if(this.move_didgravity > 0)
699 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
701 * (this.gravity ? this.gravity : 1)
702 * PHYS_GRAVITY(this);
705 this.angles = this.move_angles + dt * this.avelocity;
707 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
709 setorigin(this, this.move_origin + dt * this.velocity);
713 _Movetype_PushEntityTrace(this, dt * this.velocity);
714 if(!trace_startsolid)
715 setorigin(this, trace_endpos);
718 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
719 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
723 this.velocity = this.move_velocity;
724 this.angles = this.move_angles;
725 setorigin(this, this.move_origin);