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 thecvar != "" && thecvar && thecvar != "default" && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 float Physics_ClientOption(entity this, string option, float defaultval)
18 if(!autocvar_g_physics_clientselect)
21 if(IS_REAL_CLIENT(this) && Physics_Valid(CS(this).cvar_cl_physics))
23 string s = strcat("g_physics_", CS(this).cvar_cl_physics, "_", option);
24 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
27 if(autocvar_g_physics_clientselect_default && autocvar_g_physics_clientselect_default != "" && autocvar_g_physics_clientselect_default != "default")
29 // NOTE: not using Physics_Valid here, so the default can be forced to something normally unavailable
30 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
31 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
37 void Physics_UpdateStats(entity this)
39 // 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?)
40 STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
42 MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
43 float maxspd_mod = PHYS_HIGHSPEED(this);
45 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
46 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
47 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
49 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
50 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
52 bool vq3compat = autocvar_sv_vq3compat && autocvar_sv_vq3compat_changehitbox; // NOTE: these hitboxes are off by 1 due to engine differences
53 STAT(PL_MIN, this) = (vq3compat) ? '-15 -15 -24' : autocvar_sv_player_mins;
54 STAT(PL_MAX, this) = (vq3compat) ? '15 15 32' : autocvar_sv_player_maxs;
55 STAT(PL_VIEW_OFS, this) = (vq3compat) ? '0 0 26' : autocvar_sv_player_viewoffset;
56 STAT(PL_CROUCH_MIN, this) = (vq3compat) ? '-15 -15 -24' : autocvar_sv_player_crouch_mins;
57 STAT(PL_CROUCH_MAX, this) = (vq3compat) ? '15 15 16' : autocvar_sv_player_crouch_maxs;
58 STAT(PL_CROUCH_VIEW_OFS, this) = (vq3compat) ? '0 0 12' : autocvar_sv_player_crouch_viewoffset;
61 // fix some new settings
62 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
63 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
64 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
65 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
66 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
67 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
68 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
69 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
70 STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
71 STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
72 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
73 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
74 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
75 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
76 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
77 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
78 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
79 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
80 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
81 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
82 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
83 STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
84 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
86 MUTATOR_CALLHOOK(PlayerPhysics_PostUpdateStats, this, maxspd_mod);
90 float IsMoveInDirection(vector mv, float ang) // key mix factor
92 if (mv_x == 0 && mv_y == 0)
93 return 0; // avoid division by zero
94 ang -= RAD2DEG * atan2(mv_y, mv_x);
95 ang = remainder(ang, 360) / 45;
96 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
99 float GeomLerp(float a, float _lerp, float b)
101 return a == 0 ? (_lerp < 1 ? 0 : b)
102 : b == 0 ? (_lerp > 0 ? 0 : a)
103 : a * (fabs(b / a) ** _lerp);
106 void PM_ClientMovement_UpdateStatus(entity this)
111 bool have_hook = false;
112 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
115 entity wepent = viewmodels[slot];
117 .entity weaponentity = weaponentities[slot];
118 entity wepent = this.(weaponentity);
120 if(wepent.hook && !wasfreed(wepent.hook))
126 bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
127 if(this.viewloc && !(this.viewloc.spawnflags & VIEWLOC_FREEMOVE) && PHYS_CS(this).movement.x < 0)
131 //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
133 } else if (PHYS_INVEHICLE(this)) {
135 } else if (STAT(FROZEN, this) || IS_DEAD(this)) {
140 if (!IS_DUCKED(this)) {
142 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
143 setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
144 // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
146 } else if (IS_DUCKED(this)) {
147 tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, false, this);
148 if (!trace_startsolid) {
150 this.view_ofs = STAT(PL_VIEW_OFS, this);
151 setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
156 if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
157 PHYS_WATERJUMP_TIME(this) = 0;
161 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
163 float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
164 if(PHYS_AIRCONTROL_BACKWARDS(this))
165 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
166 if(PHYS_AIRCONTROL_SIDEWARDS(this))
168 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
169 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
172 float k = 32 * (2 * movity - 1);
176 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
178 float zspeed = this.velocity_z;
180 float xyspeed = vlen(this.velocity);
181 this.velocity = normalize(this.velocity);
183 float dot = this.velocity * wishdir;
185 if (dot > 0) // we can't change direction while slowing down
187 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
188 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
189 k *= PHYS_AIRCONTROL(this);
190 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
193 this.velocity = this.velocity * xyspeed;
194 this.velocity_z = zspeed;
197 float AdjustAirAccelQW(float accelqw, float factor)
199 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
202 // example config for alternate speed clamping:
203 // sv_airaccel_qw 0.8
204 // sv_airaccel_sideways_friction 0
205 // prvm_globalset server speedclamp_mode 1
207 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
209 float speedclamp = stretchfactor > 0 ? stretchfactor
210 : accelqw < 0 ? 1 // full clamping, no stretch
213 accelqw = fabs(accelqw);
215 if (GAMEPLAYFIX_Q2AIRACCELERATE)
216 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
218 float vel_straight = this.velocity * wishdir;
219 float vel_z = this.velocity_z;
220 vector vel_xy = vec2(this.velocity);
221 vector vel_perpend = vel_xy - vel_straight * wishdir;
223 float step = accel * dt * wishspeed0;
225 float vel_xy_current = vlen(vel_xy);
227 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
228 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
229 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
230 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
231 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
233 if (sidefric < 0 && (vel_perpend*vel_perpend))
234 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
236 float f = max(0, 1 + dt * wishspeed * sidefric);
237 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
238 // assume: themin > 1
239 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
240 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
241 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
242 // obviously, this cannot be
247 themin = sqrt(themin);
248 vel_perpend *= max(themin, f);
252 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
254 vel_xy = vel_straight * wishdir + vel_perpend;
258 float vel_xy_preclamp;
259 vel_xy_preclamp = vlen(vel_xy);
260 if (vel_xy_preclamp > 0) // prevent division by zero
262 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
263 if (vel_xy_current < vel_xy_preclamp)
264 vel_xy *= (vel_xy_current / vel_xy_preclamp);
268 this.velocity = vel_xy + vel_z * '0 0 1';
271 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
276 vector curvel = this.velocity;
278 float curspeed = vlen(curvel);
280 if (wishspeed > curspeed * 1.01)
281 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
284 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
285 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
287 vector wishvel = wishdir * wishspeed;
288 vector acceldir = wishvel - curvel;
289 float addspeed = vlen(acceldir);
290 acceldir = normalize(acceldir);
292 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
294 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
296 vector curdir = normalize(curvel);
297 float dot = acceldir * curdir;
299 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
302 this.velocity += accelspeed * acceldir;
310 When you press the jump key
311 returns true if handled
314 bool PlayerJump(entity this)
316 if (PHYS_FROZEN(this))
317 return true; // no jumping in freezetag when frozen
320 if (this.player_blocked)
321 return true; // no jumping while blocked
324 bool doublejump = false;
325 float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
326 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
328 if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
331 mjumpheight = M_ARGV(1, float);
332 doublejump = M_ARGV(2, bool);
334 if (this.waterlevel >= WATERLEVEL_SWIMMING)
344 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
350 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
351 return IS_JUMP_HELD(this);
353 if(PHYS_TRACK_CANJUMP(this))
357 if (IS_JUMP_HELD(this))
360 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
361 // velocity bounds. Final velocity is bound between (jumpheight *
362 // min + jumpheight) and (jumpheight * max + jumpheight);
364 if(PHYS_JUMPSPEEDCAP_MIN != "")
366 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
368 if (this.velocity_z < minjumpspeed)
369 mjumpheight += minjumpspeed - this.velocity_z;
372 if(PHYS_JUMPSPEEDCAP_MAX != "")
374 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
375 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
377 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
379 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
381 if (this.velocity_z > maxjumpspeed)
382 mjumpheight -= this.velocity_z - maxjumpspeed;
386 if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
389 if(autocvar_speedmeter)
390 LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
392 if(this.lastground < time - 0.3)
394 float f = (1 - PHYS_FRICTION_ONLAND(this));
395 this.velocity_x *= f;
396 this.velocity_y *= f;
399 if(this.jumppadcount > 1)
400 LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
401 this.jumppadcount = 0;
405 this.velocity_z += mjumpheight;
407 UNSET_ONGROUND(this);
413 this.oldvelocity_z = this.velocity_z;
415 animdecide_setaction(this, ANIMACTION_JUMP, true);
417 if (autocvar_g_jump_grunt)
418 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
423 void CheckWaterJump(entity this)
425 // check for a jump-out-of-water
426 makevectors(this.v_angle);
427 vector start = this.origin;
430 normalize(v_forward);
431 vector end = start + v_forward*24;
432 traceline (start, end, true, this);
433 if (trace_fraction < 1)
435 start_z = start_z + this.maxs_z - 8;
436 end = start + v_forward*24;
437 this.movedir = trace_plane_normal * -50;
438 traceline(start, end, true, this);
439 if (trace_fraction == 1)
440 { // open at eye level
441 this.velocity_z = 225;
442 this.flags |= FL_WATERJUMP;
445 PHYS_TELEPORT_TIME(this) = time + 2; // safety net
447 PHYS_WATERJUMP_TIME(this) = 2;
455 #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
457 float autocvar_cl_jetpack_jump;
458 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
460 .float jetpack_stopped;
461 void CheckPlayerJump(entity this)
464 bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
466 if (JETPACK_JUMP(this) < 2)
467 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
469 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
471 bool playerjump = PlayerJump(this); // required
473 bool air_jump = !playerjump || M_ARGV(2, bool);
474 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
475 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_AMMO);
477 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
478 else if (this.jetpack_stopped) { }
482 if (was_flying) // TODO: ran out of fuel message
483 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
485 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
487 this.jetpack_stopped = true;
488 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
490 else if (activate && !PHYS_FROZEN(this))
491 ITEMS_STAT(this) |= IT_USING_JETPACK;
495 this.jetpack_stopped = false;
496 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
498 if (!PHYS_INPUT_BUTTON_JUMP(this))
499 UNSET_JUMP_HELD(this);
501 if (this.waterlevel == WATERLEVEL_SWIMMING)
502 CheckWaterJump(this);
506 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
507 .float specialcommand_pos;
508 void SpecialCommand(entity this)
510 if(autocvar_sv_cheats || this.maycheat)
512 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
513 LOG_INFO("A hollow voice says \"Plugh\".");
516 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
520 bool PM_check_specialcommand(entity this, int buttons)
526 else if (buttons == 1)
528 else if (buttons == 2)
530 else if (buttons == 128)
532 else if (buttons == 256)
534 else if (buttons == 512)
536 else if (buttons == 1024)
541 if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
543 CS(this).specialcommand_pos += 1;
544 if (CS(this).specialcommand_pos >= strlen(specialcommand))
546 CS(this).specialcommand_pos = 0;
547 SpecialCommand(this);
551 else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
552 CS(this).specialcommand_pos = 0;
557 void PM_check_nickspam(entity this)
560 if (time >= this.nickspamtime)
562 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
564 // slight annoyance for nick change scripts
565 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
566 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;
568 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!
570 this.v_angle_x = random() * 360;
571 this.v_angle_y = random() * 360;
572 // at least I'm not forcing retardedview by also assigning to angles_z
573 this.fixangle = true;
579 void PM_check_punch(entity this, float dt)
582 if (this.punchangle != '0 0 0')
584 float f = vlen(this.punchangle) - 10 * dt;
586 this.punchangle = normalize(this.punchangle) * f;
588 this.punchangle = '0 0 0';
591 if (this.punchvector != '0 0 0')
593 float f = vlen(this.punchvector) - 30 * dt;
595 this.punchvector = normalize(this.punchvector) * f;
597 this.punchvector = '0 0 0';
602 // predict frozen movement, as frozen players CAN move in some cases
603 void PM_check_frozen(entity this)
605 if (!PHYS_FROZEN(this))
607 if (PHYS_DODGING_FROZEN(this)
609 && IS_REAL_CLIENT(this)
613 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
614 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
615 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
618 PHYS_CS(this).movement = '0 0 0';
620 vector midpoint = ((this.absmin + this.absmax) * 0.5);
621 if (pointcontents(midpoint) == CONTENT_WATER)
623 this.velocity = this.velocity * 0.5;
625 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
626 this.velocity_z = 200;
630 void PM_check_hitground(entity this)
633 if (!this.wasFlying) return;
634 this.wasFlying = false;
635 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
636 if (time < this.ladder_time) return;
637 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
639 .entity weaponentity = weaponentities[slot];
640 if(this.(weaponentity).hook)
643 this.nextstep = time + 0.3 + random() * 0.1;
644 trace_dphitq3surfaceflags = 0;
645 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
646 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
647 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
650 float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
651 GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
655 void PM_Footsteps(entity this)
658 if (!g_footsteps) return;
659 if (IS_DUCKED(this)) return;
660 if (time >= this.lastground + 0.2) return;
661 if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
662 if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
664 this.nextstep = time + 0.3 + random() * 0.1;
665 trace_dphitq3surfaceflags = 0;
666 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
667 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
668 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
671 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
676 void PM_check_slick(entity this)
678 if(!IS_ONGROUND(this))
681 if(!PHYS_SLICK_APPLYGRAVITY(this))
684 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
685 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
687 UNSET_ONGROUND(this);
694 void PM_check_blocked(entity this)
697 if (!this.player_blocked)
699 PHYS_CS(this).movement = '0 0 0';
700 this.disableclientprediction = 1;
704 void PM_jetpack(entity this, float maxspd_mod, float dt)
706 //makevectors(this.v_angle.y * '0 1 0');
707 makevectors(this.v_angle);
708 vector wishvel = v_forward * PHYS_CS(this).movement_x
709 + v_right * PHYS_CS(this).movement_y;
710 // add remaining speed as Z component
711 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
713 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
714 // add the unused velocity as up component
717 // if (PHYS_INPUT_BUTTON_JUMP(this))
718 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
720 // it is now normalized, so...
721 float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
722 float a_up = PHYS_JETPACK_ACCEL_UP(this);
723 float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
725 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
732 if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
735 //////////////////////////////////////////////////////////////////////////////////////
736 // finding the maximum over all vectors of above form
737 // with wishvel having an absolute value of 1
738 //////////////////////////////////////////////////////////////////////////////////////
739 // we're finding the maximum over
740 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
741 // for z in the range from -1 to 1
742 //////////////////////////////////////////////////////////////////////////////////////
743 // maximum is EITHER attained at the single extreme point:
744 float a_diff = a_side * a_side - a_up * a_up;
748 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
749 if (f > -1 && f < 1) // can it be attained?
751 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
755 // OR attained at z = 1:
756 f = (a_up + a_add) * (a_up + a_add);
762 // OR attained at z = -1:
763 f = (a_up - a_add) * (a_up - a_add);
770 //////////////////////////////////////////////////////////////////////////////////////
772 //print("best possible acceleration: ", ftos(best), "\n");
775 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
776 if (wishvel_z - PHYS_GRAVITY(this) > 0)
777 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
779 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
782 fvel = vlen(wishvel);
785 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
787 fvel = min(1, vlen(wishvel) / best);
788 if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
789 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
793 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
795 if (f > 0 && wishvel != '0 0 0')
797 this.velocity = this.velocity + wishvel * f * dt;
798 UNSET_ONGROUND(this);
801 if (!(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
802 TakeResource(this, RES_FUEL, PHYS_JETPACK_FUEL(this) * dt * fvel * f);
804 ITEMS_STAT(this) |= IT_USING_JETPACK;
806 // jetpack also inhibits health regeneration, but only for 1 second
807 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
812 // used for calculating airshots
813 bool IsFlying(entity this)
815 if(IS_ONGROUND(this))
817 if(this.waterlevel >= WATERLEVEL_SWIMMING)
819 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 24', MOVE_NORMAL, this);
820 //traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
821 if(trace_fraction < 1)
827 void sys_phys_update(entity this, float dt);
829 void SV_PlayerPhysics(entity this)
831 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
835 // needs to be called before physics are run!
836 if(IS_REAL_CLIENT(this))
837 PM_UpdateButtons(this, CS(this));
840 sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
843 CS(this).pm_frametime = frametime;
845 if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
846 this.csqcmodel_modelflags |= MF_ROCKET;
848 this.csqcmodel_modelflags &= ~MF_ROCKET;