3 #include <common/mapobjects/_mod.qh>
4 #include <common/viewloc.qh>
7 REPLICATE(cvar_cl_physics, string, "cl_physics");
8 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
9 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
13 #include <common/mapobjects/defs.qh>
14 #include <common/mapobjects/trigger/viewloc.qh>
15 #include <server/client.qh>
17 // client side physics
18 bool Physics_Valid(string thecvar)
20 return thecvar != "" && thecvar && thecvar != "default" && strhasword(autocvar_g_physics_clientselect_options, thecvar);
23 float Physics_ClientOption(entity this, string option, float defaultval)
25 if(!autocvar_g_physics_clientselect)
28 if(IS_REAL_CLIENT(this) && Physics_Valid(CS_CVAR(this).cvar_cl_physics))
30 string s = strcat("g_physics_", CS_CVAR(this).cvar_cl_physics, "_", option);
31 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
34 if(autocvar_g_physics_clientselect_default && autocvar_g_physics_clientselect_default != "" && autocvar_g_physics_clientselect_default != "default")
36 // NOTE: not using Physics_Valid here, so the default can be forced to something normally unavailable
37 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
38 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
44 void Physics_UpdateStats(entity this)
46 // update this first, as it's used on all stats (wouldn't want to update them all manually from a mutator hook now, would we?)
47 STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
49 MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
50 float maxspd_mod = PHYS_HIGHSPEED(this) * ((this.swampslug.active == ACTIVE_ACTIVE) ? this.swampslug.swamp_slowdown : 1);
51 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
52 if (autocvar_g_movement_highspeed_q3_compat)
54 STAT(MOVEVARS_AIRACCEL_QW, this) = Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw);
55 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw);
56 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw);
60 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
61 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
62 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
64 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
67 /* Q3 uses the following:
71 * CROUCH_MIN '-15 -15 -24'
72 * CROUCH_MAX '15 15 16'
73 * CROUCH_VIEW_OFS '0 0 12'
74 * but xon player models have a different z offset to suit the origin at 24/69
75 * at q3compat hitbox and model scale the equivalent offset is origin at 20/56
77 bool q3hb = q3compat && autocvar_sv_q3compat_changehitbox;
78 STAT(PL_MIN, this) = q3hb ? '-15 -15 -20' : autocvar_sv_player_mins;
79 STAT(PL_MAX, this) = q3hb ? '15 15 36' : autocvar_sv_player_maxs;
80 STAT(PL_VIEW_OFS, this) = q3hb ? '0 0 30' : autocvar_sv_player_viewoffset;
81 STAT(PL_CROUCH_MIN, this) = q3hb ? '-15 -15 -20' : autocvar_sv_player_crouch_mins;
82 STAT(PL_CROUCH_MAX, this) = q3hb ? '15 15 20' : autocvar_sv_player_crouch_maxs;
83 STAT(PL_CROUCH_VIEW_OFS, this) = q3hb ? '0 0 16' : autocvar_sv_player_crouch_viewoffset;
86 // fix some new settings
87 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
88 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
89 if (autocvar_g_movement_highspeed_q3_compat)
90 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed) * maxspd_mod;
92 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
94 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
95 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
96 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
97 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
98 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
99 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
100 STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
101 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
102 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
103 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
104 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
105 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
106 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
107 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
108 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
109 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
110 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
111 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
112 STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
113 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
115 MUTATOR_CALLHOOK(PlayerPhysics_PostUpdateStats, this, maxspd_mod);
119 float IsMoveInDirection(vector mv, float ang) // key mix factor
121 if (mv_x == 0 && mv_y == 0)
122 return 0; // avoid division by zero
123 ang -= RAD2DEG * atan2(mv_y, mv_x);
124 ang = remainder(ang, 360) / 45;
125 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
128 float GeomLerp(float a, float _lerp, float b)
130 return a == 0 ? (_lerp < 1 ? 0 : b)
131 : b == 0 ? (_lerp > 0 ? 0 : a)
132 : a * (fabs(b / a) ** _lerp);
135 void PM_ClientMovement_UpdateStatus(entity this)
140 bool have_hook = false;
141 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
144 entity wepent = viewmodels[slot];
146 .entity weaponentity = weaponentities[slot];
147 entity wepent = this.(weaponentity);
149 if(wepent.hook && !wasfreed(wepent.hook))
155 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
156 if(this.viewloc && !(this.viewloc.spawnflags & VIEWLOC_FREEMOVE) && PHYS_CS(this).movement.x < 0)
160 //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
162 } else if (PHYS_INVEHICLE(this)) {
164 } else if (STAT(FROZEN, this) || IS_DEAD(this)) {
168 MUTATOR_CALLHOOK(PlayerCanCrouch, this, do_crouch);
169 do_crouch = M_ARGV(1, bool);
172 if (!IS_DUCKED(this)) {
174 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
175 setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
176 // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
178 } else if (IS_DUCKED(this)) {
179 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
180 if (!trace_startsolid) {
182 this.view_ofs = STAT(PL_VIEW_OFS, this);
183 setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
187 _Movetype_CheckWater(this); // needs to be run on the client, might as well use the latest on the server too!
190 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
192 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
193 if(PHYS_AIRCONTROL_BACKWARDS(this))
194 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
195 if(PHYS_AIRCONTROL_SIDEWARDS(this))
197 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
198 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
201 float k = 32 * (2 * movity - 1);
205 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
207 float zspeed = this.velocity_z;
209 float xyspeed = vlen(this.velocity);
210 this.velocity = normalize(this.velocity);
212 float dot = this.velocity * wishdir;
214 if (dot > 0) // we can't change direction while slowing down
216 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
217 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
218 k *= PHYS_AIRCONTROL(this);
219 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
222 this.velocity = this.velocity * xyspeed;
223 this.velocity_z = zspeed;
226 float AdjustAirAccelQW(float accelqw, float factor)
228 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
231 // example config for alternate speed clamping:
232 // sv_airaccel_qw 0.8
233 // sv_airaccel_sideways_friction 0
234 // prvm_globalset server speedclamp_mode 1
236 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
238 float speedclamp = stretchfactor > 0 ? stretchfactor
239 : accelqw < 0 ? 1 // full clamping, no stretch
242 accelqw = fabs(accelqw);
244 if (GAMEPLAYFIX_Q2AIRACCELERATE)
245 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
247 float vel_straight = this.velocity * wishdir;
248 float vel_z = this.velocity_z;
249 vector vel_xy = vec2(this.velocity);
250 vector vel_perpend = vel_xy - vel_straight * wishdir;
252 float step = accel * dt * wishspeed0;
254 float vel_xy_current = vlen(vel_xy);
256 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
257 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
258 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
259 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
260 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
262 if (sidefric < 0 && (vel_perpend*vel_perpend))
263 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
265 float f = max(0, 1 + dt * wishspeed * sidefric);
266 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
267 // assume: themin > 1
268 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
269 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
270 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
271 // obviously, this cannot be
276 themin = sqrt(themin);
277 vel_perpend *= max(themin, f);
281 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
283 vel_xy = vel_straight * wishdir + vel_perpend;
287 float vel_xy_preclamp;
288 vel_xy_preclamp = vlen(vel_xy);
289 if (vel_xy_preclamp > 0) // prevent division by zero
291 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
292 if (vel_xy_current < vel_xy_preclamp)
293 vel_xy *= (vel_xy_current / vel_xy_preclamp);
297 this.velocity = vel_xy + vel_z * '0 0 1';
300 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
305 vector curvel = this.velocity;
307 float curspeed = vlen(curvel);
309 if (wishspeed > curspeed * 1.01)
310 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
313 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
314 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
316 vector wishvel = wishdir * wishspeed;
317 vector acceldir = wishvel - curvel;
318 float addspeed = vlen(acceldir);
319 acceldir = normalize(acceldir);
321 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
323 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
325 vector curdir = normalize(curvel);
326 float dot = acceldir * curdir;
328 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
331 this.velocity += accelspeed * acceldir;
339 When you press the jump key
340 returns true if handled
343 bool PlayerJump(entity this)
345 if (PHYS_FROZEN(this))
346 return true; // no jumping in freezetag when frozen
348 if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
349 return true; // no jumping while typing
352 if (this.player_blocked)
353 return true; // no jumping while blocked
356 bool doublejump = false;
357 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
358 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
360 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
363 mjumpheight = M_ARGV(1, float);
364 doublejump = M_ARGV(2, bool);
366 if (this.waterlevel >= WATERLEVEL_SWIMMING)
376 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
382 if (!IS_ONGROUND(this))
383 return IS_JUMP_HELD(this);
385 if(PHYS_TRACK_CANJUMP(this))
389 if (IS_JUMP_HELD(this))
392 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
393 // velocity bounds. Final velocity is bound between (jumpheight *
394 // min + jumpheight) and (jumpheight * max + jumpheight);
396 if(PHYS_JUMPSPEEDCAP_MIN != "")
398 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
400 if (this.velocity_z < minjumpspeed)
401 mjumpheight += minjumpspeed - this.velocity_z;
404 if(PHYS_JUMPSPEEDCAP_MAX != "")
406 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
407 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
409 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
411 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
413 if (this.velocity_z > maxjumpspeed)
414 mjumpheight -= this.velocity_z - maxjumpspeed;
418 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
421 if(autocvar_speedmeter)
422 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
424 if(this.lastground < time - 0.3)
426 float f = (1 - PHYS_FRICTION_ONLAND(this));
427 this.velocity_x *= f;
428 this.velocity_y *= f;
431 if(this.jumppadcount > 1)
432 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
433 this.jumppadcount = 0;
437 this.velocity_z += mjumpheight;
439 UNSET_ONGROUND(this);
444 animdecide_setaction(this, ANIMACTION_JUMP, true);
446 if (autocvar_g_jump_grunt)
447 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
452 void CheckWaterJump(entity this)
454 // check for a jump-out-of-water
455 makevectors(this.v_angle);
456 vector start = this.origin;
459 normalize(v_forward);
460 vector end = start + v_forward*24;
461 traceline (start, end, true, this);
462 if (trace_fraction < 1)
464 start_z = start_z + this.maxs_z - 8;
465 end = start + v_forward*24;
466 this.movedir = trace_plane_normal * -50;
467 traceline(start, end, true, this);
468 if (trace_fraction == 1)
469 { // open at eye level
470 this.velocity_z = 225;
471 this.flags |= FL_WATERJUMP;
472 this.teleport_time = time + 2; // safety net
480 #define JETPACK_JUMP(s) CS_CVAR(s).cvar_cl_jetpack_jump
482 float autocvar_cl_jetpack_jump;
483 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
485 .float jetpack_stopped;
486 void CheckPlayerJump(entity this)
489 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
491 if (JETPACK_JUMP(this) < 2)
492 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
494 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
496 bool playerjump = PlayerJump(this); // required
498 bool air_jump = !playerjump || M_ARGV(2, bool);
499 bool activate = (JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this)) || PHYS_INPUT_BUTTON_JETPACK(this);
500 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_AMMO);
502 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
503 else if (this.jetpack_stopped) { }
507 if (was_flying) // TODO: ran out of fuel message
508 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
510 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
512 this.jetpack_stopped = true;
513 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
515 else if (activate && !PHYS_FROZEN(this))
516 ITEMS_STAT(this) |= IT_USING_JETPACK;
520 this.jetpack_stopped = false;
521 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
523 if (!PHYS_INPUT_BUTTON_JUMP(this))
524 UNSET_JUMP_HELD(this);
526 if (this.waterlevel == WATERLEVEL_SWIMMING)
527 CheckWaterJump(this);
531 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
532 .float specialcommand_pos;
533 void SpecialCommand(entity this)
535 if(autocvar_sv_cheats || this.maycheat)
537 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
538 LOG_INFO("A hollow voice says \"Plugh\".");
543 bool PM_check_specialcommand(entity this, int buttons)
549 // buttons mapped in PHYS_INPUT_BUTTON_MASK
550 case 0: c = "x"; break;
551 case BIT(0): c = "1"; break;
552 case BIT(2): c = " "; break;
553 case BIT(7): c = "s"; break;
554 case BIT(8): c = "w"; break;
555 case BIT(9): c = "a"; break;
556 case BIT(10): c = "d"; break;
560 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
562 CS(this).specialcommand_pos += 1;
563 if (CS(this).specialcommand_pos >= strlen(specialcommand))
565 CS(this).specialcommand_pos = 0;
566 SpecialCommand(this);
570 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
571 CS(this).specialcommand_pos = 0;
576 void PM_check_nickspam(entity this)
579 if (time >= this.nickspamtime)
581 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
583 // slight annoyance for nick change scripts
584 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
585 PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_JUMP(this) = PHYS_INPUT_BUTTON_ATCK2(this) = PHYS_INPUT_BUTTON_ZOOM(this) = PHYS_INPUT_BUTTON_CROUCH(this) = PHYS_INPUT_BUTTON_HOOK(this) = PHYS_INPUT_BUTTON_USE(this) = false;
587 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
589 this.v_angle_x = random() * 360;
590 this.v_angle_y = random() * 360;
591 // at least I'm not forcing retardedview by also assigning to angles_z
592 this.fixangle = true;
598 void PM_check_punch(entity this, float dt)
601 if (this.punchangle != '0 0 0')
603 float f = vlen(this.punchangle) - 10 * dt;
605 this.punchangle = normalize(this.punchangle) * f;
607 this.punchangle = '0 0 0';
610 if (this.punchvector != '0 0 0')
612 float f = vlen(this.punchvector) - 30 * dt;
614 this.punchvector = normalize(this.punchvector) * f;
616 this.punchvector = '0 0 0';
621 // predict frozen movement, as frozen players CAN move in some cases
622 void PM_check_frozen(entity this)
624 if (!PHYS_FROZEN(this))
626 if (PHYS_DODGING_FROZEN(this) && IS_CLIENT(this))
628 // bind movement to a very slow speed so dodging can use .movement for directional calculations
629 PHYS_CS(this).movement_x = bound(-2, PHYS_CS(this).movement.x, 2);
630 PHYS_CS(this).movement_y = bound(-2, PHYS_CS(this).movement.y, 2);
631 PHYS_CS(this).movement_z = bound(-2, PHYS_CS(this).movement.z, 2);
634 PHYS_CS(this).movement = '0 0 0';
637 void PM_check_hitground(entity this)
640 if (!this.wasFlying) return;
641 this.wasFlying = false;
642 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
643 if (this.ladder_entity) return;
644 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
646 .entity weaponentity = weaponentities[slot];
647 if(this.(weaponentity).hook)
650 this.nextstep = time + 0.3 + random() * 0.1;
651 trace_dphitq3surfaceflags = 0;
652 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
653 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
654 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
657 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
658 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
662 void PM_Footsteps(entity this)
665 if (!autocvar_g_footsteps) return;
666 if (IS_DUCKED(this)) return;
667 if (time >= this.lastground + 0.2) return;
668 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
669 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
671 this.nextstep = time + 0.3 + random() * 0.1;
672 trace_dphitq3surfaceflags = 0;
673 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
674 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
675 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
678 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
683 void PM_check_slick(entity this)
685 if(!IS_ONGROUND(this))
688 trace_dphitq3surfaceflags = 0;
689 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
690 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
696 void PM_check_blocked(entity this)
698 if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
699 PHYS_CS(this).movement = '0 0 0';
701 if (!this.player_blocked)
703 PHYS_CS(this).movement = '0 0 0';
704 this.disableclientprediction = 1;
708 void PM_jetpack(entity this, float maxspd_mod, float dt)
710 //makevectors(this.v_angle.y * '0 1 0');
711 makevectors(this.v_angle);
712 vector wishvel = v_forward * PHYS_CS(this).movement_x
713 + v_right * PHYS_CS(this).movement_y;
714 // add remaining speed as Z component
715 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
717 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
718 // add the unused velocity as up component
721 // if (PHYS_INPUT_BUTTON_JUMP(this))
722 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
724 // it is now normalized, so...
725 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
726 float a_up = PHYS_JETPACK_ACCEL_UP(this);
727 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
729 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
736 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
739 //////////////////////////////////////////////////////////////////////////////////////
740 // finding the maximum over all vectors of above form
741 // with wishvel having an absolute value of 1
742 //////////////////////////////////////////////////////////////////////////////////////
743 // we're finding the maximum over
744 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
745 // for z in the range from -1 to 1
746 //////////////////////////////////////////////////////////////////////////////////////
747 // maximum is EITHER attained at the single extreme point:
748 float a_diff = a_side * a_side - a_up * a_up;
752 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
753 if (f > -1 && f < 1) // can it be attained?
755 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
759 // OR attained at z = 1:
760 f = (a_up + a_add) * (a_up + a_add);
766 // OR attained at z = -1:
767 f = (a_up - a_add) * (a_up - a_add);
774 //////////////////////////////////////////////////////////////////////////////////////
776 //print("best possible acceleration: ", ftos(best), "\n");
779 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
780 if (wishvel_z - PHYS_GRAVITY(this) > 0)
781 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
783 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
786 fvel = vlen(wishvel);
789 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
791 fvel = min(1, vlen(wishvel) / best);
792 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
793 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
797 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
799 if (f > 0 && wishvel != '0 0 0')
801 this.velocity = this.velocity + wishvel * f * dt;
802 UNSET_ONGROUND(this);
805 if (!(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
806 TakeResource(this, RES_FUEL, PHYS_JETPACK_FUEL(this) * dt * fvel * f);
808 ITEMS_STAT(this) |= IT_USING_JETPACK;
810 // jetpack also inhibits health regeneration, but only for 1 second
811 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
816 // used for calculating airshots
817 bool IsFlying(entity this)
819 if(IS_ONGROUND(this))
821 if(this.waterlevel >= WATERLEVEL_SWIMMING)
823 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 24', MOVE_NORMAL, this);
824 //traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
825 if(trace_fraction < 1)
831 void sys_phys_update(entity this, float dt);
833 void SV_PlayerPhysics(entity this)
835 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
839 // needs to be called before physics are run!
840 if(IS_REAL_CLIENT(this))
841 PM_UpdateButtons(this, CS(this));
844 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
847 CS(this).pm_frametime = frametime;
849 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
850 this.csqcmodel_modelflags |= MF_ROCKET;
852 this.csqcmodel_modelflags &= ~MF_ROCKET;