2 #include "../physics.qh"
5 #include "../../client/defs.qh"
8 #include "movetypes.qh"
9 #include "../../lib/csqcmodel/common.qh"
10 #include "../../server/t_items.qh"
13 #include "../../server/autocvars.qh"
16 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
20 makevectors(this.v_angle);
21 d = (stepnormal * v_forward) + 0.5;
25 i = (stepnormal * this.move_velocity);
26 into = i * stepnormal;
27 side = this.move_velocity - into;
28 this.move_velocity_x = side.x * (1 * d);
29 this.move_velocity_y = side.y * (1 * d);
33 vector planes[MAX_CLIP_PLANES];
34 int _Movetype_FlyMove(entity this, 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 this.move_didgravity = 1;
48 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
50 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(this.move_flags & FL_ONGROUND))
52 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
53 this.move_velocity_z -= grav * 0.5;
55 this.move_velocity_z -= grav;
59 original_velocity = primal_velocity = restore_velocity = this.move_velocity;
61 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
63 if(this.move_velocity == '0 0 0')
66 push = this.move_velocity * time_left;
67 _Movetype_PushEntity(this, push, true);
70 // we got teleported by a touch function
71 // let's abort the move
76 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
77 // abort move if we're stuck in the world (and didn't make it out)
78 if(trace_startsolid && trace_allsolid)
80 this.move_velocity = restore_velocity;
84 if(trace_fraction == 1)
87 float my_trace_fraction = trace_fraction;
88 vector my_trace_plane_normal = trace_plane_normal;
90 if(trace_plane_normal.z)
92 if(trace_plane_normal.z > 0.7)
99 //dprint("_Movetype_FlyMove: !trace_ent\n");
103 this.move_flags |= FL_ONGROUND;
104 this.move_groundentity = trace_ent;
109 // step - handle it immediately
110 vector org = this.move_origin;
111 vector steppush = '0 0 1' * stepheight;
113 _Movetype_PushEntity(this, steppush, true);
119 _Movetype_PushEntity(this, push, true);
125 float trace2_fraction = trace_fraction;
126 steppush = '0 0 1' * (org_z - this.move_origin_z);
127 _Movetype_PushEntity(this, steppush, true);
134 // accept the new position if it made some progress...
135 if(fabs(this.move_origin_x - org_x) >= 0.03125 || fabs(this.move_origin_y - org_y) >= 0.03125)
137 trace_endpos = this.move_origin;
138 time_left *= 1 - trace2_fraction;
143 this.move_origin = org;
147 // step - return it to caller
149 // save the trace for player extrafriction
151 stepnormal = trace_plane_normal;
154 if(my_trace_fraction >= 0.001)
156 // actually covered some distance
157 original_velocity = this.move_velocity;
161 time_left *= 1 - my_trace_fraction;
163 // clipped to another plane
164 if(numplanes >= MAX_CLIP_PLANES)
166 // this shouldn't really happen
167 this.move_velocity = '0 0 0';
172 planes[numplanes] = my_trace_plane_normal;
175 // modify original_velocity so it parallels all of the clip planes
176 vector new_velocity = '0 0 0';
177 for (i = 0;i < numplanes;i++)
179 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
180 for (j = 0;j < numplanes;j++)
185 if((new_velocity * planes[j]) < 0)
195 // go along this plane
196 this.move_velocity = new_velocity;
200 // go along the crease
203 this.move_velocity = '0 0 0';
207 vector dir = cross(planes[0], planes[1]);
208 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
209 float ilength = sqrt((dir * dir));
211 ilength = 1.0 / ilength;
215 float d = (dir * this.move_velocity);
216 this.move_velocity = dir * d;
219 // if current velocity is against the original velocity,
220 // stop dead to avoid tiny occilations in sloping corners
221 if((this.move_velocity * primal_velocity) <= 0)
223 this.move_velocity = '0 0 0';
228 // LordHavoc: this came from QW and allows you to get out of water more easily
229 if(GAMEPLAYFIX_EASIERWATERJUMP && (this.move_flags & FL_WATERJUMP) && !(blocked & 8))
230 this.move_velocity = primal_velocity;
234 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !(this.move_flags & FL_ONGROUND))
236 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
237 this.move_velocity_z -= grav * 0.5f;
244 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
246 // if(vlen(this.move_velocity) < 0.0001)
247 // this.move_velocity = '0 0 0';
250 bool _Movetype_CheckWater(entity this) // SV_CheckWater
252 vector point = this.move_origin;
253 point.z += this.mins.z + 1;
255 int nativecontents = pointcontents(point);
256 if(this.move_watertype && this.move_watertype != nativecontents)
258 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.move_watertype, nativecontents);
259 if(this.contentstransition)
260 this.contentstransition(this.move_watertype, nativecontents);
263 this.move_waterlevel = WATERLEVEL_NONE;
264 this.move_watertype = CONTENT_EMPTY;
266 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
267 if(supercontents & DPCONTENTS_LIQUIDSMASK)
269 this.move_watertype = nativecontents;
270 this.move_waterlevel = WATERLEVEL_WETFEET;
271 point.z = this.move_origin.z + (this.mins.z + this.maxs.z) * 0.5;
272 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
274 this.move_waterlevel = WATERLEVEL_SWIMMING;
275 point.z = this.move_origin.z + this.view_ofs.z;
276 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
277 this.move_waterlevel = WATERLEVEL_SUBMERGED;
281 return this.move_waterlevel > 1;
284 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
286 int contents = pointcontents(ent.move_origin);
288 if(!ent.move_watertype)
291 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
293 ent.move_watertype = contents;
294 ent.move_waterlevel = 1;
298 else if(ent.move_watertype != contents)
300 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents);
301 if(ent.contentstransition)
302 ent.contentstransition(ent.move_watertype, contents);
305 if(contents <= CONTENT_WATER)
307 ent.move_watertype = contents;
308 ent.move_waterlevel = 1;
312 ent.move_watertype = CONTENT_EMPTY;
313 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
317 void _Movetype_Impact(entity this, entity oth) // SV_Impact
319 entity oldother = other;
325 WITH(entity, self, this, this.move_touch());
334 WITH(entity, self, oth, oth.move_touch());
340 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
342 entity oldother = other;
344 for (entity e = findradius(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin)); e; e = e.chain)
346 if(e.move_nomonsters != MOVE_NOMONSTERS && e.move_nomonsters != MOVE_WORLDONLY)
347 if(e.move_touch && boxesoverlap(e.absmin, e.absmax, this.absmin, this.absmax))
351 trace_allsolid = false;
352 trace_startsolid = false;
354 trace_inwater = false;
356 trace_endpos = e.move_origin;
357 trace_plane_normal = '0 0 1';
358 trace_plane_dist = 0;
361 WITH(entity, self, e, e.move_touch());
368 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
371 if(this.solid == SOLID_BSP)
373 // TODO set the absolute bbox
382 mi += this.move_origin;
383 ma += this.move_origin;
385 if(this.move_flags & FL_ITEM)
408 _Movetype_LinkEdict_TouchAreaGrid(this);
411 bool _Movetype_TestEntityPosition(entity this, vector ofs) // SV_TestEntityPosition
413 // vector org = this.move_origin + ofs;
415 int cont = this.dphitcontentsmask;
416 this.dphitcontentsmask = DPCONTENTS_SOLID;
417 tracebox(this.move_origin, this.mins, this.maxs, this.move_origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
418 this.dphitcontentsmask = cont;
423 if(vlen(trace_endpos - this.move_origin) > 0.0001)
424 this.move_origin = trace_endpos;
428 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
430 if(!_Movetype_TestEntityPosition(this, '0 0 0')) return true;
431 if(!_Movetype_TestEntityPosition(this, '-1 0 0')) goto success;
432 if(!_Movetype_TestEntityPosition(this, '1 0 0')) goto success;
433 if(!_Movetype_TestEntityPosition(this, '0 -1 0')) goto success;
434 if(!_Movetype_TestEntityPosition(this, '0 1 0')) goto success;
435 if(!_Movetype_TestEntityPosition(this, '-1 -1 0')) goto success;
436 if(!_Movetype_TestEntityPosition(this, '1 -1 0')) goto success;
437 if(!_Movetype_TestEntityPosition(this, '-1 1 0')) goto success;
438 if(!_Movetype_TestEntityPosition(this, '1 1 0')) goto success;
439 for (int i = 1; i <= 17; ++i)
441 if(!_Movetype_TestEntityPosition(this, '0 0 -1' * i)) goto success;
442 if(!_Movetype_TestEntityPosition(this, '0 0 1' * i)) goto success;
444 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
445 etof(this), this.classname, vtos(this.move_origin));
448 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
449 etof(this), this.classname, vtos(this.move_origin));
450 _Movetype_LinkEdict(this, true);
454 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
456 vel -= ((vel * norm) * norm) * f;
458 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
459 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
460 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
465 void _Movetype_PushEntityTrace(entity this, vector push)
467 vector end = this.move_origin + push;
469 if(this.move_nomonsters)
470 type = max(0, this.move_nomonsters);
471 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
473 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
474 type = MOVE_WORLDONLY;
475 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
476 type = MOVE_NOMONSTERS;
480 tracebox(this.move_origin, this.mins, this.maxs, end, type, this);
483 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
485 _Movetype_PushEntityTrace(this, push);
487 if(trace_startsolid && failonstartsolid)
488 return trace_fraction;
490 this.move_origin = trace_endpos;
492 if(trace_fraction < 1)
493 if(this.solid >= SOLID_TRIGGER && (!(this.move_flags & FL_ONGROUND) || (this.move_groundentity != trace_ent)))
494 _Movetype_Impact(this, 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(entity this, float movedt)
559 this.move_didgravity = -1;
560 switch (this.move_movetype)
563 case MOVETYPE_FAKEPUSH:
564 _Movetype_Physics_Pusher(this, movedt);
568 case MOVETYPE_FOLLOW:
569 _Movetype_Physics_Follow(this);
571 case MOVETYPE_NOCLIP:
572 _Movetype_CheckWater(this);
573 this.move_origin = this.move_origin + TICRATE * this.move_velocity;
574 this.move_angles = this.move_angles + TICRATE * this.move_avelocity;
575 _Movetype_LinkEdict(this, false);
578 _Movetype_Physics_Step(this, movedt);
581 _Movetype_Physics_Walk(this, movedt);
584 case MOVETYPE_BOUNCE:
585 case MOVETYPE_BOUNCEMISSILE:
586 case MOVETYPE_FLYMISSILE:
588 case MOVETYPE_FLY_WORLDONLY:
589 _Movetype_Physics_Toss(this, movedt);
590 _Movetype_LinkEdict(this, true);
592 case MOVETYPE_PHYSICS:
597 void _Movetype_Physics_ClientFrame(entity this, float movedt)
599 this.move_didgravity = -1;
600 switch (this.move_movetype)
603 case MOVETYPE_FAKEPUSH:
604 _Movetype_Physics_Pusher(this, movedt);
608 case MOVETYPE_FOLLOW:
609 _Movetype_Physics_Follow(this);
611 case MOVETYPE_NOCLIP:
612 _Movetype_CheckWater(this);
613 this.move_origin = this.move_origin + TICRATE * this.move_velocity;
614 this.move_angles = this.move_angles + TICRATE * this.move_avelocity;
615 _Movetype_LinkEdict(this, false);
618 _Movetype_Physics_Step(this, movedt);
622 case MOVETYPE_FLY_WORLDONLY:
623 _Movetype_Physics_Walk(this, movedt);
626 case MOVETYPE_BOUNCE:
627 case MOVETYPE_BOUNCEMISSILE:
628 case MOVETYPE_FLYMISSILE:
629 _Movetype_Physics_Toss(this, movedt);
631 case MOVETYPE_PHYSICS:
636 void Movetype_Physics_NoMatchServer(entity this) // optimized
638 float movedt = time - this.move_time;
639 this.move_time = time;
641 _Movetype_Physics_Frame(this, movedt);
645 this.avelocity = this.move_avelocity;
646 this.velocity = this.move_velocity;
647 this.angles = this.move_angles;
648 setorigin(this, this.move_origin);
651 void Movetype_Physics_MatchServer(entity this, bool sloppy)
653 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
656 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
660 Movetype_Physics_NoMatchServer(this);
664 float dt = time - this.move_time;
666 int n = max(0, floor(dt / tr));
668 this.move_time += n * tr;
670 if(!this.move_didgravity)
671 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.move_flags & FL_ONGROUND));
673 for (int i = 0; i < n; ++i)
675 _Movetype_Physics_Frame(this, tr);
680 this.avelocity = this.move_avelocity;
682 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.move_flags & FL_ONGROUND))
684 // now continue the move from move_time to time
685 this.velocity = this.move_velocity;
687 if(this.move_didgravity > 0)
689 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
691 * (this.gravity ? this.gravity : 1)
692 * PHYS_GRAVITY(this);
695 this.angles = this.move_angles + dt * this.avelocity;
697 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
699 setorigin(this, this.move_origin + dt * this.velocity);
703 _Movetype_PushEntityTrace(this, dt * this.velocity);
704 if(!trace_startsolid)
705 setorigin(this, trace_endpos);
708 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
709 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
713 this.velocity = this.move_velocity;
714 this.angles = this.move_angles;
715 setorigin(this, this.move_origin);