2 #include "triggers/trigger/swamp.qh"
3 #include "triggers/trigger/jumppads.qh"
7 #include "../server/miscfunctions.qh"
9 void Physics_AddStats()
11 // static view offset and hitbox vectors
12 // networked for all you bandwidth pigs out there
13 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
14 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
15 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
16 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
17 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
18 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
20 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
21 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
22 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
23 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
24 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
25 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
26 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
27 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
28 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
29 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
30 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
31 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
33 // g_movementspeed hack
34 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
35 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
36 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
37 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
38 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
41 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
42 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
43 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
44 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
45 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
46 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
48 // hack to fix track_canjump
49 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
52 addstat(STAT_DOUBLEJUMP, AS_INT, stat_doublejump);
55 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
56 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
57 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, AS_INT, stat_jumpspeedcap_disable_onramps);
60 addstat(STAT_MOVEVARS_FRICTION_ONLAND, AS_FLOAT, stat_sv_friction_on_land);
61 addstat(STAT_MOVEVARS_FRICTION_SLICK, AS_FLOAT, stat_sv_friction_slick);
62 addstat(STAT_GAMEPLAYFIX_EASIERWATERJUMP, AS_INT, stat_gameplayfix_easierwaterjump);
64 addstat(STAT_GAMEPLAYFIX_UPVELOCITYCLEARSONGROUND, AS_INT, stat_gameplayfix_upvelocityclearsonground);
67 void Physics_UpdateStats(float maxspd_mod)
70 self.stat_pl_view_ofs = PL_VIEW_OFS;
71 self.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
73 self.stat_pl_min = PL_MIN;
74 self.stat_pl_max = PL_MAX;
75 self.stat_pl_crouch_min = PL_CROUCH_MIN;
76 self.stat_pl_crouch_max = PL_CROUCH_MAX;
78 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
79 if (autocvar_sv_airstrafeaccel_qw)
80 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
82 self.stat_sv_airstrafeaccel_qw = 0;
83 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
84 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
85 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
87 self.stat_doublejump = PHYS_DOUBLEJUMP;
89 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
90 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
91 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
92 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
93 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
94 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
96 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
97 self.stat_jumpspeedcap_max = PHYS_JUMPSPEEDCAP_MAX;
98 self.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
100 self.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
101 self.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
103 self.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
105 self.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
109 float IsMoveInDirection(vector mv, float ang) // key mix factor
111 if (mv_x == 0 && mv_y == 0)
112 return 0; // avoid division by zero
113 ang -= RAD2DEG * atan2(mv_y, mv_x);
114 ang = remainder(ang, 360) / 45;
115 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
118 float GeomLerp(float a, float lerp, float b)
120 return a == 0 ? (lerp < 1 ? 0 : b)
121 : b == 0 ? (lerp > 0 ? 0 : a)
122 : a * pow(fabs(b / a), lerp);
125 noref float pmove_waterjumptime;
127 const float unstick_count = 27;
128 vector unstick_offsets[unstick_count] =
130 // 1 no nudge (just return the original if this test passes)
133 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
134 '-0.125 0.000 0.000', '0.125 0.000 0.000',
135 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
136 // 4 diagonal flat nudges
137 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
138 '-0.125 0.125 0.000', '0.125 0.125 0.000',
139 // 8 diagonal upward nudges
140 '-0.125 0.000 0.125', '0.125 0.000 0.125',
141 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
142 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
143 '-0.125 0.125 0.125', '0.125 0.125 0.125',
144 // 8 diagonal downward nudges
145 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
146 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
147 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
148 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
151 void PM_ClientMovement_Unstick()
154 for (i = 0; i < unstick_count; i++)
156 vector neworigin = unstick_offsets[i] + self.origin;
157 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
158 if (!trace_startsolid)
160 setorigin(self, neworigin);
166 void PM_ClientMovement_UpdateStatus(bool ground)
168 // make sure player is not stuck
169 PM_ClientMovement_Unstick();
172 if (PHYS_INPUT_BUTTON_CROUCH(self))
174 // wants to crouch, this always works..
175 if (!IS_DUCKED(self))
180 // wants to stand, if currently crouching we need to check for a
184 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
185 if (!trace_startsolid)
191 vector origin1 = self.origin + '0 0 1';
192 vector origin2 = self.origin - '0 0 1';
196 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
197 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
201 // this code actually "predicts" an impact; so let's clip velocity first
202 float f = self.velocity * trace_plane_normal;
203 self.velocity -= f * trace_plane_normal;
206 UNSET_ONGROUND(self);
209 // set watertype/waterlevel
210 origin1 = self.origin;
211 origin1_z += self.mins_z + 1;
212 self.waterlevel = WATERLEVEL_NONE;
214 int thepoint = pointcontents(origin1);
216 self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
220 self.waterlevel = WATERLEVEL_WETFEET;
221 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
222 thepoint = pointcontents(origin1);
223 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
225 self.waterlevel = WATERLEVEL_SWIMMING;
226 origin1_z = self.origin_z + 22;
227 thepoint = pointcontents(origin1);
228 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
229 self.waterlevel = WATERLEVEL_SUBMERGED;
233 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
234 pmove_waterjumptime = 0;
237 void PM_ClientMovement_Move()
244 vector currentorigin2;
246 vector primalvelocity;
248 vector trace1_endpos = '0 0 0';
249 vector trace2_endpos = '0 0 0';
250 vector trace3_endpos = '0 0 0';
251 float trace1_fraction = 0;
252 float trace2_fraction = 0;
253 float trace3_fraction = 0;
254 vector trace1_plane_normal = '0 0 0';
255 vector trace2_plane_normal = '0 0 0';
256 vector trace3_plane_normal = '0 0 0';
259 PM_ClientMovement_UpdateStatus(false);
260 primalvelocity = self.velocity;
261 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
263 neworigin = self.origin + t * self.velocity;
264 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
265 trace1_endpos = trace_endpos;
266 trace1_fraction = trace_fraction;
267 trace1_plane_normal = trace_plane_normal;
268 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
270 // may be a step or wall, try stepping up
271 // first move forward at a higher level
272 currentorigin2 = self.origin;
273 currentorigin2_z += PHYS_STEPHEIGHT;
274 neworigin2 = neworigin;
275 neworigin2_z += PHYS_STEPHEIGHT;
276 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
277 trace2_endpos = trace_endpos;
278 trace2_fraction = trace_fraction;
279 trace2_plane_normal = trace_plane_normal;
280 if(!trace_startsolid)
282 // then move down from there
283 currentorigin2 = trace2_endpos;
284 neworigin2 = trace2_endpos;
285 neworigin2_z = self.origin_z;
286 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
287 trace3_endpos = trace_endpos;
288 trace3_fraction = trace_fraction;
289 trace3_plane_normal = trace_plane_normal;
290 // accept the new trace if it made some progress
291 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
293 trace1_endpos = trace2_endpos;
294 trace1_fraction = trace2_fraction;
295 trace1_plane_normal = trace2_plane_normal;
296 trace1_endpos = trace3_endpos;
301 // check if it moved at all
302 if(trace1_fraction >= 0.001)
303 setorigin(self, trace1_endpos);
305 // check if it moved all the way
306 if(trace1_fraction == 1)
309 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
310 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
311 // this got commented out in a change that supposedly makes the code match QW better
312 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
313 if(trace1_plane_normal_z > 0.7)
316 t -= t * trace1_fraction;
318 f = (self.velocity * trace1_plane_normal);
319 self.velocity = self.velocity + -f * trace1_plane_normal;
321 if(pmove_waterjumptime > 0)
322 self.velocity = primalvelocity;
326 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
328 float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
332 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
334 float zspeed = self.velocity_z;
336 float xyspeed = vlen(self.velocity);
337 self.velocity = normalize(self.velocity);
339 float dot = self.velocity * wishdir;
341 if (dot > 0) // we can't change direction while slowing down
343 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
344 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
345 k *= PHYS_AIRCONTROL;
346 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
349 self.velocity = self.velocity * xyspeed;
350 self.velocity_z = zspeed;
353 float AdjustAirAccelQW(float accelqw, float factor)
355 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
358 // example config for alternate speed clamping:
359 // sv_airaccel_qw 0.8
360 // sv_airaccel_sideways_friction 0
361 // prvm_globalset server speedclamp_mode 1
363 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
365 float speedclamp = stretchfactor > 0 ? stretchfactor
366 : accelqw < 0 ? 1 // full clamping, no stretch
369 accelqw = fabs(accelqw);
371 if (GAMEPLAYFIX_Q2AIRACCELERATE)
372 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
374 float vel_straight = self.velocity * wishdir;
375 float vel_z = self.velocity_z;
376 vector vel_xy = vec2(self.velocity);
377 vector vel_perpend = vel_xy - vel_straight * wishdir;
379 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
381 float vel_xy_current = vlen(vel_xy);
383 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
384 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
385 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
386 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
387 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
389 if (sidefric < 0 && (vel_perpend*vel_perpend))
390 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
392 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
393 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
395 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
396 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
397 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
398 // obviously, this cannot be
404 vel_perpend *= max(fmin, f);
408 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
410 vel_xy = vel_straight * wishdir + vel_perpend;
414 float vel_xy_preclamp;
415 vel_xy_preclamp = vlen(vel_xy);
416 if (vel_xy_preclamp > 0) // prevent division by zero
418 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
419 if (vel_xy_current < vel_xy_preclamp)
420 vel_xy *= (vel_xy_current / vel_xy_preclamp);
424 self.velocity = vel_xy + vel_z * '0 0 1';
427 void PM_AirAccelerate(vector wishdir, float wishspeed)
432 vector curvel = self.velocity;
434 float curspeed = vlen(curvel);
436 if (wishspeed > curspeed * 1.01)
437 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
440 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
441 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
443 vector wishvel = wishdir * wishspeed;
444 vector acceldir = wishvel - curvel;
445 float addspeed = vlen(acceldir);
446 acceldir = normalize(acceldir);
448 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
450 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
452 vector curdir = normalize(curvel);
453 float dot = acceldir * curdir;
455 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
458 self.velocity += accelspeed * acceldir;
466 When you press the jump key
467 returns true if handled
470 float PlayerJump (void)
472 if (PHYS_FROZEN(self))
473 return true; // no jumping in freezetag when frozen
476 if (self.player_blocked)
477 return true; // no jumping while blocked
480 float doublejump = false;
481 float mjumpheight = PHYS_JUMPVELOCITY;
483 player_multijump = doublejump;
484 player_jumpheight = mjumpheight;
486 if (MUTATOR_CALLHOOK(PlayerJump))
488 if(PM_multijump_checkjump())
492 doublejump = player_multijump;
493 mjumpheight = player_jumpheight;
497 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
498 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
502 // we MUST clip velocity here!
504 f = self.velocity * trace_plane_normal;
506 self.velocity -= f * trace_plane_normal;
510 if (self.waterlevel >= WATERLEVEL_SWIMMING)
512 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
517 if (!IS_ONGROUND(self))
518 return IS_JUMP_HELD(self);
520 if (PHYS_TRACK_CANJUMP(self))
521 if (IS_JUMP_HELD(self))
524 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
525 // velocity bounds. Final velocity is bound between (jumpheight *
526 // min + jumpheight) and (jumpheight * max + jumpheight);
528 if(PHYS_JUMPSPEEDCAP_MIN)
530 float minjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MIN;
532 if (self.velocity_z < minjumpspeed)
533 mjumpheight += minjumpspeed - self.velocity_z;
536 if(PHYS_JUMPSPEEDCAP_MAX)
538 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
539 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
541 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
543 float maxjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MAX;
545 if (self.velocity_z > maxjumpspeed)
546 mjumpheight -= self.velocity_z - maxjumpspeed;
550 if (!WAS_ONGROUND(self))
553 if(autocvar_speedmeter)
554 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
556 if(self.lastground < time - 0.3)
558 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
559 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
562 if(self.jumppadcount > 1)
563 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
564 self.jumppadcount = 0;
568 self.velocity_z += mjumpheight;
570 UNSET_ONGROUND(self);
575 self.oldvelocity_z = self.velocity_z;
577 animdecide_setaction(self, ANIMACTION_JUMP, true);
579 if (autocvar_g_jump_grunt)
580 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
585 void CheckWaterJump()
587 // check for a jump-out-of-water
588 makevectors(self.v_angle);
589 vector start = self.origin;
592 normalize(v_forward);
593 vector end = start + v_forward*24;
594 traceline (start, end, true, self);
595 if (trace_fraction < 1)
597 start_z = start_z + self.maxs_z - 8;
598 end = start + v_forward*24;
599 self.movedir = trace_plane_normal * -50;
600 traceline(start, end, true, self);
601 if (trace_fraction == 1)
602 { // open at eye level
603 self.velocity_z = 225;
604 self.flags |= FL_WATERJUMP;
606 self.teleport_time = time + 2; // safety net
613 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
615 float autocvar_cl_jetpack_jump;
616 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
618 .float jetpack_stopped;
619 // Hack: shouldn't need to know about this
620 .float multijump_count;
621 void CheckPlayerJump()
624 float was_flying = ITEMS(self) & IT_USING_JETPACK;
626 if (JETPACK_JUMP(self) < 2)
627 ITEMS(self) &= ~IT_USING_JETPACK;
629 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
631 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
632 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
633 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO;
635 if (!(ITEMS(self) & IT_JETPACK)) { }
636 else if (self.jetpack_stopped) { }
640 if (was_flying) // TODO: ran out of fuel message
641 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
643 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
645 self.jetpack_stopped = true;
646 ITEMS(self) &= ~IT_USING_JETPACK;
648 else if (activate && !PHYS_FROZEN(self))
649 ITEMS(self) |= IT_USING_JETPACK;
653 self.jetpack_stopped = false;
654 ITEMS(self) &= ~IT_USING_JETPACK;
656 if (!PHYS_INPUT_BUTTON_JUMP(self))
657 UNSET_JUMP_HELD(self);
659 if (self.waterlevel == WATERLEVEL_SWIMMING)
663 float racecar_angle(float forward, float down)
671 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
673 float angle_mult = forward / (800 + forward);
676 return ret * angle_mult + 360 * (1 - angle_mult);
678 return ret * angle_mult;
681 void RaceCarPhysics()
684 // using this move type for "big rigs"
685 // the engine does not push the entity!
689 vector angles_save = self.angles;
690 float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
691 float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
693 if (g_bugrigs_reverse_speeding)
697 // back accel is DIGITAL
698 // to prevent speedhack
708 makevectors(self.angles); // new forward direction!
710 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
712 float myspeed = self.velocity * v_forward;
713 float upspeed = self.velocity * v_up;
715 // responsiveness factor for steering and acceleration
716 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
717 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
720 if (myspeed < 0 && g_bugrigs_reverse_spinning)
721 steerfactor = -myspeed * g_bugrigs_steer;
723 steerfactor = -myspeed * f * g_bugrigs_steer;
726 if (myspeed < 0 && g_bugrigs_reverse_speeding)
727 accelfactor = g_bugrigs_accel;
729 accelfactor = f * g_bugrigs_accel;
730 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
736 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
740 if (!g_bugrigs_reverse_speeding)
741 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
748 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
752 if (g_bugrigs_reverse_stopping)
755 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
758 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
759 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
761 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
762 makevectors(self.angles); // new forward direction!
764 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
766 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
770 float myspeed = vlen(self.velocity);
772 // responsiveness factor for steering and acceleration
773 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
774 float steerfactor = -myspeed * f;
775 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
777 rigvel = self.velocity;
778 makevectors(self.angles); // new forward direction!
781 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
782 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
783 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
784 //MAXIMA: solve(total_acceleration(v) = 0, v);
786 if (g_bugrigs_planar_movement)
788 vector rigvel_xy, neworigin, up;
791 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
792 rigvel_xy = vec2(rigvel);
794 if (g_bugrigs_planar_movement_car_jumping)
797 mt = MOVE_NOMONSTERS;
799 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
800 up = trace_endpos - self.origin;
802 // BUG RIGS: align the move to the surface instead of doing collision testing
804 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
807 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
809 if (trace_fraction < 0.5)
812 neworigin = self.origin;
815 neworigin = trace_endpos;
817 if (trace_fraction < 1)
819 // now set angles_x so that the car points parallel to the surface
820 self.angles = vectoangles(
821 '1 0 0' * v_forward_x * trace_plane_normal_z
823 '0 1 0' * v_forward_y * trace_plane_normal_z
825 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
831 // now set angles_x so that the car points forward, but is tilted in velocity direction
832 UNSET_ONGROUND(self);
835 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
836 self.movetype = MOVETYPE_NOCLIP;
840 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
841 self.velocity = rigvel;
842 self.movetype = MOVETYPE_FLY;
846 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
847 if (trace_fraction != 1)
849 self.angles = vectoangles2(
850 '1 0 0' * v_forward_x * trace_plane_normal_z
852 '0 1 0' * v_forward_y * trace_plane_normal_z
854 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
862 vel_local_x = v_forward * self.velocity;
863 vel_local_y = v_right * self.velocity;
864 vel_local_z = v_up * self.velocity;
866 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
867 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
871 vector vf1, vu1, smoothangles;
872 makevectors(self.angles);
873 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
878 makevectors(angles_save);
879 vf1 = vf1 + v_forward * (1 - f);
880 vu1 = vu1 + v_up * (1 - f);
881 smoothangles = vectoangles2(vf1, vu1);
882 self.angles_x = -smoothangles_x;
883 self.angles_z = smoothangles_z;
887 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
888 .float specialcommand_pos;
889 void SpecialCommand()
895 if (!CheatImpulse(99))
896 print("A hollow voice says \"Plugh\".\n");
901 float PM_check_keepaway(void)
904 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
910 void PM_check_race_movetime(void)
913 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
914 float f = floor(self.race_movetime_frac);
915 self.race_movetime_frac -= f;
916 self.race_movetime_count += f;
917 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
921 float PM_check_specialcommand(float buttons)
927 else if (buttons == 1)
929 else if (buttons == 2)
931 else if (buttons == 128)
933 else if (buttons == 256)
935 else if (buttons == 512)
937 else if (buttons == 1024)
942 if (c == substring(specialcommand, self.specialcommand_pos, 1))
944 self.specialcommand_pos += 1;
945 if (self.specialcommand_pos >= strlen(specialcommand))
947 self.specialcommand_pos = 0;
952 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
953 self.specialcommand_pos = 0;
958 void PM_check_nickspam(void)
961 if (time >= self.nickspamtime)
963 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
965 // slight annoyance for nick change scripts
966 self.movement = -1 * self.movement;
967 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
969 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
971 self.v_angle_x = random() * 360;
972 self.v_angle_y = random() * 360;
973 // at least I'm not forcing retardedview by also assigning to angles_z
974 self.fixangle = true;
980 void PM_check_punch()
983 if (self.punchangle != '0 0 0')
985 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
987 self.punchangle = normalize(self.punchangle) * f;
989 self.punchangle = '0 0 0';
992 if (self.punchvector != '0 0 0')
994 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
996 self.punchvector = normalize(self.punchvector) * f;
998 self.punchvector = '0 0 0';
1003 void PM_check_spider(void)
1006 if (time >= self.spider_slowness)
1008 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1009 self.stat_sv_airspeedlimit_nonqw *= 0.5;
1013 // predict frozen movement, as frozen players CAN move in some cases
1014 void PM_check_frozen(void)
1016 if (!PHYS_FROZEN(self))
1018 if (PHYS_DODGING_FROZEN
1020 && IS_REAL_CLIENT(self)
1024 self.movement_x = bound(-5, self.movement.x, 5);
1025 self.movement_y = bound(-5, self.movement.y, 5);
1026 self.movement_z = bound(-5, self.movement.z, 5);
1029 self.movement = '0 0 0';
1031 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1032 if (pointcontents(midpoint) == CONTENT_WATER)
1034 self.velocity = self.velocity * 0.5;
1036 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1037 self.velocity_z = 200;
1041 void PM_check_hitground()
1044 if (IS_ONGROUND(self))
1045 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1049 if (self.waterlevel < WATERLEVEL_SWIMMING)
1050 if (time >= self.ladder_time)
1053 self.nextstep = time + 0.3 + random() * 0.1;
1054 trace_dphitq3surfaceflags = 0;
1055 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1056 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1058 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1059 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1061 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1068 void PM_check_blocked(void)
1071 if (!self.player_blocked)
1073 self.movement = '0 0 0';
1074 self.disableclientprediction = 1;
1079 float speedaward_lastsent;
1080 float speedaward_lastupdate;
1082 void PM_check_race(void)
1085 if(!(g_cts || g_race))
1087 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1089 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1090 speedaward_holder = self.netname;
1091 speedaward_uid = self.crypto_idfp;
1092 speedaward_lastupdate = time;
1094 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1096 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1097 race_send_speedaward(MSG_ALL);
1098 speedaward_lastsent = speedaward_speed;
1099 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1101 speedaward_alltimebest = speedaward_speed;
1102 speedaward_alltimebest_holder = speedaward_holder;
1103 speedaward_alltimebest_uid = speedaward_uid;
1104 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1105 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1106 race_send_speedaward_alltimebest(MSG_ALL);
1112 void PM_check_vortex(void)
1116 float xyspeed = vlen(vec2(self.velocity));
1117 if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1119 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1120 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1121 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1122 // add the extra charge
1123 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1128 void PM_fly(float maxspd_mod)
1130 // noclipping or flying
1131 UNSET_ONGROUND(self);
1133 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1134 makevectors(self.v_angle);
1135 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1136 vector wishvel = v_forward * self.movement.x
1137 + v_right * self.movement.y
1138 + '0 0 1' * self.movement.z;
1140 vector wishdir = normalize(wishvel);
1141 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1143 if (time >= self.teleport_time)
1145 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1146 PM_ClientMovement_Move();
1149 void PM_swim(float maxspd_mod)
1152 UNSET_ONGROUND(self);
1154 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1155 // water jump only in certain situations
1156 // this mimics quakeworld code
1157 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1159 vector yawangles = '0 1 0' * self.v_angle.y;
1160 makevectors(yawangles);
1161 vector forward = v_forward;
1162 vector spot = self.origin + 24 * forward;
1164 traceline(spot, spot, MOVE_NOMONSTERS, self);
1165 if (trace_startsolid)
1168 traceline(spot, spot, MOVE_NOMONSTERS, self);
1169 if (!trace_startsolid)
1171 self.velocity = forward * 50;
1172 self.velocity_z = 310;
1173 pmove_waterjumptime = 2;
1174 UNSET_ONGROUND(self);
1175 SET_JUMP_HELD(self);
1179 makevectors(self.v_angle);
1180 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1181 vector wishvel = v_forward * self.movement.x
1182 + v_right * self.movement.y
1183 + '0 0 1' * self.movement.z;
1184 if (wishvel == '0 0 0')
1185 wishvel = '0 0 -60'; // drift towards bottom
1187 vector wishdir = normalize(wishvel);
1188 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1190 if (IS_DUCKED(self))
1193 // if (pmove_waterjumptime <= 0) // TODO: use
1196 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1197 f = min(max(0, f), 1);
1200 f = wishspeed - self.velocity * wishdir;
1203 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1204 self.velocity += accelspeed * wishdir;
1207 // holding jump button swims upward slowly
1211 if (self.watertype & CONTENT_LAVA)
1212 self.velocity_z = 50;
1213 else if (self.watertype & CONTENT_SLIME)
1214 self.velocity_z = 80;
1217 if (IS_NEXUIZ_DERIVED(gamemode))
1219 self.velocity_z = 200;
1222 self.velocity_z = 100;
1227 // water acceleration
1228 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1229 PM_ClientMovement_Move();
1232 void PM_ladder(float maxspd_mod)
1234 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1235 UNSET_ONGROUND(self);
1238 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1239 if (PHYS_ENTGRAVITY(self))
1240 g *= PHYS_ENTGRAVITY(self);
1241 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1244 self.velocity_z += g;
1247 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1248 makevectors(self.v_angle);
1249 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1250 vector wishvel = v_forward * self.movement_x
1251 + v_right * self.movement_y
1252 + '0 0 1' * self.movement_z;
1253 self.velocity_z += g;
1254 if (self.ladder_entity.classname == "func_water")
1256 float f = vlen(wishvel);
1257 if (f > self.ladder_entity.speed)
1258 wishvel *= (self.ladder_entity.speed / f);
1260 self.watertype = self.ladder_entity.skin;
1261 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1262 if ((self.origin_z + self.view_ofs_z) < f)
1263 self.waterlevel = WATERLEVEL_SUBMERGED;
1264 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1265 self.waterlevel = WATERLEVEL_SWIMMING;
1266 else if ((self.origin_z + self.mins_z + 1) < f)
1267 self.waterlevel = WATERLEVEL_WETFEET;
1270 self.waterlevel = WATERLEVEL_NONE;
1271 self.watertype = CONTENT_EMPTY;
1275 vector wishdir = normalize(wishvel);
1276 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1278 if (time >= self.teleport_time)
1280 // water acceleration
1281 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1282 PM_ClientMovement_Move();
1285 void PM_jetpack(float maxspd_mod)
1287 //makevectors(self.v_angle.y * '0 1 0');
1288 makevectors(self.v_angle);
1289 vector wishvel = v_forward * self.movement_x
1290 + v_right * self.movement_y;
1291 // add remaining speed as Z component
1292 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1293 // fix speedhacks :P
1294 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1295 // add the unused velocity as up component
1298 // if (self.BUTTON_JUMP)
1299 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1301 // it is now normalized, so...
1302 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1303 float a_up = PHYS_JETPACK_ACCEL_UP;
1304 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1306 wishvel_x *= a_side;
1307 wishvel_y *= a_side;
1312 //////////////////////////////////////////////////////////////////////////////////////
1313 // finding the maximum over all vectors of above form
1314 // with wishvel having an absolute value of 1
1315 //////////////////////////////////////////////////////////////////////////////////////
1316 // we're finding the maximum over
1317 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1318 // for z in the range from -1 to 1
1319 //////////////////////////////////////////////////////////////////////////////////////
1320 // maximum is EITHER attained at the single extreme point:
1321 float a_diff = a_side * a_side - a_up * a_up;
1325 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1326 if (f > -1 && f < 1) // can it be attained?
1328 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1329 //print("middle\n");
1332 // OR attained at z = 1:
1333 f = (a_up + a_add) * (a_up + a_add);
1339 // OR attained at z = -1:
1340 f = (a_up - a_add) * (a_up - a_add);
1344 //print("bottom\n");
1347 //////////////////////////////////////////////////////////////////////////////////////
1349 //print("best possible acceleration: ", ftos(best), "\n");
1352 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1353 if (wishvel_z - PHYS_GRAVITY > 0)
1354 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1356 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1359 fvel = vlen(wishvel);
1362 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1364 fvel = min(1, vlen(wishvel) / best);
1365 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1366 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1370 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1372 if (f > 0 && wishvel != '0 0 0')
1374 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1375 UNSET_ONGROUND(self);
1378 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1379 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1381 ITEMS(self) |= IT_USING_JETPACK;
1383 // jetpack also inhibits health regeneration, but only for 1 second
1384 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1389 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1390 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1391 self.velocity_z -= g * 0.5;
1393 self.velocity_z -= g;
1394 PM_ClientMovement_Move();
1395 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1396 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1397 self.velocity_z -= g * 0.5;
1401 void PM_walk(float buttons_prev, float maxspd_mod)
1403 if (!WAS_ONGROUND(self))
1406 if (autocvar_speedmeter)
1407 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1409 if (self.lastground < time - 0.3)
1410 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1412 if (self.jumppadcount > 1)
1413 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1414 self.jumppadcount = 0;
1419 makevectors(self.v_angle.y * '0 1 0');
1420 vector wishvel = v_forward * self.movement.x
1421 + v_right * self.movement.y;
1423 vector wishdir = normalize(wishvel);
1424 float wishspeed = vlen(wishvel);
1426 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1427 if (IS_DUCKED(self))
1430 // apply edge friction
1431 float f = vlen(vec2(self.velocity));
1435 trace_dphitq3surfaceflags = 0;
1436 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1437 // TODO: apply edge friction
1438 // apply ground friction
1439 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1440 realfriction = PHYS_FRICTION_SLICK;
1442 realfriction = PHYS_FRICTION;
1444 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1448 Mathematical analysis time!
1450 Our goal is to invert this mess.
1452 For the two cases we get:
1453 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1454 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1455 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1457 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1458 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1460 These cases would be chosen ONLY if:
1462 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1463 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1465 v0 >= PHYS_STOPSPEED
1466 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1467 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1470 float addspeed = wishspeed - self.velocity * wishdir;
1473 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1474 self.velocity += accelspeed * wishdir;
1476 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1477 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1478 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1479 if (self.velocity * self.velocity)
1480 PM_ClientMovement_Move();
1481 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1482 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1483 self.velocity_z -= g * 0.5;
1486 void PM_air(float buttons_prev, float maxspd_mod)
1488 makevectors(self.v_angle.y * '0 1 0');
1489 vector wishvel = v_forward * self.movement.x
1490 + v_right * self.movement.y;
1492 vector wishdir = normalize(wishvel);
1493 float wishspeed = vlen(wishvel);
1496 if (time >= self.teleport_time)
1498 if (pmove_waterjumptime <= 0)
1501 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1503 // apply air speed limit
1504 float airaccelqw = PHYS_AIRACCEL_QW(self);
1505 float wishspeed0 = wishspeed;
1506 wishspeed = min(wishspeed, maxairspd);
1507 if (IS_DUCKED(self))
1509 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1511 float accelerating = (self.velocity * wishdir > 0);
1512 float wishspeed2 = wishspeed;
1515 if (PHYS_AIRSTOPACCELERATE)
1517 vector curdir = normalize(vec2(self.velocity));
1518 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1520 // note that for straight forward jumping:
1521 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1522 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1524 // dv/dt = accel * maxspeed (when slow)
1525 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1526 // log dv/dt = logaccel + logmaxspeed (when slow)
1527 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1528 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1529 if (PHYS_MAXAIRSTRAFESPEED)
1530 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1531 if (PHYS_AIRSTRAFEACCELERATE)
1532 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1533 if (PHYS_AIRSTRAFEACCEL_QW(self))
1535 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1537 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1540 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1541 PM_AirAccelerate(wishdir, wishspeed2);
1543 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1545 if (PHYS_AIRCONTROL)
1546 CPM_PM_Aircontrol(wishdir, wishspeed2);
1548 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1549 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1550 self.velocity_z -= g * 0.5;
1552 self.velocity_z -= g;
1553 PM_ClientMovement_Move();
1554 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1555 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1556 self.velocity_z -= g * 0.5;
1559 // used for calculating airshots
1560 bool IsFlying(entity a)
1564 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1566 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1567 if(trace_fraction < 1)
1574 float buttons = PHYS_INPUT_BUTTON_MASK(self);
1576 self.items = getstati(STAT_ITEMS, 0, 24);
1578 self.movement = PHYS_INPUT_MOVEVALUES(self);
1580 self.v_angle = PHYS_INPUT_ANGLES(self);
1581 self.angles = PHYS_WORLD_ANGLES(self);
1583 self.team = myteam + 1; // is this correct?
1584 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1585 UNSET_JUMP_HELD(self); // canjump = true
1586 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1588 PM_ClientMovement_UpdateStatus(true);
1593 WarpZone_PlayerPhysics_FixVAngle();
1595 float maxspeed_mod = 1;
1596 maxspeed_mod *= PM_check_keepaway();
1597 maxspeed_mod *= PHYS_HIGHSPEED;
1600 Physics_UpdateStats(maxspeed_mod);
1602 if (self.PlayerPhysplug)
1603 if (self.PlayerPhysplug())
1607 PM_check_race_movetime();
1609 anticheat_physics();
1612 if (PM_check_specialcommand(buttons))
1617 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1618 self.parm_idlesince = time;
1621 float buttons_prev = self.buttons_old;
1622 self.buttons_old = buttons;
1623 self.movement_old = self.movement;
1624 self.v_angle_old = self.v_angle;
1626 PM_check_nickspam();
1630 if (IS_BOT_CLIENT(self))
1632 if (playerdemo_read())
1637 if (IS_PLAYER(self))
1641 if (self.race_penalty)
1642 if (time > self.race_penalty)
1643 self.race_penalty = 0;
1646 float not_allowed_to_move = 0;
1648 if (self.race_penalty)
1649 not_allowed_to_move = 1;
1652 if (time < game_starttime)
1653 not_allowed_to_move = 1;
1656 if (not_allowed_to_move)
1658 self.velocity = '0 0 0';
1659 self.movetype = MOVETYPE_NONE;
1661 self.disableclientprediction = 2;
1665 else if (self.disableclientprediction == 2)
1667 if (self.movetype == MOVETYPE_NONE)
1668 self.movetype = MOVETYPE_WALK;
1669 self.disableclientprediction = 0;
1675 if (self.movetype == MOVETYPE_NONE)
1678 // when we get here, disableclientprediction cannot be 2
1679 self.disableclientprediction = 0;
1691 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1693 // conveyors: first fix velocity
1694 if (self.conveyor.state)
1695 self.velocity -= self.conveyor.movedir;
1698 MUTATOR_CALLHOOK(PlayerPhysics);
1704 // float forcedodge = 1;
1707 // PM_dodging_checkpressedkeys();
1710 // PM_ClientMovement_Move();
1715 if (!IS_PLAYER(self))
1717 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1718 if (!self.spectatorspeed)
1719 self.spectatorspeed = maxspeed_mod;
1720 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1722 if (self.lastclassname != "player")
1724 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1725 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1726 else if (self.impulse == 11)
1727 self.spectatorspeed = maxspeed_mod;
1728 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1729 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1730 else if (self.impulse >= 1 && self.impulse <= 9)
1731 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1732 } // otherwise just clear
1735 maxspeed_mod = self.spectatorspeed;
1738 float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED) * maxspeed_mod;
1739 if(self.speed != spd)
1742 string temps = ftos(spd);
1743 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1744 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1745 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1746 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1754 if (!self.fixangle && !g_bugrigs)
1755 self.angles = '0 1 0' * self.v_angle.y;
1758 PM_check_hitground();
1763 if (IS_PLAYER(self))
1766 if (self.flags & FL_WATERJUMP)
1768 self.velocity_x = self.movedir_x;
1769 self.velocity_y = self.movedir_y;
1770 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1772 self.flags &= ~FL_WATERJUMP;
1773 self.teleport_time = 0;
1778 else if (g_bugrigs && IS_PLAYER(self))
1782 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS(self) & BUFF_FLIGHT))
1783 PM_fly(maxspeed_mod);
1785 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1786 PM_swim(maxspeed_mod);
1788 else if (time < self.ladder_time)
1789 PM_ladder(maxspeed_mod);
1791 else if (ITEMS(self) & IT_USING_JETPACK)
1792 PM_jetpack(maxspeed_mod);
1794 else if (IS_ONGROUND(self))
1795 PM_walk(buttons_prev, maxspeed_mod);
1798 PM_air(buttons_prev, maxspeed_mod);
1801 if (!IS_OBSERVER(self))
1807 if (IS_ONGROUND(self))
1808 self.lastground = time;
1810 // conveyors: then break velocity again
1811 if(self.conveyor.state)
1812 self.velocity += self.conveyor.movedir;
1814 self.lastflags = self.flags;
1816 self.lastclassname = self.classname;
1820 void SV_PlayerPhysics(void)
1822 void CSQC_ClientMovement_PlayerMove_Frame(void)
1829 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1830 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1831 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);