2 #include "../physics.qh"
5 #include "../../client/defs.qh"
8 #include "movetypes.qh"
9 #include "../../csqcmodellib/common.qh"
10 #include "../../server/t_items.qh"
13 #include "../../server/autocvars.qh"
16 void _Movetype_WallFriction(vector stepnormal) // SV_WallFriction
20 makevectors(self.v_angle);
21 d = (stepnormal * v_forward) + 0.5;
25 i = (stepnormal * self.move_velocity);
26 into = i * stepnormal;
27 side = self.move_velocity - into;
28 self.move_velocity_x = side.x * (1 * d);
29 self.move_velocity_y = side.y * (1 * d);
33 vector planes[MAX_CLIP_PLANES];
34 int _Movetype_FlyMove(float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
36 int blocked = 0, bumpcount;
37 int i, j, numplanes = 0;
38 float time_left = dt, grav = 0;
40 vector primal_velocity, original_velocity, restore_velocity;
42 for(i = 0; i < MAX_CLIP_PLANES; ++i)
47 self.move_didgravity = 1;
48 grav = dt * (PHYS_ENTGRAVITY(self) ? PHYS_ENTGRAVITY(self) : 1) * PHYS_GRAVITY;
50 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(self.move_flags & FL_ONGROUND))
52 if(GRAVITY_UNAFFECTED_BY_TICRATE)
53 self.move_velocity_z -= grav * 0.5;
55 self.move_velocity_z -= grav;
59 original_velocity = primal_velocity = restore_velocity = self.move_velocity;
61 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
63 if(self.move_velocity == '0 0 0')
66 push = self.move_velocity * time_left;
67 vector prev_origin = self.move_origin;
68 _Movetype_PushEntity(push, true);
69 if(trace_startsolid && self.move_origin != prev_origin)
71 // we got teleported by a touch function
72 // let's abort the move
77 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
78 // abort move if we're stuck in the world (and didn't make it out)
79 if(trace_startsolid && trace_allsolid)
81 self.move_velocity = restore_velocity;
85 if(trace_fraction == 1)
88 float my_trace_fraction = trace_fraction;
89 vector my_trace_plane_normal = trace_plane_normal;
91 if(trace_plane_normal_z)
93 if(trace_plane_normal_z > 0.7)
100 //dprint("_Movetype_FlyMove: !trace_ent\n");
104 self.move_flags |= FL_ONGROUND;
105 self.move_groundentity = trace_ent;
110 // step - handle it immediately
111 vector org = self.move_origin;
112 vector steppush = '0 0 1' * stepheight;
114 _Movetype_PushEntity(steppush, true);
115 if(trace_startsolid && self.move_origin != org)
120 _Movetype_PushEntity(push, true);
121 if(trace_startsolid && self.move_origin != org)
126 float trace2_fraction = trace_fraction;
127 steppush = '0 0 1' * (org_z - self.move_origin_z);
128 _Movetype_PushEntity(steppush, true);
129 if(trace_startsolid && self.move_origin != org)
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 trace_endpos = self.move_origin;
139 time_left *= 1 - trace2_fraction;
144 self.move_origin = org;
148 // step - return it to caller
150 // save the trace for player extrafriction
152 stepnormal = trace_plane_normal;
155 if(my_trace_fraction >= 0.001)
157 // actually covered some distance
158 original_velocity = self.move_velocity;
162 time_left *= 1 - my_trace_fraction;
164 // clipped to another plane
165 if(numplanes >= MAX_CLIP_PLANES)
167 // this shouldn't really happen
168 self.move_velocity = '0 0 0';
173 planes[numplanes] = my_trace_plane_normal;
176 // modify original_velocity so it parallels all of the clip planes
177 vector new_velocity = '0 0 0';
178 for (i = 0;i < numplanes;i++)
180 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
181 for (j = 0;j < numplanes;j++)
186 if((new_velocity * planes[j]) < 0)
196 // go along this plane
197 self.move_velocity = new_velocity;
201 // go along the crease
204 self.move_velocity = '0 0 0';
208 vector dir = cross(planes[0], planes[1]);
209 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
210 float ilength = sqrt((dir * dir));
212 ilength = 1.0 / ilength;
216 float d = (dir * self.move_velocity);
217 self.move_velocity = dir * d;
220 // if current velocity is against the original velocity,
221 // stop dead to avoid tiny occilations in sloping corners
222 if((self.move_velocity * primal_velocity) <= 0)
224 self.move_velocity = '0 0 0';
229 // LordHavoc: this came from QW and allows you to get out of water more easily
230 if(GAMEPLAYFIX_EASIERWATERJUMP && (self.move_flags & FL_WATERJUMP) && !(blocked & 8))
231 self.move_velocity = primal_velocity;
235 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(self.move_flags & FL_ONGROUND))
237 if(GRAVITY_UNAFFECTED_BY_TICRATE)
238 self.move_velocity_z -= grav * 0.5f;
245 void _Movetype_CheckVelocity() // SV_CheckVelocity
247 // if(vlen(self.move_velocity) < 0.0001)
248 // self.move_velocity = '0 0 0';
251 bool _Movetype_CheckWater(entity ent) // SV_CheckWater
253 vector point = ent.move_origin;
254 point.z += (ent.mins.z + 1);
256 int nativecontents = pointcontents(point);
257 if(ent.move_watertype && ent.move_watertype != nativecontents)
259 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents);
260 if(ent.contentstransition)
261 ent.contentstransition(ent.move_watertype, nativecontents);
264 ent.move_waterlevel = 0;
265 ent.move_watertype = CONTENT_EMPTY;
267 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
268 if(supercontents & DPCONTENTS_LIQUIDSMASK)
270 ent.move_watertype = nativecontents;
271 ent.move_waterlevel = 1;
272 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
273 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
275 ent.move_waterlevel = 2;
276 point.y = ent.origin.y + ent.view_ofs.y;
277 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
278 ent.move_waterlevel = 3;
282 return ent.move_waterlevel > 1;
285 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
287 int contents = pointcontents(ent.move_origin);
289 if(!ent.move_watertype)
292 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
294 ent.move_watertype = contents;
295 ent.move_waterlevel = 1;
299 else if(ent.move_watertype != contents)
301 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents);
302 if(ent.contentstransition)
303 ent.contentstransition(ent.move_watertype, contents);
306 if(contents <= CONTENT_WATER)
308 ent.move_watertype = contents;
309 ent.move_waterlevel = 1;
313 ent.move_watertype = CONTENT_EMPTY;
314 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
318 void _Movetype_Impact(entity oth) // SV_Impact
320 entity oldother = other;
335 WITH(entity, self, oth, oth.move_touch());
341 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
343 entity oldother = other;
345 for (entity e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
347 if(e.move_touch && boxesoverlap(e.absmin, e.absmax, this.absmin, this.absmax))
352 trace_allsolid = false;
353 trace_startsolid = false;
355 trace_inwater = false;
357 trace_endpos = e.origin;
358 trace_plane_normal = '0 0 1';
359 trace_plane_dist = 0;
370 void _Movetype_LinkEdict(bool touch_triggers) // SV_LinkEdict
373 if(self.solid == SOLID_BSP)
375 // TODO set the absolute bbox
384 mi += self.move_origin;
385 ma += self.move_origin;
387 if(self.move_flags & FL_ITEM)
410 _Movetype_LinkEdict_TouchAreaGrid();
413 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
415 // vector org = self.move_origin + ofs;
417 int cont = self.dphitcontentsmask;
418 self.dphitcontentsmask = DPCONTENTS_SOLID;
419 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
420 self.dphitcontentsmask = cont;
425 if(vlen(trace_endpos - self.move_origin) > 0.0001)
426 self.move_origin = trace_endpos;
430 bool _Movetype_UnstickEntity() // SV_UnstickEntity
432 if(!_Movetype_TestEntityPosition('0 0 0')) return true;
433 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
434 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
435 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
436 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
437 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
438 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
439 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
440 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
441 for (int i = 1; i <= 17; ++i)
443 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
444 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
446 LOG_TRACEF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
447 num_for_edict(self), self.classname, vtos(self.move_origin));
450 LOG_TRACEF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
451 num_for_edict(self), self.classname, vtos(self.move_origin));
452 _Movetype_LinkEdict(true);
456 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
458 vel -= ((vel * norm) * norm) * f;
460 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
461 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
462 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
467 void _Movetype_PushEntityTrace(vector push)
469 vector end = self.move_origin + push;
471 if(self.move_nomonsters)
472 type = max(0, self.move_nomonsters);
473 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
475 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
476 type = MOVE_NOMONSTERS;
480 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
483 float _Movetype_PushEntity(vector push, bool failonstartsolid) // SV_PushEntity
485 _Movetype_PushEntityTrace(push);
487 if(trace_startsolid && failonstartsolid)
488 return trace_fraction;
490 self.move_origin = trace_endpos;
492 if(trace_fraction < 1)
493 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
494 _Movetype_Impact(trace_ent);
496 return trace_fraction;
502 // matrix version of makevectors, sets v_forward, v_right and v_up
503 void makevectors_matrix(vector myangles) // AngleVectorsFLU
505 v_forward = v_right = v_up = '0 0 0';
507 float y = myangles.y * (M_PI * 2 / 360);
510 float p = myangles.x * (M_PI * 2 / 360);
515 v_forward.x = cp * cy;
516 v_forward.y = cp * sy;
523 float r = myangles.z * (M_PI * 2 / 360);
528 v_right.x = sr * sp * cy + cr * -sy;
529 v_right.y = sr * sp * sy + cr * cy;
534 v_up.x = cr * sp * cy + -sr * -sy;
535 v_up.y = cr * sp * sy + -sr * cy;
557 void _Movetype_Physics_Frame(float movedt)
559 self.move_didgravity = -1;
560 switch (self.move_movetype)
563 case MOVETYPE_FAKEPUSH:
564 _Movetype_Physics_Pusher(movedt);
568 case MOVETYPE_FOLLOW:
569 _Movetype_Physics_Follow();
571 case MOVETYPE_NOCLIP:
572 _Movetype_CheckWater(self);
573 self.move_origin = self.move_origin + TICRATE * self.move_velocity;
574 self.move_angles = self.move_angles + TICRATE * self.move_avelocity;
575 _Movetype_LinkEdict(false);
578 _Movetype_Physics_Step(movedt);
581 _Movetype_Physics_Walk(movedt);
584 case MOVETYPE_BOUNCE:
585 case MOVETYPE_BOUNCEMISSILE:
586 case MOVETYPE_FLYMISSILE:
588 _Movetype_Physics_Toss(movedt);
593 void Movetype_Physics_NoMatchServer() // optimized
595 float movedt = time - self.move_time;
596 self.move_time = time;
598 _Movetype_Physics_Frame(movedt);
602 self.avelocity = self.move_avelocity;
603 self.velocity = self.move_velocity;
604 self.angles = self.move_angles;
605 setorigin(self, self.move_origin);
608 void Movetype_Physics_MatchServer(bool sloppy)
610 Movetype_Physics_MatchTicrate(TICRATE, sloppy);
613 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
617 Movetype_Physics_NoMatchServer();
621 float dt = time - self.move_time;
623 int n = max(0, floor(dt / tr));
625 self.move_time += n * tr;
627 if(!self.move_didgravity)
628 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
630 for (int i = 0; i < n; ++i)
632 _Movetype_Physics_Frame(tr);
637 self.avelocity = self.move_avelocity;
639 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
641 // now continue the move from move_time to time
642 self.velocity = self.move_velocity;
644 if(self.move_didgravity > 0)
646 self.velocity_z -= (GRAVITY_UNAFFECTED_BY_TICRATE ? 0.5 : 1)
648 * (self.gravity ? self.gravity : 1)
652 self.angles = self.move_angles + dt * self.avelocity;
654 if(sloppy || self.move_movetype == MOVETYPE_NOCLIP)
656 setorigin(self, self.move_origin + dt * self.velocity);
660 _Movetype_PushEntityTrace(dt * self.velocity);
661 if(!trace_startsolid)
662 setorigin(self, trace_endpos);
665 if(self.move_didgravity > 0 && GRAVITY_UNAFFECTED_BY_TICRATE)
666 self.velocity_z -= 0.5 * dt * (self.gravity ? self.gravity : 1) * PHYS_GRAVITY;
670 self.velocity = self.move_velocity;
671 self.angles = self.move_angles;
672 setorigin(self, self.move_origin);