2 #include "../triggers/include.qh"
3 #include "../viewloc.qh"
7 #include <server/miscfunctions.qh>
8 #include "../triggers/trigger/viewloc.qh"
10 bool Physics_HasWord(string thelist, string theword)
12 FOREACH_WORD(thelist, it == theword,
20 // client side physics
21 bool Physics_Valid(string thecvar)
23 return autocvar_g_physics_clientselect && thecvar != "" && thecvar && thecvar != "default" && Physics_HasWord(autocvar_g_physics_clientselect_options, thecvar);
26 float Physics_ClientOption(entity this, string option, float defaultval)
28 if(IS_REAL_CLIENT(this) && Physics_Valid(CS(this).cvar_cl_physics))
30 string s = strcat("g_physics_", CS(this).cvar_cl_physics, "_", option);
31 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
34 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
36 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
37 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
43 void Physics_UpdateStats(entity this)
45 // 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?)
46 STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
48 MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
49 float maxspd_mod = PHYS_HIGHSPEED(this);
51 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
52 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
53 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
55 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
56 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
58 STAT(PL_MIN, this) = autocvar_sv_player_mins;
59 STAT(PL_MAX, this) = autocvar_sv_player_maxs;
60 STAT(PL_VIEW_OFS, this) = autocvar_sv_player_viewoffset;
61 STAT(PL_CROUCH_MIN, this) = autocvar_sv_player_crouch_mins;
62 STAT(PL_CROUCH_MAX, this) = autocvar_sv_player_crouch_maxs;
63 STAT(PL_CROUCH_VIEW_OFS, this) = autocvar_sv_player_crouch_viewoffset;
66 // fix some new settings
67 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
68 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
69 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
70 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
71 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
72 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
73 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
74 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
75 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
76 STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
77 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
78 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
79 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
80 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
81 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
82 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
83 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
84 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
85 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
86 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
87 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
88 STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
89 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
93 float IsMoveInDirection(vector mv, float ang) // key mix factor
95 if (mv_x == 0 && mv_y == 0)
96 return 0; // avoid division by zero
97 ang -= RAD2DEG * atan2(mv_y, mv_x);
98 ang = remainder(ang, 360) / 45;
99 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
102 float GeomLerp(float a, float _lerp, float b)
104 return a == 0 ? (_lerp < 1 ? 0 : b)
105 : b == 0 ? (_lerp > 0 ? 0 : a)
106 : a * (fabs(b / a) ** _lerp);
109 void PM_ClientMovement_UpdateStatus(entity this)
116 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
117 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
119 entity wep = viewmodels[slot];
120 if(wep.hook && !wasfreed(wep.hook))
123 break; // don't bother checking the others
126 if(this.waterlevel >= WATERLEVEL_SWIMMING)
128 if(hud != HUD_NORMAL)
130 if(STAT(FROZEN, this))
135 // wants to crouch, this always works
136 if (!IS_DUCKED(this)) SET_DUCKED(this);
140 // wants to stand, if currently crouching we need to check for a low ceiling first
143 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
144 if (!trace_startsolid) UNSET_DUCKED(this);
148 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
149 PHYS_WATERJUMP_TIME(this) = 0;
153 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
155 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
156 if(PHYS_AIRCONTROL_BACKWARDS(this))
157 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
158 if(PHYS_AIRCONTROL_SIDEWARDS(this))
160 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
161 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
164 float k = 32 * (2 * movity - 1);
168 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
170 float zspeed = this.velocity_z;
172 float xyspeed = vlen(this.velocity);
173 this.velocity = normalize(this.velocity);
175 float dot = this.velocity * wishdir;
177 if (dot > 0) // we can't change direction while slowing down
179 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
180 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
181 k *= PHYS_AIRCONTROL(this);
182 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
185 this.velocity = this.velocity * xyspeed;
186 this.velocity_z = zspeed;
189 float AdjustAirAccelQW(float accelqw, float factor)
191 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
194 // example config for alternate speed clamping:
195 // sv_airaccel_qw 0.8
196 // sv_airaccel_sideways_friction 0
197 // prvm_globalset server speedclamp_mode 1
199 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
201 float speedclamp = stretchfactor > 0 ? stretchfactor
202 : accelqw < 0 ? 1 // full clamping, no stretch
205 accelqw = fabs(accelqw);
207 if (GAMEPLAYFIX_Q2AIRACCELERATE)
208 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
210 float vel_straight = this.velocity * wishdir;
211 float vel_z = this.velocity_z;
212 vector vel_xy = vec2(this.velocity);
213 vector vel_perpend = vel_xy - vel_straight * wishdir;
215 float step = accel * dt * wishspeed0;
217 float vel_xy_current = vlen(vel_xy);
219 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
220 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
221 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
222 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
223 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
225 if (sidefric < 0 && (vel_perpend*vel_perpend))
226 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
228 float f = max(0, 1 + dt * wishspeed * sidefric);
229 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
230 // assume: themin > 1
231 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
232 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
233 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
234 // obviously, this cannot be
239 themin = sqrt(themin);
240 vel_perpend *= max(themin, f);
244 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
246 vel_xy = vel_straight * wishdir + vel_perpend;
250 float vel_xy_preclamp;
251 vel_xy_preclamp = vlen(vel_xy);
252 if (vel_xy_preclamp > 0) // prevent division by zero
254 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
255 if (vel_xy_current < vel_xy_preclamp)
256 vel_xy *= (vel_xy_current / vel_xy_preclamp);
260 this.velocity = vel_xy + vel_z * '0 0 1';
263 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
268 vector curvel = this.velocity;
270 float curspeed = vlen(curvel);
272 if (wishspeed > curspeed * 1.01)
273 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
276 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
277 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
279 vector wishvel = wishdir * wishspeed;
280 vector acceldir = wishvel - curvel;
281 float addspeed = vlen(acceldir);
282 acceldir = normalize(acceldir);
284 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
286 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
288 vector curdir = normalize(curvel);
289 float dot = acceldir * curdir;
291 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
294 this.velocity += accelspeed * acceldir;
302 When you press the jump key
303 returns true if handled
306 bool PlayerJump(entity this)
308 if (PHYS_FROZEN(this))
309 return true; // no jumping in freezetag when frozen
312 if (this.player_blocked)
313 return true; // no jumping while blocked
316 bool doublejump = false;
317 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
318 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
320 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
323 mjumpheight = M_ARGV(1, float);
324 doublejump = M_ARGV(2, bool);
326 if (this.waterlevel >= WATERLEVEL_SWIMMING)
336 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
342 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
343 return IS_JUMP_HELD(this);
345 if(PHYS_TRACK_CANJUMP(this))
349 if (IS_JUMP_HELD(this))
352 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
353 // velocity bounds. Final velocity is bound between (jumpheight *
354 // min + jumpheight) and (jumpheight * max + jumpheight);
356 if(PHYS_JUMPSPEEDCAP_MIN != "")
358 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
360 if (this.velocity_z < minjumpspeed)
361 mjumpheight += minjumpspeed - this.velocity_z;
364 if(PHYS_JUMPSPEEDCAP_MAX != "")
366 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
367 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
369 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
371 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
373 if (this.velocity_z > maxjumpspeed)
374 mjumpheight -= this.velocity_z - maxjumpspeed;
378 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
381 if(autocvar_speedmeter)
382 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
384 if(this.lastground < time - 0.3)
386 float f = (1 - PHYS_FRICTION_ONLAND(this));
387 this.velocity_x *= f;
388 this.velocity_y *= f;
391 if(this.jumppadcount > 1)
392 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
393 this.jumppadcount = 0;
397 this.velocity_z += mjumpheight;
399 UNSET_ONGROUND(this);
405 this.oldvelocity_z = this.velocity_z;
407 animdecide_setaction(this, ANIMACTION_JUMP, true);
409 if (autocvar_g_jump_grunt)
410 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
415 void CheckWaterJump(entity this)
417 // check for a jump-out-of-water
418 makevectors(this.v_angle);
419 vector start = this.origin;
422 normalize(v_forward);
423 vector end = start + v_forward*24;
424 traceline (start, end, true, this);
425 if (trace_fraction < 1)
427 start_z = start_z + this.maxs_z - 8;
428 end = start + v_forward*24;
429 this.movedir = trace_plane_normal * -50;
430 traceline(start, end, true, this);
431 if (trace_fraction == 1)
432 { // open at eye level
433 this.velocity_z = 225;
434 this.flags |= FL_WATERJUMP;
437 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
439 PHYS_WATERJUMP_TIME(this) = 2;
447 #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
449 float autocvar_cl_jetpack_jump;
450 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
452 .float jetpack_stopped;
453 void CheckPlayerJump(entity this)
456 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
458 if (JETPACK_JUMP(this) < 2)
459 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
461 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
463 bool playerjump = PlayerJump(this); // required
465 bool air_jump = !playerjump || M_ARGV(2, bool);
466 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
467 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
469 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
470 else if (this.jetpack_stopped) { }
474 if (was_flying) // TODO: ran out of fuel message
475 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
477 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
479 this.jetpack_stopped = true;
480 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
482 else if (activate && !PHYS_FROZEN(this))
483 ITEMS_STAT(this) |= IT_USING_JETPACK;
487 this.jetpack_stopped = false;
488 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
490 if (!PHYS_INPUT_BUTTON_JUMP(this))
491 UNSET_JUMP_HELD(this);
493 if (this.waterlevel == WATERLEVEL_SWIMMING)
494 CheckWaterJump(this);
498 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
499 .float specialcommand_pos;
500 void SpecialCommand(entity this)
502 if(autocvar_sv_cheats || this.maycheat)
504 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
505 LOG_INFO("A hollow voice says \"Plugh\".");
508 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
512 bool PM_check_specialcommand(entity this, int buttons)
518 else if (buttons == 1)
520 else if (buttons == 2)
522 else if (buttons == 128)
524 else if (buttons == 256)
526 else if (buttons == 512)
528 else if (buttons == 1024)
533 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
535 CS(this).specialcommand_pos += 1;
536 if (CS(this).specialcommand_pos >= strlen(specialcommand))
538 CS(this).specialcommand_pos = 0;
539 SpecialCommand(this);
543 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
544 CS(this).specialcommand_pos = 0;
549 void PM_check_nickspam(entity this)
552 if (time >= this.nickspamtime)
554 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
556 // slight annoyance for nick change scripts
557 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
558 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;
560 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!
562 this.v_angle_x = random() * 360;
563 this.v_angle_y = random() * 360;
564 // at least I'm not forcing retardedview by also assigning to angles_z
565 this.fixangle = true;
571 void PM_check_punch(entity this, float dt)
574 if (this.punchangle != '0 0 0')
576 float f = vlen(this.punchangle) - 10 * dt;
578 this.punchangle = normalize(this.punchangle) * f;
580 this.punchangle = '0 0 0';
583 if (this.punchvector != '0 0 0')
585 float f = vlen(this.punchvector) - 30 * dt;
587 this.punchvector = normalize(this.punchvector) * f;
589 this.punchvector = '0 0 0';
594 // predict frozen movement, as frozen players CAN move in some cases
595 void PM_check_frozen(entity this)
597 if (!PHYS_FROZEN(this))
599 if (PHYS_DODGING_FROZEN(this)
601 && IS_REAL_CLIENT(this)
605 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
606 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
607 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
610 PHYS_CS(this).movement = '0 0 0';
612 vector midpoint = ((this.absmin + this.absmax) * 0.5);
613 if (pointcontents(midpoint) == CONTENT_WATER)
615 this.velocity = this.velocity * 0.5;
617 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
618 this.velocity_z = 200;
622 void PM_check_hitground(entity this)
625 if (!this.wasFlying) return;
626 this.wasFlying = false;
627 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
628 if (time < this.ladder_time) return;
629 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
631 .entity weaponentity = weaponentities[slot];
632 if(this.(weaponentity).hook)
635 this.nextstep = time + 0.3 + random() * 0.1;
636 trace_dphitq3surfaceflags = 0;
637 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
638 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
639 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
642 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
643 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
647 void PM_Footsteps(entity this)
650 if (!g_footsteps) return;
651 if (IS_DUCKED(this)) return;
652 if (time >= this.lastground + 0.2) return;
653 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
654 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
656 this.nextstep = time + 0.3 + random() * 0.1;
657 trace_dphitq3surfaceflags = 0;
658 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
659 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
660 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
663 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
668 void PM_check_slick(entity this)
670 if(!IS_ONGROUND(this))
673 if(!PHYS_SLICK_APPLYGRAVITY(this))
676 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
677 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
679 UNSET_ONGROUND(this);
686 void PM_check_blocked(entity this)
689 if (!this.player_blocked)
691 PHYS_CS(this).movement = '0 0 0';
692 this.disableclientprediction = 1;
696 void PM_jetpack(entity this, float maxspd_mod, float dt)
698 //makevectors(this.v_angle.y * '0 1 0');
699 makevectors(this.v_angle);
700 vector wishvel = v_forward * PHYS_CS(this).movement_x
701 + v_right * PHYS_CS(this).movement_y;
702 // add remaining speed as Z component
703 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
705 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
706 // add the unused velocity as up component
709 // if (PHYS_INPUT_BUTTON_JUMP(this))
710 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
712 // it is now normalized, so...
713 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
714 float a_up = PHYS_JETPACK_ACCEL_UP(this);
715 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
717 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
724 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
727 //////////////////////////////////////////////////////////////////////////////////////
728 // finding the maximum over all vectors of above form
729 // with wishvel having an absolute value of 1
730 //////////////////////////////////////////////////////////////////////////////////////
731 // we're finding the maximum over
732 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
733 // for z in the range from -1 to 1
734 //////////////////////////////////////////////////////////////////////////////////////
735 // maximum is EITHER attained at the single extreme point:
736 float a_diff = a_side * a_side - a_up * a_up;
740 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
741 if (f > -1 && f < 1) // can it be attained?
743 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
747 // OR attained at z = 1:
748 f = (a_up + a_add) * (a_up + a_add);
754 // OR attained at z = -1:
755 f = (a_up - a_add) * (a_up - a_add);
762 //////////////////////////////////////////////////////////////////////////////////////
764 //print("best possible acceleration: ", ftos(best), "\n");
767 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
768 if (wishvel_z - PHYS_GRAVITY(this) > 0)
769 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
771 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
774 fvel = vlen(wishvel);
777 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
779 fvel = min(1, vlen(wishvel) / best);
780 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
781 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
785 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
787 if (f > 0 && wishvel != '0 0 0')
789 this.velocity = this.velocity + wishvel * f * dt;
790 UNSET_ONGROUND(this);
793 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
794 this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
796 ITEMS_STAT(this) |= IT_USING_JETPACK;
798 // jetpack also inhibits health regeneration, but only for 1 second
799 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
804 // used for calculating airshots
805 bool IsFlying(entity this)
807 if(IS_ONGROUND(this))
809 if(this.waterlevel >= WATERLEVEL_SWIMMING)
811 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
812 if(trace_fraction < 1)
818 void sys_phys_update(entity this, float dt);
820 void SV_PlayerPhysics(entity this)
822 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
826 // needs to be called before physics are run!
827 if(IS_REAL_CLIENT(this))
828 PM_UpdateButtons(this, CS(this));
831 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
834 CS(this).pm_frametime = frametime;
836 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
837 this.csqcmodel_modelflags |= MF_ROCKET;
839 this.csqcmodel_modelflags &= ~MF_ROCKET;