2 #include "../physics.qh"
5 #include "../../dpdefs/csprogsdefs.qh"
6 #include "../../client/defs.qh"
9 #include "movetypes.qh"
10 #include "../../csqcmodellib/common.qh"
11 #include "../../server/t_items.qh"
14 #include "../../server/autocvars.qh"
17 void _Movetype_WallFriction(vector stepnormal) // SV_WallFriction
21 makevectors(self.v_angle);
22 d = (stepnormal * v_forward) + 0.5;
26 i = (stepnormal * self.move_velocity);
27 into = i * stepnormal;
28 side = self.move_velocity - into;
29 self.move_velocity_x = side.x * (1 * d);
30 self.move_velocity_y = side.y * (1 * d);
34 vector planes[MAX_CLIP_PLANES];
35 int _Movetype_FlyMove(float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
37 int blocked = 0, bumpcount;
38 int i, j, numplanes = 0;
39 float time_left = dt, grav = 0;
41 vector primal_velocity, original_velocity, new_velocity = '0 0 0', restore_velocity;
43 for(i = 0; i <= MAX_CLIP_PLANES; ++i)
48 restore_velocity = self.move_velocity;
52 self.move_didgravity = 1;
53 grav = dt * (PHYS_ENTGRAVITY(self) ? PHYS_ENTGRAVITY(self) : 1) * PHYS_GRAVITY;
55 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(self.move_flags & FL_ONGROUND))
57 if(GRAVITY_UNAFFECTED_BY_TICRATE)
58 self.move_velocity_z -= grav * 0.5;
60 self.move_velocity_z -= grav;
64 original_velocity = primal_velocity = self.move_velocity;
66 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
68 if(!self.move_velocity_x && !self.move_velocity_y && !self.move_velocity_z)
71 push = self.move_velocity * time_left;
72 if(!_Movetype_PushEntity(push, false))
74 // we got teleported by a touch function
75 // let's abort the move
80 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
81 // abort move if we're stuck in the world (and didn't make it out)
82 if(trace_startsolid && trace_allsolid)
84 self.move_velocity = restore_velocity;
88 if(trace_fraction == 1)
90 if(trace_plane_normal_z)
92 if(trace_plane_normal_z > 0.7)
99 //dprint("_Movetype_FlyMove: !trace_ent\n");
103 self.move_flags |= FL_ONGROUND;
104 self.move_groundentity = trace_ent;
109 // step - handle it immediately
112 //Con_Printf("step %f %f %f : ", self.move_origin_x, PRVM_serveredictvector(ent, origin)[1], PRVM_serveredictvector(ent, origin)[2]);
113 steppush = '0 0 1' * stepheight;
114 org = self.move_origin;
115 if(!_Movetype_PushEntity(steppush, false))
120 //Con_Printf("%f %f %f : ", self.move_origin_x, PRVM_serveredictvector(ent, origin)[1], PRVM_serveredictvector(ent, origin)[2]);
121 if(!_Movetype_PushEntity(push, false))
126 float trace2_fraction = trace_fraction;
127 //Con_Printf("%f %f %f : ", self.move_origin_x, PRVM_serveredictvector(ent, origin)[1], PRVM_serveredictvector(ent, origin)[2]);
128 steppush = '0 0 1' * (org_z - self.move_origin_z);
129 if(!_Movetype_PushEntity(steppush, false))
134 //Con_Printf("%f %f %f : ", self.move_origin_x, PRVM_serveredictvector(ent, origin)[1], PRVM_serveredictvector(ent, origin)[2]);
135 // accept the new position if it made some progress...
136 if(fabs(self.move_origin_x - org_x) >= 0.03125 || fabs(self.move_origin_y - org_y) >= 0.03125)
138 //Con_Printf("accepted (delta %f %f %f)\n", self.move_origin_x - org_x, PRVM_serveredictvector(ent, origin)[1] - org[1], PRVM_serveredictvector(ent, origin)[2] - org[2]);
139 trace_endpos = self.move_origin;
140 time_left *= 1 - trace2_fraction;
146 //Con_Printf("REJECTED (delta %f %f %f)\n", self.move_origin_x - org_x, PRVM_serveredictvector(ent, origin)[1] - org[1], PRVM_serveredictvector(ent, origin)[2] - org[2]);
147 self.move_origin = org;
152 // step - return it to caller
154 // save the trace for player extrafriction
156 stepnormal = trace_plane_normal;
158 if(trace_fraction >= 0.001)
160 // actually covered some distance
161 original_velocity = self.move_velocity;
165 time_left *= 1 - trace_fraction;
167 // clipped to another plane
168 if(numplanes >= MAX_CLIP_PLANES)
170 // this shouldn't really happen
171 self.move_velocity = '0 0 0';
176 planes[numplanes] = trace_plane_normal;
179 // modify original_velocity so it parallels all of the clip planes
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 self.move_velocity = new_velocity;
203 // go along the crease
206 self.move_velocity = '0 0 0';
211 dir.x = planes[0].y * planes[1].z - planes[0].z * planes[1].y;
212 dir.y = planes[0].z * planes[1].x - planes[0].x * planes[1].z;
213 dir.z = planes[0].x * planes[1].y - planes[0].y * planes[1].x;
214 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
215 float ilength = sqrt((dir * dir));
217 ilength = 1.0 / ilength;
221 float d = (dir * self.move_velocity);
222 self.move_velocity = dir * d;
225 // if current velocity is against the original velocity,
226 // stop dead to avoid tiny occilations in sloping corners
227 if((self.move_velocity * primal_velocity) <= 0)
229 self.move_velocity = '0 0 0';
234 // LordHavoc: this came from QW and allows you to get out of water more easily
235 if(GAMEPLAYFIX_EASIERWATERJUMP && (self.move_flags & FL_WATERJUMP) && !(blocked & 8))
236 self.move_velocity = primal_velocity;
240 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(self.move_flags & FL_ONGROUND))
242 if(GRAVITY_UNAFFECTED_BY_TICRATE)
243 self.move_velocity_z -= grav * 0.5f;
250 void _Movetype_CheckVelocity() // SV_CheckVelocity
252 // if(vlen(self.move_velocity) < 0.0001)
253 // self.move_velocity = '0 0 0';
256 bool _Movetype_CheckWater(entity ent) // SV_CheckWater
258 vector point = ent.move_origin;
259 point.z += (ent.mins.z + 1);
261 int nativecontents = pointcontents(point);
262 if(ent.move_watertype && ent.move_watertype != nativecontents)
264 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents);
265 if(ent.contentstransition)
266 ent.contentstransition(ent.move_watertype, nativecontents);
269 ent.move_waterlevel = 0;
270 ent.move_watertype = CONTENT_EMPTY;
272 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
273 if(supercontents & DPCONTENTS_LIQUIDSMASK)
275 ent.move_watertype = nativecontents;
276 ent.move_waterlevel = 1;
277 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
278 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
280 ent.move_waterlevel = 2;
281 point.y = ent.origin.y + ent.view_ofs.y;
282 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
283 ent.move_waterlevel = 3;
287 return ent.move_waterlevel > 1;
290 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
292 int contents = pointcontents(ent.move_origin);
294 if(!ent.move_watertype)
297 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
299 ent.move_watertype = contents;
300 ent.move_waterlevel = 1;
304 else if(ent.move_watertype != contents)
306 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents);
307 if(ent.contentstransition)
308 ent.contentstransition(ent.move_watertype, contents);
311 if(contents <= CONTENT_WATER)
313 ent.move_watertype = contents;
314 ent.move_waterlevel = 1;
318 ent.move_watertype = CONTENT_EMPTY;
319 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
323 void _Movetype_Impact(entity oth) // SV_Impact
325 entity oldother = other;
340 WITH(entity, self, oth, oth.move_touch());
346 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
348 entity oldother = other;
350 for (entity e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
352 if(e.move_touch && boxesoverlap(e.absmin, e.absmax, this.absmin, this.absmax))
357 trace_allsolid = false;
358 trace_startsolid = false;
360 trace_inwater = false;
362 trace_endpos = e.origin;
363 trace_plane_normal = '0 0 1';
364 trace_plane_dist = 0;
375 void _Movetype_LinkEdict(bool touch_triggers) // SV_LinkEdict
378 if(self.solid == SOLID_BSP)
380 // TODO set the absolute bbox
389 mi += self.move_origin;
390 ma += self.move_origin;
392 if(self.move_flags & FL_ITEM)
415 _Movetype_LinkEdict_TouchAreaGrid();
418 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
420 // vector org = self.move_origin + ofs;
422 int cont = self.dphitcontentsmask;
423 self.dphitcontentsmask = DPCONTENTS_SOLID;
424 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
425 self.dphitcontentsmask = cont;
430 if(vlen(trace_endpos - self.move_origin) > 0.0001)
431 self.move_origin = trace_endpos;
435 bool _Movetype_UnstickEntity() // SV_UnstickEntity
437 if(!_Movetype_TestEntityPosition('0 0 0')) return true;
438 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
439 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
440 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
441 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
442 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
443 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
444 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
445 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
446 for (int i = 1; i <= 17; ++i)
448 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
449 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
451 LOG_TRACEF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
452 num_for_edict(self), self.classname, vtos(self.move_origin));
455 LOG_TRACEF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
456 num_for_edict(self), self.classname, vtos(self.move_origin));
457 _Movetype_LinkEdict(true);
461 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
463 vel -= ((vel * norm) * norm) * f;
465 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
466 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
467 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
472 void _Movetype_PushEntityTrace(vector push)
474 vector end = self.move_origin + push;
476 if(self.move_nomonsters)
477 type = max(0, self.move_nomonsters);
478 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
480 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
481 type = MOVE_NOMONSTERS;
485 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
488 float _Movetype_PushEntity(vector push, bool failonstartsolid) // SV_PushEntity
490 _Movetype_PushEntityTrace(push);
492 if(trace_startsolid && failonstartsolid)
493 return trace_fraction;
495 self.move_origin = trace_endpos;
497 if(trace_fraction < 1)
498 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
499 _Movetype_Impact(trace_ent);
501 return trace_fraction;
507 // matrix version of makevectors, sets v_forward, v_right and v_up
508 void makevectors_matrix(vector myangles) // AngleVectorsFLU
510 v_forward = v_right = v_up = '0 0 0';
512 float y = myangles.y * (M_PI * 2 / 360);
515 float p = myangles.x * (M_PI * 2 / 360);
520 v_forward.x = cp * cy;
521 v_forward.y = cp * sy;
528 float r = myangles.z * (M_PI * 2 / 360);
533 v_right.x = sr * sp * cy + cr * -sy;
534 v_right.y = sr * sp * sy + cr * cy;
539 v_up.x = cr * sp * cy + -sr * -sy;
540 v_up.y = cr * sp * sy + -sr * cy;
562 void _Movetype_Physics_Frame(float movedt)
564 self.move_didgravity = -1;
565 switch (self.move_movetype)
568 case MOVETYPE_FAKEPUSH:
569 _Movetype_Physics_Pusher(movedt);
573 case MOVETYPE_FOLLOW:
574 _Movetype_Physics_Follow();
576 case MOVETYPE_NOCLIP:
577 _Movetype_CheckWater(self);
578 self.move_origin = self.move_origin + TICRATE * self.move_velocity;
579 self.move_angles = self.move_angles + TICRATE * self.move_avelocity;
580 _Movetype_LinkEdict(false);
583 _Movetype_Physics_Step(movedt);
586 _Movetype_Physics_Walk(movedt);
589 case MOVETYPE_BOUNCE:
590 case MOVETYPE_BOUNCEMISSILE:
591 case MOVETYPE_FLYMISSILE:
593 _Movetype_Physics_Toss(movedt);
598 void Movetype_Physics_NoMatchServer() // optimized
600 float movedt = time - self.move_time;
601 self.move_time = time;
603 _Movetype_Physics_Frame(movedt);
607 self.avelocity = self.move_avelocity;
608 self.velocity = self.move_velocity;
609 self.angles = self.move_angles;
610 setorigin(self, self.move_origin);
613 void Movetype_Physics_MatchServer(bool sloppy)
615 Movetype_Physics_MatchTicrate(TICRATE, sloppy);
618 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
622 Movetype_Physics_NoMatchServer();
626 float dt = time - self.move_time;
628 int n = max(0, floor(dt / tr));
630 self.move_time += n * tr;
632 if(!self.move_didgravity)
633 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
635 for (int i = 0; i < n; ++i)
637 _Movetype_Physics_Frame(tr);
642 self.avelocity = self.move_avelocity;
644 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
646 // now continue the move from move_time to time
647 self.velocity = self.move_velocity;
649 if(self.move_didgravity > 0)
651 self.velocity_z -= (GRAVITY_UNAFFECTED_BY_TICRATE ? 0.5 : 1)
653 * (self.gravity ? self.gravity : 1)
657 self.angles = self.move_angles + dt * self.avelocity;
659 if(sloppy || self.move_movetype == MOVETYPE_NOCLIP)
661 setorigin(self, self.move_origin + dt * self.velocity);
665 _Movetype_PushEntityTrace(dt * self.velocity);
666 if(!trace_startsolid)
667 setorigin(self, trace_endpos);
670 if(self.move_didgravity > 0 && GRAVITY_UNAFFECTED_BY_TICRATE)
671 self.velocity_z -= 0.5 * dt * (self.gravity ? self.gravity : 1) * PHYS_GRAVITY;
675 self.velocity = self.move_velocity;
676 self.angles = self.move_angles;
677 setorigin(self, self.move_origin);