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 ent) // SV_CheckWater
252 vector point = ent.move_origin;
253 point.z += (ent.mins.z + 1);
255 int nativecontents = pointcontents(point);
256 if(ent.move_watertype && ent.move_watertype != nativecontents)
258 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents);
259 if(ent.contentstransition)
260 ent.contentstransition(ent.move_watertype, nativecontents);
263 ent.move_waterlevel = 0;
264 ent.move_watertype = CONTENT_EMPTY;
266 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
267 if(supercontents & DPCONTENTS_LIQUIDSMASK)
269 ent.move_watertype = nativecontents;
270 ent.move_waterlevel = 1;
271 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
272 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
274 ent.move_waterlevel = 2;
275 point.y = ent.origin.y + ent.view_ofs.y;
276 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
277 ent.move_waterlevel = 3;
281 return ent.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_touch && boxesoverlap(e.absmin, e.absmax, this.absmin, this.absmax))
350 trace_allsolid = false;
351 trace_startsolid = false;
353 trace_inwater = false;
355 trace_endpos = e.origin;
356 trace_plane_normal = '0 0 1';
357 trace_plane_dist = 0;
360 WITH(entity, self, e, e.move_touch());
367 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
370 if(this.solid == SOLID_BSP)
372 // TODO set the absolute bbox
381 mi += this.move_origin;
382 ma += this.move_origin;
384 if(this.move_flags & FL_ITEM)
407 _Movetype_LinkEdict_TouchAreaGrid(this);
410 bool _Movetype_TestEntityPosition(entity this, vector ofs) // SV_TestEntityPosition
412 // vector org = this.move_origin + ofs;
414 int cont = this.dphitcontentsmask;
415 this.dphitcontentsmask = DPCONTENTS_SOLID;
416 tracebox(this.move_origin, this.mins, this.maxs, this.move_origin, MOVE_NOMONSTERS, this);
417 this.dphitcontentsmask = cont;
422 if(vlen(trace_endpos - this.move_origin) > 0.0001)
423 this.move_origin = trace_endpos;
427 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
429 if(!_Movetype_TestEntityPosition(this, '0 0 0')) return true;
430 if(!_Movetype_TestEntityPosition(this, '-1 0 0')) goto success;
431 if(!_Movetype_TestEntityPosition(this, '1 0 0')) goto success;
432 if(!_Movetype_TestEntityPosition(this, '0 -1 0')) goto success;
433 if(!_Movetype_TestEntityPosition(this, '0 1 0')) goto success;
434 if(!_Movetype_TestEntityPosition(this, '-1 -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 for (int i = 1; i <= 17; ++i)
440 if(!_Movetype_TestEntityPosition(this, '0 0 -1' * i)) goto success;
441 if(!_Movetype_TestEntityPosition(this, '0 0 1' * i)) goto success;
443 LOG_TRACEF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n",
444 num_for_edict(this), this.classname, vtos(this.move_origin));
447 LOG_TRACEF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n",
448 num_for_edict(this), this.classname, vtos(this.move_origin));
449 _Movetype_LinkEdict(this, true);
453 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
455 vel -= ((vel * norm) * norm) * f;
457 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
458 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
459 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
464 void _Movetype_PushEntityTrace(entity this, vector push)
466 vector end = this.move_origin + push;
468 if(this.move_nomonsters)
469 type = max(0, this.move_nomonsters);
470 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
472 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
473 type = MOVE_NOMONSTERS;
477 tracebox(this.move_origin, this.mins, this.maxs, end, type, this);
480 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
482 _Movetype_PushEntityTrace(this, push);
484 if(trace_startsolid && failonstartsolid)
485 return trace_fraction;
487 this.move_origin = trace_endpos;
489 if(trace_fraction < 1)
490 if(this.solid >= SOLID_TRIGGER && (!(this.move_flags & FL_ONGROUND) || (this.move_groundentity != trace_ent)))
491 _Movetype_Impact(this, trace_ent);
493 return trace_fraction;
499 // matrix version of makevectors, sets v_forward, v_right and v_up
500 void makevectors_matrix(vector myangles) // AngleVectorsFLU
502 v_forward = v_right = v_up = '0 0 0';
504 float y = myangles.y * (M_PI * 2 / 360);
507 float p = myangles.x * (M_PI * 2 / 360);
512 v_forward.x = cp * cy;
513 v_forward.y = cp * sy;
520 float r = myangles.z * (M_PI * 2 / 360);
525 v_right.x = sr * sp * cy + cr * -sy;
526 v_right.y = sr * sp * sy + cr * cy;
531 v_up.x = cr * sp * cy + -sr * -sy;
532 v_up.y = cr * sp * sy + -sr * cy;
554 void _Movetype_Physics_Frame(entity this, float movedt)
556 this.move_didgravity = -1;
557 switch (this.move_movetype)
560 case MOVETYPE_FAKEPUSH:
561 _Movetype_Physics_Pusher(this, movedt);
565 case MOVETYPE_FOLLOW:
566 _Movetype_Physics_Follow(this);
568 case MOVETYPE_NOCLIP:
569 _Movetype_CheckWater(this);
570 this.move_origin = this.move_origin + TICRATE * this.move_velocity;
571 this.move_angles = this.move_angles + TICRATE * this.move_avelocity;
572 _Movetype_LinkEdict(this, false);
575 _Movetype_Physics_Step(this, movedt);
578 _Movetype_Physics_Walk(this, movedt);
581 case MOVETYPE_BOUNCE:
582 case MOVETYPE_BOUNCEMISSILE:
583 case MOVETYPE_FLYMISSILE:
585 _Movetype_Physics_Toss(this, movedt);
590 void Movetype_Physics_NoMatchServer(entity this) // optimized
592 float movedt = time - this.move_time;
593 this.move_time = time;
595 _Movetype_Physics_Frame(this, movedt);
599 this.avelocity = this.move_avelocity;
600 this.velocity = this.move_velocity;
601 this.angles = this.move_angles;
602 setorigin(this, this.move_origin);
605 void Movetype_Physics_MatchServer(entity this, bool sloppy)
607 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
610 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
614 Movetype_Physics_NoMatchServer(this);
618 float dt = time - this.move_time;
620 int n = max(0, floor(dt / tr));
622 this.move_time += n * tr;
624 if(!this.move_didgravity)
625 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.move_flags & FL_ONGROUND));
627 for (int i = 0; i < n; ++i)
629 _Movetype_Physics_Frame(this, tr);
634 this.avelocity = this.move_avelocity;
636 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.move_flags & FL_ONGROUND))
638 // now continue the move from move_time to time
639 this.velocity = this.move_velocity;
641 if(this.move_didgravity > 0)
643 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
645 * (this.gravity ? this.gravity : 1)
646 * PHYS_GRAVITY(this);
649 this.angles = this.move_angles + dt * this.avelocity;
651 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
653 setorigin(this, this.move_origin + dt * this.velocity);
657 _Movetype_PushEntityTrace(this, dt * this.velocity);
658 if(!trace_startsolid)
659 setorigin(this, trace_endpos);
662 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
663 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
667 this.velocity = this.move_velocity;
668 this.angles = this.move_angles;
669 setorigin(this, this.move_origin);