2 #include "../triggers/include.qh"
3 #include "../viewloc.qh"
7 #include <server/miscfunctions.qh>
8 #include "../triggers/trigger/viewloc.qh"
10 // client side physics
11 bool Physics_Valid(string thecvar)
13 return autocvar_g_physics_clientselect && thecvar != "" && thecvar && thecvar != "default" && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 float Physics_ClientOption(entity this, string option, float defaultval)
18 if(IS_REAL_CLIENT(this) && Physics_Valid(CS(this).cvar_cl_physics))
20 string s = strcat("g_physics_", CS(this).cvar_cl_physics, "_", option);
21 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
24 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default && autocvar_g_physics_clientselect_default != "")
26 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
27 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
33 void Physics_UpdateStats(entity this)
35 // 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?)
36 STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
38 MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
39 float maxspd_mod = PHYS_HIGHSPEED(this);
41 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
42 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
43 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
45 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
46 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
48 STAT(PL_MIN, this) = autocvar_sv_player_mins;
49 STAT(PL_MAX, this) = autocvar_sv_player_maxs;
50 STAT(PL_VIEW_OFS, this) = autocvar_sv_player_viewoffset;
51 STAT(PL_CROUCH_MIN, this) = autocvar_sv_player_crouch_mins;
52 STAT(PL_CROUCH_MAX, this) = autocvar_sv_player_crouch_maxs;
53 STAT(PL_CROUCH_VIEW_OFS, this) = autocvar_sv_player_crouch_viewoffset;
56 // fix some new settings
57 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
58 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
59 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
60 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
61 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
62 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
63 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
64 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
65 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
66 STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
67 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
68 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
69 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
70 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
71 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
72 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
73 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
74 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
75 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
76 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
77 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
78 STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
79 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
81 MUTATOR_CALLHOOK(PlayerPhysics_PostUpdateStats, this, maxspd_mod);
85 float IsMoveInDirection(vector mv, float ang) // key mix factor
87 if (mv_x == 0 && mv_y == 0)
88 return 0; // avoid division by zero
89 ang -= RAD2DEG * atan2(mv_y, mv_x);
90 ang = remainder(ang, 360) / 45;
91 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
94 float GeomLerp(float a, float _lerp, float b)
96 return a == 0 ? (_lerp < 1 ? 0 : b)
97 : b == 0 ? (_lerp > 0 ? 0 : a)
98 : a * (fabs(b / a) ** _lerp);
101 void PM_ClientMovement_UpdateStatus(entity this)
106 bool have_hook = false;
107 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
110 entity wepent = viewmodels[slot];
112 .entity weaponentity = weaponentities[slot];
113 entity wepent = this.(weaponentity);
115 if(wepent.hook && !wasfreed(wepent.hook))
121 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
124 //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
126 } else if (PHYS_INVEHICLE(this)) {
128 } else if (STAT(FROZEN, this)) {
133 if (!IS_DUCKED(this)) {
135 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
136 setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
137 // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
139 } else if (IS_DUCKED(this)) {
140 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, false, this);
141 if (!trace_startsolid) {
143 this.view_ofs = STAT(PL_VIEW_OFS, this);
144 setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
149 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
150 PHYS_WATERJUMP_TIME(this) = 0;
154 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
156 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
157 if(PHYS_AIRCONTROL_BACKWARDS(this))
158 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
159 if(PHYS_AIRCONTROL_SIDEWARDS(this))
161 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
162 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
165 float k = 32 * (2 * movity - 1);
169 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
171 float zspeed = this.velocity_z;
173 float xyspeed = vlen(this.velocity);
174 this.velocity = normalize(this.velocity);
176 float dot = this.velocity * wishdir;
178 if (dot > 0) // we can't change direction while slowing down
180 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
181 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
182 k *= PHYS_AIRCONTROL(this);
183 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
186 this.velocity = this.velocity * xyspeed;
187 this.velocity_z = zspeed;
190 float AdjustAirAccelQW(float accelqw, float factor)
192 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
195 // example config for alternate speed clamping:
196 // sv_airaccel_qw 0.8
197 // sv_airaccel_sideways_friction 0
198 // prvm_globalset server speedclamp_mode 1
200 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
202 float speedclamp = stretchfactor > 0 ? stretchfactor
203 : accelqw < 0 ? 1 // full clamping, no stretch
206 accelqw = fabs(accelqw);
208 if (GAMEPLAYFIX_Q2AIRACCELERATE)
209 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
211 float vel_straight = this.velocity * wishdir;
212 float vel_z = this.velocity_z;
213 vector vel_xy = vec2(this.velocity);
214 vector vel_perpend = vel_xy - vel_straight * wishdir;
216 float step = accel * dt * wishspeed0;
218 float vel_xy_current = vlen(vel_xy);
220 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
221 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
222 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
223 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
224 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
226 if (sidefric < 0 && (vel_perpend*vel_perpend))
227 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
229 float f = max(0, 1 + dt * wishspeed * sidefric);
230 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
231 // assume: themin > 1
232 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
233 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
234 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
235 // obviously, this cannot be
240 themin = sqrt(themin);
241 vel_perpend *= max(themin, f);
245 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
247 vel_xy = vel_straight * wishdir + vel_perpend;
251 float vel_xy_preclamp;
252 vel_xy_preclamp = vlen(vel_xy);
253 if (vel_xy_preclamp > 0) // prevent division by zero
255 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
256 if (vel_xy_current < vel_xy_preclamp)
257 vel_xy *= (vel_xy_current / vel_xy_preclamp);
261 this.velocity = vel_xy + vel_z * '0 0 1';
264 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
269 vector curvel = this.velocity;
271 float curspeed = vlen(curvel);
273 if (wishspeed > curspeed * 1.01)
274 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
277 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
278 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
280 vector wishvel = wishdir * wishspeed;
281 vector acceldir = wishvel - curvel;
282 float addspeed = vlen(acceldir);
283 acceldir = normalize(acceldir);
285 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
287 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
289 vector curdir = normalize(curvel);
290 float dot = acceldir * curdir;
292 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
295 this.velocity += accelspeed * acceldir;
303 When you press the jump key
304 returns true if handled
307 bool PlayerJump(entity this)
309 if (PHYS_FROZEN(this))
310 return true; // no jumping in freezetag when frozen
313 if (this.player_blocked)
314 return true; // no jumping while blocked
317 bool doublejump = false;
318 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
319 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
321 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
324 mjumpheight = M_ARGV(1, float);
325 doublejump = M_ARGV(2, bool);
327 if (this.waterlevel >= WATERLEVEL_SWIMMING)
337 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
343 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
344 return IS_JUMP_HELD(this);
346 if(PHYS_TRACK_CANJUMP(this))
350 if (IS_JUMP_HELD(this))
353 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
354 // velocity bounds. Final velocity is bound between (jumpheight *
355 // min + jumpheight) and (jumpheight * max + jumpheight);
357 if(PHYS_JUMPSPEEDCAP_MIN != "")
359 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
361 if (this.velocity_z < minjumpspeed)
362 mjumpheight += minjumpspeed - this.velocity_z;
365 if(PHYS_JUMPSPEEDCAP_MAX != "")
367 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
368 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
370 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
372 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
374 if (this.velocity_z > maxjumpspeed)
375 mjumpheight -= this.velocity_z - maxjumpspeed;
379 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
382 if(autocvar_speedmeter)
383 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
385 if(this.lastground < time - 0.3)
387 float f = (1 - PHYS_FRICTION_ONLAND(this));
388 this.velocity_x *= f;
389 this.velocity_y *= f;
392 if(this.jumppadcount > 1)
393 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
394 this.jumppadcount = 0;
398 this.velocity_z += mjumpheight;
400 UNSET_ONGROUND(this);
406 this.oldvelocity_z = this.velocity_z;
408 animdecide_setaction(this, ANIMACTION_JUMP, true);
410 if (autocvar_g_jump_grunt)
411 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
416 void CheckWaterJump(entity this)
418 // check for a jump-out-of-water
419 makevectors(this.v_angle);
420 vector start = this.origin;
423 normalize(v_forward);
424 vector end = start + v_forward*24;
425 traceline (start, end, true, this);
426 if (trace_fraction < 1)
428 start_z = start_z + this.maxs_z - 8;
429 end = start + v_forward*24;
430 this.movedir = trace_plane_normal * -50;
431 traceline(start, end, true, this);
432 if (trace_fraction == 1)
433 { // open at eye level
434 this.velocity_z = 225;
435 this.flags |= FL_WATERJUMP;
438 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
440 PHYS_WATERJUMP_TIME(this) = 2;
448 #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
450 float autocvar_cl_jetpack_jump;
451 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
453 .float jetpack_stopped;
454 void CheckPlayerJump(entity this)
457 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
459 if (JETPACK_JUMP(this) < 2)
460 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
462 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
464 bool playerjump = PlayerJump(this); // required
466 bool air_jump = !playerjump || M_ARGV(2, bool);
467 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
468 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
470 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
471 else if (this.jetpack_stopped) { }
475 if (was_flying) // TODO: ran out of fuel message
476 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
478 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
480 this.jetpack_stopped = true;
481 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
483 else if (activate && !PHYS_FROZEN(this))
484 ITEMS_STAT(this) |= IT_USING_JETPACK;
488 this.jetpack_stopped = false;
489 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
491 if (!PHYS_INPUT_BUTTON_JUMP(this))
492 UNSET_JUMP_HELD(this);
494 if (this.waterlevel == WATERLEVEL_SWIMMING)
495 CheckWaterJump(this);
499 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
500 .float specialcommand_pos;
501 void SpecialCommand(entity this)
503 if(autocvar_sv_cheats || this.maycheat)
505 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
506 LOG_INFO("A hollow voice says \"Plugh\".");
509 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
513 bool PM_check_specialcommand(entity this, int buttons)
519 else if (buttons == 1)
521 else if (buttons == 2)
523 else if (buttons == 128)
525 else if (buttons == 256)
527 else if (buttons == 512)
529 else if (buttons == 1024)
534 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
536 CS(this).specialcommand_pos += 1;
537 if (CS(this).specialcommand_pos >= strlen(specialcommand))
539 CS(this).specialcommand_pos = 0;
540 SpecialCommand(this);
544 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
545 CS(this).specialcommand_pos = 0;
550 void PM_check_nickspam(entity this)
553 if (time >= this.nickspamtime)
555 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
557 // slight annoyance for nick change scripts
558 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
559 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;
561 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!
563 this.v_angle_x = random() * 360;
564 this.v_angle_y = random() * 360;
565 // at least I'm not forcing retardedview by also assigning to angles_z
566 this.fixangle = true;
572 void PM_check_punch(entity this, float dt)
575 if (this.punchangle != '0 0 0')
577 float f = vlen(this.punchangle) - 10 * dt;
579 this.punchangle = normalize(this.punchangle) * f;
581 this.punchangle = '0 0 0';
584 if (this.punchvector != '0 0 0')
586 float f = vlen(this.punchvector) - 30 * dt;
588 this.punchvector = normalize(this.punchvector) * f;
590 this.punchvector = '0 0 0';
595 // predict frozen movement, as frozen players CAN move in some cases
596 void PM_check_frozen(entity this)
598 if (!PHYS_FROZEN(this))
600 if (PHYS_DODGING_FROZEN(this)
602 && IS_REAL_CLIENT(this)
606 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
607 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
608 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
611 PHYS_CS(this).movement = '0 0 0';
613 vector midpoint = ((this.absmin + this.absmax) * 0.5);
614 if (pointcontents(midpoint) == CONTENT_WATER)
616 this.velocity = this.velocity * 0.5;
618 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
619 this.velocity_z = 200;
623 void PM_check_hitground(entity this)
626 if (!this.wasFlying) return;
627 this.wasFlying = false;
628 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
629 if (time < this.ladder_time) return;
630 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
632 .entity weaponentity = weaponentities[slot];
633 if(this.(weaponentity).hook)
636 this.nextstep = time + 0.3 + random() * 0.1;
637 trace_dphitq3surfaceflags = 0;
638 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
639 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
640 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
643 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
644 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
648 void PM_Footsteps(entity this)
651 if (!g_footsteps) return;
652 if (IS_DUCKED(this)) return;
653 if (time >= this.lastground + 0.2) return;
654 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
655 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
657 this.nextstep = time + 0.3 + random() * 0.1;
658 trace_dphitq3surfaceflags = 0;
659 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
660 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
661 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
664 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
669 void PM_check_slick(entity this)
671 if(!IS_ONGROUND(this))
674 if(!PHYS_SLICK_APPLYGRAVITY(this))
677 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
678 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
680 UNSET_ONGROUND(this);
687 void PM_check_blocked(entity this)
690 if (!this.player_blocked)
692 PHYS_CS(this).movement = '0 0 0';
693 this.disableclientprediction = 1;
697 void PM_jetpack(entity this, float maxspd_mod, float dt)
699 //makevectors(this.v_angle.y * '0 1 0');
700 makevectors(this.v_angle);
701 vector wishvel = v_forward * PHYS_CS(this).movement_x
702 + v_right * PHYS_CS(this).movement_y;
703 // add remaining speed as Z component
704 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
706 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
707 // add the unused velocity as up component
710 // if (PHYS_INPUT_BUTTON_JUMP(this))
711 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
713 // it is now normalized, so...
714 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
715 float a_up = PHYS_JETPACK_ACCEL_UP(this);
716 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
718 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
725 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
728 //////////////////////////////////////////////////////////////////////////////////////
729 // finding the maximum over all vectors of above form
730 // with wishvel having an absolute value of 1
731 //////////////////////////////////////////////////////////////////////////////////////
732 // we're finding the maximum over
733 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
734 // for z in the range from -1 to 1
735 //////////////////////////////////////////////////////////////////////////////////////
736 // maximum is EITHER attained at the single extreme point:
737 float a_diff = a_side * a_side - a_up * a_up;
741 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
742 if (f > -1 && f < 1) // can it be attained?
744 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
748 // OR attained at z = 1:
749 f = (a_up + a_add) * (a_up + a_add);
755 // OR attained at z = -1:
756 f = (a_up - a_add) * (a_up - a_add);
763 //////////////////////////////////////////////////////////////////////////////////////
765 //print("best possible acceleration: ", ftos(best), "\n");
768 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
769 if (wishvel_z - PHYS_GRAVITY(this) > 0)
770 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
772 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
775 fvel = vlen(wishvel);
778 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
780 fvel = min(1, vlen(wishvel) / best);
781 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
782 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
786 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
788 if (f > 0 && wishvel != '0 0 0')
790 this.velocity = this.velocity + wishvel * f * dt;
791 UNSET_ONGROUND(this);
794 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
795 this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
797 ITEMS_STAT(this) |= IT_USING_JETPACK;
799 // jetpack also inhibits health regeneration, but only for 1 second
800 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
805 // used for calculating airshots
806 bool IsFlying(entity this)
808 if(IS_ONGROUND(this))
810 if(this.waterlevel >= WATERLEVEL_SWIMMING)
812 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
813 if(trace_fraction < 1)
819 void sys_phys_update(entity this, float dt);
821 void SV_PlayerPhysics(entity this)
823 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
827 // needs to be called before physics are run!
828 if(IS_REAL_CLIENT(this))
829 PM_UpdateButtons(this, CS(this));
832 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
835 CS(this).pm_frametime = frametime;
837 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
838 this.csqcmodel_modelflags |= MF_ROCKET;
840 this.csqcmodel_modelflags &= ~MF_ROCKET;