3 #include <common/mapobjects/_mod.qh>
4 #include <common/viewloc.qh>
7 REPLICATE(cvar_cl_physics, string, "cl_physics");
8 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
9 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
13 #include <common/mapobjects/defs.qh>
14 #include <common/mapobjects/trigger/viewloc.qh>
15 #include <server/client.qh>
17 // client side physics
18 bool Physics_Valid(string thecvar)
20 return thecvar != "" && thecvar && thecvar != "default" && strhasword(autocvar_g_physics_clientselect_options, thecvar);
23 float Physics_ClientOption(entity this, string option, float defaultval)
25 if(!autocvar_g_physics_clientselect)
28 if(IS_REAL_CLIENT(this) && Physics_Valid(CS_CVAR(this).cvar_cl_physics))
30 string s = strcat("g_physics_", CS_CVAR(this).cvar_cl_physics, "_", option);
31 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
34 if(autocvar_g_physics_clientselect_default && autocvar_g_physics_clientselect_default != "" && autocvar_g_physics_clientselect_default != "default")
36 // NOTE: not using Physics_Valid here, so the default can be forced to something normally unavailable
37 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
38 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
44 void Physics_UpdateStats(entity this)
46 // 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?)
47 STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
49 MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
50 float maxspd_mod = PHYS_HIGHSPEED(this) * ((this.swampslug.active == ACTIVE_ACTIVE) ? this.swampslug.swamp_slowdown : 1);
51 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
52 if (autocvar_g_movement_highspeed_q3_compat)
54 STAT(MOVEVARS_AIRACCEL_QW, this) = Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw);
55 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw);
56 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw);
60 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
61 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
62 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
64 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
67 bool q3hb = q3compat && autocvar_sv_q3compat_changehitbox;
68 STAT(PL_MIN, this) = q3hb ? '-15 -15 -24' : autocvar_sv_player_mins;
69 STAT(PL_MAX, this) = q3hb ? '15 15 32' : autocvar_sv_player_maxs;
70 STAT(PL_VIEW_OFS, this) = q3hb ? '0 0 26' : autocvar_sv_player_viewoffset;
71 STAT(PL_CROUCH_MIN, this) = q3hb ? '-15 -15 -24' : autocvar_sv_player_crouch_mins;
72 STAT(PL_CROUCH_MAX, this) = q3hb ? '15 15 16' : autocvar_sv_player_crouch_maxs;
73 STAT(PL_CROUCH_VIEW_OFS, this) = q3hb ? '0 0 12' : autocvar_sv_player_crouch_viewoffset;
76 // fix some new settings
77 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
78 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
79 if (autocvar_g_movement_highspeed_q3_compat)
80 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed) * maxspd_mod;
82 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
84 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
85 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
86 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
87 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
88 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
89 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
90 STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
91 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
92 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
93 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
94 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
95 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
96 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
97 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
98 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
99 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
100 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
101 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
102 STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
103 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
105 MUTATOR_CALLHOOK(PlayerPhysics_PostUpdateStats, this, maxspd_mod);
109 float IsMoveInDirection(vector mv, float ang) // key mix factor
111 if (mv_x == 0 && mv_y == 0)
112 return 0; // avoid division by zero
113 ang -= RAD2DEG * atan2(mv_y, mv_x);
114 ang = remainder(ang, 360) / 45;
115 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
118 float GeomLerp(float a, float _lerp, float b)
120 return a == 0 ? (_lerp < 1 ? 0 : b)
121 : b == 0 ? (_lerp > 0 ? 0 : a)
122 : a * (fabs(b / a) ** _lerp);
125 void PM_ClientMovement_UpdateStatus(entity this)
130 bool have_hook = false;
131 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
134 entity wepent = viewmodels[slot];
136 .entity weaponentity = weaponentities[slot];
137 entity wepent = this.(weaponentity);
139 if(wepent.hook && !wasfreed(wepent.hook))
145 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
146 if(this.viewloc && !(this.viewloc.spawnflags & VIEWLOC_FREEMOVE) && PHYS_CS(this).movement.x < 0)
150 //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
152 } else if (PHYS_INVEHICLE(this)) {
154 } else if (STAT(FROZEN, this) || IS_DEAD(this)) {
158 MUTATOR_CALLHOOK(PlayerCanCrouch, this, do_crouch);
159 do_crouch = M_ARGV(1, bool);
162 if (!IS_DUCKED(this)) {
164 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
165 setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
166 // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
168 } else if (IS_DUCKED(this)) {
169 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
170 if (!trace_startsolid) {
172 this.view_ofs = STAT(PL_VIEW_OFS, this);
173 setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
177 _Movetype_CheckWater(this); // needs to be run on the client, might as well use the latest on the server too!
180 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
182 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
183 if(PHYS_AIRCONTROL_BACKWARDS(this))
184 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
185 if(PHYS_AIRCONTROL_SIDEWARDS(this))
187 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
188 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
191 float k = 32 * (2 * movity - 1);
195 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
197 float zspeed = this.velocity_z;
199 float xyspeed = vlen(this.velocity);
200 this.velocity = normalize(this.velocity);
202 float dot = this.velocity * wishdir;
204 if (dot > 0) // we can't change direction while slowing down
206 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
207 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
208 k *= PHYS_AIRCONTROL(this);
209 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
212 this.velocity = this.velocity * xyspeed;
213 this.velocity_z = zspeed;
216 float AdjustAirAccelQW(float accelqw, float factor)
218 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
221 // example config for alternate speed clamping:
222 // sv_airaccel_qw 0.8
223 // sv_airaccel_sideways_friction 0
224 // prvm_globalset server speedclamp_mode 1
226 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
228 float speedclamp = stretchfactor > 0 ? stretchfactor
229 : accelqw < 0 ? 1 // full clamping, no stretch
232 accelqw = fabs(accelqw);
234 if (GAMEPLAYFIX_Q2AIRACCELERATE)
235 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
237 float vel_straight = this.velocity * wishdir;
238 float vel_z = this.velocity_z;
239 vector vel_xy = vec2(this.velocity);
240 vector vel_perpend = vel_xy - vel_straight * wishdir;
242 float step = accel * dt * wishspeed0;
244 float vel_xy_current = vlen(vel_xy);
246 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
247 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
248 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
249 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
250 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
252 if (sidefric < 0 && (vel_perpend*vel_perpend))
253 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
255 float f = max(0, 1 + dt * wishspeed * sidefric);
256 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
257 // assume: themin > 1
258 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
259 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
260 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
261 // obviously, this cannot be
266 themin = sqrt(themin);
267 vel_perpend *= max(themin, f);
271 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
273 vel_xy = vel_straight * wishdir + vel_perpend;
277 float vel_xy_preclamp;
278 vel_xy_preclamp = vlen(vel_xy);
279 if (vel_xy_preclamp > 0) // prevent division by zero
281 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
282 if (vel_xy_current < vel_xy_preclamp)
283 vel_xy *= (vel_xy_current / vel_xy_preclamp);
287 this.velocity = vel_xy + vel_z * '0 0 1';
290 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
295 vector curvel = this.velocity;
297 float curspeed = vlen(curvel);
299 if (wishspeed > curspeed * 1.01)
300 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
303 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
304 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
306 vector wishvel = wishdir * wishspeed;
307 vector acceldir = wishvel - curvel;
308 float addspeed = vlen(acceldir);
309 acceldir = normalize(acceldir);
311 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
313 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
315 vector curdir = normalize(curvel);
316 float dot = acceldir * curdir;
318 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
321 this.velocity += accelspeed * acceldir;
329 When you press the jump key
330 returns true if handled
333 bool PlayerJump(entity this)
335 if (PHYS_FROZEN(this))
336 return true; // no jumping in freezetag when frozen
338 if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
339 return true; // no jumping while typing
342 if (this.player_blocked)
343 return true; // no jumping while blocked
346 bool doublejump = false;
347 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
348 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
350 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
353 mjumpheight = M_ARGV(1, float);
354 doublejump = M_ARGV(2, bool);
356 if (this.waterlevel >= WATERLEVEL_SWIMMING)
366 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
372 if (!IS_ONGROUND(this))
373 return IS_JUMP_HELD(this);
375 if(PHYS_TRACK_CANJUMP(this))
379 if (IS_JUMP_HELD(this))
382 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
383 // velocity bounds. Final velocity is bound between (jumpheight *
384 // min + jumpheight) and (jumpheight * max + jumpheight);
386 if(PHYS_JUMPSPEEDCAP_MIN != "")
388 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
390 if (this.velocity_z < minjumpspeed)
391 mjumpheight += minjumpspeed - this.velocity_z;
394 if(PHYS_JUMPSPEEDCAP_MAX != "")
396 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
397 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
399 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
401 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
403 if (this.velocity_z > maxjumpspeed)
404 mjumpheight -= this.velocity_z - maxjumpspeed;
408 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
411 if(autocvar_speedmeter)
412 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
414 if(this.lastground < time - 0.3)
416 float f = (1 - PHYS_FRICTION_ONLAND(this));
417 this.velocity_x *= f;
418 this.velocity_y *= f;
421 if(this.jumppadcount > 1)
422 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
423 this.jumppadcount = 0;
427 this.velocity_z += mjumpheight;
429 UNSET_ONGROUND(this);
434 animdecide_setaction(this, ANIMACTION_JUMP, true);
436 if (autocvar_g_jump_grunt)
437 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
442 void CheckWaterJump(entity this)
444 // check for a jump-out-of-water
445 makevectors(this.v_angle);
446 vector start = this.origin;
449 normalize(v_forward);
450 vector end = start + v_forward*24;
451 traceline (start, end, true, this);
452 if (trace_fraction < 1)
454 start_z = start_z + this.maxs_z - 8;
455 end = start + v_forward*24;
456 this.movedir = trace_plane_normal * -50;
457 traceline(start, end, true, this);
458 if (trace_fraction == 1)
459 { // open at eye level
460 this.velocity_z = 225;
461 this.flags |= FL_WATERJUMP;
462 this.teleport_time = time + 2; // safety net
470 #define JETPACK_JUMP(s) CS_CVAR(s).cvar_cl_jetpack_jump
472 float autocvar_cl_jetpack_jump;
473 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
475 .float jetpack_stopped;
476 void CheckPlayerJump(entity this)
479 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
481 if (JETPACK_JUMP(this) < 2)
482 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
484 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
486 bool playerjump = PlayerJump(this); // required
488 bool air_jump = !playerjump || M_ARGV(2, bool);
489 bool activate = (JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this)) || PHYS_INPUT_BUTTON_JETPACK(this);
490 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_AMMO);
492 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
493 else if (this.jetpack_stopped) { }
497 if (was_flying) // TODO: ran out of fuel message
498 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
500 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
502 this.jetpack_stopped = true;
503 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
505 else if (activate && !PHYS_FROZEN(this))
506 ITEMS_STAT(this) |= IT_USING_JETPACK;
510 this.jetpack_stopped = false;
511 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
513 if (!PHYS_INPUT_BUTTON_JUMP(this))
514 UNSET_JUMP_HELD(this);
516 if (this.waterlevel == WATERLEVEL_SWIMMING)
517 CheckWaterJump(this);
521 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
522 .float specialcommand_pos;
523 void SpecialCommand(entity this)
525 if(autocvar_sv_cheats || this.maycheat)
527 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
528 LOG_INFO("A hollow voice says \"Plugh\".");
533 bool PM_check_specialcommand(entity this, int buttons)
539 // buttons mapped in PHYS_INPUT_BUTTON_MASK
540 case 0: c = "x"; break;
541 case BIT(0): c = "1"; break;
542 case BIT(2): c = " "; break;
543 case BIT(7): c = "s"; break;
544 case BIT(8): c = "w"; break;
545 case BIT(9): c = "a"; break;
546 case BIT(10): c = "d"; break;
550 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
552 CS(this).specialcommand_pos += 1;
553 if (CS(this).specialcommand_pos >= strlen(specialcommand))
555 CS(this).specialcommand_pos = 0;
556 SpecialCommand(this);
560 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
561 CS(this).specialcommand_pos = 0;
566 void PM_check_nickspam(entity this)
569 if (time >= this.nickspamtime)
571 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
573 // slight annoyance for nick change scripts
574 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
575 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;
577 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!
579 this.v_angle_x = random() * 360;
580 this.v_angle_y = random() * 360;
581 // at least I'm not forcing retardedview by also assigning to angles_z
582 this.fixangle = true;
588 void PM_check_punch(entity this, float dt)
591 if (this.punchangle != '0 0 0')
593 float f = vlen(this.punchangle) - 10 * dt;
595 this.punchangle = normalize(this.punchangle) * f;
597 this.punchangle = '0 0 0';
600 if (this.punchvector != '0 0 0')
602 float f = vlen(this.punchvector) - 30 * dt;
604 this.punchvector = normalize(this.punchvector) * f;
606 this.punchvector = '0 0 0';
611 // predict frozen movement, as frozen players CAN move in some cases
612 void PM_check_frozen(entity this)
614 if (!PHYS_FROZEN(this))
616 if (PHYS_DODGING_FROZEN(this) && IS_CLIENT(this))
618 // bind movement to a very slow speed so dodging can use .movement for directional calculations
619 PHYS_CS(this).movement_x = bound(-2, PHYS_CS(this).movement.x, 2);
620 PHYS_CS(this).movement_y = bound(-2, PHYS_CS(this).movement.y, 2);
621 PHYS_CS(this).movement_z = bound(-2, PHYS_CS(this).movement.z, 2);
624 PHYS_CS(this).movement = '0 0 0';
627 void PM_check_hitground(entity this)
630 if (!this.wasFlying) return;
631 this.wasFlying = false;
632 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
633 if (this.ladder_entity) return;
634 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
636 .entity weaponentity = weaponentities[slot];
637 if(this.(weaponentity).hook)
640 this.nextstep = time + 0.3 + random() * 0.1;
641 trace_dphitq3surfaceflags = 0;
642 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
643 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
644 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
647 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
648 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
652 void PM_Footsteps(entity this)
655 if (!autocvar_g_footsteps) return;
656 if (IS_DUCKED(this)) return;
657 if (time >= this.lastground + 0.2) return;
658 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
659 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
661 this.nextstep = time + 0.3 + random() * 0.1;
662 trace_dphitq3surfaceflags = 0;
663 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
664 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
665 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
668 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
673 void PM_check_slick(entity this)
675 if(!IS_ONGROUND(this))
678 trace_dphitq3surfaceflags = 0;
679 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
680 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
686 void PM_check_blocked(entity this)
688 if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
689 PHYS_CS(this).movement = '0 0 0';
691 if (!this.player_blocked)
693 PHYS_CS(this).movement = '0 0 0';
694 this.disableclientprediction = 1;
698 void PM_jetpack(entity this, float maxspd_mod, float dt)
700 //makevectors(this.v_angle.y * '0 1 0');
701 makevectors(this.v_angle);
702 vector wishvel = v_forward * PHYS_CS(this).movement_x
703 + v_right * PHYS_CS(this).movement_y;
704 // add remaining speed as Z component
705 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
707 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
708 // add the unused velocity as up component
711 // if (PHYS_INPUT_BUTTON_JUMP(this))
712 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
714 // it is now normalized, so...
715 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
716 float a_up = PHYS_JETPACK_ACCEL_UP(this);
717 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
719 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
726 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
729 //////////////////////////////////////////////////////////////////////////////////////
730 // finding the maximum over all vectors of above form
731 // with wishvel having an absolute value of 1
732 //////////////////////////////////////////////////////////////////////////////////////
733 // we're finding the maximum over
734 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
735 // for z in the range from -1 to 1
736 //////////////////////////////////////////////////////////////////////////////////////
737 // maximum is EITHER attained at the single extreme point:
738 float a_diff = a_side * a_side - a_up * a_up;
742 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
743 if (f > -1 && f < 1) // can it be attained?
745 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
749 // OR attained at z = 1:
750 f = (a_up + a_add) * (a_up + a_add);
756 // OR attained at z = -1:
757 f = (a_up - a_add) * (a_up - a_add);
764 //////////////////////////////////////////////////////////////////////////////////////
766 //print("best possible acceleration: ", ftos(best), "\n");
769 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
770 if (wishvel_z - PHYS_GRAVITY(this) > 0)
771 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
773 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
776 fvel = vlen(wishvel);
779 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
781 fvel = min(1, vlen(wishvel) / best);
782 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
783 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
787 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
789 if (f > 0 && wishvel != '0 0 0')
791 this.velocity = this.velocity + wishvel * f * dt;
792 UNSET_ONGROUND(this);
795 if (!(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
796 TakeResource(this, RES_FUEL, PHYS_JETPACK_FUEL(this) * dt * fvel * f);
798 ITEMS_STAT(this) |= IT_USING_JETPACK;
800 // jetpack also inhibits health regeneration, but only for 1 second
801 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
806 // used for calculating airshots
807 bool IsFlying(entity this)
809 if(IS_ONGROUND(this))
811 if(this.waterlevel >= WATERLEVEL_SWIMMING)
813 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 24', MOVE_NORMAL, this);
814 //traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
815 if(trace_fraction < 1)
821 void sys_phys_update(entity this, float dt);
823 void SV_PlayerPhysics(entity this)
825 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
829 // needs to be called before physics are run!
830 if(IS_REAL_CLIENT(this))
831 PM_UpdateButtons(this, CS(this));
834 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
837 CS(this).pm_frametime = frametime;
839 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
840 this.csqcmodel_modelflags |= MF_ROCKET;
842 this.csqcmodel_modelflags &= ~MF_ROCKET;