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 oldself = self;
326 entity oldother = other;
349 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
351 entity oldself = self;
352 entity oldother = other;
354 for (entity e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
356 if(e.move_touch && boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
361 trace_allsolid = false;
362 trace_startsolid = false;
364 trace_inwater = false;
366 trace_endpos = e.origin;
367 trace_plane_normal = '0 0 1';
368 trace_plane_dist = 0;
379 void _Movetype_LinkEdict(bool touch_triggers) // SV_LinkEdict
382 if(self.solid == SOLID_BSP)
384 // TODO set the absolute bbox
393 mi += self.move_origin;
394 ma += self.move_origin;
396 if(self.move_flags & FL_ITEM)
419 _Movetype_LinkEdict_TouchAreaGrid();
422 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
424 // vector org = self.move_origin + ofs;
426 int cont = self.dphitcontentsmask;
427 self.dphitcontentsmask = DPCONTENTS_SOLID;
428 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
429 self.dphitcontentsmask = cont;
434 if(vlen(trace_endpos - self.move_origin) > 0.0001)
435 self.move_origin = trace_endpos;
439 bool _Movetype_UnstickEntity() // SV_UnstickEntity
441 if(!_Movetype_TestEntityPosition('0 0 0')) return true;
442 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
443 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
444 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
445 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
446 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
447 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
448 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
449 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
450 for (int i = 1; i <= 17; ++i)
452 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
453 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
455 LOG_TRACEF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
456 num_for_edict(self), self.classname, vtos(self.move_origin));
459 LOG_TRACEF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
460 num_for_edict(self), self.classname, vtos(self.move_origin));
461 _Movetype_LinkEdict(true);
465 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
467 vel -= ((vel * norm) * norm) * f;
469 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
470 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
471 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
476 void _Movetype_PushEntityTrace(vector push)
478 vector end = self.move_origin + push;
480 if(self.move_nomonsters)
481 type = max(0, self.move_nomonsters);
482 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
484 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
485 type = MOVE_NOMONSTERS;
489 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
492 float _Movetype_PushEntity(vector push, bool failonstartsolid) // SV_PushEntity
494 _Movetype_PushEntityTrace(push);
496 if(trace_startsolid && failonstartsolid)
497 return trace_fraction;
499 self.move_origin = trace_endpos;
501 if(trace_fraction < 1)
502 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
503 _Movetype_Impact(trace_ent);
505 return trace_fraction;
511 // matrix version of makevectors, sets v_forward, v_right and v_up
512 void makevectors_matrix(vector myangles) // AngleVectorsFLU
514 v_forward = v_right = v_up = '0 0 0';
516 float y = myangles.y * (M_PI * 2 / 360);
519 float p = myangles.x * (M_PI * 2 / 360);
524 v_forward.x = cp * cy;
525 v_forward.y = cp * sy;
532 float r = myangles.z * (M_PI * 2 / 360);
537 v_right.x = sr * sp * cy + cr * -sy;
538 v_right.y = sr * sp * sy + cr * cy;
543 v_up.x = cr * sp * cy + -sr * -sy;
544 v_up.y = cr * sp * sy + -sr * cy;
566 void _Movetype_Physics_Frame(float movedt)
568 self.move_didgravity = -1;
569 switch (self.move_movetype)
572 case MOVETYPE_FAKEPUSH:
573 _Movetype_Physics_Pusher(movedt);
577 case MOVETYPE_FOLLOW:
578 _Movetype_Physics_Follow();
580 case MOVETYPE_NOCLIP:
581 _Movetype_CheckWater(self);
582 self.move_origin = self.move_origin + TICRATE * self.move_velocity;
583 self.move_angles = self.move_angles + TICRATE * self.move_avelocity;
584 _Movetype_LinkEdict(false);
587 _Movetype_Physics_Step(movedt);
590 _Movetype_Physics_Walk(movedt);
593 case MOVETYPE_BOUNCE:
594 case MOVETYPE_BOUNCEMISSILE:
595 case MOVETYPE_FLYMISSILE:
597 _Movetype_Physics_Toss(movedt);
602 void Movetype_Physics_NoMatchServer() // optimized
604 float movedt = time - self.move_time;
605 self.move_time = time;
607 _Movetype_Physics_Frame(movedt);
611 self.avelocity = self.move_avelocity;
612 self.velocity = self.move_velocity;
613 self.angles = self.move_angles;
614 setorigin(self, self.move_origin);
617 void Movetype_Physics_MatchServer(bool sloppy)
619 Movetype_Physics_MatchTicrate(TICRATE, sloppy);
622 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
626 Movetype_Physics_NoMatchServer();
630 float dt = time - self.move_time;
632 int n = max(0, floor(dt / tr));
634 self.move_time += n * tr;
636 if(!self.move_didgravity)
637 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
639 for (int i = 0; i < n; ++i)
641 _Movetype_Physics_Frame(tr);
646 self.avelocity = self.move_avelocity;
648 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
650 // now continue the move from move_time to time
651 self.velocity = self.move_velocity;
653 if(self.move_didgravity > 0)
655 self.velocity_z -= (GRAVITY_UNAFFECTED_BY_TICRATE ? 0.5 : 1)
657 * (self.gravity ? self.gravity : 1)
661 self.angles = self.move_angles + dt * self.avelocity;
663 if(sloppy || self.move_movetype == MOVETYPE_NOCLIP)
665 setorigin(self, self.move_origin + dt * self.velocity);
669 _Movetype_PushEntityTrace(dt * self.velocity);
670 if(!trace_startsolid)
671 setorigin(self, trace_endpos);
674 if(self.move_didgravity > 0 && GRAVITY_UNAFFECTED_BY_TICRATE)
675 self.velocity_z -= 0.5 * dt * (self.gravity ? self.gravity : 1) * PHYS_GRAVITY;
679 self.velocity = self.move_velocity;
680 self.angles = self.move_angles;
681 setorigin(self, self.move_origin);