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 && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 float Physics_ClientOption(entity this, string option, float defaultval)
18 if(Physics_Valid(this.cvar_cl_physics))
20 string s = sprintf("g_physics_%s_%s", this.cvar_cl_physics, option);
21 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
24 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
26 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
27 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
33 void Physics_UpdateStats(entity this, float maxspd_mod)
35 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
36 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
37 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
39 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
40 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
42 STAT(PL_MIN, this) = autocvar_sv_player_mins;
43 STAT(PL_MAX, this) = autocvar_sv_player_maxs;
44 STAT(PL_VIEW_OFS, this) = autocvar_sv_player_viewoffset;
45 STAT(PL_CROUCH_MIN, this) = autocvar_sv_player_crouch_mins;
46 STAT(PL_CROUCH_MAX, this) = autocvar_sv_player_crouch_maxs;
47 STAT(PL_CROUCH_VIEW_OFS, this) = autocvar_sv_player_crouch_viewoffset;
50 // fix some new settings
51 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
52 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
53 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
54 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
55 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
56 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
57 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
58 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
59 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
60 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
61 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
62 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
63 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
64 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
65 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
66 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
67 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
68 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
69 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
70 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
71 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
75 float IsMoveInDirection(vector mv, float ang) // key mix factor
77 if (mv_x == 0 && mv_y == 0)
78 return 0; // avoid division by zero
79 ang -= RAD2DEG * atan2(mv_y, mv_x);
80 ang = remainder(ang, 360) / 45;
81 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
84 float GeomLerp(float a, float _lerp, float b)
86 return a == 0 ? (_lerp < 1 ? 0 : b)
87 : b == 0 ? (_lerp > 0 ? 0 : a)
88 : a * pow(fabs(b / a), _lerp);
91 void PM_ClientMovement_UpdateStatus(entity this)
98 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
99 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
101 entity wep = viewmodels[slot];
102 if(wep.hook && !wasfreed(wep.hook))
105 break; // don't bother checking the others
108 if(this.waterlevel >= WATERLEVEL_SWIMMING)
110 if(hud != HUD_NORMAL)
112 if(STAT(FROZEN, this))
117 // wants to crouch, this always works
118 if (!IS_DUCKED(this)) SET_DUCKED(this);
122 // wants to stand, if currently crouching we need to check for a low ceiling first
125 tracebox(this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), this.origin, MOVE_NORMAL, this);
126 if (!trace_startsolid) UNSET_DUCKED(this);
130 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
131 PHYS_WATERJUMP_TIME(this) = 0;
135 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
137 float movity = IsMoveInDirection(this.movement, 0);
138 if(PHYS_AIRCONTROL_BACKWARDS(this))
139 movity += IsMoveInDirection(this.movement, 180);
141 float k = 32 * (2 * movity - 1);
145 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
147 float zspeed = this.velocity_z;
149 float xyspeed = vlen(this.velocity);
150 this.velocity = normalize(this.velocity);
152 float dot = this.velocity * wishdir;
154 if (dot > 0) // we can't change direction while slowing down
156 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * dt;
157 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
158 k *= PHYS_AIRCONTROL(this);
159 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
162 this.velocity = this.velocity * xyspeed;
163 this.velocity_z = zspeed;
166 float AdjustAirAccelQW(float accelqw, float factor)
168 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
171 // example config for alternate speed clamping:
172 // sv_airaccel_qw 0.8
173 // sv_airaccel_sideways_friction 0
174 // prvm_globalset server speedclamp_mode 1
176 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
178 float speedclamp = stretchfactor > 0 ? stretchfactor
179 : accelqw < 0 ? 1 // full clamping, no stretch
182 accelqw = fabs(accelqw);
184 if (GAMEPLAYFIX_Q2AIRACCELERATE)
185 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
187 float vel_straight = this.velocity * wishdir;
188 float vel_z = this.velocity_z;
189 vector vel_xy = vec2(this.velocity);
190 vector vel_perpend = vel_xy - vel_straight * wishdir;
192 float step = accel * dt * wishspeed0;
194 float vel_xy_current = vlen(vel_xy);
196 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
197 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
198 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
199 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
200 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
202 if (sidefric < 0 && (vel_perpend*vel_perpend))
203 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
205 float f = max(0, 1 + dt * wishspeed * sidefric);
206 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
207 // assume: themin > 1
208 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
209 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
210 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
211 // obviously, this cannot be
216 themin = sqrt(themin);
217 vel_perpend *= max(themin, f);
221 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
223 vel_xy = vel_straight * wishdir + vel_perpend;
227 float vel_xy_preclamp;
228 vel_xy_preclamp = vlen(vel_xy);
229 if (vel_xy_preclamp > 0) // prevent division by zero
231 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
232 if (vel_xy_current < vel_xy_preclamp)
233 vel_xy *= (vel_xy_current / vel_xy_preclamp);
237 this.velocity = vel_xy + vel_z * '0 0 1';
240 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
245 vector curvel = this.velocity;
247 float curspeed = vlen(curvel);
249 if (wishspeed > curspeed * 1.01)
250 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
253 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
254 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
256 vector wishvel = wishdir * wishspeed;
257 vector acceldir = wishvel - curvel;
258 float addspeed = vlen(acceldir);
259 acceldir = normalize(acceldir);
261 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
263 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
265 vector curdir = normalize(curvel);
266 float dot = acceldir * curdir;
268 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
271 this.velocity += accelspeed * acceldir;
279 When you press the jump key
280 returns true if handled
283 bool PlayerJump(entity this)
285 if (PHYS_FROZEN(this))
286 return true; // no jumping in freezetag when frozen
289 if (this.player_blocked)
290 return true; // no jumping while blocked
293 bool doublejump = false;
294 float mjumpheight = PHYS_JUMPVELOCITY(this);
296 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
299 mjumpheight = M_ARGV(1, float);
300 doublejump = M_ARGV(2, bool);
302 if (this.waterlevel >= WATERLEVEL_SWIMMING)
311 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
317 if (!IS_ONGROUND(this))
318 return IS_JUMP_HELD(this);
320 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
321 if(PHYS_TRACK_CANJUMP(this))
325 if (IS_JUMP_HELD(this))
328 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
329 // velocity bounds. Final velocity is bound between (jumpheight *
330 // min + jumpheight) and (jumpheight * max + jumpheight);
332 if(PHYS_JUMPSPEEDCAP_MIN != "")
334 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
336 if (this.velocity_z < minjumpspeed)
337 mjumpheight += minjumpspeed - this.velocity_z;
340 if(PHYS_JUMPSPEEDCAP_MAX != "")
342 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
343 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
345 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
347 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
349 if (this.velocity_z > maxjumpspeed)
350 mjumpheight -= this.velocity_z - maxjumpspeed;
354 if (!WAS_ONGROUND(this))
357 if(autocvar_speedmeter)
358 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
360 if(this.lastground < time - 0.3)
362 float f = (1 - PHYS_FRICTION_ONLAND(this));
363 this.velocity_x *= f;
364 this.velocity_y *= f;
367 if(this.jumppadcount > 1)
368 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
369 this.jumppadcount = 0;
373 this.velocity_z += mjumpheight;
375 UNSET_ONGROUND(this);
380 this.oldvelocity_z = this.velocity_z;
382 animdecide_setaction(this, ANIMACTION_JUMP, true);
384 if (autocvar_g_jump_grunt)
385 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
390 void CheckWaterJump(entity this)
392 // check for a jump-out-of-water
393 makevectors(this.v_angle);
394 vector start = this.origin;
397 normalize(v_forward);
398 vector end = start + v_forward*24;
399 traceline (start, end, true, this);
400 if (trace_fraction < 1)
402 start_z = start_z + this.maxs_z - 8;
403 end = start + v_forward*24;
404 this.movedir = trace_plane_normal * -50;
405 traceline(start, end, true, this);
406 if (trace_fraction == 1)
407 { // open at eye level
408 this.velocity_z = 225;
409 this.flags |= FL_WATERJUMP;
412 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
414 PHYS_WATERJUMP_TIME(this) = 2;
422 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
424 float autocvar_cl_jetpack_jump;
425 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
427 .float jetpack_stopped;
428 void CheckPlayerJump(entity this)
431 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
433 if (JETPACK_JUMP(this) < 2)
434 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
436 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
438 bool playerjump = PlayerJump(this); // required
440 bool air_jump = !playerjump || M_ARGV(2, bool);
441 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
442 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
444 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
445 else if (this.jetpack_stopped) { }
449 if (was_flying) // TODO: ran out of fuel message
450 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
452 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
454 this.jetpack_stopped = true;
455 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
457 else if (activate && !PHYS_FROZEN(this))
458 ITEMS_STAT(this) |= IT_USING_JETPACK;
462 this.jetpack_stopped = false;
463 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
465 if (!PHYS_INPUT_BUTTON_JUMP(this))
466 UNSET_JUMP_HELD(this);
468 if (this.waterlevel == WATERLEVEL_SWIMMING)
469 CheckWaterJump(this);
472 float racecar_angle(float forward, float down)
480 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
482 float angle_mult = forward / (800 + forward);
485 return ret * angle_mult + 360 * (1 - angle_mult);
487 return ret * angle_mult;
490 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
491 .float specialcommand_pos;
492 void SpecialCommand(entity this)
495 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
496 LOG_INFO("A hollow voice says \"Plugh\".\n");
500 bool PM_check_specialcommand(entity this, int buttons)
506 else if (buttons == 1)
508 else if (buttons == 2)
510 else if (buttons == 128)
512 else if (buttons == 256)
514 else if (buttons == 512)
516 else if (buttons == 1024)
521 if (c == substring(specialcommand, this.specialcommand_pos, 1))
523 this.specialcommand_pos += 1;
524 if (this.specialcommand_pos >= strlen(specialcommand))
526 this.specialcommand_pos = 0;
527 SpecialCommand(this);
531 else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
532 this.specialcommand_pos = 0;
537 void PM_check_nickspam(entity this)
540 if (time >= this.nickspamtime)
542 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
544 // slight annoyance for nick change scripts
545 this.movement = -1 * this.movement;
546 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;
548 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!
550 this.v_angle_x = random() * 360;
551 this.v_angle_y = random() * 360;
552 // at least I'm not forcing retardedview by also assigning to angles_z
553 this.fixangle = true;
559 void PM_check_punch(entity this, float dt)
562 if (this.punchangle != '0 0 0')
564 float f = vlen(this.punchangle) - 10 * dt;
566 this.punchangle = normalize(this.punchangle) * f;
568 this.punchangle = '0 0 0';
571 if (this.punchvector != '0 0 0')
573 float f = vlen(this.punchvector) - 30 * dt;
575 this.punchvector = normalize(this.punchvector) * f;
577 this.punchvector = '0 0 0';
582 // predict frozen movement, as frozen players CAN move in some cases
583 void PM_check_frozen(entity this)
585 if (!PHYS_FROZEN(this))
587 if (PHYS_DODGING_FROZEN(this)
589 && IS_REAL_CLIENT(this)
593 this.movement_x = bound(-5, this.movement.x, 5);
594 this.movement_y = bound(-5, this.movement.y, 5);
595 this.movement_z = bound(-5, this.movement.z, 5);
598 this.movement = '0 0 0';
600 vector midpoint = ((this.absmin + this.absmax) * 0.5);
601 if (pointcontents(midpoint) == CONTENT_WATER)
603 this.velocity = this.velocity * 0.5;
605 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
606 this.velocity_z = 200;
610 void PM_check_hitground(entity this)
613 if (!this.wasFlying) return;
614 this.wasFlying = false;
615 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
616 if (time < this.ladder_time) return;
617 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
619 .entity weaponentity = weaponentities[slot];
620 if(this.(weaponentity).hook)
623 this.nextstep = time + 0.3 + random() * 0.1;
624 trace_dphitq3surfaceflags = 0;
625 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
626 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
627 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
630 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
631 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
635 void PM_Footsteps(entity this)
638 if (!g_footsteps) return;
639 if (IS_DUCKED(this)) return;
640 if (time >= this.lastground + 0.2) return;
641 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
642 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
644 this.nextstep = time + 0.3 + random() * 0.1;
645 trace_dphitq3surfaceflags = 0;
646 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
647 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
648 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
651 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
656 void PM_check_blocked(entity this)
659 if (!this.player_blocked)
661 this.movement = '0 0 0';
662 this.disableclientprediction = 1;
668 void PM_jetpack(entity this, float maxspd_mod, float dt)
670 //makevectors(this.v_angle.y * '0 1 0');
671 makevectors(this.v_angle);
672 vector wishvel = v_forward * this.movement_x
673 + v_right * this.movement_y;
674 // add remaining speed as Z component
675 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
677 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
678 // add the unused velocity as up component
681 // if (PHYS_INPUT_BUTTON_JUMP(this))
682 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
684 // it is now normalized, so...
685 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
686 float a_up = PHYS_JETPACK_ACCEL_UP(this);
687 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
689 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
696 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
699 //////////////////////////////////////////////////////////////////////////////////////
700 // finding the maximum over all vectors of above form
701 // with wishvel having an absolute value of 1
702 //////////////////////////////////////////////////////////////////////////////////////
703 // we're finding the maximum over
704 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
705 // for z in the range from -1 to 1
706 //////////////////////////////////////////////////////////////////////////////////////
707 // maximum is EITHER attained at the single extreme point:
708 float a_diff = a_side * a_side - a_up * a_up;
712 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
713 if (f > -1 && f < 1) // can it be attained?
715 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
719 // OR attained at z = 1:
720 f = (a_up + a_add) * (a_up + a_add);
726 // OR attained at z = -1:
727 f = (a_up - a_add) * (a_up - a_add);
734 //////////////////////////////////////////////////////////////////////////////////////
736 //print("best possible acceleration: ", ftos(best), "\n");
739 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
740 if (wishvel_z - PHYS_GRAVITY(this) > 0)
741 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
743 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
746 fvel = vlen(wishvel);
749 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
751 fvel = min(1, vlen(wishvel) / best);
752 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
753 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
757 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
759 if (f > 0 && wishvel != '0 0 0')
761 this.velocity = this.velocity + wishvel * f * dt;
762 UNSET_ONGROUND(this);
765 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
766 this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
768 ITEMS_STAT(this) |= IT_USING_JETPACK;
770 // jetpack also inhibits health regeneration, but only for 1 second
771 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
776 // used for calculating airshots
777 bool IsFlying(entity this)
779 if(IS_ONGROUND(this))
781 if(this.waterlevel >= WATERLEVEL_SWIMMING)
783 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
784 if(trace_fraction < 1)
790 void sys_phys_update(entity this, float dt);
792 void SV_PlayerPhysics(entity this)
794 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
797 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
800 this.pm_frametime = frametime;