11 .float spectatorspeed;
16 .string lastclassname;
18 .float() PlayerPhysplug;
19 float AdjustAirAccelQW(float accelqw, float factor);
26 .float stat_sv_airaccel_qw;
27 .float stat_sv_airstrafeaccel_qw;
28 .float stat_sv_airspeedlimit_nonqw;
29 .float stat_sv_maxspeed;
30 .float stat_movement_highspeed;
32 .float stat_jetpack_accel_side;
33 .float stat_jetpack_accel_up;
34 .float stat_jetpack_antigravity;
35 .float stat_jetpack_fuel;
36 .float stat_jetpack_maxspeed_up;
37 .float stat_jetpack_maxspeed_side;
39 void Physics_AddStats()
41 // g_movementspeed hack
42 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
43 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
44 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
45 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
46 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
49 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
50 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
51 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
52 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
53 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
54 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
57 void Physics_UpdateStats(float maxspd_mod)
59 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
60 if (autocvar_sv_airstrafeaccel_qw)
61 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
63 self.stat_sv_airstrafeaccel_qw = 0;
64 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
65 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
66 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
68 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
69 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
70 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
71 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
72 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
73 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
77 float IsMoveInDirection(vector mv, float angle) // key mix factor
79 if (mv_x == 0 && mv_y == 0)
80 return 0; // avoid division by zero
81 angle -= RAD2DEG * atan2(mv_y, mv_x);
82 angle = remainder(angle, 360) / 45;
83 return angle > 1 ? 0 : angle < -1 ? 0 : 1 - fabs(angle);
86 float GeomLerp(float a, float lerp, float b)
88 return a == 0 ? (lerp < 1 ? 0 : b)
89 : b == 0 ? (lerp > 0 ? 0 : a)
90 : a * pow(fabs(b / a), lerp);
93 noref float pmove_waterjumptime;
95 const float unstick_count = 27;
96 vector unstick_offsets[unstick_count] =
98 // 1 no nudge (just return the original if this test passes)
101 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
102 '-0.125 0.000 0.000', '0.125 0.000 0.000',
103 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
104 // 4 diagonal flat nudges
105 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
106 '-0.125 0.125 0.000', '0.125 0.125 0.000',
107 // 8 diagonal upward nudges
108 '-0.125 0.000 0.125', '0.125 0.000 0.125',
109 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
110 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
111 '-0.125 0.125 0.125', '0.125 0.125 0.125',
112 // 8 diagonal downward nudges
113 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
114 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
115 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
116 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
119 void PM_ClientMovement_Unstick()
122 for (i = 0; i < unstick_count; i++)
124 vector neworigin = unstick_offsets[i] + self.origin;
125 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
126 if (!trace_startsolid)
128 self.origin = neworigin;
134 void PM_ClientMovement_UpdateStatus()
136 // make sure player is not stuck
137 PM_ClientMovement_Unstick();
140 if (PHYS_INPUT_BUTTON_CROUCH(self))
142 // wants to crouch, this always works..
143 if (!IS_DUCKED(self))
148 // wants to stand, if currently crouching we need to check for a
152 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
153 if (!trace_startsolid)
159 vector origin1 = self.origin + '0 0 1';
160 vector origin2 = self.origin - '0 0 1';
162 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
163 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
167 // this code actually "predicts" an impact; so let's clip velocity first
168 float f = dotproduct(self.velocity, trace_plane_normal);
169 if (f < 0) // only if moving downwards actually
170 self.velocity -= f * trace_plane_normal;
173 UNSET_ONGROUND(self);
175 // set watertype/waterlevel
176 origin1 = self.origin;
177 origin1_z += self.mins_z + 1;
178 self.waterlevel = WATERLEVEL_NONE;
180 self.watertype = (pointcontents(origin1) == CONTENT_WATER);
184 self.waterlevel = WATERLEVEL_WETFEET;
185 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
186 if(pointcontents(origin1) == CONTENT_WATER)
188 self.waterlevel = WATERLEVEL_SWIMMING;
189 origin1_z = self.origin_z + 22;
190 if(pointcontents(origin1) == CONTENT_WATER)
191 self.waterlevel = WATERLEVEL_SUBMERGED;
195 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
196 pmove_waterjumptime = 0;
199 void PM_ClientMovement_Move()
202 float t = PHYS_INPUT_TIMELENGTH;
203 vector primalvelocity = self.velocity;
204 PM_ClientMovement_UpdateStatus();
206 for (bump = 0; bump < 8 && self.velocity * self.velocity > 0; bump++)
208 vector neworigin = self.origin + t * self.velocity;
209 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
210 float old_trace1_fraction = trace_fraction;
211 vector old_trace1_endpos = trace_endpos;
212 vector old_trace1_plane_normal = trace_plane_normal;
213 if (trace_fraction < 1 && trace_plane_normal_z == 0)
215 // may be a step or wall, try stepping up
216 // first move forward at a higher level
217 vector currentorigin2 = self.origin;
218 currentorigin2_z += PHYS_STEPHEIGHT;
219 vector neworigin2 = neworigin;
220 neworigin2_z = self.origin_z + PHYS_STEPHEIGHT;
221 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
222 if (!trace_startsolid)
224 // then move down from there
225 currentorigin2 = trace_endpos;
226 neworigin2 = trace_endpos;
227 neworigin2_z = self.origin_z;
228 float old_trace2_fraction = trace_fraction;
229 vector old_trace2_plane_normal = trace_plane_normal;
230 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
231 //Con_Printf("%f %f %f %f : %f %f %f %f : %f %f %f %f\n", trace.fraction, trace.endpos[0], trace.endpos[1], trace.endpos[2], trace2.fraction, trace2.endpos[0], trace2.endpos[1], trace2.endpos[2], trace3.fraction, trace3.endpos[0], trace3.endpos[1], trace3.endpos[2]);
232 // accept the new trace if it made some progress
233 if (fabs(trace_endpos_x - old_trace1_endpos_x) >= 0.03125 || fabs(trace_endpos_y - old_trace1_endpos_y) >= 0.03125)
235 trace_fraction = old_trace2_fraction;
236 trace_endpos = trace_endpos;
237 trace_plane_normal = old_trace2_plane_normal;
241 trace_fraction = old_trace1_fraction;
242 trace_endpos = old_trace1_endpos;
243 trace_plane_normal = old_trace1_plane_normal;
248 // check if it moved at all
249 if (trace_fraction >= 0.001)
250 self.origin = trace_endpos;
252 // check if it moved all the way
253 if (trace_fraction == 1)
256 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
257 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
258 // this got commented out in a change that supposedly makes the code match QW better
259 // so if this is broken, maybe put it in an if (cls.protocol != PROTOCOL_QUAKEWORLD) block
260 if (trace_plane_normal_z > 0.7)
263 t -= t * trace_fraction;
265 float f = dotproduct(self.velocity, trace_plane_normal);
266 self.velocity -= f * trace_plane_normal;
268 if (pmove_waterjumptime > 0)
269 self.velocity = primalvelocity;
273 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
277 // this doesn't play well with analog input
278 if (PHYS_INPUT_MOVEVALUES(self).x == 0 || PHYS_INPUT_MOVEVALUES(self).y != 0)
279 return; // can't control movement if not moving forward or backward
282 k = 32 * (2 * IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), 0) - 1);
287 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
289 float zspeed = self.velocity_z;
291 float xyspeed = vlen(self.velocity);
292 self.velocity = normalize(self.velocity);
294 float dot = self.velocity * wishdir;
296 if (dot > 0) // we can't change direction while slowing down
298 k *= pow(dot, PHYS_AIRCONTROL_POWER)*PHYS_INPUT_TIMELENGTH;
299 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
300 k *= PHYS_AIRCONTROL;
301 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
304 self.velocity = self.velocity * xyspeed;
305 self.velocity_z = zspeed;
308 float AdjustAirAccelQW(float accelqw, float factor)
310 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
313 // example config for alternate speed clamping:
314 // sv_airaccel_qw 0.8
315 // sv_airaccel_sideways_friction 0
316 // prvm_globalset server speedclamp_mode 1
318 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
320 float speedclamp = stretchfactor > 0 ? stretchfactor
321 : accelqw < 0 ? 1 // full clamping, no stretch
324 accelqw = fabs(accelqw);
326 if (GAMEPLAYFIX_Q2AIRACCELERATE)
327 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
329 float vel_straight = self.velocity * wishdir;
330 float vel_z = self.velocity_z;
331 vector vel_xy = vec2(self.velocity);
332 vector vel_perpend = vel_xy - vel_straight * wishdir;
334 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
336 float vel_xy_current = vlen(vel_xy);
338 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
339 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
340 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
341 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
342 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
344 if (sidefric < 0 && (vel_perpend*vel_perpend))
345 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
347 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
348 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
350 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
351 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
352 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
353 // obviously, this cannot be
359 vel_perpend *= max(fmin, f);
363 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
365 vel_xy = vel_straight * wishdir + vel_perpend;
369 float vel_xy_preclamp;
370 vel_xy_preclamp = vlen(vel_xy);
371 if (vel_xy_preclamp > 0) // prevent division by zero
373 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
374 if (vel_xy_current < vel_xy_preclamp)
375 vel_xy *= (vel_xy_current / vel_xy_preclamp);
379 self.velocity = vel_xy + vel_z * '0 0 1';
382 void PM_AirAccelerate(vector wishdir, float wishspeed)
387 vector curvel = self.velocity;
389 float curspeed = vlen(curvel);
391 if (wishspeed > curspeed * 1.01)
392 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
395 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
396 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
398 vector wishvel = wishdir * wishspeed;
399 vector acceldir = wishvel - curvel;
400 float addspeed = vlen(acceldir);
401 acceldir = normalize(acceldir);
403 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
405 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
407 vector curdir = normalize(curvel);
408 float dot = acceldir * curdir;
410 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
413 self.velocity += accelspeed * acceldir;
421 When you press the jump key
424 void PlayerJump (void)
427 if (PHYS_FROZEN(self))
428 return; // no jumping in freezetag when frozen
430 if (self.player_blocked)
431 return; // no jumping while blocked
433 float doublejump = FALSE;
434 float mjumpheight = PHYS_JUMPVELOCITY;
436 player_multijump = doublejump;
437 player_jumpheight = mjumpheight;
438 if (MUTATOR_CALLHOOK(PlayerJump))
441 doublejump = player_multijump;
442 mjumpheight = player_jumpheight;
444 if (autocvar_sv_doublejump)
446 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
447 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
451 // we MUST clip velocity here!
453 f = self.velocity * trace_plane_normal;
455 self.velocity -= f * trace_plane_normal;
459 if (self.waterlevel >= WATERLEVEL_SWIMMING)
461 self.velocity_z = self.stat_sv_maxspeed * 0.7;
466 if (!IS_ONGROUND(self))
469 if (self.cvar_cl_movement_track_canjump)
470 if (!(self.flags & FL_JUMPRELEASED))
473 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
474 // velocity bounds. Final velocity is bound between (jumpheight *
475 // min + jumpheight) and (jumpheight * max + jumpheight);
477 if (autocvar_sv_jumpspeedcap_min != "")
479 float minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
481 if (self.velocity_z < minjumpspeed)
482 mjumpheight += minjumpspeed - self.velocity_z;
485 if (autocvar_sv_jumpspeedcap_max != "")
487 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
488 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
490 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
492 float maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
494 if (self.velocity_z > maxjumpspeed)
495 mjumpheight -= self.velocity_z - maxjumpspeed;
499 if (!(self.lastflags & FL_ONGROUND))
501 if (autocvar_speedmeter)
502 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
503 if (self.lastground < time - 0.3)
505 self.velocity_x *= (1 - autocvar_sv_friction_on_land);
506 self.velocity_y *= (1 - autocvar_sv_friction_on_land);
508 if (self.jumppadcount > 1)
509 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
510 self.jumppadcount = 0;
513 self.oldvelocity_z = self.velocity_z += mjumpheight;
515 UNSET_ONGROUND(self);
516 self.flags &= ~FL_JUMPRELEASED;
518 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
520 if (autocvar_g_jump_grunt)
521 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
523 self.restart_jump = -1; // restart jump anim next time
524 // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
528 void CheckWaterJump()
530 // check for a jump-out-of-water
531 makevectors(PHYS_INPUT_ANGLES(self));
532 vector start = self.origin;
535 normalize(v_forward);
536 vector end = start + v_forward*24;
537 traceline (start, end, TRUE, self);
538 if (trace_fraction < 1)
540 start_z = start_z + self.maxs_z - 8;
541 end = start + v_forward*24;
542 self.movedir = trace_plane_normal * -50;
543 traceline(start, end, TRUE, self);
544 if (trace_fraction == 1)
545 { // open at eye level
546 self.velocity_z = 225;
548 self.flags |= FL_WATERJUMP;
549 self.flags &= ~FL_JUMPRELEASED;
550 self.teleport_time = time + 2; // safety net
556 void CheckPlayerJump()
559 if (self.BUTTON_JUMP)
562 self.flags |= FL_JUMPRELEASED;
565 if (self.waterlevel == WATERLEVEL_SWIMMING)
569 float racecar_angle(float forward, float down)
577 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
579 float angle_mult = forward / (800 + forward);
582 return ret * angle_mult + 360 * (1 - angle_mult);
584 return ret * angle_mult;
587 void RaceCarPhysics()
590 // using this move type for "big rigs"
591 // the engine does not push the entity!
595 vector angles_save = self.angles;
596 float accel = bound(-1, PHYS_INPUT_MOVEVALUES(self).x / self.stat_sv_maxspeed, 1);
597 float steer = bound(-1, PHYS_INPUT_MOVEVALUES(self).y / self.stat_sv_maxspeed, 1);
599 if (g_bugrigs_reverse_speeding)
603 // back accel is DIGITAL
604 // to prevent speedhack
614 makevectors(self.angles); // new forward direction!
616 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
618 float myspeed = self.velocity * v_forward;
619 float upspeed = self.velocity * v_up;
621 // responsiveness factor for steering and acceleration
622 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
623 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
626 if (myspeed < 0 && g_bugrigs_reverse_spinning)
627 steerfactor = -myspeed * g_bugrigs_steer;
629 steerfactor = -myspeed * f * g_bugrigs_steer;
632 if (myspeed < 0 && g_bugrigs_reverse_speeding)
633 accelfactor = g_bugrigs_accel;
635 accelfactor = f * g_bugrigs_accel;
636 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
642 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
646 if (!g_bugrigs_reverse_speeding)
647 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
654 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
658 if (g_bugrigs_reverse_stopping)
661 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
664 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
665 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
667 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
668 makevectors(self.angles); // new forward direction!
670 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
672 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
676 float myspeed = vlen(self.velocity);
678 // responsiveness factor for steering and acceleration
679 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
680 float steerfactor = -myspeed * f;
681 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
683 rigvel = self.velocity;
684 makevectors(self.angles); // new forward direction!
687 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
688 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
689 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
690 //MAXIMA: solve(total_acceleration(v) = 0, v);
692 if (g_bugrigs_planar_movement)
694 vector rigvel_xy, neworigin, up;
697 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
698 rigvel_xy = vec2(rigvel);
700 if (g_bugrigs_planar_movement_car_jumping)
703 mt = MOVE_NOMONSTERS;
705 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
706 up = trace_endpos - self.origin;
708 // BUG RIGS: align the move to the surface instead of doing collision testing
710 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
713 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
715 if (trace_fraction < 0.5)
718 neworigin = self.origin;
721 neworigin = trace_endpos;
723 if (trace_fraction < 1)
725 // now set angles_x so that the car points parallel to the surface
726 self.angles = vectoangles(
727 '1 0 0' * v_forward_x * trace_plane_normal_z
729 '0 1 0' * v_forward_y * trace_plane_normal_z
731 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
737 // now set angles_x so that the car points forward, but is tilted in velocity direction
738 UNSET_ONGROUND(self);
741 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
742 self.movetype = MOVETYPE_NOCLIP;
746 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
747 self.velocity = rigvel;
748 self.movetype = MOVETYPE_FLY;
752 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
753 if (trace_fraction != 1)
755 self.angles = vectoangles2(
756 '1 0 0' * v_forward_x * trace_plane_normal_z
758 '0 1 0' * v_forward_y * trace_plane_normal_z
760 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
768 vel_local_x = v_forward * self.velocity;
769 vel_local_y = v_right * self.velocity;
770 vel_local_z = v_up * self.velocity;
772 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
773 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
777 vector vf1, vu1, smoothangles;
778 makevectors(self.angles);
779 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
784 makevectors(angles_save);
785 vf1 = vf1 + v_forward * (1 - f);
786 vu1 = vu1 + v_up * (1 - f);
787 smoothangles = vectoangles2(vf1, vu1);
788 self.angles_x = -smoothangles_x;
789 self.angles_z = smoothangles_z;
793 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
794 .float specialcommand_pos;
795 void SpecialCommand()
801 if (!CheatImpulse(99))
802 print("A hollow voice says \"Plugh\".\n");
808 float speedaward_speed;
809 string speedaward_holder;
810 string speedaward_uid;
812 void race_send_speedaward(float msg)
815 // send the best speed of the round
816 WriteByte(msg, SVC_TEMPENTITY);
817 WriteByte(msg, TE_CSQC_RACE);
818 WriteByte(msg, RACE_NET_SPEED_AWARD);
819 WriteInt24_t(msg, floor(speedaward_speed+0.5));
820 WriteString(msg, speedaward_holder);
825 float speedaward_alltimebest;
826 string speedaward_alltimebest_holder;
827 string speedaward_alltimebest_uid;
829 void race_send_speedaward_alltimebest(float msg)
832 // send the best speed
833 WriteByte(msg, SVC_TEMPENTITY);
834 WriteByte(msg, TE_CSQC_RACE);
835 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
836 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
837 WriteString(msg, speedaward_alltimebest_holder);
841 float PM_check_keepaway(void)
844 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
850 void PM_check_race_movetime(void)
853 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
854 float f = floor(self.race_movetime_frac);
855 self.race_movetime_frac -= f;
856 self.race_movetime_count += f;
857 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
861 float PM_check_specialcommand(float buttons)
867 else if (buttons == 1)
869 else if (buttons == 2)
871 else if (buttons == 128)
873 else if (buttons == 256)
875 else if (buttons == 512)
877 else if (buttons == 1024)
882 if (c == substring(specialcommand, self.specialcommand_pos, 1))
884 self.specialcommand_pos += 1;
885 if (self.specialcommand_pos >= strlen(specialcommand))
887 self.specialcommand_pos = 0;
892 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
893 self.specialcommand_pos = 0;
898 void PM_check_nickspam(void)
901 if (time >= self.nickspamtime)
903 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
905 // slight annoyance for nick change scripts
906 PHYS_INPUT_MOVEVALUES(self) = -1 * PHYS_INPUT_MOVEVALUES(self);
907 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
909 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!
911 PHYS_INPUT_ANGLES(self)_x = random() * 360;
912 PHYS_INPUT_ANGLES(self)_y = random() * 360;
913 // at least I'm not forcing retardedview by also assigning to angles_z
914 self.fixangle = TRUE;
920 void PM_check_punch()
923 if (self.punchangle != '0 0 0')
925 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
927 self.punchangle = normalize(self.punchangle) * f;
929 self.punchangle = '0 0 0';
932 if (self.punchvector != '0 0 0')
934 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
936 self.punchvector = normalize(self.punchvector) * f;
938 self.punchvector = '0 0 0';
943 void PM_check_spider(void)
946 if (time >= self.spider_slowness)
948 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
949 self.stat_sv_airspeedlimit_nonqw *= 0.5;
953 // predict frozen movement, as frozen players CAN move in some cases
954 void PM_check_frozen(void)
956 if (!PHYS_FROZEN(self))
958 if (PHYS_DODGING_FROZEN
960 && IS_REAL_CLIENT(self)
964 PHYS_INPUT_MOVEVALUES(self)_x = bound(-5, PHYS_INPUT_MOVEVALUES(self).x, 5);
965 PHYS_INPUT_MOVEVALUES(self)_y = bound(-5, PHYS_INPUT_MOVEVALUES(self).y, 5);
966 PHYS_INPUT_MOVEVALUES(self)_z = bound(-5, PHYS_INPUT_MOVEVALUES(self).z, 5);
969 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
971 vector midpoint = ((self.absmin + self.absmax) * 0.5);
972 if (pointcontents(midpoint) == CONTENT_WATER)
974 self.velocity = self.velocity * 0.5;
976 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
977 self.velocity_z = 200;
981 void PM_check_blocked(void)
984 if (!self.player_blocked)
986 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
987 self.disableclientprediction = 1;
992 float speedaward_lastsent;
993 float speedaward_lastupdate;
994 string GetMapname(void);
996 void PM_check_race(void)
999 if not(g_cts || g_race)
1001 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1003 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1004 speedaward_holder = self.netname;
1005 speedaward_uid = self.crypto_idfp;
1006 speedaward_lastupdate = time;
1008 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1010 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1011 race_send_speedaward(MSG_ALL);
1012 speedaward_lastsent = speedaward_speed;
1013 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1015 speedaward_alltimebest = speedaward_speed;
1016 speedaward_alltimebest_holder = speedaward_holder;
1017 speedaward_alltimebest_uid = speedaward_uid;
1018 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1019 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1020 race_send_speedaward_alltimebest(MSG_ALL);
1026 void PM_check_vortex(void)
1029 float xyspeed = vlen(vec2(self.velocity));
1030 if (self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1032 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1033 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1034 float f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1035 // add the extra charge
1036 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * PHYS_INPUT_TIMELENGTH);
1041 void PM_fly(float maxspd_mod)
1043 // noclipping or flying
1044 UNSET_ONGROUND(self);
1046 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1047 makevectors(PHYS_INPUT_ANGLES(self));
1048 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1049 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1050 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1051 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1053 vector wishdir = normalize(wishvel);
1054 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1055 if (time >= self.teleport_time)
1056 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1059 void PM_swim(float maxspd_mod)
1062 UNSET_ONGROUND(self);
1064 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1065 // water jump only in certain situations
1066 // this mimics quakeworld code
1067 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1069 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1070 makevectors(yawangles);
1071 vector forward = v_forward;
1072 vector spot = self.origin + 24 * forward;
1074 traceline(spot, spot, MOVE_NOMONSTERS, self);
1075 if (trace_startsolid)
1078 traceline(spot, spot, MOVE_NOMONSTERS, self);
1079 if (!trace_startsolid)
1081 self.velocity = forward * 50;
1082 self.velocity_z = 310;
1083 pmove_waterjumptime = 2;
1084 UNSET_ONGROUND(self);
1085 SET_JUMP_HELD(self);
1089 makevectors(PHYS_INPUT_ANGLES(self));
1090 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1091 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1092 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1093 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1094 if (wishvel == '0 0 0')
1095 wishvel = '0 0 -60'; // drift towards bottom
1097 vector wishdir = normalize(wishvel);
1098 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1100 if (IS_DUCKED(self))
1103 // if (pmove_waterjumptime <= 0) // TODO: use
1106 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1107 f = min(max(0, f), 1);
1110 f = wishspeed - self.velocity * wishdir;
1113 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1114 self.velocity += accelspeed * wishdir;
1117 // holding jump button swims upward slowly
1121 if (self.watertype & CONTENT_LAVA)
1122 self.velocity_z = 50;
1123 else if (self.watertype & CONTENT_SLIME)
1124 self.velocity_z = 80;
1127 if (IS_NEXUIZ_DERIVED(gamemode))
1129 self.velocity_z = 200;
1132 self.velocity_z = 100;
1137 PM_ClientMovement_Move();
1138 // water acceleration
1139 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1142 void PM_ladder(float maxspd_mod)
1145 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1146 UNSET_ONGROUND(self);
1149 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1150 if (PHYS_ENTGRAVITY(self))
1151 g *= PHYS_ENTGRAVITY(self);
1152 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1155 self.velocity_z += g;
1158 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1159 makevectors(PHYS_INPUT_ANGLES(self));
1160 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1161 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1162 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1163 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1164 self.velocity_z += g;
1165 if (self.ladder_entity.classname == "func_water")
1167 float f = vlen(wishvel);
1168 if (f > self.ladder_entity.speed)
1169 wishvel *= (self.ladder_entity.speed / f);
1171 self.watertype = self.ladder_entity.skin;
1172 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1173 if ((self.origin_z + self.view_ofs_z) < f)
1174 self.waterlevel = WATERLEVEL_SUBMERGED;
1175 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1176 self.waterlevel = WATERLEVEL_SWIMMING;
1177 else if ((self.origin_z + self.mins_z + 1) < f)
1178 self.waterlevel = WATERLEVEL_WETFEET;
1181 self.waterlevel = WATERLEVEL_NONE;
1182 self.watertype = CONTENT_EMPTY;
1186 vector wishdir = normalize(wishvel);
1187 float wishspeed = min(vlen(wishvel), self.stat_sv_maxspeed * maxspd_mod);
1188 if (time >= self.teleport_time)
1189 // water acceleration
1190 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1194 void PM_jetpack(float maxspd_mod)
1196 //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1197 makevectors(PHYS_INPUT_ANGLES(self));
1198 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1199 + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1200 // add remaining speed as Z component
1201 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1202 // fix speedhacks :P
1203 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1204 // add the unused velocity as up component
1207 // if (self.BUTTON_JUMP)
1208 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1210 // it is now normalized, so...
1211 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1212 float a_up = PHYS_JETPACK_ACCEL_UP;
1213 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1215 wishvel_x *= a_side;
1216 wishvel_y *= a_side;
1221 //////////////////////////////////////////////////////////////////////////////////////
1222 // finding the maximum over all vectors of above form
1223 // with wishvel having an absolute value of 1
1224 //////////////////////////////////////////////////////////////////////////////////////
1225 // we're finding the maximum over
1226 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1227 // for z in the range from -1 to 1
1228 //////////////////////////////////////////////////////////////////////////////////////
1229 // maximum is EITHER attained at the single extreme point:
1230 float a_diff = a_side * a_side - a_up * a_up;
1234 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1235 if (f > -1 && f < 1) // can it be attained?
1237 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1238 //print("middle\n");
1241 // OR attained at z = 1:
1242 f = (a_up + a_add) * (a_up + a_add);
1248 // OR attained at z = -1:
1249 f = (a_up - a_add) * (a_up - a_add);
1253 //print("bottom\n");
1256 //////////////////////////////////////////////////////////////////////////////////////
1258 //print("best possible acceleration: ", ftos(best), "\n");
1261 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1262 if (wishvel_z - PHYS_GRAVITY > 0)
1263 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1265 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1268 fvel = vlen(wishvel);
1271 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1273 fvel = min(1, vlen(wishvel) / best);
1274 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1275 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1279 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1281 if (f > 0 && wishvel != '0 0 0')
1283 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1284 UNSET_ONGROUND(self);
1287 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1288 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1290 self.items |= IT_USING_JETPACK;
1292 // jetpack also inhibits health regeneration, but only for 1 second
1293 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1298 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1299 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1300 self.velocity_z -= g * 0.5;
1302 self.velocity_z -= g;
1303 PM_ClientMovement_Move();
1304 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1305 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1306 self.velocity_z -= g * 0.5;
1310 void PM_walk(float buttons_prev, float maxspd_mod)
1313 // we get here if we ran out of ammo
1314 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1315 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1316 if (!(self.lastflags & FL_ONGROUND))
1318 if (autocvar_speedmeter)
1319 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1320 if (self.lastground < time - 0.3)
1321 self.velocity *= (1 - autocvar_sv_friction_on_land);
1322 if (self.jumppadcount > 1)
1323 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1324 self.jumppadcount = 0;
1328 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1329 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1330 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1332 vector wishdir = normalize(wishvel);
1333 float wishspeed = vlen(wishvel);
1335 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1336 if (IS_DUCKED(self))
1339 // apply edge friction
1340 float f = vlen(vec2(self.velocity));
1343 // TODO: apply edge friction
1344 // apply ground friction
1345 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1349 Mathematical analysis time!
1351 Our goal is to invert this mess.
1353 For the two cases we get:
1354 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1355 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1356 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1358 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1359 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1361 These cases would be chosen ONLY if:
1363 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1364 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1366 v0 >= PHYS_STOPSPEED
1367 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1368 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1371 float addspeed = wishspeed - self.velocity * wishdir;
1374 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1375 self.velocity += accelspeed * wishdir;
1377 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1378 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1379 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1380 if (self.velocity * self.velocity)
1381 PM_ClientMovement_Move();
1382 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1383 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1384 self.velocity_z -= g * 0.5;
1387 void PM_air(float buttons_prev, float maxspd_mod)
1390 // we get here if we ran out of ammo
1391 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && PHYS_AMMO_FUEL(self) < 0.01)
1392 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1394 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1395 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1396 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1398 vector wishdir = normalize(wishvel);
1399 float wishspeed = vlen(wishvel);
1402 if (time >= self.teleport_time)
1404 if (pmove_waterjumptime <= 0)
1407 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1409 // apply air speed limit
1410 float airaccelqw = PHYS_AIRACCEL_QW(self);
1411 float wishspeed0 = wishspeed;
1412 wishspeed = min(wishspeed, maxairspd);
1413 if (IS_DUCKED(self))
1415 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1417 float accelerating = (self.velocity * wishdir > 0);
1418 float wishspeed2 = wishspeed;
1421 if (PHYS_AIRSTOPACCELERATE)
1423 vector curdir = normalize(vec2(self.velocity));
1424 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1426 // note that for straight forward jumping:
1427 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1428 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1430 // dv/dt = accel * maxspeed (when slow)
1431 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1432 // log dv/dt = logaccel + logmaxspeed (when slow)
1433 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1434 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1435 if (PHYS_MAXAIRSTRAFESPEED)
1436 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1437 if (PHYS_AIRSTRAFEACCELERATE)
1438 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1439 if (PHYS_AIRSTRAFEACCEL_QW(self))
1441 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1443 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1446 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1447 PM_AirAccelerate(wishdir, wishspeed2);
1449 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1451 if (PHYS_AIRCONTROL)
1452 CPM_PM_Aircontrol(wishdir, wishspeed2);
1454 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1455 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1456 self.velocity_z -= g * 0.5;
1458 self.velocity_z -= g;
1459 PM_ClientMovement_Move();
1460 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1461 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1462 self.velocity_z -= g * 0.5;
1465 // used for calculating airshots
1466 float PM_is_flying()
1468 if (IS_ONGROUND(self))
1470 if (self.waterlevel >= WATERLEVEL_SWIMMING)
1472 traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1473 return trace_fraction >= 1;
1478 float buttons = PHYS_INPUT_BUTTON_MASK(self);
1480 //Con_Printf(" %f", PHYS_INPUT_TIMELENGTH);
1481 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1482 UNSET_JUMP_HELD(self); // canjump = true
1483 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1484 PM_ClientMovement_UpdateStatus();
1488 WarpZone_PlayerPhysics_FixVAngle();
1490 float maxspeed_mod = 1;
1491 maxspeed_mod *= PM_check_keepaway();
1492 maxspeed_mod *= PHYS_HIGHSPEED;
1495 Physics_UpdateStats(maxspeed_mod);
1497 if (self.PlayerPhysplug)
1498 if (self.PlayerPhysplug())
1502 PM_check_race_movetime();
1504 anticheat_physics();
1507 if (PM_check_specialcommand(buttons))
1512 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1513 self.parm_idlesince = time;
1516 float buttons_prev = self.buttons_old;
1517 self.buttons_old = buttons;
1518 self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1519 self.v_angle_old = PHYS_INPUT_ANGLES(self);
1521 PM_check_nickspam();
1525 if (IS_BOT_CLIENT(self))
1527 if (playerdemo_read())
1532 self.items &= ~IT_USING_JETPACK;
1534 if (IS_PLAYER(self))
1538 if (self.race_penalty)
1539 if (time > self.race_penalty)
1540 self.race_penalty = 0;
1543 float not_allowed_to_move = 0;
1545 if (self.race_penalty)
1546 not_allowed_to_move = 1;
1549 if (!autocvar_sv_ready_restart_after_countdown)
1550 if (time < game_starttime)
1551 not_allowed_to_move = 1;
1554 if (not_allowed_to_move)
1556 self.velocity = '0 0 0';
1557 self.movetype = MOVETYPE_NONE;
1559 self.disableclientprediction = 2;
1563 else if (self.disableclientprediction == 2)
1565 if (self.movetype == MOVETYPE_NONE)
1566 self.movetype = MOVETYPE_WALK;
1567 self.disableclientprediction = 0;
1573 if (self.movetype == MOVETYPE_NONE)
1578 // when we get here, disableclientprediction cannot be 2
1579 self.disableclientprediction = 0;
1580 if (time < self.ladder_time)
1581 self.disableclientprediction = 1;
1593 if (self.in_swamp) {
1594 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1599 // conveyors: first fix velocity
1600 if (self.conveyor.state)
1601 self.velocity -= self.conveyor.movedir;
1605 MUTATOR_CALLHOOK(PlayerPhysics);
1607 // float forcedodge = 1;
1610 // PM_dodging_checkpressedkeys();
1613 // PM_ClientMovement_Move();
1618 if (!IS_PLAYER(self))
1620 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1621 if (!self.spectatorspeed)
1622 self.spectatorspeed = maxspeed_mod;
1623 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1625 if (self.lastclassname != "player")
1627 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1628 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1629 else if (self.impulse == 11)
1630 self.spectatorspeed = maxspeed_mod;
1631 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1632 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1633 else if (self.impulse >= 1 && self.impulse <= 9)
1634 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1635 } // otherwise just clear
1638 maxspeed_mod *= self.spectatorspeed;
1643 // if dead, behave differently
1644 // in CSQC, physics don't handle dead player
1650 if (!self.fixangle && !g_bugrigs)
1651 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1655 if (IS_ONGROUND(self))
1656 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1660 if (self.waterlevel < WATERLEVEL_SWIMMING)
1661 if (time >= self.ladder_time)
1664 self.nextstep = time + 0.3 + random() * 0.1;
1665 trace_dphitq3surfaceflags = 0;
1666 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1667 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1669 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1670 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1672 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1682 if (IS_PLAYER(self))
1687 if (self.flags & /* FL_WATERJUMP */ 2048)
1689 self.velocity_x = self.movedir_x;
1690 self.velocity_y = self.movedir_y;
1691 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1693 self.flags &= ~/* FL_WATERJUMP */ 2048;
1694 self.teleport_time = 0;
1699 else if (g_bugrigs && IS_PLAYER(self))
1703 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1704 PM_fly(maxspeed_mod);
1706 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1707 PM_swim(maxspeed_mod);
1710 else if (time < self.ladder_time)
1711 PM_ladder(maxspeed_mod);
1714 else if ((ITEMS(self) & IT_JETPACK) && PHYS_INPUT_BUTTON_HOOK(self) && (!PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) > 0 || (ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO)) && !PHYS_FROZEN(self))
1715 PM_jetpack(maxspeed_mod);
1720 // jump if on ground with jump button pressed but only if it has been
1721 // released at least once since the last jump
1722 if (PHYS_INPUT_BUTTON_JUMP(self))
1724 if (IS_ONGROUND(self) && (!IS_JUMP_HELD(self) || !cvar("cl_movement_track_canjump")))
1726 self.velocity_z += PHYS_JUMPVELOCITY;
1727 UNSET_ONGROUND(self);
1728 SET_JUMP_HELD(self); // canjump = false
1732 UNSET_JUMP_HELD(self); // canjump = true
1734 if (IS_ONGROUND(self))
1735 PM_walk(buttons_prev, maxspeed_mod);
1737 PM_air(buttons_prev, maxspeed_mod);
1741 if (!IS_OBSERVER(self))
1747 if (IS_ONGROUND(self))
1748 self.lastground = time;
1751 // conveyors: then break velocity again
1752 if (self.conveyor.state)
1753 self.velocity += self.conveyor.movedir;
1755 self.lastflags = self.flags;
1756 self.lastclassname = self.classname;
1759 void CSQC_ClientMovement_PlayerMove_Frame()
1761 // if a move is more than 50ms, do it as two moves (matching qwsv)
1762 //Con_Printf("%i ", self.cmd.msec);
1763 if (PHYS_INPUT_TIMELENGTH > 0.0005)
1765 if (PHYS_INPUT_TIMELENGTH > 0.05)
1767 PHYS_INPUT_TIMELENGTH /= 2;
1773 // we REALLY need this handling to happen, even if the move is not executed
1774 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1775 UNSET_JUMP_HELD(self); // canjump = true
1780 void SV_PlayerPhysics(void)