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)
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);
83 float IsMoveInDirection(vector mv, float ang) // key mix factor
85 if (mv_x == 0 && mv_y == 0)
86 return 0; // avoid division by zero
87 ang -= RAD2DEG * atan2(mv_y, mv_x);
88 ang = remainder(ang, 360) / 45;
89 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
92 float GeomLerp(float a, float _lerp, float b)
94 return a == 0 ? (_lerp < 1 ? 0 : b)
95 : b == 0 ? (_lerp > 0 ? 0 : a)
96 : a * (fabs(b / a) ** _lerp);
99 void PM_ClientMovement_UpdateStatus(entity this)
106 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
107 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
109 entity wep = viewmodels[slot];
110 if(wep.hook && !wasfreed(wep.hook))
113 break; // don't bother checking the others
116 if(this.waterlevel >= WATERLEVEL_SWIMMING)
118 if(hud != HUD_NORMAL)
120 if(STAT(FROZEN, this))
125 // wants to crouch, this always works
126 if (!IS_DUCKED(this)) SET_DUCKED(this);
130 // wants to stand, if currently crouching we need to check for a low ceiling first
133 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
134 if (!trace_startsolid) UNSET_DUCKED(this);
138 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
139 PHYS_WATERJUMP_TIME(this) = 0;
143 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
145 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
146 if(PHYS_AIRCONTROL_BACKWARDS(this))
147 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
148 if(PHYS_AIRCONTROL_SIDEWARDS(this))
150 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
151 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
154 float k = 32 * (2 * movity - 1);
158 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
160 float zspeed = this.velocity_z;
162 float xyspeed = vlen(this.velocity);
163 this.velocity = normalize(this.velocity);
165 float dot = this.velocity * wishdir;
167 if (dot > 0) // we can't change direction while slowing down
169 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
170 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
171 k *= PHYS_AIRCONTROL(this);
172 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
175 this.velocity = this.velocity * xyspeed;
176 this.velocity_z = zspeed;
179 float AdjustAirAccelQW(float accelqw, float factor)
181 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
184 // example config for alternate speed clamping:
185 // sv_airaccel_qw 0.8
186 // sv_airaccel_sideways_friction 0
187 // prvm_globalset server speedclamp_mode 1
189 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
191 float speedclamp = stretchfactor > 0 ? stretchfactor
192 : accelqw < 0 ? 1 // full clamping, no stretch
195 accelqw = fabs(accelqw);
197 if (GAMEPLAYFIX_Q2AIRACCELERATE)
198 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
200 float vel_straight = this.velocity * wishdir;
201 float vel_z = this.velocity_z;
202 vector vel_xy = vec2(this.velocity);
203 vector vel_perpend = vel_xy - vel_straight * wishdir;
205 float step = accel * dt * wishspeed0;
207 float vel_xy_current = vlen(vel_xy);
209 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
210 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
211 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
212 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
213 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
215 if (sidefric < 0 && (vel_perpend*vel_perpend))
216 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
218 float f = max(0, 1 + dt * wishspeed * sidefric);
219 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
220 // assume: themin > 1
221 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
222 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
223 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
224 // obviously, this cannot be
229 themin = sqrt(themin);
230 vel_perpend *= max(themin, f);
234 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
236 vel_xy = vel_straight * wishdir + vel_perpend;
240 float vel_xy_preclamp;
241 vel_xy_preclamp = vlen(vel_xy);
242 if (vel_xy_preclamp > 0) // prevent division by zero
244 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
245 if (vel_xy_current < vel_xy_preclamp)
246 vel_xy *= (vel_xy_current / vel_xy_preclamp);
250 this.velocity = vel_xy + vel_z * '0 0 1';
253 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
258 vector curvel = this.velocity;
260 float curspeed = vlen(curvel);
262 if (wishspeed > curspeed * 1.01)
263 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
266 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
267 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
269 vector wishvel = wishdir * wishspeed;
270 vector acceldir = wishvel - curvel;
271 float addspeed = vlen(acceldir);
272 acceldir = normalize(acceldir);
274 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
276 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
278 vector curdir = normalize(curvel);
279 float dot = acceldir * curdir;
281 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
284 this.velocity += accelspeed * acceldir;
292 When you press the jump key
293 returns true if handled
296 bool PlayerJump(entity this)
298 if (PHYS_FROZEN(this))
299 return true; // no jumping in freezetag when frozen
302 if (this.player_blocked)
303 return true; // no jumping while blocked
306 bool doublejump = false;
307 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
308 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
310 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
313 mjumpheight = M_ARGV(1, float);
314 doublejump = M_ARGV(2, bool);
316 if (this.waterlevel >= WATERLEVEL_SWIMMING)
326 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
332 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
333 return IS_JUMP_HELD(this);
335 if(PHYS_TRACK_CANJUMP(this))
339 if (IS_JUMP_HELD(this))
342 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
343 // velocity bounds. Final velocity is bound between (jumpheight *
344 // min + jumpheight) and (jumpheight * max + jumpheight);
346 if(PHYS_JUMPSPEEDCAP_MIN != "")
348 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
350 if (this.velocity_z < minjumpspeed)
351 mjumpheight += minjumpspeed - this.velocity_z;
354 if(PHYS_JUMPSPEEDCAP_MAX != "")
356 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
357 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
359 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
361 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
363 if (this.velocity_z > maxjumpspeed)
364 mjumpheight -= this.velocity_z - maxjumpspeed;
368 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
371 if(autocvar_speedmeter)
372 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
374 if(this.lastground < time - 0.3)
376 float f = (1 - PHYS_FRICTION_ONLAND(this));
377 this.velocity_x *= f;
378 this.velocity_y *= f;
381 if(this.jumppadcount > 1)
382 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
383 this.jumppadcount = 0;
387 this.velocity_z += mjumpheight;
389 UNSET_ONGROUND(this);
395 this.oldvelocity_z = this.velocity_z;
397 animdecide_setaction(this, ANIMACTION_JUMP, true);
399 if (autocvar_g_jump_grunt)
400 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
405 void CheckWaterJump(entity this)
407 // check for a jump-out-of-water
408 makevectors(this.v_angle);
409 vector start = this.origin;
412 normalize(v_forward);
413 vector end = start + v_forward*24;
414 traceline (start, end, true, this);
415 if (trace_fraction < 1)
417 start_z = start_z + this.maxs_z - 8;
418 end = start + v_forward*24;
419 this.movedir = trace_plane_normal * -50;
420 traceline(start, end, true, this);
421 if (trace_fraction == 1)
422 { // open at eye level
423 this.velocity_z = 225;
424 this.flags |= FL_WATERJUMP;
427 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
429 PHYS_WATERJUMP_TIME(this) = 2;
437 #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
439 float autocvar_cl_jetpack_jump;
440 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
442 .float jetpack_stopped;
443 void CheckPlayerJump(entity this)
446 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
448 if (JETPACK_JUMP(this) < 2)
449 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
451 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
453 bool playerjump = PlayerJump(this); // required
455 bool air_jump = !playerjump || M_ARGV(2, bool);
456 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
457 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
459 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
460 else if (this.jetpack_stopped) { }
464 if (was_flying) // TODO: ran out of fuel message
465 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
467 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
469 this.jetpack_stopped = true;
470 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
472 else if (activate && !PHYS_FROZEN(this))
473 ITEMS_STAT(this) |= IT_USING_JETPACK;
477 this.jetpack_stopped = false;
478 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
480 if (!PHYS_INPUT_BUTTON_JUMP(this))
481 UNSET_JUMP_HELD(this);
483 if (this.waterlevel == WATERLEVEL_SWIMMING)
484 CheckWaterJump(this);
488 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
489 .float specialcommand_pos;
490 void SpecialCommand(entity this)
492 if(autocvar_sv_cheats || this.maycheat)
494 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
495 LOG_INFO("A hollow voice says \"Plugh\".");
498 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
502 bool PM_check_specialcommand(entity this, int buttons)
508 else if (buttons == 1)
510 else if (buttons == 2)
512 else if (buttons == 128)
514 else if (buttons == 256)
516 else if (buttons == 512)
518 else if (buttons == 1024)
523 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
525 CS(this).specialcommand_pos += 1;
526 if (CS(this).specialcommand_pos >= strlen(specialcommand))
528 CS(this).specialcommand_pos = 0;
529 SpecialCommand(this);
533 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
534 CS(this).specialcommand_pos = 0;
539 void PM_check_nickspam(entity this)
542 if (time >= this.nickspamtime)
544 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
546 // slight annoyance for nick change scripts
547 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
548 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;
550 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!
552 this.v_angle_x = random() * 360;
553 this.v_angle_y = random() * 360;
554 // at least I'm not forcing retardedview by also assigning to angles_z
555 this.fixangle = true;
561 void PM_check_punch(entity this, float dt)
564 if (this.punchangle != '0 0 0')
566 float f = vlen(this.punchangle) - 10 * dt;
568 this.punchangle = normalize(this.punchangle) * f;
570 this.punchangle = '0 0 0';
573 if (this.punchvector != '0 0 0')
575 float f = vlen(this.punchvector) - 30 * dt;
577 this.punchvector = normalize(this.punchvector) * f;
579 this.punchvector = '0 0 0';
584 // predict frozen movement, as frozen players CAN move in some cases
585 void PM_check_frozen(entity this)
587 if (!PHYS_FROZEN(this))
589 if (PHYS_DODGING_FROZEN(this)
591 && IS_REAL_CLIENT(this)
595 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
596 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
597 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
600 PHYS_CS(this).movement = '0 0 0';
602 vector midpoint = ((this.absmin + this.absmax) * 0.5);
603 if (pointcontents(midpoint) == CONTENT_WATER)
605 this.velocity = this.velocity * 0.5;
607 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
608 this.velocity_z = 200;
612 void PM_check_hitground(entity this)
615 if (!this.wasFlying) return;
616 this.wasFlying = false;
617 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
618 if (time < this.ladder_time) return;
619 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
621 .entity weaponentity = weaponentities[slot];
622 if(this.(weaponentity).hook)
625 this.nextstep = time + 0.3 + random() * 0.1;
626 trace_dphitq3surfaceflags = 0;
627 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
628 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
629 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
632 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
633 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
637 void PM_Footsteps(entity this)
640 if (!g_footsteps) return;
641 if (IS_DUCKED(this)) return;
642 if (time >= this.lastground + 0.2) return;
643 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
644 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
646 this.nextstep = time + 0.3 + random() * 0.1;
647 trace_dphitq3surfaceflags = 0;
648 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
649 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
650 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
653 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
658 void PM_check_slick(entity this)
660 if(!IS_ONGROUND(this))
663 if(!PHYS_SLICK_APPLYGRAVITY(this))
666 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
667 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
669 UNSET_ONGROUND(this);
676 void PM_check_blocked(entity this)
679 if (!this.player_blocked)
681 PHYS_CS(this).movement = '0 0 0';
682 this.disableclientprediction = 1;
686 void PM_jetpack(entity this, float maxspd_mod, float dt)
688 //makevectors(this.v_angle.y * '0 1 0');
689 makevectors(this.v_angle);
690 vector wishvel = v_forward * PHYS_CS(this).movement_x
691 + v_right * PHYS_CS(this).movement_y;
692 // add remaining speed as Z component
693 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
695 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
696 // add the unused velocity as up component
699 // if (PHYS_INPUT_BUTTON_JUMP(this))
700 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
702 // it is now normalized, so...
703 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
704 float a_up = PHYS_JETPACK_ACCEL_UP(this);
705 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
707 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
714 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
717 //////////////////////////////////////////////////////////////////////////////////////
718 // finding the maximum over all vectors of above form
719 // with wishvel having an absolute value of 1
720 //////////////////////////////////////////////////////////////////////////////////////
721 // we're finding the maximum over
722 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
723 // for z in the range from -1 to 1
724 //////////////////////////////////////////////////////////////////////////////////////
725 // maximum is EITHER attained at the single extreme point:
726 float a_diff = a_side * a_side - a_up * a_up;
730 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
731 if (f > -1 && f < 1) // can it be attained?
733 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
737 // OR attained at z = 1:
738 f = (a_up + a_add) * (a_up + a_add);
744 // OR attained at z = -1:
745 f = (a_up - a_add) * (a_up - a_add);
752 //////////////////////////////////////////////////////////////////////////////////////
754 //print("best possible acceleration: ", ftos(best), "\n");
757 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
758 if (wishvel_z - PHYS_GRAVITY(this) > 0)
759 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
761 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
764 fvel = vlen(wishvel);
767 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
769 fvel = min(1, vlen(wishvel) / best);
770 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
771 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
775 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
777 if (f > 0 && wishvel != '0 0 0')
779 this.velocity = this.velocity + wishvel * f * dt;
780 UNSET_ONGROUND(this);
783 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
784 this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
786 ITEMS_STAT(this) |= IT_USING_JETPACK;
788 // jetpack also inhibits health regeneration, but only for 1 second
789 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
794 // used for calculating airshots
795 bool IsFlying(entity this)
797 if(IS_ONGROUND(this))
799 if(this.waterlevel >= WATERLEVEL_SWIMMING)
801 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
802 if(trace_fraction < 1)
808 void sys_phys_update(entity this, float dt);
810 void SV_PlayerPhysics(entity this)
812 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
816 // needs to be called before physics are run!
817 if(IS_REAL_CLIENT(this))
818 PM_UpdateButtons(this, CS(this));
821 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
824 CS(this).pm_frametime = frametime;
826 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
827 this.csqcmodel_modelflags |= MF_ROCKET;
829 this.csqcmodel_modelflags &= ~MF_ROCKET;