2 #include "triggers/trigger/swamp.qh"
3 #include "triggers/trigger/jumppads.qh"
7 #include "../server/miscfunctions.qh"
9 void Physics_AddStats()
11 // g_movementspeed hack
12 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
13 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
14 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
15 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
16 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
19 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
20 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
21 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
22 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
23 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
24 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
26 // hack to fix track_canjump
27 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
30 addstat(STAT_DOUBLEJUMP, AS_INT, stat_doublejump);
33 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
34 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
35 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, AS_INT, stat_jumpspeedcap_disable_onramps);
38 addstat(STAT_MOVEVARS_FRICTION_ONLAND, AS_FLOAT, stat_sv_friction_on_land);
39 addstat(STAT_MOVEVARS_FRICTION_SLICK, AS_FLOAT, stat_sv_friction_slick);
40 addstat(STAT_GAMEPLAYFIX_EASIERWATERJUMP, AS_INT, stat_gameplayfix_easierwaterjump);
43 void Physics_UpdateStats(float maxspd_mod)
45 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
46 if (autocvar_sv_airstrafeaccel_qw)
47 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
49 self.stat_sv_airstrafeaccel_qw = 0;
50 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
51 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
52 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
54 self.stat_doublejump = PHYS_DOUBLEJUMP;
56 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
57 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
58 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
59 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
60 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
61 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
63 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
64 self.stat_jumpspeedcap_max = PHYS_JUMPSPEEDCAP_MAX;
65 self.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
67 self.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
68 self.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
70 self.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
74 float IsMoveInDirection(vector mv, float angle) // key mix factor
76 if (mv_x == 0 && mv_y == 0)
77 return 0; // avoid division by zero
78 angle -= RAD2DEG * atan2(mv_y, mv_x);
79 angle = remainder(angle, 360) / 45;
80 return angle > 1 ? 0 : angle < -1 ? 0 : 1 - fabs(angle);
83 float GeomLerp(float a, float lerp, float b)
85 return a == 0 ? (lerp < 1 ? 0 : b)
86 : b == 0 ? (lerp > 0 ? 0 : a)
87 : a * pow(fabs(b / a), lerp);
90 noref float pmove_waterjumptime;
92 const float unstick_count = 27;
93 vector unstick_offsets[unstick_count] =
95 // 1 no nudge (just return the original if this test passes)
98 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
99 '-0.125 0.000 0.000', '0.125 0.000 0.000',
100 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
101 // 4 diagonal flat nudges
102 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
103 '-0.125 0.125 0.000', '0.125 0.125 0.000',
104 // 8 diagonal upward nudges
105 '-0.125 0.000 0.125', '0.125 0.000 0.125',
106 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
107 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
108 '-0.125 0.125 0.125', '0.125 0.125 0.125',
109 // 8 diagonal downward 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',
116 void PM_ClientMovement_Unstick()
119 for (i = 0; i < unstick_count; i++)
121 vector neworigin = unstick_offsets[i] + self.origin;
122 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
123 if (!trace_startsolid)
125 setorigin(self, neworigin);
131 void PM_ClientMovement_UpdateStatus(bool ground)
133 // make sure player is not stuck
134 PM_ClientMovement_Unstick();
137 if (PHYS_INPUT_BUTTON_CROUCH(self))
139 // wants to crouch, this always works..
140 if (!IS_DUCKED(self))
145 // wants to stand, if currently crouching we need to check for a
149 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
150 if (!trace_startsolid)
156 vector origin1 = self.origin + '0 0 1';
157 vector origin2 = self.origin - '0 0 1';
161 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
162 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
166 // this code actually "predicts" an impact; so let's clip velocity first
167 float f = self.velocity * trace_plane_normal;
168 self.velocity -= f * trace_plane_normal;
171 UNSET_ONGROUND(self);
174 // set watertype/waterlevel
175 origin1 = self.origin;
176 origin1_z += self.mins_z + 1;
177 self.waterlevel = WATERLEVEL_NONE;
179 self.watertype = (pointcontents(origin1) == CONTENT_WATER);
183 self.waterlevel = WATERLEVEL_WETFEET;
184 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
185 if(pointcontents(origin1) == CONTENT_WATER)
187 self.waterlevel = WATERLEVEL_SWIMMING;
188 origin1_z = self.origin_z + 22;
189 if(pointcontents(origin1) == CONTENT_WATER)
190 self.waterlevel = WATERLEVEL_SUBMERGED;
194 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
195 pmove_waterjumptime = 0;
198 void PM_ClientMovement_Move()
201 float t = PHYS_INPUT_TIMELENGTH;
202 vector primalvelocity = self.velocity;
203 PM_ClientMovement_UpdateStatus(false);
205 for (bump = 0; bump < MAX_CLIP_PLANES && (self.velocity * self.velocity) > 0; bump++)
207 vector neworigin = self.origin + t * self.velocity;
208 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
209 float old_trace1_fraction = trace_fraction;
210 vector old_trace1_endpos = trace_endpos;
211 vector old_trace1_plane_normal = trace_plane_normal;
212 if (trace_fraction < 1 && trace_plane_normal_z == 0)
214 // may be a step or wall, try stepping up
215 // first move forward at a higher level
216 vector currentorigin2 = self.origin;
217 currentorigin2_z += PHYS_STEPHEIGHT;
218 vector neworigin2 = neworigin;
219 neworigin2_z = self.origin_z + PHYS_STEPHEIGHT;
220 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
221 if (!trace_startsolid)
223 // then move down from there
224 currentorigin2 = trace_endpos;
225 neworigin2 = trace_endpos;
226 neworigin2_z = self.origin_z;
227 float old_trace2_fraction = trace_fraction;
228 vector old_trace2_plane_normal = trace_plane_normal;
229 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
230 // accept the new trace if it made some progress
231 if (fabs(trace_endpos_x - old_trace1_endpos_x) >= 0.03125 || fabs(trace_endpos_y - old_trace1_endpos_y) >= 0.03125)
233 trace_fraction = old_trace2_fraction;
234 trace_endpos = trace_endpos;
235 trace_plane_normal = old_trace2_plane_normal;
239 trace_fraction = old_trace1_fraction;
240 trace_endpos = old_trace1_endpos;
241 trace_plane_normal = old_trace1_plane_normal;
246 // check if it moved at all
247 if (trace_fraction >= 0.001)
248 setorigin(self, trace_endpos);
250 // check if it moved all the way
251 if (trace_fraction == 1)
254 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
255 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
256 // this got commented out in a change that supposedly makes the code match QW better
257 // so if this is broken, maybe put it in an if (cls.protocol != PROTOCOL_QUAKEWORLD) block
258 if (trace_plane_normal_z > 0.7)
261 t -= t * trace_fraction;
263 float f = self.velocity * trace_plane_normal;
264 self.velocity -= f * trace_plane_normal;
266 if (pmove_waterjumptime > 0)
267 self.velocity = primalvelocity;
271 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
273 float k = 32 * (2 * IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), 0) - 1);
277 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
279 float zspeed = self.velocity_z;
281 float xyspeed = vlen(self.velocity);
282 self.velocity = normalize(self.velocity);
284 float dot = self.velocity * wishdir;
286 if (dot > 0) // we can't change direction while slowing down
288 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
289 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
290 k *= PHYS_AIRCONTROL;
291 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
294 self.velocity = self.velocity * xyspeed;
295 self.velocity_z = zspeed;
298 float AdjustAirAccelQW(float accelqw, float factor)
300 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
303 // example config for alternate speed clamping:
304 // sv_airaccel_qw 0.8
305 // sv_airaccel_sideways_friction 0
306 // prvm_globalset server speedclamp_mode 1
308 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
310 float speedclamp = stretchfactor > 0 ? stretchfactor
311 : accelqw < 0 ? 1 // full clamping, no stretch
314 accelqw = fabs(accelqw);
316 if (GAMEPLAYFIX_Q2AIRACCELERATE)
317 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
319 float vel_straight = self.velocity * wishdir;
320 float vel_z = self.velocity_z;
321 vector vel_xy = vec2(self.velocity);
322 vector vel_perpend = vel_xy - vel_straight * wishdir;
324 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
326 float vel_xy_current = vlen(vel_xy);
328 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
329 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
330 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
331 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
332 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
334 if (sidefric < 0 && (vel_perpend*vel_perpend))
335 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
337 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
338 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
340 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
341 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
342 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
343 // obviously, this cannot be
349 vel_perpend *= max(fmin, f);
353 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
355 vel_xy = vel_straight * wishdir + vel_perpend;
359 float vel_xy_preclamp;
360 vel_xy_preclamp = vlen(vel_xy);
361 if (vel_xy_preclamp > 0) // prevent division by zero
363 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
364 if (vel_xy_current < vel_xy_preclamp)
365 vel_xy *= (vel_xy_current / vel_xy_preclamp);
369 self.velocity = vel_xy + vel_z * '0 0 1';
372 void PM_AirAccelerate(vector wishdir, float wishspeed)
377 vector curvel = self.velocity;
379 float curspeed = vlen(curvel);
381 if (wishspeed > curspeed * 1.01)
382 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
385 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
386 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
388 vector wishvel = wishdir * wishspeed;
389 vector acceldir = wishvel - curvel;
390 float addspeed = vlen(acceldir);
391 acceldir = normalize(acceldir);
393 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
395 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
397 vector curdir = normalize(curvel);
398 float dot = acceldir * curdir;
400 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
403 self.velocity += accelspeed * acceldir;
411 When you press the jump key
412 returns true if handled
415 float PlayerJump (void)
417 if (PHYS_FROZEN(self))
418 return true; // no jumping in freezetag when frozen
421 if (self.player_blocked)
422 return true; // no jumping while blocked
425 float doublejump = false;
426 float mjumpheight = PHYS_JUMPVELOCITY;
428 player_multijump = doublejump;
429 player_jumpheight = mjumpheight;
431 if (MUTATOR_CALLHOOK(PlayerJump))
433 if(PM_multijump_checkjump())
437 doublejump = player_multijump;
438 mjumpheight = player_jumpheight;
442 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
443 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
447 // we MUST clip velocity here!
449 f = self.velocity * trace_plane_normal;
451 self.velocity -= f * trace_plane_normal;
455 if (self.waterlevel >= WATERLEVEL_SWIMMING)
457 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
462 if (!IS_ONGROUND(self))
463 return IS_JUMP_HELD(self);
465 if (PHYS_TRACK_CANJUMP(self))
466 if (IS_JUMP_HELD(self))
469 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
470 // velocity bounds. Final velocity is bound between (jumpheight *
471 // min + jumpheight) and (jumpheight * max + jumpheight);
473 if(PHYS_JUMPSPEEDCAP_MIN)
475 float minjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MIN;
477 if (self.velocity_z < minjumpspeed)
478 mjumpheight += minjumpspeed - self.velocity_z;
481 if(PHYS_JUMPSPEEDCAP_MAX)
483 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
484 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
486 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
488 float maxjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MAX;
490 if (self.velocity_z > maxjumpspeed)
491 mjumpheight -= self.velocity_z - maxjumpspeed;
495 if (!WAS_ONGROUND(self))
498 if(autocvar_speedmeter)
499 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
501 if(self.lastground < time - 0.3)
503 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
504 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
507 if(self.jumppadcount > 1)
508 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
509 self.jumppadcount = 0;
513 self.velocity_z += mjumpheight;
515 UNSET_ONGROUND(self);
520 self.oldvelocity_z = self.velocity_z;
522 animdecide_setaction(self, ANIMACTION_JUMP, true);
524 if (autocvar_g_jump_grunt)
525 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
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;
549 self.flags |= FL_WATERJUMP;
551 self.teleport_time = time + 2; // safety net
558 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
560 float autocvar_cl_jetpack_jump;
561 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
563 .float jetpack_stopped;
564 // Hack: shouldn't need to know about this
565 .float multijump_count;
566 void CheckPlayerJump()
569 float was_flying = ITEMS(self) & IT_USING_JETPACK;
571 if (JETPACK_JUMP(self) < 2)
572 ITEMS(self) &= ~IT_USING_JETPACK;
574 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
576 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
577 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
578 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO;
580 if (!(ITEMS(self) & IT_JETPACK)) { }
581 else if (self.jetpack_stopped) { }
585 if (was_flying) // TODO: ran out of fuel message
586 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
588 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
590 self.jetpack_stopped = true;
591 ITEMS(self) &= ~IT_USING_JETPACK;
593 else if (activate && !PHYS_FROZEN(self))
594 ITEMS(self) |= IT_USING_JETPACK;
598 self.jetpack_stopped = false;
599 ITEMS(self) &= ~IT_USING_JETPACK;
601 if (!PHYS_INPUT_BUTTON_JUMP(self))
602 UNSET_JUMP_HELD(self);
604 if (self.waterlevel == WATERLEVEL_SWIMMING)
608 float racecar_angle(float forward, float down)
616 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
618 float angle_mult = forward / (800 + forward);
621 return ret * angle_mult + 360 * (1 - angle_mult);
623 return ret * angle_mult;
626 void RaceCarPhysics()
629 // using this move type for "big rigs"
630 // the engine does not push the entity!
634 vector angles_save = self.angles;
635 float accel = bound(-1, PHYS_INPUT_MOVEVALUES(self).x / PHYS_MAXSPEED(self), 1);
636 float steer = bound(-1, PHYS_INPUT_MOVEVALUES(self).y / PHYS_MAXSPEED(self), 1);
638 if (g_bugrigs_reverse_speeding)
642 // back accel is DIGITAL
643 // to prevent speedhack
653 makevectors(self.angles); // new forward direction!
655 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
657 float myspeed = self.velocity * v_forward;
658 float upspeed = self.velocity * v_up;
660 // responsiveness factor for steering and acceleration
661 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
662 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
665 if (myspeed < 0 && g_bugrigs_reverse_spinning)
666 steerfactor = -myspeed * g_bugrigs_steer;
668 steerfactor = -myspeed * f * g_bugrigs_steer;
671 if (myspeed < 0 && g_bugrigs_reverse_speeding)
672 accelfactor = g_bugrigs_accel;
674 accelfactor = f * g_bugrigs_accel;
675 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
681 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
685 if (!g_bugrigs_reverse_speeding)
686 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
693 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
697 if (g_bugrigs_reverse_stopping)
700 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
703 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
704 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
706 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
707 makevectors(self.angles); // new forward direction!
709 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
711 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
715 float myspeed = vlen(self.velocity);
717 // responsiveness factor for steering and acceleration
718 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
719 float steerfactor = -myspeed * f;
720 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
722 rigvel = self.velocity;
723 makevectors(self.angles); // new forward direction!
726 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
727 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
728 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
729 //MAXIMA: solve(total_acceleration(v) = 0, v);
731 if (g_bugrigs_planar_movement)
733 vector rigvel_xy, neworigin, up;
736 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
737 rigvel_xy = vec2(rigvel);
739 if (g_bugrigs_planar_movement_car_jumping)
742 mt = MOVE_NOMONSTERS;
744 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
745 up = trace_endpos - self.origin;
747 // BUG RIGS: align the move to the surface instead of doing collision testing
749 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
752 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
754 if (trace_fraction < 0.5)
757 neworigin = self.origin;
760 neworigin = trace_endpos;
762 if (trace_fraction < 1)
764 // now set angles_x so that the car points parallel to the surface
765 self.angles = vectoangles(
766 '1 0 0' * v_forward_x * trace_plane_normal_z
768 '0 1 0' * v_forward_y * trace_plane_normal_z
770 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
776 // now set angles_x so that the car points forward, but is tilted in velocity direction
777 UNSET_ONGROUND(self);
780 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
781 self.movetype = MOVETYPE_NOCLIP;
785 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
786 self.velocity = rigvel;
787 self.movetype = MOVETYPE_FLY;
791 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
792 if (trace_fraction != 1)
794 self.angles = vectoangles2(
795 '1 0 0' * v_forward_x * trace_plane_normal_z
797 '0 1 0' * v_forward_y * trace_plane_normal_z
799 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
807 vel_local_x = v_forward * self.velocity;
808 vel_local_y = v_right * self.velocity;
809 vel_local_z = v_up * self.velocity;
811 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
812 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
816 vector vf1, vu1, smoothangles;
817 makevectors(self.angles);
818 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
823 makevectors(angles_save);
824 vf1 = vf1 + v_forward * (1 - f);
825 vu1 = vu1 + v_up * (1 - f);
826 smoothangles = vectoangles2(vf1, vu1);
827 self.angles_x = -smoothangles_x;
828 self.angles_z = smoothangles_z;
832 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
833 .float specialcommand_pos;
834 void SpecialCommand()
840 if (!CheatImpulse(99))
841 print("A hollow voice says \"Plugh\".\n");
846 float PM_check_keepaway(void)
849 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
855 void PM_check_race_movetime(void)
858 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
859 float f = floor(self.race_movetime_frac);
860 self.race_movetime_frac -= f;
861 self.race_movetime_count += f;
862 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
866 float PM_check_specialcommand(float buttons)
872 else if (buttons == 1)
874 else if (buttons == 2)
876 else if (buttons == 128)
878 else if (buttons == 256)
880 else if (buttons == 512)
882 else if (buttons == 1024)
887 if (c == substring(specialcommand, self.specialcommand_pos, 1))
889 self.specialcommand_pos += 1;
890 if (self.specialcommand_pos >= strlen(specialcommand))
892 self.specialcommand_pos = 0;
897 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
898 self.specialcommand_pos = 0;
903 void PM_check_nickspam(void)
906 if (time >= self.nickspamtime)
908 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
910 // slight annoyance for nick change scripts
911 PHYS_INPUT_MOVEVALUES(self) = -1 * PHYS_INPUT_MOVEVALUES(self);
912 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
914 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!
916 PHYS_INPUT_ANGLES(self)_x = random() * 360;
917 PHYS_INPUT_ANGLES(self)_y = random() * 360;
918 // at least I'm not forcing retardedview by also assigning to angles_z
919 self.fixangle = true;
925 void PM_check_punch()
928 if (self.punchangle != '0 0 0')
930 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
932 self.punchangle = normalize(self.punchangle) * f;
934 self.punchangle = '0 0 0';
937 if (self.punchvector != '0 0 0')
939 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
941 self.punchvector = normalize(self.punchvector) * f;
943 self.punchvector = '0 0 0';
948 void PM_check_spider(void)
951 if (time >= self.spider_slowness)
953 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
954 self.stat_sv_airspeedlimit_nonqw *= 0.5;
958 // predict frozen movement, as frozen players CAN move in some cases
959 void PM_check_frozen(void)
961 if (!PHYS_FROZEN(self))
963 if (PHYS_DODGING_FROZEN
965 && IS_REAL_CLIENT(self)
969 PHYS_INPUT_MOVEVALUES(self)_x = bound(-5, PHYS_INPUT_MOVEVALUES(self).x, 5);
970 PHYS_INPUT_MOVEVALUES(self)_y = bound(-5, PHYS_INPUT_MOVEVALUES(self).y, 5);
971 PHYS_INPUT_MOVEVALUES(self)_z = bound(-5, PHYS_INPUT_MOVEVALUES(self).z, 5);
974 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
976 vector midpoint = ((self.absmin + self.absmax) * 0.5);
977 if (pointcontents(midpoint) == CONTENT_WATER)
979 self.velocity = self.velocity * 0.5;
981 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
982 self.velocity_z = 200;
986 void PM_check_hitground()
989 if (IS_ONGROUND(self))
990 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
994 if (self.waterlevel < WATERLEVEL_SWIMMING)
995 if (time >= self.ladder_time)
998 self.nextstep = time + 0.3 + random() * 0.1;
999 trace_dphitq3surfaceflags = 0;
1000 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1001 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1003 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1004 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1006 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1013 void PM_check_blocked(void)
1016 if (!self.player_blocked)
1018 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
1019 self.disableclientprediction = 1;
1024 float speedaward_lastsent;
1025 float speedaward_lastupdate;
1027 void PM_check_race(void)
1030 if(!(g_cts || g_race))
1032 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1034 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1035 speedaward_holder = self.netname;
1036 speedaward_uid = self.crypto_idfp;
1037 speedaward_lastupdate = time;
1039 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1041 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1042 race_send_speedaward(MSG_ALL);
1043 speedaward_lastsent = speedaward_speed;
1044 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1046 speedaward_alltimebest = speedaward_speed;
1047 speedaward_alltimebest_holder = speedaward_holder;
1048 speedaward_alltimebest_uid = speedaward_uid;
1049 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1050 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1051 race_send_speedaward_alltimebest(MSG_ALL);
1057 void PM_check_vortex(void)
1061 float xyspeed = vlen(vec2(self.velocity));
1062 if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1064 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1065 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1066 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1067 // add the extra charge
1068 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1073 void PM_fly(float maxspd_mod)
1075 // noclipping or flying
1076 UNSET_ONGROUND(self);
1078 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1079 makevectors(PHYS_INPUT_ANGLES(self));
1080 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1081 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1082 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1083 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1085 vector wishdir = normalize(wishvel);
1086 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1088 if (time >= self.teleport_time)
1090 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1091 PM_ClientMovement_Move();
1094 void PM_swim(float maxspd_mod)
1097 UNSET_ONGROUND(self);
1099 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1100 // water jump only in certain situations
1101 // this mimics quakeworld code
1102 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1104 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1105 makevectors(yawangles);
1106 vector forward = v_forward;
1107 vector spot = self.origin + 24 * forward;
1109 traceline(spot, spot, MOVE_NOMONSTERS, self);
1110 if (trace_startsolid)
1113 traceline(spot, spot, MOVE_NOMONSTERS, self);
1114 if (!trace_startsolid)
1116 self.velocity = forward * 50;
1117 self.velocity_z = 310;
1118 pmove_waterjumptime = 2;
1119 UNSET_ONGROUND(self);
1120 SET_JUMP_HELD(self);
1124 makevectors(PHYS_INPUT_ANGLES(self));
1125 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1126 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1127 + v_right * PHYS_INPUT_MOVEVALUES(self).y
1128 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1129 if (wishvel == '0 0 0')
1130 wishvel = '0 0 -60'; // drift towards bottom
1132 vector wishdir = normalize(wishvel);
1133 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1135 if (IS_DUCKED(self))
1138 // if (pmove_waterjumptime <= 0) // TODO: use
1141 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1142 f = min(max(0, f), 1);
1145 f = wishspeed - self.velocity * wishdir;
1148 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1149 self.velocity += accelspeed * wishdir;
1152 // holding jump button swims upward slowly
1156 if (self.watertype & CONTENT_LAVA)
1157 self.velocity_z = 50;
1158 else if (self.watertype & CONTENT_SLIME)
1159 self.velocity_z = 80;
1162 if (IS_NEXUIZ_DERIVED(gamemode))
1164 self.velocity_z = 200;
1167 self.velocity_z = 100;
1172 // water acceleration
1173 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1174 PM_ClientMovement_Move();
1177 void PM_ladder(float maxspd_mod)
1179 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1180 UNSET_ONGROUND(self);
1183 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1184 if (PHYS_ENTGRAVITY(self))
1185 g *= PHYS_ENTGRAVITY(self);
1186 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1189 self.velocity_z += g;
1192 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1193 makevectors(PHYS_INPUT_ANGLES(self));
1194 //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1195 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1196 + v_right * PHYS_INPUT_MOVEVALUES(self)_y
1197 + '0 0 1' * PHYS_INPUT_MOVEVALUES(self)_z;
1198 self.velocity_z += g;
1199 if (self.ladder_entity.classname == "func_water")
1201 float f = vlen(wishvel);
1202 if (f > self.ladder_entity.speed)
1203 wishvel *= (self.ladder_entity.speed / f);
1205 self.watertype = self.ladder_entity.skin;
1206 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1207 if ((self.origin_z + self.view_ofs_z) < f)
1208 self.waterlevel = WATERLEVEL_SUBMERGED;
1209 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1210 self.waterlevel = WATERLEVEL_SWIMMING;
1211 else if ((self.origin_z + self.mins_z + 1) < f)
1212 self.waterlevel = WATERLEVEL_WETFEET;
1215 self.waterlevel = WATERLEVEL_NONE;
1216 self.watertype = CONTENT_EMPTY;
1220 vector wishdir = normalize(wishvel);
1221 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1223 if (time >= self.teleport_time)
1225 // water acceleration
1226 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1227 PM_ClientMovement_Move();
1230 void PM_jetpack(float maxspd_mod)
1232 //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1233 makevectors(PHYS_INPUT_ANGLES(self));
1234 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1235 + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1236 // add remaining speed as Z component
1237 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1238 // fix speedhacks :P
1239 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1240 // add the unused velocity as up component
1243 // if (self.BUTTON_JUMP)
1244 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1246 // it is now normalized, so...
1247 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1248 float a_up = PHYS_JETPACK_ACCEL_UP;
1249 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1251 wishvel_x *= a_side;
1252 wishvel_y *= a_side;
1257 //////////////////////////////////////////////////////////////////////////////////////
1258 // finding the maximum over all vectors of above form
1259 // with wishvel having an absolute value of 1
1260 //////////////////////////////////////////////////////////////////////////////////////
1261 // we're finding the maximum over
1262 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1263 // for z in the range from -1 to 1
1264 //////////////////////////////////////////////////////////////////////////////////////
1265 // maximum is EITHER attained at the single extreme point:
1266 float a_diff = a_side * a_side - a_up * a_up;
1270 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1271 if (f > -1 && f < 1) // can it be attained?
1273 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1274 //print("middle\n");
1277 // OR attained at z = 1:
1278 f = (a_up + a_add) * (a_up + a_add);
1284 // OR attained at z = -1:
1285 f = (a_up - a_add) * (a_up - a_add);
1289 //print("bottom\n");
1292 //////////////////////////////////////////////////////////////////////////////////////
1294 //print("best possible acceleration: ", ftos(best), "\n");
1297 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1298 if (wishvel_z - PHYS_GRAVITY > 0)
1299 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1301 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1304 fvel = vlen(wishvel);
1307 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1309 fvel = min(1, vlen(wishvel) / best);
1310 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1311 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1315 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1317 if (f > 0 && wishvel != '0 0 0')
1319 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1320 UNSET_ONGROUND(self);
1323 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1324 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1326 ITEMS(self) |= IT_USING_JETPACK;
1328 // jetpack also inhibits health regeneration, but only for 1 second
1329 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1334 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1335 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1336 self.velocity_z -= g * 0.5;
1338 self.velocity_z -= g;
1339 PM_ClientMovement_Move();
1340 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1341 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1342 self.velocity_z -= g * 0.5;
1346 void PM_walk(float buttons_prev, float maxspd_mod)
1348 if (!WAS_ONGROUND(self))
1351 if (autocvar_speedmeter)
1352 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1354 if (self.lastground < time - 0.3)
1355 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1357 if (self.jumppadcount > 1)
1358 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1359 self.jumppadcount = 0;
1364 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1365 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1366 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1368 vector wishdir = normalize(wishvel);
1369 float wishspeed = vlen(wishvel);
1371 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1372 if (IS_DUCKED(self))
1375 // apply edge friction
1376 float f = vlen(vec2(self.velocity));
1380 trace_dphitq3surfaceflags = 0;
1381 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1382 // TODO: apply edge friction
1383 // apply ground friction
1384 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1385 realfriction = PHYS_FRICTION_SLICK;
1387 realfriction = PHYS_FRICTION;
1389 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1393 Mathematical analysis time!
1395 Our goal is to invert this mess.
1397 For the two cases we get:
1398 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1399 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1400 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1402 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1403 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1405 These cases would be chosen ONLY if:
1407 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1408 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1410 v0 >= PHYS_STOPSPEED
1411 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1412 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1415 float addspeed = wishspeed - self.velocity * wishdir;
1418 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1419 self.velocity += accelspeed * wishdir;
1421 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1422 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1423 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1424 if (self.velocity * self.velocity)
1425 PM_ClientMovement_Move();
1426 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1427 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1428 self.velocity_z -= g * 0.5;
1431 void PM_air(float buttons_prev, float maxspd_mod)
1433 makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1434 vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1435 + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1437 vector wishdir = normalize(wishvel);
1438 float wishspeed = vlen(wishvel);
1441 if (time >= self.teleport_time)
1443 if (pmove_waterjumptime <= 0)
1446 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1448 // apply air speed limit
1449 float airaccelqw = PHYS_AIRACCEL_QW(self);
1450 float wishspeed0 = wishspeed;
1451 wishspeed = min(wishspeed, maxairspd);
1452 if (IS_DUCKED(self))
1454 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1456 float accelerating = (self.velocity * wishdir > 0);
1457 float wishspeed2 = wishspeed;
1460 if (PHYS_AIRSTOPACCELERATE)
1462 vector curdir = normalize(vec2(self.velocity));
1463 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1465 // note that for straight forward jumping:
1466 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1467 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1469 // dv/dt = accel * maxspeed (when slow)
1470 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1471 // log dv/dt = logaccel + logmaxspeed (when slow)
1472 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1473 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1474 if (PHYS_MAXAIRSTRAFESPEED)
1475 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1476 if (PHYS_AIRSTRAFEACCELERATE)
1477 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1478 if (PHYS_AIRSTRAFEACCEL_QW(self))
1480 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1482 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1485 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1486 PM_AirAccelerate(wishdir, wishspeed2);
1488 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1490 if (PHYS_AIRCONTROL)
1491 CPM_PM_Aircontrol(wishdir, wishspeed2);
1493 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1494 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1495 self.velocity_z -= g * 0.5;
1497 self.velocity_z -= g;
1498 PM_ClientMovement_Move();
1499 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1500 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1501 self.velocity_z -= g * 0.5;
1504 // used for calculating airshots
1505 bool IsFlying(entity a)
1509 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1511 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1512 if(trace_fraction < 1)
1519 float buttons = PHYS_INPUT_BUTTON_MASK(self);
1521 self.items = getstati(STAT_ITEMS, 0, 24);
1523 self.team = myteam + 1; // is this correct?
1524 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1525 UNSET_JUMP_HELD(self); // canjump = true
1526 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1528 PM_ClientMovement_UpdateStatus(true);
1533 WarpZone_PlayerPhysics_FixVAngle();
1535 float maxspeed_mod = 1;
1536 maxspeed_mod *= PM_check_keepaway();
1537 maxspeed_mod *= PHYS_HIGHSPEED;
1540 Physics_UpdateStats(maxspeed_mod);
1542 if (self.PlayerPhysplug)
1543 if (self.PlayerPhysplug())
1547 PM_check_race_movetime();
1549 anticheat_physics();
1552 if (PM_check_specialcommand(buttons))
1557 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1558 self.parm_idlesince = time;
1561 float buttons_prev = self.buttons_old;
1562 self.buttons_old = buttons;
1563 self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1564 self.v_angle_old = PHYS_INPUT_ANGLES(self);
1566 PM_check_nickspam();
1570 if (IS_BOT_CLIENT(self))
1572 if (playerdemo_read())
1577 if (IS_PLAYER(self))
1581 if (self.race_penalty)
1582 if (time > self.race_penalty)
1583 self.race_penalty = 0;
1586 float not_allowed_to_move = 0;
1588 if (self.race_penalty)
1589 not_allowed_to_move = 1;
1592 if (time < game_starttime)
1593 not_allowed_to_move = 1;
1596 if (not_allowed_to_move)
1598 self.velocity = '0 0 0';
1599 self.movetype = MOVETYPE_NONE;
1601 self.disableclientprediction = 2;
1605 else if (self.disableclientprediction == 2)
1607 if (self.movetype == MOVETYPE_NONE)
1608 self.movetype = MOVETYPE_WALK;
1609 self.disableclientprediction = 0;
1615 if (self.movetype == MOVETYPE_NONE)
1618 // when we get here, disableclientprediction cannot be 2
1619 self.disableclientprediction = 0;
1631 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1633 // conveyors: first fix velocity
1634 if (self.conveyor.state)
1635 self.velocity -= self.conveyor.movedir;
1638 MUTATOR_CALLHOOK(PlayerPhysics);
1644 // float forcedodge = 1;
1647 // PM_dodging_checkpressedkeys();
1650 // PM_ClientMovement_Move();
1655 if (!IS_PLAYER(self))
1657 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1658 if (!self.spectatorspeed)
1659 self.spectatorspeed = maxspeed_mod;
1660 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1662 if (self.lastclassname != "player")
1664 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1665 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1666 else if (self.impulse == 11)
1667 self.spectatorspeed = maxspeed_mod;
1668 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1669 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1670 else if (self.impulse >= 1 && self.impulse <= 9)
1671 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1672 } // otherwise just clear
1675 maxspeed_mod *= self.spectatorspeed;
1683 if (!self.fixangle && !g_bugrigs)
1684 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1687 PM_check_hitground();
1692 if (IS_PLAYER(self))
1695 if (self.flags & FL_WATERJUMP)
1697 self.velocity_x = self.movedir_x;
1698 self.velocity_y = self.movedir_y;
1699 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1701 self.flags &= ~FL_WATERJUMP;
1702 self.teleport_time = 0;
1707 else if (g_bugrigs && IS_PLAYER(self))
1711 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS(self) & BUFF_FLIGHT))
1712 PM_fly(maxspeed_mod);
1714 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1715 PM_swim(maxspeed_mod);
1717 else if (time < self.ladder_time)
1718 PM_ladder(maxspeed_mod);
1720 else if (ITEMS(self) & IT_USING_JETPACK)
1721 PM_jetpack(maxspeed_mod);
1723 else if (IS_ONGROUND(self))
1724 PM_walk(buttons_prev, maxspeed_mod);
1727 PM_air(buttons_prev, maxspeed_mod);
1730 if (!IS_OBSERVER(self))
1736 if (IS_ONGROUND(self))
1737 self.lastground = time;
1739 // conveyors: then break velocity again
1740 if(self.conveyor.state)
1741 self.velocity += self.conveyor.movedir;
1743 self.lastflags = self.flags;
1745 self.lastclassname = self.classname;
1749 void SV_PlayerPhysics(void)
1751 void CSQC_ClientMovement_PlayerMove_Frame(void)
1758 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1759 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1760 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);