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(this.cvar_cl_physics))
20 string s = strcat("g_physics_", 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 = 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_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
82 float IsMoveInDirection(vector mv, float ang) // key mix factor
84 if (mv_x == 0 && mv_y == 0)
85 return 0; // avoid division by zero
86 ang -= RAD2DEG * atan2(mv_y, mv_x);
87 ang = remainder(ang, 360) / 45;
88 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
91 float GeomLerp(float a, float _lerp, float b)
93 return a == 0 ? (_lerp < 1 ? 0 : b)
94 : b == 0 ? (_lerp > 0 ? 0 : a)
95 : a * (fabs(b / a) ** _lerp);
98 void PM_ClientMovement_UpdateStatus(entity this)
105 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
106 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
108 entity wep = viewmodels[slot];
109 if(wep.hook && !wasfreed(wep.hook))
112 break; // don't bother checking the others
115 if(this.waterlevel >= WATERLEVEL_SWIMMING)
117 if(hud != HUD_NORMAL)
119 if(STAT(FROZEN, this))
124 // wants to crouch, this always works
125 if (!IS_DUCKED(this)) SET_DUCKED(this);
129 // wants to stand, if currently crouching we need to check for a low ceiling first
132 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
133 if (!trace_startsolid) UNSET_DUCKED(this);
137 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
138 PHYS_WATERJUMP_TIME(this) = 0;
142 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
144 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
145 if(PHYS_AIRCONTROL_BACKWARDS(this))
146 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
147 if(PHYS_AIRCONTROL_SIDEWARDS(this))
149 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
150 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
153 float k = 32 * (2 * movity - 1);
157 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
159 float zspeed = this.velocity_z;
161 float xyspeed = vlen(this.velocity);
162 this.velocity = normalize(this.velocity);
164 float dot = this.velocity * wishdir;
166 if (dot > 0) // we can't change direction while slowing down
168 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
169 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
170 k *= PHYS_AIRCONTROL(this);
171 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
174 this.velocity = this.velocity * xyspeed;
175 this.velocity_z = zspeed;
178 float AdjustAirAccelQW(float accelqw, float factor)
180 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
183 // example config for alternate speed clamping:
184 // sv_airaccel_qw 0.8
185 // sv_airaccel_sideways_friction 0
186 // prvm_globalset server speedclamp_mode 1
188 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
190 float speedclamp = stretchfactor > 0 ? stretchfactor
191 : accelqw < 0 ? 1 // full clamping, no stretch
194 accelqw = fabs(accelqw);
196 if (GAMEPLAYFIX_Q2AIRACCELERATE)
197 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
199 float vel_straight = this.velocity * wishdir;
200 float vel_z = this.velocity_z;
201 vector vel_xy = vec2(this.velocity);
202 vector vel_perpend = vel_xy - vel_straight * wishdir;
204 float step = accel * dt * wishspeed0;
206 float vel_xy_current = vlen(vel_xy);
208 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
209 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
210 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
211 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
212 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
214 if (sidefric < 0 && (vel_perpend*vel_perpend))
215 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
217 float f = max(0, 1 + dt * wishspeed * sidefric);
218 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
219 // assume: themin > 1
220 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
221 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
222 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
223 // obviously, this cannot be
228 themin = sqrt(themin);
229 vel_perpend *= max(themin, f);
233 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
235 vel_xy = vel_straight * wishdir + vel_perpend;
239 float vel_xy_preclamp;
240 vel_xy_preclamp = vlen(vel_xy);
241 if (vel_xy_preclamp > 0) // prevent division by zero
243 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
244 if (vel_xy_current < vel_xy_preclamp)
245 vel_xy *= (vel_xy_current / vel_xy_preclamp);
249 this.velocity = vel_xy + vel_z * '0 0 1';
252 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
257 vector curvel = this.velocity;
259 float curspeed = vlen(curvel);
261 if (wishspeed > curspeed * 1.01)
262 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
265 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
266 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
268 vector wishvel = wishdir * wishspeed;
269 vector acceldir = wishvel - curvel;
270 float addspeed = vlen(acceldir);
271 acceldir = normalize(acceldir);
273 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
275 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
277 vector curdir = normalize(curvel);
278 float dot = acceldir * curdir;
280 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
283 this.velocity += accelspeed * acceldir;
291 When you press the jump key
292 returns true if handled
295 bool PlayerJump(entity this)
297 if (PHYS_FROZEN(this))
298 return true; // no jumping in freezetag when frozen
301 if (this.player_blocked)
302 return true; // no jumping while blocked
305 bool doublejump = false;
306 float mjumpheight = PHYS_JUMPVELOCITY(this);
307 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
309 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
312 mjumpheight = M_ARGV(1, float);
313 doublejump = M_ARGV(2, bool);
315 if (this.waterlevel >= WATERLEVEL_SWIMMING)
325 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
331 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
332 return IS_JUMP_HELD(this);
334 if(PHYS_TRACK_CANJUMP(this))
338 if (IS_JUMP_HELD(this))
341 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
342 // velocity bounds. Final velocity is bound between (jumpheight *
343 // min + jumpheight) and (jumpheight * max + jumpheight);
345 if(PHYS_JUMPSPEEDCAP_MIN != "")
347 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
349 if (this.velocity_z < minjumpspeed)
350 mjumpheight += minjumpspeed - this.velocity_z;
353 if(PHYS_JUMPSPEEDCAP_MAX != "")
355 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
356 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
358 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
360 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
362 if (this.velocity_z > maxjumpspeed)
363 mjumpheight -= this.velocity_z - maxjumpspeed;
367 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
370 if(autocvar_speedmeter)
371 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
373 if(this.lastground < time - 0.3)
375 float f = (1 - PHYS_FRICTION_ONLAND(this));
376 this.velocity_x *= f;
377 this.velocity_y *= f;
380 if(this.jumppadcount > 1)
381 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
382 this.jumppadcount = 0;
386 this.velocity_z += mjumpheight;
388 UNSET_ONGROUND(this);
394 this.oldvelocity_z = this.velocity_z;
396 animdecide_setaction(this, ANIMACTION_JUMP, true);
398 if (autocvar_g_jump_grunt)
399 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
404 void CheckWaterJump(entity this)
406 // check for a jump-out-of-water
407 makevectors(this.v_angle);
408 vector start = this.origin;
411 normalize(v_forward);
412 vector end = start + v_forward*24;
413 traceline (start, end, true, this);
414 if (trace_fraction < 1)
416 start_z = start_z + this.maxs_z - 8;
417 end = start + v_forward*24;
418 this.movedir = trace_plane_normal * -50;
419 traceline(start, end, true, this);
420 if (trace_fraction == 1)
421 { // open at eye level
422 this.velocity_z = 225;
423 this.flags |= FL_WATERJUMP;
426 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
428 PHYS_WATERJUMP_TIME(this) = 2;
436 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
438 float autocvar_cl_jetpack_jump;
439 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
441 .float jetpack_stopped;
442 void CheckPlayerJump(entity this)
445 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
447 if (JETPACK_JUMP(this) < 2)
448 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
450 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
452 bool playerjump = PlayerJump(this); // required
454 bool air_jump = !playerjump || M_ARGV(2, bool);
455 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
456 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
458 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
459 else if (this.jetpack_stopped) { }
463 if (was_flying) // TODO: ran out of fuel message
464 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
466 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
468 this.jetpack_stopped = true;
469 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
471 else if (activate && !PHYS_FROZEN(this))
472 ITEMS_STAT(this) |= IT_USING_JETPACK;
476 this.jetpack_stopped = false;
477 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
479 if (!PHYS_INPUT_BUTTON_JUMP(this))
480 UNSET_JUMP_HELD(this);
482 if (this.waterlevel == WATERLEVEL_SWIMMING)
483 CheckWaterJump(this);
487 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
488 .float specialcommand_pos;
489 void SpecialCommand(entity this)
491 if(autocvar_sv_cheats || this.maycheat)
493 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
494 LOG_INFO("A hollow voice says \"Plugh\".\n");
497 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
501 bool PM_check_specialcommand(entity this, int buttons)
507 else if (buttons == 1)
509 else if (buttons == 2)
511 else if (buttons == 128)
513 else if (buttons == 256)
515 else if (buttons == 512)
517 else if (buttons == 1024)
522 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
524 CS(this).specialcommand_pos += 1;
525 if (CS(this).specialcommand_pos >= strlen(specialcommand))
527 CS(this).specialcommand_pos = 0;
528 SpecialCommand(this);
532 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
533 CS(this).specialcommand_pos = 0;
538 void PM_check_nickspam(entity this)
541 if (time >= this.nickspamtime)
543 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
545 // slight annoyance for nick change scripts
546 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
547 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;
549 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!
551 this.v_angle_x = random() * 360;
552 this.v_angle_y = random() * 360;
553 // at least I'm not forcing retardedview by also assigning to angles_z
554 this.fixangle = true;
560 void PM_check_punch(entity this, float dt)
563 if (this.punchangle != '0 0 0')
565 float f = vlen(this.punchangle) - 10 * dt;
567 this.punchangle = normalize(this.punchangle) * f;
569 this.punchangle = '0 0 0';
572 if (this.punchvector != '0 0 0')
574 float f = vlen(this.punchvector) - 30 * dt;
576 this.punchvector = normalize(this.punchvector) * f;
578 this.punchvector = '0 0 0';
583 // predict frozen movement, as frozen players CAN move in some cases
584 void PM_check_frozen(entity this)
586 if (!PHYS_FROZEN(this))
588 if (PHYS_DODGING_FROZEN(this)
590 && IS_REAL_CLIENT(this)
594 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
595 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
596 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
599 PHYS_CS(this).movement = '0 0 0';
601 vector midpoint = ((this.absmin + this.absmax) * 0.5);
602 if (pointcontents(midpoint) == CONTENT_WATER)
604 this.velocity = this.velocity * 0.5;
606 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
607 this.velocity_z = 200;
611 void PM_check_hitground(entity this)
614 if (!this.wasFlying) return;
615 this.wasFlying = false;
616 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
617 if (time < this.ladder_time) return;
618 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
620 .entity weaponentity = weaponentities[slot];
621 if(this.(weaponentity).hook)
624 this.nextstep = time + 0.3 + random() * 0.1;
625 trace_dphitq3surfaceflags = 0;
626 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
627 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
628 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
631 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
632 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
636 void PM_Footsteps(entity this)
639 if (!g_footsteps) return;
640 if (IS_DUCKED(this)) return;
641 if (time >= this.lastground + 0.2) return;
642 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
643 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
645 this.nextstep = time + 0.3 + random() * 0.1;
646 trace_dphitq3surfaceflags = 0;
647 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
648 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
649 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
652 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
657 void PM_check_slick(entity this)
659 if(!IS_ONGROUND(this))
662 if(!PHYS_SLICK_APPLYGRAVITY(this))
665 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
666 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
668 UNSET_ONGROUND(this);
675 void PM_check_blocked(entity this)
678 if (!this.player_blocked)
680 PHYS_CS(this).movement = '0 0 0';
681 this.disableclientprediction = 1;
685 void PM_jetpack(entity this, float maxspd_mod, float dt)
687 //makevectors(this.v_angle.y * '0 1 0');
688 makevectors(this.v_angle);
689 vector wishvel = v_forward * PHYS_CS(this).movement_x
690 + v_right * PHYS_CS(this).movement_y;
691 // add remaining speed as Z component
692 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
694 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
695 // add the unused velocity as up component
698 // if (PHYS_INPUT_BUTTON_JUMP(this))
699 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
701 // it is now normalized, so...
702 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
703 float a_up = PHYS_JETPACK_ACCEL_UP(this);
704 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
706 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
713 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
716 //////////////////////////////////////////////////////////////////////////////////////
717 // finding the maximum over all vectors of above form
718 // with wishvel having an absolute value of 1
719 //////////////////////////////////////////////////////////////////////////////////////
720 // we're finding the maximum over
721 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
722 // for z in the range from -1 to 1
723 //////////////////////////////////////////////////////////////////////////////////////
724 // maximum is EITHER attained at the single extreme point:
725 float a_diff = a_side * a_side - a_up * a_up;
729 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
730 if (f > -1 && f < 1) // can it be attained?
732 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
736 // OR attained at z = 1:
737 f = (a_up + a_add) * (a_up + a_add);
743 // OR attained at z = -1:
744 f = (a_up - a_add) * (a_up - a_add);
751 //////////////////////////////////////////////////////////////////////////////////////
753 //print("best possible acceleration: ", ftos(best), "\n");
756 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
757 if (wishvel_z - PHYS_GRAVITY(this) > 0)
758 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
760 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
763 fvel = vlen(wishvel);
766 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
768 fvel = min(1, vlen(wishvel) / best);
769 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
770 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
774 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
776 if (f > 0 && wishvel != '0 0 0')
778 this.velocity = this.velocity + wishvel * f * dt;
779 UNSET_ONGROUND(this);
782 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
783 this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
785 ITEMS_STAT(this) |= IT_USING_JETPACK;
787 // jetpack also inhibits health regeneration, but only for 1 second
788 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
793 // used for calculating airshots
794 bool IsFlying(entity this)
796 if(IS_ONGROUND(this))
798 if(this.waterlevel >= WATERLEVEL_SWIMMING)
800 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
801 if(trace_fraction < 1)
807 void sys_phys_update(entity this, float dt);
809 void SV_PlayerPhysics(entity this)
811 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
815 // needs to be called before physics are run!
816 if(IS_REAL_CLIENT(this))
817 PM_UpdateButtons(this, CS(this));
820 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
823 CS(this).pm_frametime = frametime;
825 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
826 this.csqcmodel_modelflags |= MF_ROCKET;
828 this.csqcmodel_modelflags &= ~MF_ROCKET;