1 #include "./physics.qh"
13 .float spectatorspeed;
18 .string lastclassname;
20 .float() PlayerPhysplug;
21 float AdjustAirAccelQW(float accelqw, float factor);
28 .float stat_sv_airaccel_qw;
29 .float stat_sv_airstrafeaccel_qw;
30 .float stat_sv_airspeedlimit_nonqw;
31 .float stat_sv_maxspeed;
32 .float stat_movement_highspeed;
34 .float stat_jetpack_accel_side;
35 .float stat_jetpack_accel_up;
36 .float stat_jetpack_antigravity;
37 .float stat_jetpack_fuel;
38 .float stat_jetpack_maxspeed_up;
39 .float stat_jetpack_maxspeed_side;
41 void Physics_AddStats()
43 // g_movementspeed hack
44 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
45 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
46 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
47 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
48 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
51 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
52 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
53 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
54 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
55 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
56 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
59 void Physics_UpdateStats(float maxspd_mod)
61 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
62 if (autocvar_sv_airstrafeaccel_qw)
63 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
65 self.stat_sv_airstrafeaccel_qw = 0;
66 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
67 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
68 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
70 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
71 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
72 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
73 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
74 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
75 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
79 float IsMoveInDirection(vector mv, float angle) // key mix factor
81 if (mv_x == 0 && mv_y == 0)
82 return 0; // avoid division by zero
83 angle -= RAD2DEG * atan2(mv_y, mv_x);
84 angle = remainder(angle, 360) / 45;
85 return angle > 1 ? 0 : angle < -1 ? 0 : 1 - fabs(angle);
88 float GeomLerp(float a, float lerp, float b)
90 return a == 0 ? (lerp < 1 ? 0 : b)
91 : b == 0 ? (lerp > 0 ? 0 : a)
92 : a * pow(fabs(b / a), lerp);
95 noref float pmove_waterjumptime;
97 const float unstick_count = 27;
98 vector unstick_offsets[unstick_count] =
100 // 1 no nudge (just return the original if this test passes)
103 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
104 '-0.125 0.000 0.000', '0.125 0.000 0.000',
105 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
106 // 4 diagonal flat nudges
107 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
108 '-0.125 0.125 0.000', '0.125 0.125 0.000',
109 // 8 diagonal upward nudges
110 '-0.125 0.000 0.125', '0.125 0.000 0.125',
111 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
112 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
113 '-0.125 0.125 0.125', '0.125 0.125 0.125',
114 // 8 diagonal downward nudges
115 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
116 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
117 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
118 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
121 void PM_ClientMovement_Unstick()
124 for (i = 0; i < unstick_count; i++)
126 vector neworigin = unstick_offsets[i] + self.origin;
127 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
128 if (!trace_startsolid)
130 self.origin = neworigin;
136 void PM_ClientMovement_UpdateStatus()
138 // make sure player is not stuck
139 PM_ClientMovement_Unstick();
142 if (PHYS_INPUT_BUTTONS(self) & 16)
144 // wants to crouch, this always works..
145 if (!IS_DUCKED(self))
150 // wants to stand, if currently crouching we need to check for a
154 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
155 if (!trace_startsolid)
161 vector origin1 = self.origin + '0 0 1';
162 vector origin2 = self.origin - '0 0 1';
164 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
165 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
169 // this code actually "predicts" an impact; so let's clip velocity first
170 float f = dotproduct(self.velocity, trace_plane_normal);
171 if (f < 0) // only if moving downwards actually
172 self.velocity -= f * trace_plane_normal;
175 UNSET_ONGROUND(self);
177 // set watertype/waterlevel
178 origin1 = self.origin;
179 origin1_z += self.mins_z + 1;
180 self.waterlevel = WATERLEVEL_NONE;
182 self.watertype = (pointcontents(origin1) == CONTENT_WATER);
186 self.waterlevel = WATERLEVEL_WETFEET;
187 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
188 if(pointcontents(origin1) == CONTENT_WATER)
190 self.waterlevel = WATERLEVEL_SWIMMING;
191 origin1_z = self.origin_z + 22;
192 if(pointcontents(origin1) == CONTENT_WATER)
193 self.waterlevel = WATERLEVEL_SUBMERGED;
197 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
198 pmove_waterjumptime = 0;
201 void PM_ClientMovement_Move()
204 float t = PHYS_INPUT_TIMELENGTH;
205 vector primalvelocity = self.velocity;
206 PM_ClientMovement_UpdateStatus();
208 for (bump = 0; bump < 8 && self.velocity * self.velocity > 0; bump++)
210 vector neworigin = self.origin + t * self.velocity;
211 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
212 float old_trace1_fraction = trace_fraction;
213 vector old_trace1_endpos = trace_endpos;
214 vector old_trace1_plane_normal = trace_plane_normal;
215 if (trace_fraction < 1 && trace_plane_normal_z == 0)
217 // may be a step or wall, try stepping up
218 // first move forward at a higher level
219 vector currentorigin2 = self.origin;
220 currentorigin2_z += PHYS_STEPHEIGHT;
221 vector neworigin2 = neworigin;
222 neworigin2_z = self.origin_z + PHYS_STEPHEIGHT;
223 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
224 if (!trace_startsolid)
226 // then move down from there
227 currentorigin2 = trace_endpos;
228 neworigin2 = trace_endpos;
229 neworigin2_z = self.origin_z;
230 float old_trace2_fraction = trace_fraction;
231 vector old_trace2_plane_normal = trace_plane_normal;
232 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
233 //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]);
234 // accept the new trace if it made some progress
235 if (fabs(trace_endpos_x - old_trace1_endpos_x) >= 0.03125 || fabs(trace_endpos_y - old_trace1_endpos_y) >= 0.03125)
237 trace_fraction = old_trace2_fraction;
238 trace_endpos = trace_endpos;
239 trace_plane_normal = old_trace2_plane_normal;
243 trace_fraction = old_trace1_fraction;
244 trace_endpos = old_trace1_endpos;
245 trace_plane_normal = old_trace1_plane_normal;
250 // check if it moved at all
251 if (trace_fraction >= 0.001)
252 self.origin = trace_endpos;
254 // check if it moved all the way
255 if (trace_fraction == 1)
258 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
259 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
260 // this got commented out in a change that supposedly makes the code match QW better
261 // so if this is broken, maybe put it in an if (cls.protocol != PROTOCOL_QUAKEWORLD) block
262 if (trace_plane_normal_z > 0.7)
265 t -= t * trace_fraction;
267 float f = dotproduct(self.velocity, trace_plane_normal);
268 self.velocity -= f * trace_plane_normal;
270 if (pmove_waterjumptime > 0)
271 self.velocity = primalvelocity;
275 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
279 // this doesn't play well with analog input
280 if (PHYS_INPUT_MOVEVALUES(self).x == 0 || PHYS_INPUT_MOVEVALUES(self).y != 0)
281 return; // can't control movement if not moving forward or backward
284 k = 32 * (2 * IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), 0) - 1);
289 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
291 float zspeed = self.velocity_z;
293 float xyspeed = vlen(self.velocity);
294 self.velocity = normalize(self.velocity);
296 float dot = self.velocity * wishdir;
298 if (dot > 0) // we can't change direction while slowing down
300 k *= pow(dot, PHYS_AIRCONTROL_POWER)*PHYS_INPUT_TIMELENGTH;
301 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
302 k *= PHYS_AIRCONTROL;
303 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
306 self.velocity = self.velocity * xyspeed;
307 self.velocity_z = zspeed;
310 float AdjustAirAccelQW(float accelqw, float factor)
312 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
315 // example config for alternate speed clamping:
316 // sv_airaccel_qw 0.8
317 // sv_airaccel_sideways_friction 0
318 // prvm_globalset server speedclamp_mode 1
320 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
322 float speedclamp = stretchfactor > 0 ? stretchfactor
323 : accelqw < 0 ? 1 // full clamping, no stretch
326 accelqw = fabs(accelqw);
328 if (GAMEPLAYFIX_Q2AIRACCELERATE)
329 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
331 float vel_straight = self.velocity * wishdir;
332 float vel_z = self.velocity_z;
333 vector vel_xy = vec2(self.velocity);
334 vector vel_perpend = vel_xy - vel_straight * wishdir;
336 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
338 float vel_xy_current = vlen(vel_xy);
340 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
341 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
342 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
343 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
344 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
346 if (sidefric < 0 && (vel_perpend*vel_perpend))
347 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
349 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
350 float fmin = (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_straight*vel_straight + vel_perpend*vel_perpend
354 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
355 // obviously, this cannot be
361 vel_perpend *= max(fmin, f);
365 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
367 vel_xy = vel_straight * wishdir + vel_perpend;
371 float vel_xy_preclamp;
372 vel_xy_preclamp = vlen(vel_xy);
373 if (vel_xy_preclamp > 0) // prevent division by zero
375 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
376 if (vel_xy_current < vel_xy_preclamp)
377 vel_xy *= (vel_xy_current / vel_xy_preclamp);
381 self.velocity = vel_xy + vel_z * '0 0 1';
384 void PM_AirAccelerate(vector wishdir, float wishspeed)
389 vector curvel = self.velocity;
391 float curspeed = vlen(curvel);
393 if (wishspeed > curspeed * 1.01)
394 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
397 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
398 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
400 vector wishvel = wishdir * wishspeed;
401 vector acceldir = wishvel - curvel;
402 float addspeed = vlen(acceldir);
403 acceldir = normalize(acceldir);
405 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
407 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
409 vector curdir = normalize(curvel);
410 float dot = acceldir * curdir;
412 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
415 self.velocity += accelspeed * acceldir;
423 When you press the jump key
426 void PlayerJump (void)
429 if (PHYS_FROZEN(self))
430 return; // no jumping in freezetag when frozen
432 if (self.player_blocked)
433 return; // no jumping while blocked
435 float doublejump = FALSE;
436 float mjumpheight = PHYS_JUMPVELOCITY;
438 player_multijump = doublejump;
439 player_jumpheight = mjumpheight;
440 if (MUTATOR_CALLHOOK(PlayerJump))
443 doublejump = player_multijump;
444 mjumpheight = player_jumpheight;
446 if (autocvar_sv_doublejump)
448 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
449 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
453 // we MUST clip velocity here!
455 f = self.velocity * trace_plane_normal;
457 self.velocity -= f * trace_plane_normal;
461 if (self.waterlevel >= WATERLEVEL_SWIMMING)
463 self.velocity_z = self.stat_sv_maxspeed * 0.7;
468 if (!IS_ONGROUND(self))
471 if (self.cvar_cl_movement_track_canjump)
472 if (!(self.flags & FL_JUMPRELEASED))
475 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
476 // velocity bounds. Final velocity is bound between (jumpheight *
477 // min + jumpheight) and (jumpheight * max + jumpheight);
479 if (autocvar_sv_jumpspeedcap_min != "")
481 float minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
483 if (self.velocity_z < minjumpspeed)
484 mjumpheight += minjumpspeed - self.velocity_z;
487 if (autocvar_sv_jumpspeedcap_max != "")
489 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
490 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
492 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
494 float maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
496 if (self.velocity_z > maxjumpspeed)
497 mjumpheight -= self.velocity_z - maxjumpspeed;
501 if (!(self.lastflags & FL_ONGROUND))
503 if (autocvar_speedmeter)
504 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
505 if (self.lastground < time - 0.3)
507 self.velocity_x *= (1 - autocvar_sv_friction_on_land);
508 self.velocity_y *= (1 - autocvar_sv_friction_on_land);
510 if (self.jumppadcount > 1)
511 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
512 self.jumppadcount = 0;
515 self.oldvelocity_z = self.velocity_z += mjumpheight;
517 UNSET_ONGROUND(self);
518 self.flags &= ~FL_JUMPRELEASED;
520 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
522 if (autocvar_g_jump_grunt)
523 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
525 self.restart_jump = -1; // restart jump anim next time
526 // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
530 void CheckWaterJump()
532 // check for a jump-out-of-water
533 makevectors(PHYS_INPUT_ANGLES(self));
534 vector start = self.origin;
537 normalize(v_forward);
538 vector end = start + v_forward*24;
539 traceline (start, end, TRUE, self);
540 if (trace_fraction < 1)
542 start_z = start_z + self.maxs_z - 8;
543 end = start + v_forward*24;
544 self.movedir = trace_plane_normal * -50;
545 traceline(start, end, TRUE, self);
546 if (trace_fraction == 1)
547 { // open at eye level
548 self.velocity_z = 225;
550 self.flags |= FL_WATERJUMP;
551 self.flags &= ~FL_JUMPRELEASED;
552 self.teleport_time = time + 2; // safety net
558 void CheckPlayerJump()
561 if (self.BUTTON_JUMP)
564 self.flags |= FL_JUMPRELEASED;
567 if (self.waterlevel == WATERLEVEL_SWIMMING)
571 float racecar_angle(float forward, float down)
579 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
581 float angle_mult = forward / (800 + forward);
584 return ret * angle_mult + 360 * (1 - angle_mult);
586 return ret * angle_mult;
589 void RaceCarPhysics()
592 // using this move type for "big rigs"
593 // the engine does not push the entity!
597 vector angles_save = self.angles;
598 float accel = bound(-1, PHYS_INPUT_MOVEVALUES(self).x / self.stat_sv_maxspeed, 1);
599 float steer = bound(-1, PHYS_INPUT_MOVEVALUES(self).y / self.stat_sv_maxspeed, 1);
601 if (g_bugrigs_reverse_speeding)
605 // back accel is DIGITAL
606 // to prevent speedhack
616 makevectors(self.angles); // new forward direction!
618 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
620 float myspeed = self.velocity * v_forward;
621 float upspeed = self.velocity * v_up;
623 // responsiveness factor for steering and acceleration
624 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
625 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
628 if (myspeed < 0 && g_bugrigs_reverse_spinning)
629 steerfactor = -myspeed * g_bugrigs_steer;
631 steerfactor = -myspeed * f * g_bugrigs_steer;
634 if (myspeed < 0 && g_bugrigs_reverse_speeding)
635 accelfactor = g_bugrigs_accel;
637 accelfactor = f * g_bugrigs_accel;
638 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
644 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
648 if (!g_bugrigs_reverse_speeding)
649 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
656 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
660 if (g_bugrigs_reverse_stopping)
663 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
666 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
667 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
669 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
670 makevectors(self.angles); // new forward direction!
672 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
674 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
678 float myspeed = vlen(self.velocity);
680 // responsiveness factor for steering and acceleration
681 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
682 float steerfactor = -myspeed * f;
683 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
685 rigvel = self.velocity;
686 makevectors(self.angles); // new forward direction!
689 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
690 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
691 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
692 //MAXIMA: solve(total_acceleration(v) = 0, v);
694 if (g_bugrigs_planar_movement)
696 vector rigvel_xy, neworigin, up;
699 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
700 rigvel_xy = vec2(rigvel);
702 if (g_bugrigs_planar_movement_car_jumping)
705 mt = MOVE_NOMONSTERS;
707 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
708 up = trace_endpos - self.origin;
710 // BUG RIGS: align the move to the surface instead of doing collision testing
712 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
715 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
717 if (trace_fraction < 0.5)
720 neworigin = self.origin;
723 neworigin = trace_endpos;
725 if (trace_fraction < 1)
727 // now set angles_x so that the car points parallel to the surface
728 self.angles = vectoangles(
729 '1 0 0' * v_forward_x * trace_plane_normal_z
731 '0 1 0' * v_forward_y * trace_plane_normal_z
733 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
739 // now set angles_x so that the car points forward, but is tilted in velocity direction
740 UNSET_ONGROUND(self);
743 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
744 self.movetype = MOVETYPE_NOCLIP;
748 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
749 self.velocity = rigvel;
750 self.movetype = MOVETYPE_FLY;
754 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
755 if (trace_fraction != 1)
757 self.angles = vectoangles2(
758 '1 0 0' * v_forward_x * trace_plane_normal_z
760 '0 1 0' * v_forward_y * trace_plane_normal_z
762 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
770 vel_local_x = v_forward * self.velocity;
771 vel_local_y = v_right * self.velocity;
772 vel_local_z = v_up * self.velocity;
774 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
775 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
779 vector vf1, vu1, smoothangles;
780 makevectors(self.angles);
781 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
786 makevectors(angles_save);
787 vf1 = vf1 + v_forward * (1 - f);
788 vu1 = vu1 + v_up * (1 - f);
789 smoothangles = vectoangles2(vf1, vu1);
790 self.angles_x = -smoothangles_x;
791 self.angles_z = smoothangles_z;
795 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
796 .float specialcommand_pos;
797 void SpecialCommand()
803 if (!CheatImpulse(99))
804 print("A hollow voice says \"Plugh\".\n");
810 float speedaward_speed;
811 string speedaward_holder;
812 string speedaward_uid;
814 void race_send_speedaward(float msg)
817 // send the best speed of the round
818 WriteByte(msg, SVC_TEMPENTITY);
819 WriteByte(msg, TE_CSQC_RACE);
820 WriteByte(msg, RACE_NET_SPEED_AWARD);
821 WriteInt24_t(msg, floor(speedaward_speed+0.5));
822 WriteString(msg, speedaward_holder);
827 float speedaward_alltimebest;
828 string speedaward_alltimebest_holder;
829 string speedaward_alltimebest_uid;
831 void race_send_speedaward_alltimebest(float msg)
834 // send the best speed
835 WriteByte(msg, SVC_TEMPENTITY);
836 WriteByte(msg, TE_CSQC_RACE);
837 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
838 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
839 WriteString(msg, speedaward_alltimebest_holder);
843 float PM_check_keepaway(void)
846 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
852 void PM_check_race_movetime(void)
855 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
856 float f = floor(self.race_movetime_frac);
857 self.race_movetime_frac -= f;
858 self.race_movetime_count += f;
859 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
863 float PM_check_specialcommand(float buttons)
869 else if (buttons == 1)
871 else if (buttons == 2)
873 else if (buttons == 128)
875 else if (buttons == 256)
877 else if (buttons == 512)
879 else if (buttons == 1024)
884 if (c == substring(specialcommand, self.specialcommand_pos, 1))
886 self.specialcommand_pos += 1;
887 if (self.specialcommand_pos >= strlen(specialcommand))
889 self.specialcommand_pos = 0;
894 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
895 self.specialcommand_pos = 0;
900 void PM_check_nickspam(void)
903 if (time >= self.nickspamtime)
905 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
907 // slight annoyance for nick change scripts
908 PHYS_INPUT_MOVEVALUES(self) = -1 * PHYS_INPUT_MOVEVALUES(self);
909 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
911 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!
913 PHYS_INPUT_ANGLES(self)_x = random() * 360;
914 PHYS_INPUT_ANGLES(self)_y = random() * 360;
915 // at least I'm not forcing retardedview by also assigning to angles_z
916 self.fixangle = TRUE;
922 void PM_check_punch()
925 if (self.punchangle != '0 0 0')
927 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
929 self.punchangle = normalize(self.punchangle) * f;
931 self.punchangle = '0 0 0';
934 if (self.punchvector != '0 0 0')
936 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
938 self.punchvector = normalize(self.punchvector) * f;
940 self.punchvector = '0 0 0';
945 void PM_check_spider(void)
948 if (time >= self.spider_slowness)
950 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
951 self.stat_sv_airspeedlimit_nonqw *= 0.5;
955 // predict frozen movement, as frozen players CAN move in some cases
956 void PM_check_frozen(void)
958 if (!PHYS_FROZEN(self))
960 if (PHYS_DODGING_FROZEN
962 && IS_REAL_CLIENT(self)
966 PHYS_INPUT_MOVEVALUES(self)_x = bound(-5, PHYS_INPUT_MOVEVALUES(self).x, 5);
967 PHYS_INPUT_MOVEVALUES(self)_y = bound(-5, PHYS_INPUT_MOVEVALUES(self).y, 5);
968 PHYS_INPUT_MOVEVALUES(self)_z = bound(-5, PHYS_INPUT_MOVEVALUES(self).z, 5);
971 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
973 vector midpoint = ((self.absmin + self.absmax) * 0.5);
974 if (pointcontents(midpoint) == CONTENT_WATER)
976 self.velocity = self.velocity * 0.5;
978 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
979 self.velocity_z = 200;
983 void PM_check_blocked(void)
986 if (!self.player_blocked)
988 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
989 self.disableclientprediction = 1;
994 float speedaward_lastsent;
995 float speedaward_lastupdate;
996 string GetMapname(void);
998 void PM_check_race(void)
1001 if not(g_cts || g_race)
1003 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1005 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1006 speedaward_holder = self.netname;
1007 speedaward_uid = self.crypto_idfp;
1008 speedaward_lastupdate = time;
1010 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1012 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1013 race_send_speedaward(MSG_ALL);
1014 speedaward_lastsent = speedaward_speed;
1015 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1017 speedaward_alltimebest = speedaward_speed;
1018 speedaward_alltimebest_holder = speedaward_holder;
1019 speedaward_alltimebest_uid = speedaward_uid;
1020 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1021 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1022 race_send_speedaward_alltimebest(MSG_ALL);
1028 void PM_check_vortex(void)
1031 float xyspeed = vlen(vec2(self.velocity));
1032 if (self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1034 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1035 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1036 float f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1037 // add the extra charge
1038 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * PHYS_INPUT_TIMELENGTH);
1043 void PM_fly(float maxspd_mod)
1045 // noclipping or flying
1046 UNSET_ONGROUND(self);
1048 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1049 makevectors(PHYS_INPUT_ANGLES(self));
1050 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1051 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1052 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1053 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1055 vector wishdir = normalize(wishvel);
1056 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1057 if (time >= self.teleport_time)
1058 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1061 void PM_swim(float maxspd_mod)
1064 UNSET_ONGROUND(self);
1066 float jump = PHYS_INPUT_BUTTONS(self) & 2;
1067 // water jump only in certain situations
1068 // this mimics quakeworld code
1069 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1071 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1072 makevectors(yawangles);
1073 vector forward = v_forward;
1074 vector spot = self.origin + 24 * forward;
1076 traceline(spot, spot, MOVE_NOMONSTERS, self);
1077 if (trace_startsolid)
1080 traceline(spot, spot, MOVE_NOMONSTERS, self);
1081 if (!trace_startsolid)
1083 self.velocity = forward * 50;
1084 self.velocity_z = 310;
1085 pmove_waterjumptime = 2;
1086 UNSET_ONGROUND(self);
1087 SET_JUMP_HELD(self);
1091 makevectors(PHYS_INPUT_ANGLES(self));
1092 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1093 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1094 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1095 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1096 if (wishvel == '0 0 0')
1097 wishvel = '0 0 -60'; // drift towards bottom
1099 vector wishdir = normalize(wishvel);
1100 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1102 if (IS_DUCKED(self))
1105 // if (pmove_waterjumptime <= 0) // TODO: use
1108 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1109 f = min(max(0, f), 1);
1112 f = wishspeed - self.velocity * wishdir;
1115 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1116 self.velocity += accelspeed * wishdir;
1119 // holding jump button swims upward slowly
1123 if (self.watertype & CONTENT_LAVA)
1124 self.velocity_z = 50;
1125 else if (self.watertype & CONTENT_SLIME)
1126 self.velocity_z = 80;
1129 if (IS_NEXUIZ_DERIVED(gamemode))
1131 self.velocity_z = 200;
1134 self.velocity_z = 100;
1139 PM_ClientMovement_Move();
1140 // water acceleration
1141 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1144 void PM_ladder(float maxspd_mod)
1147 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1148 UNSET_ONGROUND(self);
1151 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1152 if (PHYS_ENTGRAVITY(self))
1153 g *= PHYS_ENTGRAVITY(self);
1154 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1157 self.velocity_z += g;
1160 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1161 makevectors(PHYS_INPUT_ANGLES(self));
1162 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1163 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1164 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1165 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1166 self.velocity_z += g;
1167 if (self.ladder_entity.classname == "func_water")
1169 float f = vlen(wishvel);
1170 if (f > self.ladder_entity.speed)
1171 wishvel *= (self.ladder_entity.speed / f);
1173 self.watertype = self.ladder_entity.skin;
1174 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1175 if ((self.origin_z + self.view_ofs_z) < f)
1176 self.waterlevel = WATERLEVEL_SUBMERGED;
1177 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1178 self.waterlevel = WATERLEVEL_SWIMMING;
1179 else if ((self.origin_z + self.mins_z + 1) < f)
1180 self.waterlevel = WATERLEVEL_WETFEET;
1183 self.waterlevel = WATERLEVEL_NONE;
1184 self.watertype = CONTENT_EMPTY;
1188 vector wishdir = normalize(wishvel);
1189 float wishspeed = min(vlen(wishvel), self.stat_sv_maxspeed * maxspd_mod);
1190 if (time >= self.teleport_time)
1191 // water acceleration
1192 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1196 void PM_jetpack(float maxspd_mod)
1198 //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1199 makevectors(PHYS_INPUT_ANGLES(self));
1200 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1201 + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1202 // add remaining speed as Z component
1203 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1204 // fix speedhacks :P
1205 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1206 // add the unused velocity as up component
1209 // if (self.BUTTON_JUMP)
1210 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1212 // it is now normalized, so...
1213 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1214 float a_up = PHYS_JETPACK_ACCEL_UP;
1215 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1217 wishvel_x *= a_side;
1218 wishvel_y *= a_side;
1223 //////////////////////////////////////////////////////////////////////////////////////
1224 // finding the maximum over all vectors of above form
1225 // with wishvel having an absolute value of 1
1226 //////////////////////////////////////////////////////////////////////////////////////
1227 // we're finding the maximum over
1228 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1229 // for z in the range from -1 to 1
1230 //////////////////////////////////////////////////////////////////////////////////////
1231 // maximum is EITHER attained at the single extreme point:
1232 float a_diff = a_side * a_side - a_up * a_up;
1236 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1237 if (f > -1 && f < 1) // can it be attained?
1239 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1240 //print("middle\n");
1243 // OR attained at z = 1:
1244 f = (a_up + a_add) * (a_up + a_add);
1250 // OR attained at z = -1:
1251 f = (a_up - a_add) * (a_up - a_add);
1255 //print("bottom\n");
1258 //////////////////////////////////////////////////////////////////////////////////////
1260 //print("best possible acceleration: ", ftos(best), "\n");
1263 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1264 if (wishvel_z - PHYS_GRAVITY > 0)
1265 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1267 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1270 fvel = vlen(wishvel);
1273 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1275 fvel = min(1, vlen(wishvel) / best);
1276 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1277 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1281 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1283 if (f > 0 && wishvel != '0 0 0')
1285 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1286 UNSET_ONGROUND(self);
1289 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1290 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1292 self.items |= IT_USING_JETPACK;
1294 // jetpack also inhibits health regeneration, but only for 1 second
1295 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1300 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1301 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1302 self.velocity_z -= g * 0.5;
1304 self.velocity_z -= g;
1305 PM_ClientMovement_Move();
1306 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1307 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1308 self.velocity_z -= g * 0.5;
1312 void PM_walk(float buttons_prev, float maxspd_mod)
1315 // we get here if we ran out of ammo
1316 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1317 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1318 if (!(self.lastflags & FL_ONGROUND))
1320 if (autocvar_speedmeter)
1321 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1322 if (self.lastground < time - 0.3)
1323 self.velocity *= (1 - autocvar_sv_friction_on_land);
1324 if (self.jumppadcount > 1)
1325 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1326 self.jumppadcount = 0;
1330 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1331 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1332 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1334 vector wishdir = normalize(wishvel);
1335 float wishspeed = vlen(wishvel);
1337 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1338 if (IS_DUCKED(self))
1341 // apply edge friction
1342 float f = vlen(vec2(self.velocity));
1345 // TODO: apply edge friction
1346 // apply ground friction
1347 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1351 Mathematical analysis time!
1353 Our goal is to invert this mess.
1355 For the two cases we get:
1356 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1357 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1358 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1360 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1361 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1363 These cases would be chosen ONLY if:
1365 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1366 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1368 v0 >= PHYS_STOPSPEED
1369 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1370 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1373 float addspeed = wishspeed - self.velocity * wishdir;
1376 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1377 self.velocity += accelspeed * wishdir;
1379 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1380 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1381 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1382 if (self.velocity * self.velocity)
1383 PM_ClientMovement_Move();
1384 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1385 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1386 self.velocity_z -= g * 0.5;
1389 void PM_air(float buttons_prev, float maxspd_mod)
1392 // we get here if we ran out of ammo
1393 if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && PHYS_AMMO_FUEL(self) < 0.01)
1394 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1396 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1397 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1398 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1400 vector wishdir = normalize(wishvel);
1401 float wishspeed = vlen(wishvel);
1404 if (time >= self.teleport_time)
1406 if (pmove_waterjumptime <= 0)
1409 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1411 // apply air speed limit
1412 float airaccelqw = PHYS_AIRACCEL_QW(self);
1413 float wishspeed0 = wishspeed;
1414 wishspeed = min(wishspeed, maxairspd);
1415 if (IS_DUCKED(self))
1417 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1419 float accelerating = (self.velocity * wishdir > 0);
1420 float wishspeed2 = wishspeed;
1423 if (PHYS_AIRSTOPACCELERATE)
1425 vector curdir = normalize(vec2(self.velocity));
1426 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1428 // note that for straight forward jumping:
1429 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1430 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1432 // dv/dt = accel * maxspeed (when slow)
1433 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1434 // log dv/dt = logaccel + logmaxspeed (when slow)
1435 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1436 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1437 if (PHYS_MAXAIRSTRAFESPEED)
1438 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1439 if (PHYS_AIRSTRAFEACCELERATE)
1440 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1441 if (PHYS_AIRSTRAFEACCEL_QW(self))
1443 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1445 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1448 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1449 PM_AirAccelerate(wishdir, wishspeed2);
1451 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1453 if (PHYS_AIRCONTROL)
1454 CPM_PM_Aircontrol(wishdir, wishspeed2);
1456 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1457 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1458 self.velocity_z -= g * 0.5;
1460 self.velocity_z -= g;
1461 PM_ClientMovement_Move();
1462 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1463 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1464 self.velocity_z -= g * 0.5;
1467 // used for calculating airshots
1468 float PM_is_flying()
1470 if (IS_ONGROUND(self))
1472 if (self.waterlevel >= WATERLEVEL_SWIMMING)
1474 traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1475 return trace_fraction >= 1;
1481 //Con_Printf(" %f", PHYS_INPUT_TIMELENGTH);
1482 if (!(PHYS_INPUT_BUTTONS(self) & 2)) // !jump
1483 UNSET_JUMP_HELD(self); // canjump = true
1484 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1485 PM_ClientMovement_UpdateStatus();
1489 WarpZone_PlayerPhysics_FixVAngle();
1491 float maxspeed_mod = 1;
1492 maxspeed_mod *= PM_check_keepaway();
1493 maxspeed_mod *= PHYS_HIGHSPEED;
1496 Physics_UpdateStats(maxspeed_mod);
1498 if (self.PlayerPhysplug)
1499 if (self.PlayerPhysplug())
1503 PM_check_race_movetime();
1505 anticheat_physics();
1507 float buttons = PHYS_INPUT_BUTTONS(self);
1509 if (PM_check_specialcommand(buttons))
1514 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1515 self.parm_idlesince = time;
1518 float buttons_prev = self.buttons_old;
1519 self.buttons_old = buttons;
1520 self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1521 self.v_angle_old = PHYS_INPUT_ANGLES(self);
1523 PM_check_nickspam();
1527 if (IS_BOT_CLIENT(self))
1529 if (playerdemo_read())
1534 self.items &= ~IT_USING_JETPACK;
1536 if (IS_PLAYER(self))
1540 if (self.race_penalty)
1541 if (time > self.race_penalty)
1542 self.race_penalty = 0;
1545 float not_allowed_to_move = 0;
1547 if (self.race_penalty)
1548 not_allowed_to_move = 1;
1551 if (!autocvar_sv_ready_restart_after_countdown)
1552 if (time < game_starttime)
1553 not_allowed_to_move = 1;
1556 if (not_allowed_to_move)
1558 self.velocity = '0 0 0';
1559 self.movetype = MOVETYPE_NONE;
1561 self.disableclientprediction = 2;
1565 else if (self.disableclientprediction == 2)
1567 if (self.movetype == MOVETYPE_NONE)
1568 self.movetype = MOVETYPE_WALK;
1569 self.disableclientprediction = 0;
1575 if (self.movetype == MOVETYPE_NONE)
1580 // when we get here, disableclientprediction cannot be 2
1581 self.disableclientprediction = 0;
1582 if (time < self.ladder_time)
1583 self.disableclientprediction = 1;
1591 MUTATOR_CALLHOOK(PlayerPhysics);
1602 if (self.in_swamp) {
1603 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1608 // conveyors: first fix velocity
1609 if (self.conveyor.state)
1610 self.velocity -= self.conveyor.movedir;
1614 if (!IS_PLAYER(self))
1616 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1617 if (!self.spectatorspeed)
1618 self.spectatorspeed = maxspeed_mod;
1619 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1621 if (self.lastclassname != "player")
1623 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1624 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1625 else if (self.impulse == 11)
1626 self.spectatorspeed = maxspeed_mod;
1627 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1628 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1629 else if (self.impulse >= 1 && self.impulse <= 9)
1630 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1631 } // otherwise just clear
1634 maxspeed_mod *= self.spectatorspeed;
1639 // if dead, behave differently
1640 // in CSQC, physics don't handle dead player
1646 if (!self.fixangle && !g_bugrigs)
1647 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1651 if (IS_ONGROUND(self))
1652 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1656 if (self.waterlevel < WATERLEVEL_SWIMMING)
1657 if (time >= self.ladder_time)
1660 self.nextstep = time + 0.3 + random() * 0.1;
1661 trace_dphitq3surfaceflags = 0;
1662 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1663 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1665 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1666 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1668 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1678 if (IS_PLAYER(self))
1683 if (self.flags & /* FL_WATERJUMP */ 2048)
1685 self.velocity_x = self.movedir_x;
1686 self.velocity_y = self.movedir_y;
1687 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1689 self.flags &= ~/* FL_WATERJUMP */ 2048;
1690 self.teleport_time = 0;
1695 else if (g_bugrigs && IS_PLAYER(self))
1699 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1700 PM_fly(maxspeed_mod);
1702 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1703 PM_swim(maxspeed_mod);
1706 else if (time < self.ladder_time)
1707 PM_ladder(maxspeed_mod);
1710 else if ((ITEMS(self) & IT_JETPACK) && PHYS_BUTTON_HOOK(self) && (!PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) > 0 || (ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO)) && !PHYS_FROZEN(self))
1711 PM_jetpack(maxspeed_mod);
1716 // jump if on ground with jump button pressed but only if it has been
1717 // released at least once since the last jump
1718 if (PHYS_INPUT_BUTTONS(self) & 2)
1720 if (IS_ONGROUND(self) && (!IS_JUMP_HELD(self) || !cvar("cl_movement_track_canjump")))
1722 self.velocity_z += PHYS_JUMPVELOCITY;
1723 UNSET_ONGROUND(self);
1724 SET_JUMP_HELD(self); // canjump = false
1728 UNSET_JUMP_HELD(self); // canjump = true
1730 if (IS_ONGROUND(self))
1731 PM_walk(buttons_prev, maxspeed_mod);
1733 PM_air(buttons_prev, maxspeed_mod);
1737 if (!IS_OBSERVER(self))
1743 if (IS_ONGROUND(self))
1744 self.lastground = time;
1747 // conveyors: then break velocity again
1748 if (self.conveyor.state)
1749 self.velocity += self.conveyor.movedir;
1751 self.lastflags = self.flags;
1752 self.lastclassname = self.classname;
1755 void CSQC_ClientMovement_PlayerMove_Frame()
1757 // if a move is more than 50ms, do it as two moves (matching qwsv)
1758 //Con_Printf("%i ", self.cmd.msec);
1759 if (PHYS_INPUT_TIMELENGTH > 0.0005)
1761 if (PHYS_INPUT_TIMELENGTH > 0.05)
1763 PHYS_INPUT_TIMELENGTH /= 2;
1769 // we REALLY need this handling to happen, even if the move is not executed
1770 if (!(PHYS_INPUT_BUTTONS(self) & 2)) // !jump
1771 UNSET_JUMP_HELD(self); // canjump = true
1776 void SV_PlayerPhysics(void)