2 #include "../mapobjects/_mod.qh"
3 #include "../viewloc.qh"
7 #include <server/miscfunctions.qh>
8 #include "../mapobjects/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);
122 if(this.viewloc && !(this.viewloc.spawnflags & VIEWLOC_FREEMOVE) && PHYS_CS(this).movement.x < 0)
126 //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
128 } else if (PHYS_INVEHICLE(this)) {
130 } else if (STAT(FROZEN, this)) {
135 if (!IS_DUCKED(this)) {
137 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
138 setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
139 // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
141 } else if (IS_DUCKED(this)) {
142 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, false, this);
143 if (!trace_startsolid) {
145 this.view_ofs = STAT(PL_VIEW_OFS, this);
146 setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
151 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
152 PHYS_WATERJUMP_TIME(this) = 0;
156 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
158 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
159 if(PHYS_AIRCONTROL_BACKWARDS(this))
160 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
161 if(PHYS_AIRCONTROL_SIDEWARDS(this))
163 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
164 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
167 float k = 32 * (2 * movity - 1);
171 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
173 float zspeed = this.velocity_z;
175 float xyspeed = vlen(this.velocity);
176 this.velocity = normalize(this.velocity);
178 float dot = this.velocity * wishdir;
180 if (dot > 0) // we can't change direction while slowing down
182 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
183 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
184 k *= PHYS_AIRCONTROL(this);
185 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
188 this.velocity = this.velocity * xyspeed;
189 this.velocity_z = zspeed;
192 float AdjustAirAccelQW(float accelqw, float factor)
194 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
197 // example config for alternate speed clamping:
198 // sv_airaccel_qw 0.8
199 // sv_airaccel_sideways_friction 0
200 // prvm_globalset server speedclamp_mode 1
202 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
204 float speedclamp = stretchfactor > 0 ? stretchfactor
205 : accelqw < 0 ? 1 // full clamping, no stretch
208 accelqw = fabs(accelqw);
210 if (GAMEPLAYFIX_Q2AIRACCELERATE)
211 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
213 float vel_straight = this.velocity * wishdir;
214 float vel_z = this.velocity_z;
215 vector vel_xy = vec2(this.velocity);
216 vector vel_perpend = vel_xy - vel_straight * wishdir;
218 float step = accel * dt * wishspeed0;
220 float vel_xy_current = vlen(vel_xy);
222 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
223 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
224 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
225 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
226 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
228 if (sidefric < 0 && (vel_perpend*vel_perpend))
229 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
231 float f = max(0, 1 + dt * wishspeed * sidefric);
232 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
233 // assume: themin > 1
234 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
235 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
236 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
237 // obviously, this cannot be
242 themin = sqrt(themin);
243 vel_perpend *= max(themin, f);
247 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
249 vel_xy = vel_straight * wishdir + vel_perpend;
253 float vel_xy_preclamp;
254 vel_xy_preclamp = vlen(vel_xy);
255 if (vel_xy_preclamp > 0) // prevent division by zero
257 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
258 if (vel_xy_current < vel_xy_preclamp)
259 vel_xy *= (vel_xy_current / vel_xy_preclamp);
263 this.velocity = vel_xy + vel_z * '0 0 1';
266 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
271 vector curvel = this.velocity;
273 float curspeed = vlen(curvel);
275 if (wishspeed > curspeed * 1.01)
276 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
279 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
280 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
282 vector wishvel = wishdir * wishspeed;
283 vector acceldir = wishvel - curvel;
284 float addspeed = vlen(acceldir);
285 acceldir = normalize(acceldir);
287 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
289 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
291 vector curdir = normalize(curvel);
292 float dot = acceldir * curdir;
294 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
297 this.velocity += accelspeed * acceldir;
305 When you press the jump key
306 returns true if handled
309 bool PlayerJump(entity this)
311 if (PHYS_FROZEN(this))
312 return true; // no jumping in freezetag when frozen
315 if (this.player_blocked)
316 return true; // no jumping while blocked
319 bool doublejump = false;
320 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
321 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
323 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
326 mjumpheight = M_ARGV(1, float);
327 doublejump = M_ARGV(2, bool);
329 if (this.waterlevel >= WATERLEVEL_SWIMMING)
339 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
345 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
346 return IS_JUMP_HELD(this);
348 if(PHYS_TRACK_CANJUMP(this))
352 if (IS_JUMP_HELD(this))
355 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
356 // velocity bounds. Final velocity is bound between (jumpheight *
357 // min + jumpheight) and (jumpheight * max + jumpheight);
359 if(PHYS_JUMPSPEEDCAP_MIN != "")
361 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
363 if (this.velocity_z < minjumpspeed)
364 mjumpheight += minjumpspeed - this.velocity_z;
367 if(PHYS_JUMPSPEEDCAP_MAX != "")
369 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
370 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
372 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
374 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
376 if (this.velocity_z > maxjumpspeed)
377 mjumpheight -= this.velocity_z - maxjumpspeed;
381 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
384 if(autocvar_speedmeter)
385 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
387 if(this.lastground < time - 0.3)
389 float f = (1 - PHYS_FRICTION_ONLAND(this));
390 this.velocity_x *= f;
391 this.velocity_y *= f;
394 if(this.jumppadcount > 1)
395 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
396 this.jumppadcount = 0;
400 this.velocity_z += mjumpheight;
402 UNSET_ONGROUND(this);
408 this.oldvelocity_z = this.velocity_z;
410 animdecide_setaction(this, ANIMACTION_JUMP, true);
412 if (autocvar_g_jump_grunt)
413 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
418 void CheckWaterJump(entity this)
420 // check for a jump-out-of-water
421 makevectors(this.v_angle);
422 vector start = this.origin;
425 normalize(v_forward);
426 vector end = start + v_forward*24;
427 traceline (start, end, true, this);
428 if (trace_fraction < 1)
430 start_z = start_z + this.maxs_z - 8;
431 end = start + v_forward*24;
432 this.movedir = trace_plane_normal * -50;
433 traceline(start, end, true, this);
434 if (trace_fraction == 1)
435 { // open at eye level
436 this.velocity_z = 225;
437 this.flags |= FL_WATERJUMP;
440 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
442 PHYS_WATERJUMP_TIME(this) = 2;
450 #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
452 float autocvar_cl_jetpack_jump;
453 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
455 .float jetpack_stopped;
456 void CheckPlayerJump(entity this)
459 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
461 if (JETPACK_JUMP(this) < 2)
462 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
464 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
466 bool playerjump = PlayerJump(this); // required
468 bool air_jump = !playerjump || M_ARGV(2, bool);
469 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
470 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
472 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
473 else if (this.jetpack_stopped) { }
477 if (was_flying) // TODO: ran out of fuel message
478 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
480 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
482 this.jetpack_stopped = true;
483 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
485 else if (activate && !PHYS_FROZEN(this))
486 ITEMS_STAT(this) |= IT_USING_JETPACK;
490 this.jetpack_stopped = false;
491 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
493 if (!PHYS_INPUT_BUTTON_JUMP(this))
494 UNSET_JUMP_HELD(this);
496 if (this.waterlevel == WATERLEVEL_SWIMMING)
497 CheckWaterJump(this);
501 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
502 .float specialcommand_pos;
503 void SpecialCommand(entity this)
505 if(autocvar_sv_cheats || this.maycheat)
507 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
508 LOG_INFO("A hollow voice says \"Plugh\".");
511 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
515 bool PM_check_specialcommand(entity this, int buttons)
521 else if (buttons == 1)
523 else if (buttons == 2)
525 else if (buttons == 128)
527 else if (buttons == 256)
529 else if (buttons == 512)
531 else if (buttons == 1024)
536 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
538 CS(this).specialcommand_pos += 1;
539 if (CS(this).specialcommand_pos >= strlen(specialcommand))
541 CS(this).specialcommand_pos = 0;
542 SpecialCommand(this);
546 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
547 CS(this).specialcommand_pos = 0;
552 void PM_check_nickspam(entity this)
555 if (time >= this.nickspamtime)
557 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
559 // slight annoyance for nick change scripts
560 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
561 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;
563 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!
565 this.v_angle_x = random() * 360;
566 this.v_angle_y = random() * 360;
567 // at least I'm not forcing retardedview by also assigning to angles_z
568 this.fixangle = true;
574 void PM_check_punch(entity this, float dt)
577 if (this.punchangle != '0 0 0')
579 float f = vlen(this.punchangle) - 10 * dt;
581 this.punchangle = normalize(this.punchangle) * f;
583 this.punchangle = '0 0 0';
586 if (this.punchvector != '0 0 0')
588 float f = vlen(this.punchvector) - 30 * dt;
590 this.punchvector = normalize(this.punchvector) * f;
592 this.punchvector = '0 0 0';
597 // predict frozen movement, as frozen players CAN move in some cases
598 void PM_check_frozen(entity this)
600 if (!PHYS_FROZEN(this))
602 if (PHYS_DODGING_FROZEN(this)
604 && IS_REAL_CLIENT(this)
608 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
609 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
610 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
613 PHYS_CS(this).movement = '0 0 0';
615 vector midpoint = ((this.absmin + this.absmax) * 0.5);
616 if (pointcontents(midpoint) == CONTENT_WATER)
618 this.velocity = this.velocity * 0.5;
620 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
621 this.velocity_z = 200;
625 void PM_check_hitground(entity this)
628 if (!this.wasFlying) return;
629 this.wasFlying = false;
630 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
631 if (time < this.ladder_time) return;
632 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
634 .entity weaponentity = weaponentities[slot];
635 if(this.(weaponentity).hook)
638 this.nextstep = time + 0.3 + random() * 0.1;
639 trace_dphitq3surfaceflags = 0;
640 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
641 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
642 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
645 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
646 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
650 void PM_Footsteps(entity this)
653 if (!g_footsteps) return;
654 if (IS_DUCKED(this)) return;
655 if (time >= this.lastground + 0.2) return;
656 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
657 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
659 this.nextstep = time + 0.3 + random() * 0.1;
660 trace_dphitq3surfaceflags = 0;
661 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
662 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
663 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
666 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
671 void PM_check_slick(entity this)
673 if(!IS_ONGROUND(this))
676 if(!PHYS_SLICK_APPLYGRAVITY(this))
679 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
680 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
682 UNSET_ONGROUND(this);
689 void PM_check_blocked(entity this)
692 if (!this.player_blocked)
694 PHYS_CS(this).movement = '0 0 0';
695 this.disableclientprediction = 1;
699 void PM_jetpack(entity this, float maxspd_mod, float dt)
701 //makevectors(this.v_angle.y * '0 1 0');
702 makevectors(this.v_angle);
703 vector wishvel = v_forward * PHYS_CS(this).movement_x
704 + v_right * PHYS_CS(this).movement_y;
705 // add remaining speed as Z component
706 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
708 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
709 // add the unused velocity as up component
712 // if (PHYS_INPUT_BUTTON_JUMP(this))
713 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
715 // it is now normalized, so...
716 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
717 float a_up = PHYS_JETPACK_ACCEL_UP(this);
718 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
720 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
727 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
730 //////////////////////////////////////////////////////////////////////////////////////
731 // finding the maximum over all vectors of above form
732 // with wishvel having an absolute value of 1
733 //////////////////////////////////////////////////////////////////////////////////////
734 // we're finding the maximum over
735 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
736 // for z in the range from -1 to 1
737 //////////////////////////////////////////////////////////////////////////////////////
738 // maximum is EITHER attained at the single extreme point:
739 float a_diff = a_side * a_side - a_up * a_up;
743 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
744 if (f > -1 && f < 1) // can it be attained?
746 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
750 // OR attained at z = 1:
751 f = (a_up + a_add) * (a_up + a_add);
757 // OR attained at z = -1:
758 f = (a_up - a_add) * (a_up - a_add);
765 //////////////////////////////////////////////////////////////////////////////////////
767 //print("best possible acceleration: ", ftos(best), "\n");
770 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
771 if (wishvel_z - PHYS_GRAVITY(this) > 0)
772 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
774 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
777 fvel = vlen(wishvel);
780 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
782 fvel = min(1, vlen(wishvel) / best);
783 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
784 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
788 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
790 if (f > 0 && wishvel != '0 0 0')
792 this.velocity = this.velocity + wishvel * f * dt;
793 UNSET_ONGROUND(this);
796 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
797 TakeResource(this, RESOURCE_FUEL, PHYS_JETPACK_FUEL(this) * dt * fvel * f);
799 ITEMS_STAT(this) |= IT_USING_JETPACK;
801 // jetpack also inhibits health regeneration, but only for 1 second
802 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
807 // used for calculating airshots
808 bool IsFlying(entity this)
810 if(IS_ONGROUND(this))
812 if(this.waterlevel >= WATERLEVEL_SWIMMING)
814 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 24', MOVE_NORMAL, this);
815 //traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
816 if(trace_fraction < 1)
822 void sys_phys_update(entity this, float dt);
824 void SV_PlayerPhysics(entity this)
826 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
830 // needs to be called before physics are run!
831 if(IS_REAL_CLIENT(this))
832 PM_UpdateButtons(this, CS(this));
835 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
838 CS(this).pm_frametime = frametime;
840 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
841 this.csqcmodel_modelflags |= MF_ROCKET;
843 this.csqcmodel_modelflags &= ~MF_ROCKET;