14 .string lastclassname;
16 .float() PlayerPhysplug;
17 float AdjustAirAccelQW(float accelqw, float factor);
24 .float stat_sv_airaccel_qw;
25 .float stat_sv_airstrafeaccel_qw;
26 .float stat_sv_airspeedlimit_nonqw;
27 .float stat_sv_maxspeed;
28 .float stat_movement_highspeed;
30 .float stat_jetpack_accel_side;
31 .float stat_jetpack_accel_up;
32 .float stat_jetpack_antigravity;
33 .float stat_jetpack_fuel;
34 .float stat_jetpack_maxspeed_up;
35 .float stat_jetpack_maxspeed_side;
37 void Physics_AddStats()
39 // g_movementspeed hack
40 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
41 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
42 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
43 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
44 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
47 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
48 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
49 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
50 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
51 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
52 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
54 // hack to fix track_canjump
55 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
58 void Physics_UpdateStats(float maxspd_mod)
60 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
61 if (autocvar_sv_airstrafeaccel_qw)
62 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
64 self.stat_sv_airstrafeaccel_qw = 0;
65 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
66 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
67 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
69 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
70 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
71 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
72 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
73 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
74 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
78 float IsMoveInDirection(vector mv, float angle) // key mix factor
80 if (mv_x == 0 && mv_y == 0)
81 return 0; // avoid division by zero
82 angle -= RAD2DEG * atan2(mv_y, mv_x);
83 angle = remainder(angle, 360) / 45;
84 return angle > 1 ? 0 : angle < -1 ? 0 : 1 - fabs(angle);
87 float GeomLerp(float a, float lerp, float b)
89 return a == 0 ? (lerp < 1 ? 0 : b)
90 : b == 0 ? (lerp > 0 ? 0 : a)
91 : a * pow(fabs(b / a), lerp);
94 noref float pmove_waterjumptime;
96 const float unstick_count = 27;
97 vector unstick_offsets[unstick_count] =
99 // 1 no nudge (just return the original if this test passes)
102 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
103 '-0.125 0.000 0.000', '0.125 0.000 0.000',
104 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
105 // 4 diagonal flat nudges
106 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
107 '-0.125 0.125 0.000', '0.125 0.125 0.000',
108 // 8 diagonal upward nudges
109 '-0.125 0.000 0.125', '0.125 0.000 0.125',
110 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
111 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
112 '-0.125 0.125 0.125', '0.125 0.125 0.125',
113 // 8 diagonal downward nudges
114 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
115 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
116 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
117 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
120 void PM_ClientMovement_Unstick()
123 for (i = 0; i < unstick_count; i++)
125 vector neworigin = unstick_offsets[i] + self.origin;
126 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
127 if (!trace_startsolid)
129 self.origin = neworigin;
135 void PM_ClientMovement_UpdateStatus()
137 // make sure player is not stuck
138 PM_ClientMovement_Unstick();
141 if (PHYS_INPUT_BUTTON_CROUCH(self))
143 // wants to crouch, this always works..
144 if (!IS_DUCKED(self))
149 // wants to stand, if currently crouching we need to check for a
153 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
154 if (!trace_startsolid)
160 vector origin1 = self.origin + '0 0 1';
161 vector origin2 = self.origin - '0 0 1';
163 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
164 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
168 // this code actually "predicts" an impact; so let's clip velocity first
169 float f = dotproduct(self.velocity, trace_plane_normal);
170 if (f < 0) // only if moving downwards actually
171 self.velocity -= f * trace_plane_normal;
174 UNSET_ONGROUND(self);
176 // set watertype/waterlevel
177 origin1 = self.origin;
178 origin1_z += self.mins_z + 1;
179 self.waterlevel = WATERLEVEL_NONE;
181 self.watertype = (pointcontents(origin1) == CONTENT_WATER);
185 self.waterlevel = WATERLEVEL_WETFEET;
186 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
187 if(pointcontents(origin1) == CONTENT_WATER)
189 self.waterlevel = WATERLEVEL_SWIMMING;
190 origin1_z = self.origin_z + 22;
191 if(pointcontents(origin1) == CONTENT_WATER)
192 self.waterlevel = WATERLEVEL_SUBMERGED;
196 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
197 pmove_waterjumptime = 0;
200 void PM_ClientMovement_Move()
203 float t = PHYS_INPUT_TIMELENGTH;
204 vector primalvelocity = self.velocity;
205 PM_ClientMovement_UpdateStatus();
207 for (bump = 0; bump < 8 && self.velocity * self.velocity > 0; bump++)
209 vector neworigin = self.origin + t * self.velocity;
210 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
211 float old_trace1_fraction = trace_fraction;
212 vector old_trace1_endpos = trace_endpos;
213 vector old_trace1_plane_normal = trace_plane_normal;
214 if (trace_fraction < 1 && trace_plane_normal_z == 0)
216 // may be a step or wall, try stepping up
217 // first move forward at a higher level
218 vector currentorigin2 = self.origin;
219 currentorigin2_z += PHYS_STEPHEIGHT;
220 vector neworigin2 = neworigin;
221 neworigin2_z = self.origin_z + PHYS_STEPHEIGHT;
222 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
223 if (!trace_startsolid)
225 // then move down from there
226 currentorigin2 = trace_endpos;
227 neworigin2 = trace_endpos;
228 neworigin2_z = self.origin_z;
229 float old_trace2_fraction = trace_fraction;
230 vector old_trace2_plane_normal = trace_plane_normal;
231 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
232 //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]);
233 // accept the new trace if it made some progress
234 if (fabs(trace_endpos_x - old_trace1_endpos_x) >= 0.03125 || fabs(trace_endpos_y - old_trace1_endpos_y) >= 0.03125)
236 trace_fraction = old_trace2_fraction;
237 trace_endpos = trace_endpos;
238 trace_plane_normal = old_trace2_plane_normal;
242 trace_fraction = old_trace1_fraction;
243 trace_endpos = old_trace1_endpos;
244 trace_plane_normal = old_trace1_plane_normal;
249 // check if it moved at all
250 if (trace_fraction >= 0.001)
251 self.origin = trace_endpos;
253 // check if it moved all the way
254 if (trace_fraction == 1)
257 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
258 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
259 // this got commented out in a change that supposedly makes the code match QW better
260 // so if this is broken, maybe put it in an if (cls.protocol != PROTOCOL_QUAKEWORLD) block
261 if (trace_plane_normal_z > 0.7)
264 t -= t * trace_fraction;
266 float f = dotproduct(self.velocity, trace_plane_normal);
267 self.velocity -= f * trace_plane_normal;
269 if (pmove_waterjumptime > 0)
270 self.velocity = primalvelocity;
274 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
278 // this doesn't play well with analog input
279 if (PHYS_INPUT_MOVEVALUES(self).x == 0 || PHYS_INPUT_MOVEVALUES(self).y != 0)
280 return; // can't control movement if not moving forward or backward
283 k = 32 * (2 * IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), 0) - 1);
288 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
290 float zspeed = self.velocity_z;
292 float xyspeed = vlen(self.velocity);
293 self.velocity = normalize(self.velocity);
295 float dot = self.velocity * wishdir;
297 if (dot > 0) // we can't change direction while slowing down
299 k *= pow(dot, PHYS_AIRCONTROL_POWER)*PHYS_INPUT_TIMELENGTH;
300 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
301 k *= PHYS_AIRCONTROL;
302 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
305 self.velocity = self.velocity * xyspeed;
306 self.velocity_z = zspeed;
309 float AdjustAirAccelQW(float accelqw, float factor)
311 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
314 // example config for alternate speed clamping:
315 // sv_airaccel_qw 0.8
316 // sv_airaccel_sideways_friction 0
317 // prvm_globalset server speedclamp_mode 1
319 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
321 float speedclamp = stretchfactor > 0 ? stretchfactor
322 : accelqw < 0 ? 1 // full clamping, no stretch
325 accelqw = fabs(accelqw);
327 if (GAMEPLAYFIX_Q2AIRACCELERATE)
328 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
330 float vel_straight = self.velocity * wishdir;
331 float vel_z = self.velocity_z;
332 vector vel_xy = vec2(self.velocity);
333 vector vel_perpend = vel_xy - vel_straight * wishdir;
335 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
337 float vel_xy_current = vlen(vel_xy);
339 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
340 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
341 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
342 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
343 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
345 if (sidefric < 0 && (vel_perpend*vel_perpend))
346 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
348 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
349 float fmin = (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_straight*vel_straight + vel_perpend*vel_perpend
353 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
354 // obviously, this cannot be
360 vel_perpend *= max(fmin, f);
364 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
366 vel_xy = vel_straight * wishdir + vel_perpend;
370 float vel_xy_preclamp;
371 vel_xy_preclamp = vlen(vel_xy);
372 if (vel_xy_preclamp > 0) // prevent division by zero
374 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
375 if (vel_xy_current < vel_xy_preclamp)
376 vel_xy *= (vel_xy_current / vel_xy_preclamp);
380 self.velocity = vel_xy + vel_z * '0 0 1';
383 void PM_AirAccelerate(vector wishdir, float wishspeed)
388 vector curvel = self.velocity;
390 float curspeed = vlen(curvel);
392 if (wishspeed > curspeed * 1.01)
393 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
396 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
397 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
399 vector wishvel = wishdir * wishspeed;
400 vector acceldir = wishvel - curvel;
401 float addspeed = vlen(acceldir);
402 acceldir = normalize(acceldir);
404 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
406 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
408 vector curdir = normalize(curvel);
409 float dot = acceldir * curdir;
411 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
414 self.velocity += accelspeed * acceldir;
422 When you press the jump key
425 void PlayerJump (void)
428 if (PHYS_FROZEN(self))
429 return; // no jumping in freezetag when frozen
431 if (self.player_blocked)
432 return; // no jumping while blocked
434 float doublejump = FALSE;
435 float mjumpheight = PHYS_JUMPVELOCITY;
437 player_multijump = doublejump;
438 player_jumpheight = mjumpheight;
439 if (MUTATOR_CALLHOOK(PlayerJump))
442 doublejump = player_multijump;
443 mjumpheight = player_jumpheight;
445 if (autocvar_sv_doublejump)
447 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
448 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
452 // we MUST clip velocity here!
454 f = self.velocity * trace_plane_normal;
456 self.velocity -= f * trace_plane_normal;
460 if (self.waterlevel >= WATERLEVEL_SWIMMING)
462 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
467 if (!IS_ONGROUND(self))
470 if (PHYS_TRACK_CANJUMP(self))
471 if (IS_JUMP_HELD(self))
474 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
475 // velocity bounds. Final velocity is bound between (jumpheight *
476 // min + jumpheight) and (jumpheight * max + jumpheight);
478 if (autocvar_sv_jumpspeedcap_min != "")
480 float minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
482 if (self.velocity_z < minjumpspeed)
483 mjumpheight += minjumpspeed - self.velocity_z;
486 if (autocvar_sv_jumpspeedcap_max != "")
488 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
489 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
491 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
493 float maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
495 if (self.velocity_z > maxjumpspeed)
496 mjumpheight -= self.velocity_z - maxjumpspeed;
500 if (!(self.lastflags & FL_ONGROUND))
502 if (autocvar_speedmeter)
503 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
504 if (self.lastground < time - 0.3)
506 self.velocity_x *= (1 - autocvar_sv_friction_on_land);
507 self.velocity_y *= (1 - autocvar_sv_friction_on_land);
509 if (self.jumppadcount > 1)
510 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
511 self.jumppadcount = 0;
514 self.oldvelocity_z = self.velocity_z += mjumpheight;
516 UNSET_ONGROUND(self);
519 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
521 if (autocvar_g_jump_grunt)
522 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
524 self.restart_jump = -1; // restart jump anim next time
525 // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
529 void CheckWaterJump()
531 // check for a jump-out-of-water
532 makevectors(PHYS_INPUT_ANGLES(self));
533 vector start = self.origin;
536 normalize(v_forward);
537 vector end = start + v_forward*24;
538 traceline (start, end, TRUE, self);
539 if (trace_fraction < 1)
541 start_z = start_z + self.maxs_z - 8;
542 end = start + v_forward*24;
543 self.movedir = trace_plane_normal * -50;
544 traceline(start, end, TRUE, self);
545 if (trace_fraction == 1)
546 { // open at eye level
547 self.velocity_z = 225;
549 self.flags |= FL_WATERJUMP;
551 self.teleport_time = time + 2; // safety net
557 void CheckPlayerJump()
560 if (self.BUTTON_JUMP)
563 UNSET_JUMP_HELD(self);
567 // jump if on ground with jump button pressed but only if it has been
568 // released at least once since the last jump
569 if (PHYS_INPUT_BUTTON_JUMP(self))
571 pm_multijump = FALSE;
572 PM_multijump_checkjump();
573 if((IS_ONGROUND(self) || pm_multijump) && (!IS_JUMP_HELD(self) || !PHYS_TRACK_CANJUMP(self)))
575 self.velocity_z += PHYS_JUMPVELOCITY;
576 UNSET_ONGROUND(self);
577 SET_JUMP_HELD(self); // canjump = false
581 UNSET_JUMP_HELD(self); // canjump = true
583 if (self.waterlevel == WATERLEVEL_SWIMMING)
587 float racecar_angle(float forward, float down)
595 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
597 float angle_mult = forward / (800 + forward);
600 return ret * angle_mult + 360 * (1 - angle_mult);
602 return ret * angle_mult;
605 void RaceCarPhysics()
608 // using this move type for "big rigs"
609 // the engine does not push the entity!
613 vector angles_save = self.angles;
614 float accel = bound(-1, PHYS_INPUT_MOVEVALUES(self).x / PHYS_MAXSPEED(self), 1);
615 float steer = bound(-1, PHYS_INPUT_MOVEVALUES(self).y / PHYS_MAXSPEED(self), 1);
617 if (g_bugrigs_reverse_speeding)
621 // back accel is DIGITAL
622 // to prevent speedhack
632 makevectors(self.angles); // new forward direction!
634 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
636 float myspeed = self.velocity * v_forward;
637 float upspeed = self.velocity * v_up;
639 // responsiveness factor for steering and acceleration
640 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
641 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
644 if (myspeed < 0 && g_bugrigs_reverse_spinning)
645 steerfactor = -myspeed * g_bugrigs_steer;
647 steerfactor = -myspeed * f * g_bugrigs_steer;
650 if (myspeed < 0 && g_bugrigs_reverse_speeding)
651 accelfactor = g_bugrigs_accel;
653 accelfactor = f * g_bugrigs_accel;
654 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
660 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
664 if (!g_bugrigs_reverse_speeding)
665 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
672 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
676 if (g_bugrigs_reverse_stopping)
679 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
682 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
683 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
685 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
686 makevectors(self.angles); // new forward direction!
688 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
690 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
694 float myspeed = vlen(self.velocity);
696 // responsiveness factor for steering and acceleration
697 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
698 float steerfactor = -myspeed * f;
699 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
701 rigvel = self.velocity;
702 makevectors(self.angles); // new forward direction!
705 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
706 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
707 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
708 //MAXIMA: solve(total_acceleration(v) = 0, v);
710 if (g_bugrigs_planar_movement)
712 vector rigvel_xy, neworigin, up;
715 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
716 rigvel_xy = vec2(rigvel);
718 if (g_bugrigs_planar_movement_car_jumping)
721 mt = MOVE_NOMONSTERS;
723 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
724 up = trace_endpos - self.origin;
726 // BUG RIGS: align the move to the surface instead of doing collision testing
728 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
731 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
733 if (trace_fraction < 0.5)
736 neworigin = self.origin;
739 neworigin = trace_endpos;
741 if (trace_fraction < 1)
743 // now set angles_x so that the car points parallel to the surface
744 self.angles = vectoangles(
745 '1 0 0' * v_forward_x * trace_plane_normal_z
747 '0 1 0' * v_forward_y * trace_plane_normal_z
749 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
755 // now set angles_x so that the car points forward, but is tilted in velocity direction
756 UNSET_ONGROUND(self);
759 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
760 self.movetype = MOVETYPE_NOCLIP;
764 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
765 self.velocity = rigvel;
766 self.movetype = MOVETYPE_FLY;
770 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
771 if (trace_fraction != 1)
773 self.angles = vectoangles2(
774 '1 0 0' * v_forward_x * trace_plane_normal_z
776 '0 1 0' * v_forward_y * trace_plane_normal_z
778 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
786 vel_local_x = v_forward * self.velocity;
787 vel_local_y = v_right * self.velocity;
788 vel_local_z = v_up * self.velocity;
790 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
791 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
795 vector vf1, vu1, smoothangles;
796 makevectors(self.angles);
797 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
802 makevectors(angles_save);
803 vf1 = vf1 + v_forward * (1 - f);
804 vu1 = vu1 + v_up * (1 - f);
805 smoothangles = vectoangles2(vf1, vu1);
806 self.angles_x = -smoothangles_x;
807 self.angles_z = smoothangles_z;
811 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
812 .float specialcommand_pos;
813 void SpecialCommand()
819 if (!CheatImpulse(99))
820 print("A hollow voice says \"Plugh\".\n");
826 float speedaward_speed;
827 string speedaward_holder;
828 string speedaward_uid;
830 void race_send_speedaward(float msg)
833 // send the best speed of the round
834 WriteByte(msg, SVC_TEMPENTITY);
835 WriteByte(msg, TE_CSQC_RACE);
836 WriteByte(msg, RACE_NET_SPEED_AWARD);
837 WriteInt24_t(msg, floor(speedaward_speed+0.5));
838 WriteString(msg, speedaward_holder);
843 float speedaward_alltimebest;
844 string speedaward_alltimebest_holder;
845 string speedaward_alltimebest_uid;
847 void race_send_speedaward_alltimebest(float msg)
850 // send the best speed
851 WriteByte(msg, SVC_TEMPENTITY);
852 WriteByte(msg, TE_CSQC_RACE);
853 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
854 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
855 WriteString(msg, speedaward_alltimebest_holder);
859 float PM_check_keepaway(void)
862 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
868 void PM_check_race_movetime(void)
871 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
872 float f = floor(self.race_movetime_frac);
873 self.race_movetime_frac -= f;
874 self.race_movetime_count += f;
875 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
879 float PM_check_specialcommand(float buttons)
885 else if (buttons == 1)
887 else if (buttons == 2)
889 else if (buttons == 128)
891 else if (buttons == 256)
893 else if (buttons == 512)
895 else if (buttons == 1024)
900 if (c == substring(specialcommand, self.specialcommand_pos, 1))
902 self.specialcommand_pos += 1;
903 if (self.specialcommand_pos >= strlen(specialcommand))
905 self.specialcommand_pos = 0;
910 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
911 self.specialcommand_pos = 0;
916 void PM_check_nickspam(void)
919 if (time >= self.nickspamtime)
921 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
923 // slight annoyance for nick change scripts
924 PHYS_INPUT_MOVEVALUES(self) = -1 * PHYS_INPUT_MOVEVALUES(self);
925 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
927 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!
929 PHYS_INPUT_ANGLES(self)_x = random() * 360;
930 PHYS_INPUT_ANGLES(self)_y = random() * 360;
931 // at least I'm not forcing retardedview by also assigning to angles_z
932 self.fixangle = TRUE;
938 void PM_check_punch()
941 if (self.punchangle != '0 0 0')
943 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
945 self.punchangle = normalize(self.punchangle) * f;
947 self.punchangle = '0 0 0';
950 if (self.punchvector != '0 0 0')
952 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
954 self.punchvector = normalize(self.punchvector) * f;
956 self.punchvector = '0 0 0';
961 void PM_check_spider(void)
964 if (time >= self.spider_slowness)
966 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
967 self.stat_sv_airspeedlimit_nonqw *= 0.5;
971 // predict frozen movement, as frozen players CAN move in some cases
972 void PM_check_frozen(void)
974 if (!PHYS_FROZEN(self))
976 if (PHYS_DODGING_FROZEN
978 && IS_REAL_CLIENT(self)
982 PHYS_INPUT_MOVEVALUES(self)_x = bound(-5, PHYS_INPUT_MOVEVALUES(self).x, 5);
983 PHYS_INPUT_MOVEVALUES(self)_y = bound(-5, PHYS_INPUT_MOVEVALUES(self).y, 5);
984 PHYS_INPUT_MOVEVALUES(self)_z = bound(-5, PHYS_INPUT_MOVEVALUES(self).z, 5);
987 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
989 vector midpoint = ((self.absmin + self.absmax) * 0.5);
990 if (pointcontents(midpoint) == CONTENT_WATER)
992 self.velocity = self.velocity * 0.5;
994 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
995 self.velocity_z = 200;
999 void PM_check_blocked(void)
1002 if (!self.player_blocked)
1004 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
1005 self.disableclientprediction = 1;
1010 float speedaward_lastsent;
1011 float speedaward_lastupdate;
1012 string GetMapname(void);
1014 void PM_check_race(void)
1017 if not(g_cts || g_race)
1019 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1021 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1022 speedaward_holder = self.netname;
1023 speedaward_uid = self.crypto_idfp;
1024 speedaward_lastupdate = time;
1026 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1028 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1029 race_send_speedaward(MSG_ALL);
1030 speedaward_lastsent = speedaward_speed;
1031 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1033 speedaward_alltimebest = speedaward_speed;
1034 speedaward_alltimebest_holder = speedaward_holder;
1035 speedaward_alltimebest_uid = speedaward_uid;
1036 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1037 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1038 race_send_speedaward_alltimebest(MSG_ALL);
1044 void PM_check_vortex(void)
1048 float xyspeed = vlen(vec2(self.velocity));
1049 if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1051 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1052 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1053 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1054 // add the extra charge
1055 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * frametime);
1060 void PM_fly(float maxspd_mod)
1062 // noclipping or flying
1063 UNSET_ONGROUND(self);
1065 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1066 makevectors(PHYS_INPUT_ANGLES(self));
1067 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1068 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1069 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1070 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1072 vector wishdir = normalize(wishvel);
1073 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1074 if (time >= self.teleport_time)
1075 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1078 void PM_swim(float maxspd_mod)
1081 UNSET_ONGROUND(self);
1083 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1084 // water jump only in certain situations
1085 // this mimics quakeworld code
1086 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1088 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1089 makevectors(yawangles);
1090 vector forward = v_forward;
1091 vector spot = self.origin + 24 * forward;
1093 traceline(spot, spot, MOVE_NOMONSTERS, self);
1094 if (trace_startsolid)
1097 traceline(spot, spot, MOVE_NOMONSTERS, self);
1098 if (!trace_startsolid)
1100 self.velocity = forward * 50;
1101 self.velocity_z = 310;
1102 pmove_waterjumptime = 2;
1103 UNSET_ONGROUND(self);
1104 SET_JUMP_HELD(self);
1108 makevectors(PHYS_INPUT_ANGLES(self));
1109 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1110 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1111 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1112 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1113 if (wishvel == '0 0 0')
1114 wishvel = '0 0 -60'; // drift towards bottom
1116 vector wishdir = normalize(wishvel);
1117 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1119 if (IS_DUCKED(self))
1122 // if (pmove_waterjumptime <= 0) // TODO: use
1125 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1126 f = min(max(0, f), 1);
1129 f = wishspeed - self.velocity * wishdir;
1132 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1133 self.velocity += accelspeed * wishdir;
1136 // holding jump button swims upward slowly
1140 if (self.watertype & CONTENT_LAVA)
1141 self.velocity_z = 50;
1142 else if (self.watertype & CONTENT_SLIME)
1143 self.velocity_z = 80;
1146 if (IS_NEXUIZ_DERIVED(gamemode))
1148 self.velocity_z = 200;
1151 self.velocity_z = 100;
1156 PM_ClientMovement_Move();
1157 // water acceleration
1158 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1161 void PM_ladder(float maxspd_mod)
1163 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1164 UNSET_ONGROUND(self);
1167 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1168 if (PHYS_ENTGRAVITY(self))
1169 g *= PHYS_ENTGRAVITY(self);
1170 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1173 self.velocity_z += g;
1176 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1177 makevectors(PHYS_INPUT_ANGLES(self));
1178 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1179 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1180 + v_right * PHYS_INPUT_MOVEVALUES(self)_y
1181 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self)_z;
1182 self.velocity_z += g;
1183 if (self.ladder_entity.classname == "func_water")
1185 float f = vlen(wishvel);
1186 if (f > self.ladder_entity.speed)
1187 wishvel *= (self.ladder_entity.speed / f);
1189 self.watertype = self.ladder_entity.skin;
1190 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1191 if ((self.origin_z + self.view_ofs_z) < f)
1192 self.waterlevel = WATERLEVEL_SUBMERGED;
1193 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1194 self.waterlevel = WATERLEVEL_SWIMMING;
1195 else if ((self.origin_z + self.mins_z + 1) < f)
1196 self.waterlevel = WATERLEVEL_WETFEET;
1199 self.waterlevel = WATERLEVEL_NONE;
1200 self.watertype = CONTENT_EMPTY;
1204 vector wishdir = normalize(wishvel);
1205 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1206 PM_ClientMovement_Move();
1208 if (time >= self.teleport_time)
1210 // water acceleration
1211 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1214 void PM_check_jumppad()
1217 entity oldself = self;
1219 for(self = world; (self = find(self, classname, "jumppad")); )
1220 trigger_push_draw();
1226 void PM_jetpack(float maxspd_mod)
1228 //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1229 makevectors(PHYS_INPUT_ANGLES(self));
1230 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1231 + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1232 // add remaining speed as Z component
1233 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1234 // fix speedhacks :P
1235 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1236 // add the unused velocity as up component
1239 // if (self.BUTTON_JUMP)
1240 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1242 // it is now normalized, so...
1243 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1244 float a_up = PHYS_JETPACK_ACCEL_UP;
1245 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1247 wishvel_x *= a_side;
1248 wishvel_y *= a_side;
1253 //////////////////////////////////////////////////////////////////////////////////////
1254 // finding the maximum over all vectors of above form
1255 // with wishvel having an absolute value of 1
1256 //////////////////////////////////////////////////////////////////////////////////////
1257 // we're finding the maximum over
1258 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1259 // for z in the range from -1 to 1
1260 //////////////////////////////////////////////////////////////////////////////////////
1261 // maximum is EITHER attained at the single extreme point:
1262 float a_diff = a_side * a_side - a_up * a_up;
1266 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1267 if (f > -1 && f < 1) // can it be attained?
1269 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1270 //print("middle\n");
1273 // OR attained at z = 1:
1274 f = (a_up + a_add) * (a_up + a_add);
1280 // OR attained at z = -1:
1281 f = (a_up - a_add) * (a_up - a_add);
1285 //print("bottom\n");
1288 //////////////////////////////////////////////////////////////////////////////////////
1290 //print("best possible acceleration: ", ftos(best), "\n");
1293 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1294 if (wishvel_z - PHYS_GRAVITY > 0)
1295 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1297 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1300 fvel = vlen(wishvel);
1303 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1305 fvel = min(1, vlen(wishvel) / best);
1306 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1307 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1311 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1313 if (f > 0 && wishvel != '0 0 0')
1315 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1316 UNSET_ONGROUND(self);
1319 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1320 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1322 self.items |= IT_USING_JETPACK;
1324 // jetpack also inhibits health regeneration, but only for 1 second
1325 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1330 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1331 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1332 self.velocity_z -= g * 0.5;
1334 self.velocity_z -= g;
1335 PM_ClientMovement_Move();
1336 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1337 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1338 self.velocity_z -= g * 0.5;
1342 void PM_walk(float buttons_prev, float maxspd_mod)
1345 // we get here if we ran out of ammo
1346 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1347 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1348 if (!(self.lastflags & FL_ONGROUND))
1350 if (autocvar_speedmeter)
1351 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1352 if (self.lastground < time - 0.3)
1353 self.velocity *= (1 - autocvar_sv_friction_on_land);
1354 if (self.jumppadcount > 1)
1355 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1356 self.jumppadcount = 0;
1360 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1361 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1362 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1364 vector wishdir = normalize(wishvel);
1365 float wishspeed = vlen(wishvel);
1367 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1368 if (IS_DUCKED(self))
1371 // apply edge friction
1372 float f = vlen(vec2(self.velocity));
1375 // TODO: apply edge friction
1376 // apply ground friction
1377 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1381 Mathematical analysis time!
1383 Our goal is to invert this mess.
1385 For the two cases we get:
1386 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1387 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1388 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1390 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1391 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1393 These cases would be chosen ONLY if:
1395 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1396 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1398 v0 >= PHYS_STOPSPEED
1399 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1400 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1403 float addspeed = wishspeed - self.velocity * wishdir;
1406 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1407 self.velocity += accelspeed * wishdir;
1409 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1410 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1411 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1412 if (self.velocity * self.velocity)
1413 PM_ClientMovement_Move();
1414 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1415 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1416 self.velocity_z -= g * 0.5;
1419 void PM_air(float buttons_prev, float maxspd_mod)
1422 // we get here if we ran out of ammo
1423 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && PHYS_AMMO_FUEL(self) < 0.01)
1424 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1426 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1427 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1428 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1430 vector wishdir = normalize(wishvel);
1431 float wishspeed = vlen(wishvel);
1434 if (time >= self.teleport_time)
1436 if (pmove_waterjumptime <= 0)
1439 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1441 // apply air speed limit
1442 float airaccelqw = PHYS_AIRACCEL_QW(self);
1443 float wishspeed0 = wishspeed;
1444 wishspeed = min(wishspeed, maxairspd);
1445 if (IS_DUCKED(self))
1447 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1449 float accelerating = (self.velocity * wishdir > 0);
1450 float wishspeed2 = wishspeed;
1453 if (PHYS_AIRSTOPACCELERATE)
1455 vector curdir = normalize(vec2(self.velocity));
1456 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1458 // note that for straight forward jumping:
1459 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1460 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1462 // dv/dt = accel * maxspeed (when slow)
1463 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1464 // log dv/dt = logaccel + logmaxspeed (when slow)
1465 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1466 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1467 if (PHYS_MAXAIRSTRAFESPEED)
1468 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1469 if (PHYS_AIRSTRAFEACCELERATE)
1470 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1471 if (PHYS_AIRSTRAFEACCEL_QW(self))
1473 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1475 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1478 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1479 PM_AirAccelerate(wishdir, wishspeed2);
1481 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1483 if (PHYS_AIRCONTROL)
1484 CPM_PM_Aircontrol(wishdir, wishspeed2);
1486 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1487 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1488 self.velocity_z -= g * 0.5;
1490 self.velocity_z -= g;
1491 PM_ClientMovement_Move();
1492 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1493 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1494 self.velocity_z -= g * 0.5;
1497 // used for calculating airshots
1498 float PM_is_flying()
1500 if (IS_ONGROUND(self))
1502 if (self.waterlevel >= WATERLEVEL_SWIMMING)
1504 traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1505 return trace_fraction >= 1;
1511 float buttons = PHYS_INPUT_BUTTON_MASK(self);
1513 self.team = myteam + 1; // is this correct?
1514 //Con_Printf(" %f", PHYS_INPUT_TIMELENGTH);
1515 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1516 UNSET_JUMP_HELD(self); // canjump = true
1517 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1518 PM_ClientMovement_UpdateStatus();
1522 WarpZone_PlayerPhysics_FixVAngle();
1524 float maxspeed_mod = 1;
1525 maxspeed_mod *= PM_check_keepaway();
1526 maxspeed_mod *= PHYS_HIGHSPEED;
1529 Physics_UpdateStats(maxspeed_mod);
1531 if (self.PlayerPhysplug)
1532 if (self.PlayerPhysplug())
1536 PM_check_race_movetime();
1538 anticheat_physics();
1541 if (PM_check_specialcommand(buttons))
1546 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1547 self.parm_idlesince = time;
1550 float buttons_prev = self.buttons_old;
1551 self.buttons_old = buttons;
1552 self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1553 self.v_angle_old = PHYS_INPUT_ANGLES(self);
1555 PM_check_nickspam();
1559 if (IS_BOT_CLIENT(self))
1561 if (playerdemo_read())
1566 self.items &= ~IT_USING_JETPACK;
1568 if (IS_PLAYER(self))
1572 if (self.race_penalty)
1573 if (time > self.race_penalty)
1574 self.race_penalty = 0;
1577 float not_allowed_to_move = 0;
1579 if (self.race_penalty)
1580 not_allowed_to_move = 1;
1583 if (!autocvar_sv_ready_restart_after_countdown)
1584 if (time < game_starttime)
1585 not_allowed_to_move = 1;
1588 if (not_allowed_to_move)
1590 self.velocity = '0 0 0';
1591 self.movetype = MOVETYPE_NONE;
1593 self.disableclientprediction = 2;
1597 else if (self.disableclientprediction == 2)
1599 if (self.movetype == MOVETYPE_NONE)
1600 self.movetype = MOVETYPE_WALK;
1601 self.disableclientprediction = 0;
1607 if (self.movetype == MOVETYPE_NONE)
1610 // when we get here, disableclientprediction cannot be 2
1611 self.disableclientprediction = 0;
1623 if (self.in_swamp) {
1624 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1629 // conveyors: first fix velocity
1630 if (self.conveyor.state)
1631 self.velocity -= self.conveyor.movedir;
1635 MUTATOR_CALLHOOK(PlayerPhysics);
1641 // float forcedodge = 1;
1644 // PM_dodging_checkpressedkeys();
1647 // PM_ClientMovement_Move();
1652 if (!IS_PLAYER(self))
1654 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1655 if (!self.spectatorspeed)
1656 self.spectatorspeed = maxspeed_mod;
1657 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1659 if (self.lastclassname != "player")
1661 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1662 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1663 else if (self.impulse == 11)
1664 self.spectatorspeed = maxspeed_mod;
1665 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1666 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1667 else if (self.impulse >= 1 && self.impulse <= 9)
1668 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1669 } // otherwise just clear
1672 maxspeed_mod *= self.spectatorspeed;
1677 // if dead, behave differently
1678 // in CSQC, physics don't handle dead player
1684 if (!self.fixangle && !g_bugrigs)
1685 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1689 if (IS_ONGROUND(self))
1690 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1694 if (self.waterlevel < WATERLEVEL_SWIMMING)
1695 if (time >= self.ladder_time)
1698 self.nextstep = time + 0.3 + random() * 0.1;
1699 trace_dphitq3surfaceflags = 0;
1700 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1701 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1703 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1704 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1706 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1716 if (IS_PLAYER(self))
1720 if (self.flags & /* FL_WATERJUMP */ 2048)
1722 self.velocity_x = self.movedir_x;
1723 self.velocity_y = self.movedir_y;
1724 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1726 self.flags &= ~/* FL_WATERJUMP */ 2048;
1727 self.teleport_time = 0;
1732 else if (g_bugrigs && IS_PLAYER(self))
1736 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1737 PM_fly(maxspeed_mod);
1739 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1740 PM_swim(maxspeed_mod);
1742 else if (time < self.ladder_time)
1743 PM_ladder(maxspeed_mod);
1745 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))
1746 PM_jetpack(maxspeed_mod);
1750 if (IS_ONGROUND(self))
1751 PM_walk(buttons_prev, maxspeed_mod);
1753 PM_air(buttons_prev, maxspeed_mod);
1757 if (!IS_OBSERVER(self))
1763 if (IS_ONGROUND(self))
1764 self.lastground = time;
1767 // conveyors: then break velocity again
1768 if (self.conveyor.state)
1769 self.velocity += self.conveyor.movedir;
1771 self.lastflags = self.flags;
1772 self.lastclassname = self.classname;
1776 void SV_PlayerPhysics(void)
1778 void CSQC_ClientMovement_PlayerMove_Frame(void)