2 #include "triggers/include.qh"
7 #include "../server/miscfunctions.qh"
8 #include "triggers/trigger/viewloc.qh"
10 // client side physics
11 bool Physics_Valid(string thecvar)
13 return autocvar_g_physics_clientselect && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 float Physics_ClientOption(entity pl, string option)
18 if(Physics_Valid(pl.cvar_cl_physics))
20 string s = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
21 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
24 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
26 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
27 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
30 return cvar(strcat("sv_", option));
33 void Physics_AddStats()
35 // static view offset and hitbox vectors
36 // networked for all you bandwidth pigs out there
37 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
38 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
39 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
40 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
41 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
42 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
44 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
45 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
46 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
47 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
48 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
49 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
50 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
51 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
52 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
53 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
54 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
55 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
58 void Physics_UpdateStats(entity this, float maxspd_mod)
61 this.stat_pl_view_ofs = PL_VIEW_OFS;
62 this.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
64 this.stat_pl_min = PL_MIN;
65 this.stat_pl_max = PL_MAX;
66 this.stat_pl_crouch_min = PL_CROUCH_MIN;
67 this.stat_pl_crouch_max = PL_CROUCH_MAX;
69 STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw"), maxspd_mod);
70 STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw"))
71 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw"), maxspd_mod)
73 STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw") * maxspd_mod;
74 STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed") * maxspd_mod; // also slow walking
77 // fix some new settings
78 STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor");
79 STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed");
80 STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed");
81 STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate");
82 STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel");
83 STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction");
84 STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol");
85 STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power");
86 STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty");
87 STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel");
88 STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed");
89 STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel");
90 STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio");
91 STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction");
92 STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate");
93 STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed");
94 STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate");
95 STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate");
96 STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity");
97 STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump");
101 float IsMoveInDirection(vector mv, float ang) // key mix factor
103 if (mv_x == 0 && mv_y == 0)
104 return 0; // avoid division by zero
105 ang -= RAD2DEG * atan2(mv_y, mv_x);
106 ang = remainder(ang, 360) / 45;
107 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
110 float GeomLerp(float a, float lerp, float b)
112 return a == 0 ? (lerp < 1 ? 0 : b)
113 : b == 0 ? (lerp > 0 ? 0 : a)
114 : a * pow(fabs(b / a), lerp);
117 noref float pmove_waterjumptime;
119 const float unstick_count = 27;
120 vector unstick_offsets[unstick_count] =
122 // 1 no nudge (just return the original if this test passes)
125 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
126 '-0.125 0.000 0.000', '0.125 0.000 0.000',
127 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
128 // 4 diagonal flat nudges
129 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
130 '-0.125 0.125 0.000', '0.125 0.125 0.000',
131 // 8 diagonal upward nudges
132 '-0.125 0.000 0.125', '0.125 0.000 0.125',
133 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
134 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
135 '-0.125 0.125 0.125', '0.125 0.125 0.125',
136 // 8 diagonal downward nudges
137 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
138 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
139 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
140 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
143 void PM_ClientMovement_Unstick()
146 for (i = 0; i < unstick_count; i++)
148 vector neworigin = unstick_offsets[i] + self.origin;
149 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
150 if (!trace_startsolid)
152 setorigin(self, neworigin);
158 void PM_ClientMovement_UpdateStatus(bool ground)
160 // make sure player is not stuck
161 PM_ClientMovement_Unstick();
164 if (PHYS_INPUT_BUTTON_CROUCH(self))
166 // wants to crouch, this always works..
167 if (!IS_DUCKED(self))
172 // wants to stand, if currently crouching we need to check for a
176 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
177 if (!trace_startsolid)
183 vector origin1 = self.origin + '0 0 1';
184 vector origin2 = self.origin - '0 0 1';
188 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
189 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
193 // this code actually "predicts" an impact; so let's clip velocity first
194 float f = self.velocity * trace_plane_normal;
195 self.velocity -= f * trace_plane_normal;
198 UNSET_ONGROUND(self);
201 // set watertype/waterlevel
202 origin1 = self.origin;
203 origin1_z += self.mins_z + 1;
204 self.waterlevel = WATERLEVEL_NONE;
206 int thepoint = pointcontents(origin1);
208 self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
212 self.waterlevel = WATERLEVEL_WETFEET;
213 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
214 thepoint = pointcontents(origin1);
215 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
217 self.waterlevel = WATERLEVEL_SWIMMING;
218 origin1_z = self.origin_z + 22;
219 thepoint = pointcontents(origin1);
220 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
221 self.waterlevel = WATERLEVEL_SUBMERGED;
225 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
226 pmove_waterjumptime = 0;
229 void PM_ClientMovement_Move()
236 vector currentorigin2;
238 vector primalvelocity;
240 vector trace1_endpos = '0 0 0';
241 vector trace2_endpos = '0 0 0';
242 vector trace3_endpos = '0 0 0';
243 float trace1_fraction = 0;
244 float trace2_fraction = 0;
245 float trace3_fraction = 0;
246 vector trace1_plane_normal = '0 0 0';
247 vector trace2_plane_normal = '0 0 0';
248 vector trace3_plane_normal = '0 0 0';
251 PM_ClientMovement_UpdateStatus(false);
252 primalvelocity = self.velocity;
253 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
255 neworigin = self.origin + t * self.velocity;
256 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
257 trace1_endpos = trace_endpos;
258 trace1_fraction = trace_fraction;
259 trace1_plane_normal = trace_plane_normal;
260 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
262 // may be a step or wall, try stepping up
263 // first move forward at a higher level
264 currentorigin2 = self.origin;
265 currentorigin2_z += PHYS_STEPHEIGHT;
266 neworigin2 = neworigin;
267 neworigin2_z += PHYS_STEPHEIGHT;
268 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
269 trace2_endpos = trace_endpos;
270 trace2_fraction = trace_fraction;
271 trace2_plane_normal = trace_plane_normal;
272 if(!trace_startsolid)
274 // then move down from there
275 currentorigin2 = trace2_endpos;
276 neworigin2 = trace2_endpos;
277 neworigin2_z = self.origin_z;
278 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
279 trace3_endpos = trace_endpos;
280 trace3_fraction = trace_fraction;
281 trace3_plane_normal = trace_plane_normal;
282 // accept the new trace if it made some progress
283 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
285 trace1_endpos = trace2_endpos;
286 trace1_fraction = trace2_fraction;
287 trace1_plane_normal = trace2_plane_normal;
288 trace1_endpos = trace3_endpos;
293 // check if it moved at all
294 if(trace1_fraction >= 0.001)
295 setorigin(self, trace1_endpos);
297 // check if it moved all the way
298 if(trace1_fraction == 1)
301 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
302 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
303 // this got commented out in a change that supposedly makes the code match QW better
304 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
305 if(trace1_plane_normal_z > 0.7)
308 t -= t * trace1_fraction;
310 f = (self.velocity * trace1_plane_normal);
311 self.velocity = self.velocity + -f * trace1_plane_normal;
313 if(pmove_waterjumptime > 0)
314 self.velocity = primalvelocity;
318 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
320 float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
324 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(self), 1);
326 float zspeed = self.velocity_z;
328 float xyspeed = vlen(self.velocity);
329 self.velocity = normalize(self.velocity);
331 float dot = self.velocity * wishdir;
333 if (dot > 0) // we can't change direction while slowing down
335 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
336 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
337 k *= PHYS_AIRCONTROL;
338 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
341 self.velocity = self.velocity * xyspeed;
342 self.velocity_z = zspeed;
345 float AdjustAirAccelQW(float accelqw, float factor)
347 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
350 // example config for alternate speed clamping:
351 // sv_airaccel_qw 0.8
352 // sv_airaccel_sideways_friction 0
353 // prvm_globalset server speedclamp_mode 1
355 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
357 float speedclamp = stretchfactor > 0 ? stretchfactor
358 : accelqw < 0 ? 1 // full clamping, no stretch
361 accelqw = fabs(accelqw);
363 if (GAMEPLAYFIX_Q2AIRACCELERATE)
364 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
366 float vel_straight = self.velocity * wishdir;
367 float vel_z = self.velocity_z;
368 vector vel_xy = vec2(self.velocity);
369 vector vel_perpend = vel_xy - vel_straight * wishdir;
371 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
373 float vel_xy_current = vlen(vel_xy);
375 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
376 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
377 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
378 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
379 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
381 if (sidefric < 0 && (vel_perpend*vel_perpend))
382 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
384 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
385 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
387 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
388 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
389 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
390 // obviously, this cannot be
396 vel_perpend *= max(fmin, f);
400 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
402 vel_xy = vel_straight * wishdir + vel_perpend;
406 float vel_xy_preclamp;
407 vel_xy_preclamp = vlen(vel_xy);
408 if (vel_xy_preclamp > 0) // prevent division by zero
410 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
411 if (vel_xy_current < vel_xy_preclamp)
412 vel_xy *= (vel_xy_current / vel_xy_preclamp);
416 self.velocity = vel_xy + vel_z * '0 0 1';
419 void PM_AirAccelerate(vector wishdir, float wishspeed)
424 vector curvel = self.velocity;
426 float curspeed = vlen(curvel);
428 if (wishspeed > curspeed * 1.01)
429 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
432 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
433 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
435 vector wishvel = wishdir * wishspeed;
436 vector acceldir = wishvel - curvel;
437 float addspeed = vlen(acceldir);
438 acceldir = normalize(acceldir);
440 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
442 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
444 vector curdir = normalize(curvel);
445 float dot = acceldir * curdir;
447 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
450 self.velocity += accelspeed * acceldir;
458 When you press the jump key
459 returns true if handled
464 if (PHYS_FROZEN(self))
465 return true; // no jumping in freezetag when frozen
468 if (self.player_blocked)
469 return true; // no jumping while blocked
472 bool doublejump = false;
473 float mjumpheight = PHYS_JUMPVELOCITY;
475 player_multijump = doublejump;
476 player_jumpheight = mjumpheight;
479 if (MUTATOR_CALLHOOK(PlayerJump, doublejump, mjumpheight)
481 || PM_multijump_checkjump()
485 doublejump = player_multijump;
486 mjumpheight = player_jumpheight;
490 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
491 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
495 // we MUST clip velocity here!
497 f = self.velocity * trace_plane_normal;
499 self.velocity -= f * trace_plane_normal;
503 if (self.waterlevel >= WATERLEVEL_SWIMMING)
505 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
510 if (!IS_ONGROUND(self))
511 return IS_JUMP_HELD(self);
513 bool track_jump = PHYS_CL_TRACK_CANJUMP(self);
514 if(PHYS_TRACK_CANJUMP(self))
518 if (IS_JUMP_HELD(self))
521 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
522 // velocity bounds. Final velocity is bound between (jumpheight *
523 // min + jumpheight) and (jumpheight * max + jumpheight);
525 if(PHYS_JUMPSPEEDCAP_MIN != "")
527 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
529 if (self.velocity_z < minjumpspeed)
530 mjumpheight += minjumpspeed - self.velocity_z;
533 if(PHYS_JUMPSPEEDCAP_MAX != "")
535 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
536 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
538 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
540 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
542 if (self.velocity_z > maxjumpspeed)
543 mjumpheight -= self.velocity_z - maxjumpspeed;
547 if (!WAS_ONGROUND(self))
550 if(autocvar_speedmeter)
551 LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
553 if(self.lastground < time - 0.3)
555 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
556 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
559 if(self.jumppadcount > 1)
560 LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
561 self.jumppadcount = 0;
565 self.velocity_z += mjumpheight;
567 UNSET_ONGROUND(self);
572 self.oldvelocity_z = self.velocity_z;
574 animdecide_setaction(self, ANIMACTION_JUMP, true);
576 if (autocvar_g_jump_grunt)
577 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
582 void CheckWaterJump()
584 // check for a jump-out-of-water
585 makevectors(self.v_angle);
586 vector start = self.origin;
589 normalize(v_forward);
590 vector end = start + v_forward*24;
591 traceline (start, end, true, self);
592 if (trace_fraction < 1)
594 start_z = start_z + self.maxs_z - 8;
595 end = start + v_forward*24;
596 self.movedir = trace_plane_normal * -50;
597 traceline(start, end, true, self);
598 if (trace_fraction == 1)
599 { // open at eye level
600 self.velocity_z = 225;
601 self.flags |= FL_WATERJUMP;
604 self.teleport_time = time + 2; // safety net
606 pmove_waterjumptime = time + 2;
614 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
616 float autocvar_cl_jetpack_jump;
617 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
619 .float jetpack_stopped;
620 // Hack: shouldn't need to know about this
621 .float multijump_count;
622 void CheckPlayerJump()
625 float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
627 if (JETPACK_JUMP(self) < 2)
628 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
630 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
632 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
633 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
634 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
636 if (!(ITEMS_STAT(self) & ITEM_Jetpack.m_itemid)) { }
637 else if (self.jetpack_stopped) { }
641 if (was_flying) // TODO: ran out of fuel message
642 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
644 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
646 self.jetpack_stopped = true;
647 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
649 else if (activate && !PHYS_FROZEN(self))
650 ITEMS_STAT(self) |= IT_USING_JETPACK;
654 self.jetpack_stopped = false;
655 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
657 if (!PHYS_INPUT_BUTTON_JUMP(self))
658 UNSET_JUMP_HELD(self);
660 if (self.waterlevel == WATERLEVEL_SWIMMING)
664 float racecar_angle(float forward, float down)
672 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
674 float angle_mult = forward / (800 + forward);
677 return ret * angle_mult + 360 * (1 - angle_mult);
679 return ret * angle_mult;
682 void RaceCarPhysics()
685 // using this move type for "big rigs"
686 // the engine does not push the entity!
690 vector angles_save = self.angles;
691 float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
692 float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
694 if (g_bugrigs_reverse_speeding)
698 // back accel is DIGITAL
699 // to prevent speedhack
709 makevectors(self.angles); // new forward direction!
711 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
713 float myspeed = self.velocity * v_forward;
714 float upspeed = self.velocity * v_up;
716 // responsiveness factor for steering and acceleration
717 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
718 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
721 if (myspeed < 0 && g_bugrigs_reverse_spinning)
722 steerfactor = -myspeed * g_bugrigs_steer;
724 steerfactor = -myspeed * f * g_bugrigs_steer;
727 if (myspeed < 0 && g_bugrigs_reverse_speeding)
728 accelfactor = g_bugrigs_accel;
730 accelfactor = f * g_bugrigs_accel;
731 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
737 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
741 if (!g_bugrigs_reverse_speeding)
742 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
749 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
753 if (g_bugrigs_reverse_stopping)
756 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
759 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
760 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
762 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
763 makevectors(self.angles); // new forward direction!
765 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
767 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
771 float myspeed = vlen(self.velocity);
773 // responsiveness factor for steering and acceleration
774 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
775 float steerfactor = -myspeed * f;
776 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
778 rigvel = self.velocity;
779 makevectors(self.angles); // new forward direction!
782 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
783 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
784 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
785 //MAXIMA: solve(total_acceleration(v) = 0, v);
787 if (g_bugrigs_planar_movement)
789 vector rigvel_xy, neworigin, up;
792 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY(this); // 4x gravity plays better
793 rigvel_xy = vec2(rigvel);
795 if (g_bugrigs_planar_movement_car_jumping)
798 mt = MOVE_NOMONSTERS;
800 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
801 up = trace_endpos - self.origin;
803 // BUG RIGS: align the move to the surface instead of doing collision testing
805 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
808 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
810 if (trace_fraction < 0.5)
813 neworigin = self.origin;
816 neworigin = trace_endpos;
818 if (trace_fraction < 1)
820 // now set angles_x so that the car points parallel to the surface
821 self.angles = vectoangles(
822 '1 0 0' * v_forward_x * trace_plane_normal_z
824 '0 1 0' * v_forward_y * trace_plane_normal_z
826 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
832 // now set angles_x so that the car points forward, but is tilted in velocity direction
833 UNSET_ONGROUND(self);
836 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
837 self.movetype = MOVETYPE_NOCLIP;
841 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY(this); // 4x gravity plays better
842 self.velocity = rigvel;
843 self.movetype = MOVETYPE_FLY;
847 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
848 if (trace_fraction != 1)
850 self.angles = vectoangles2(
851 '1 0 0' * v_forward_x * trace_plane_normal_z
853 '0 1 0' * v_forward_y * trace_plane_normal_z
855 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
863 vel_local_x = v_forward * self.velocity;
864 vel_local_y = v_right * self.velocity;
865 vel_local_z = v_up * self.velocity;
867 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
868 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
872 vector vf1, vu1, smoothangles;
873 makevectors(self.angles);
874 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
879 makevectors(angles_save);
880 vf1 = vf1 + v_forward * (1 - f);
881 vu1 = vu1 + v_up * (1 - f);
882 smoothangles = vectoangles2(vf1, vu1);
883 self.angles_x = -smoothangles_x;
884 self.angles_z = smoothangles_z;
888 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
889 .float specialcommand_pos;
890 void SpecialCommand()
896 if (!CheatImpulse(99))
897 LOG_INFO("A hollow voice says \"Plugh\".\n");
902 float PM_check_specialcommand(float buttons)
908 else if (buttons == 1)
910 else if (buttons == 2)
912 else if (buttons == 128)
914 else if (buttons == 256)
916 else if (buttons == 512)
918 else if (buttons == 1024)
923 if (c == substring(specialcommand, self.specialcommand_pos, 1))
925 self.specialcommand_pos += 1;
926 if (self.specialcommand_pos >= strlen(specialcommand))
928 self.specialcommand_pos = 0;
933 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
934 self.specialcommand_pos = 0;
939 void PM_check_nickspam()
942 if (time >= self.nickspamtime)
944 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
946 // slight annoyance for nick change scripts
947 self.movement = -1 * self.movement;
948 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
950 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
952 self.v_angle_x = random() * 360;
953 self.v_angle_y = random() * 360;
954 // at least I'm not forcing retardedview by also assigning to angles_z
955 self.fixangle = true;
961 void PM_check_punch()
964 if (self.punchangle != '0 0 0')
966 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
968 self.punchangle = normalize(self.punchangle) * f;
970 self.punchangle = '0 0 0';
973 if (self.punchvector != '0 0 0')
975 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
977 self.punchvector = normalize(self.punchvector) * f;
979 self.punchvector = '0 0 0';
984 void PM_check_spider()
987 if (time >= self.spider_slowness)
989 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
990 PHYS_MAXAIRSPEED(self) *= 0.5;
991 PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
992 PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
996 // predict frozen movement, as frozen players CAN move in some cases
997 void PM_check_frozen()
999 if (!PHYS_FROZEN(self))
1001 if (PHYS_DODGING_FROZEN
1003 && IS_REAL_CLIENT(self)
1007 self.movement_x = bound(-5, self.movement.x, 5);
1008 self.movement_y = bound(-5, self.movement.y, 5);
1009 self.movement_z = bound(-5, self.movement.z, 5);
1012 self.movement = '0 0 0';
1014 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1015 if (pointcontents(midpoint) == CONTENT_WATER)
1017 self.velocity = self.velocity * 0.5;
1019 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1020 self.velocity_z = 200;
1024 void PM_check_hitground()
1027 if (IS_ONGROUND(self))
1028 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1032 if (self.waterlevel < WATERLEVEL_SWIMMING)
1033 if (time >= self.ladder_time)
1036 self.nextstep = time + 0.3 + random() * 0.1;
1037 trace_dphitq3surfaceflags = 0;
1038 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1039 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1041 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1042 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1044 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1051 void PM_check_blocked()
1054 if (!self.player_blocked)
1056 self.movement = '0 0 0';
1057 self.disableclientprediction = 1;
1061 void PM_check_vortex()
1065 float xyspeed = vlen(vec2(self.velocity));
1066 if (self.weapon == WEP_VORTEX.m_id && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1068 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1069 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1070 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1071 // add the extra charge
1072 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1077 void PM_fly(float maxspd_mod)
1079 // noclipping or flying
1080 UNSET_ONGROUND(self);
1082 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1083 makevectors(self.v_angle);
1084 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1085 vector wishvel = v_forward * self.movement.x
1086 + v_right * self.movement.y
1087 + '0 0 1' * self.movement.z;
1089 vector wishdir = normalize(wishvel);
1090 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1092 if (time >= self.teleport_time)
1094 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1095 PM_ClientMovement_Move();
1098 void PM_swim(float maxspd_mod)
1101 UNSET_ONGROUND(self);
1103 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1104 // water jump only in certain situations
1105 // this mimics quakeworld code
1106 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1108 vector yawangles = '0 1 0' * self.v_angle.y;
1109 makevectors(yawangles);
1110 vector forward = v_forward;
1111 vector spot = self.origin + 24 * forward;
1113 traceline(spot, spot, MOVE_NOMONSTERS, self);
1114 if (trace_startsolid)
1117 traceline(spot, spot, MOVE_NOMONSTERS, self);
1118 if (!trace_startsolid)
1120 self.velocity = forward * 50;
1121 self.velocity_z = 310;
1122 pmove_waterjumptime = 2;
1123 UNSET_ONGROUND(self);
1124 SET_JUMP_HELD(self);
1128 makevectors(self.v_angle);
1129 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1130 vector wishvel = v_forward * self.movement.x
1131 + v_right * self.movement.y
1132 + '0 0 1' * self.movement.z;
1133 if (wishvel == '0 0 0')
1134 wishvel = '0 0 -60'; // drift towards bottom
1136 vector wishdir = normalize(wishvel);
1137 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1139 if (IS_DUCKED(self))
1142 // if (pmove_waterjumptime <= 0) // TODO: use
1145 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1146 f = min(max(0, f), 1);
1149 f = wishspeed - self.velocity * wishdir;
1152 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1153 self.velocity += accelspeed * wishdir;
1156 // holding jump button swims upward slowly
1160 if (self.watertype & CONTENT_LAVA)
1161 self.velocity_z = 50;
1162 else if (self.watertype & CONTENT_SLIME)
1163 self.velocity_z = 80;
1166 if (IS_NEXUIZ_DERIVED(gamemode))
1168 self.velocity_z = 200;
1171 self.velocity_z = 100;
1176 // water acceleration
1177 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1178 PM_ClientMovement_Move();
1181 void PM_ladder(float maxspd_mod)
1183 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1184 UNSET_ONGROUND(self);
1187 g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1188 if (PHYS_ENTGRAVITY(self))
1189 g *= PHYS_ENTGRAVITY(self);
1190 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1193 self.velocity_z += g;
1196 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1197 makevectors(self.v_angle);
1198 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1199 vector wishvel = v_forward * self.movement_x
1200 + v_right * self.movement_y
1201 + '0 0 1' * self.movement_z;
1202 self.velocity_z += g;
1203 if (self.ladder_entity.classname == "func_water")
1205 float f = vlen(wishvel);
1206 if (f > self.ladder_entity.speed)
1207 wishvel *= (self.ladder_entity.speed / f);
1209 self.watertype = self.ladder_entity.skin;
1210 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1211 if ((self.origin_z + self.view_ofs_z) < f)
1212 self.waterlevel = WATERLEVEL_SUBMERGED;
1213 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1214 self.waterlevel = WATERLEVEL_SWIMMING;
1215 else if ((self.origin_z + self.mins_z + 1) < f)
1216 self.waterlevel = WATERLEVEL_WETFEET;
1219 self.waterlevel = WATERLEVEL_NONE;
1220 self.watertype = CONTENT_EMPTY;
1224 vector wishdir = normalize(wishvel);
1225 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1227 if (time >= self.teleport_time)
1229 // water acceleration
1230 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1231 PM_ClientMovement_Move();
1234 void PM_jetpack(float maxspd_mod)
1236 //makevectors(self.v_angle.y * '0 1 0');
1237 makevectors(self.v_angle);
1238 vector wishvel = v_forward * self.movement_x
1239 + v_right * self.movement_y;
1240 // add remaining speed as Z component
1241 float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1242 // fix speedhacks :P
1243 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1244 // add the unused velocity as up component
1247 // if (self.BUTTON_JUMP)
1248 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1250 // it is now normalized, so...
1251 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1252 float a_up = PHYS_JETPACK_ACCEL_UP;
1253 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY(this);
1255 wishvel_x *= a_side;
1256 wishvel_y *= a_side;
1261 //////////////////////////////////////////////////////////////////////////////////////
1262 // finding the maximum over all vectors of above form
1263 // with wishvel having an absolute value of 1
1264 //////////////////////////////////////////////////////////////////////////////////////
1265 // we're finding the maximum over
1266 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1267 // for z in the range from -1 to 1
1268 //////////////////////////////////////////////////////////////////////////////////////
1269 // maximum is EITHER attained at the single extreme point:
1270 float a_diff = a_side * a_side - a_up * a_up;
1274 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1275 if (f > -1 && f < 1) // can it be attained?
1277 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1278 //print("middle\n");
1281 // OR attained at z = 1:
1282 f = (a_up + a_add) * (a_up + a_add);
1288 // OR attained at z = -1:
1289 f = (a_up - a_add) * (a_up - a_add);
1293 //print("bottom\n");
1296 //////////////////////////////////////////////////////////////////////////////////////
1298 //print("best possible acceleration: ", ftos(best), "\n");
1301 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1302 if (wishvel_z - PHYS_GRAVITY(this) > 0)
1303 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1305 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1308 fvel = vlen(wishvel);
1311 wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
1313 fvel = min(1, vlen(wishvel) / best);
1314 if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1315 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1319 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1321 if (f > 0 && wishvel != '0 0 0')
1323 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1324 UNSET_ONGROUND(self);
1327 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1328 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1330 ITEMS_STAT(self) |= IT_USING_JETPACK;
1332 // jetpack also inhibits health regeneration, but only for 1 second
1333 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1338 float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1339 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1340 self.velocity_z -= g * 0.5;
1342 self.velocity_z -= g;
1343 PM_ClientMovement_Move();
1344 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1345 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1346 self.velocity_z -= g * 0.5;
1350 void PM_walk(float buttons_prev, float maxspd_mod)
1352 if (!WAS_ONGROUND(self))
1355 if (autocvar_speedmeter)
1356 LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1358 if (self.lastground < time - 0.3)
1359 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1361 if (self.jumppadcount > 1)
1362 LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1363 self.jumppadcount = 0;
1368 makevectors(self.v_angle.y * '0 1 0');
1369 vector wishvel = v_forward * self.movement.x
1370 + v_right * self.movement.y;
1372 vector wishdir = normalize(wishvel);
1373 float wishspeed = vlen(wishvel);
1375 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1376 if (IS_DUCKED(self))
1379 // apply edge friction
1380 float f = vlen(vec2(self.velocity));
1384 trace_dphitq3surfaceflags = 0;
1385 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1386 // TODO: apply edge friction
1387 // apply ground friction
1388 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1389 realfriction = PHYS_FRICTION_SLICK;
1391 realfriction = PHYS_FRICTION;
1393 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1397 Mathematical analysis time!
1399 Our goal is to invert this mess.
1401 For the two cases we get:
1402 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1403 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1404 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1406 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1407 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1409 These cases would be chosen ONLY if:
1411 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1412 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1414 v0 >= PHYS_STOPSPEED
1415 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1416 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1419 float addspeed = wishspeed - self.velocity * wishdir;
1422 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1423 self.velocity += accelspeed * wishdir;
1425 float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1426 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1427 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1428 if (self.velocity * self.velocity)
1429 PM_ClientMovement_Move();
1430 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1431 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1432 self.velocity_z -= g * 0.5;
1435 void PM_air(float buttons_prev, float maxspd_mod)
1437 makevectors(self.v_angle.y * '0 1 0');
1438 vector wishvel = v_forward * self.movement.x
1439 + v_right * self.movement.y;
1441 vector wishdir = normalize(wishvel);
1442 float wishspeed = vlen(wishvel);
1445 if (time >= self.teleport_time)
1447 if (pmove_waterjumptime <= 0)
1450 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1452 // apply air speed limit
1453 float airaccelqw = PHYS_AIRACCEL_QW(self);
1454 float wishspeed0 = wishspeed;
1455 wishspeed = min(wishspeed, maxairspd);
1456 if (IS_DUCKED(self))
1458 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1460 float accelerating = (self.velocity * wishdir > 0);
1461 float wishspeed2 = wishspeed;
1464 if (PHYS_AIRSTOPACCELERATE)
1466 vector curdir = normalize(vec2(self.velocity));
1467 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1469 // note that for straight forward jumping:
1470 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1471 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1473 // dv/dt = accel * maxspeed (when slow)
1474 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1475 // log dv/dt = logaccel + logmaxspeed (when slow)
1476 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1477 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1478 if (PHYS_MAXAIRSTRAFESPEED)
1479 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1480 if (PHYS_AIRSTRAFEACCELERATE(self))
1481 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1482 if (PHYS_AIRSTRAFEACCEL_QW(self))
1484 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1486 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1489 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1490 PM_AirAccelerate(wishdir, wishspeed2);
1492 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1494 if (PHYS_AIRCONTROL)
1495 CPM_PM_Aircontrol(wishdir, wishspeed2);
1497 float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1498 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1499 self.velocity_z -= g * 0.5;
1501 self.velocity_z -= g;
1502 PM_ClientMovement_Move();
1503 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1504 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1505 self.velocity_z -= g * 0.5;
1508 // used for calculating airshots
1509 bool IsFlying(entity a)
1513 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1515 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1516 if(trace_fraction < 1)
1523 int buttons = PHYS_INPUT_BUTTON_MASK(self);
1525 self.items = getstati(STAT_ITEMS, 0, 24);
1527 self.movement = PHYS_INPUT_MOVEVALUES(self);
1529 vector oldv_angle = self.v_angle;
1530 vector oldangles = self.angles; // we need to save these, as they're abused by other code
1531 self.v_angle = PHYS_INPUT_ANGLES(self);
1532 self.angles = PHYS_WORLD_ANGLES(self);
1534 self.team = myteam + 1; // is this correct?
1535 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1536 UNSET_JUMP_HELD(self); // canjump = true
1537 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1539 PM_ClientMovement_UpdateStatus(true);
1544 WarpZone_PlayerPhysics_FixVAngle();
1546 float maxspeed_mod = 1;
1547 maxspeed_mod *= PHYS_HIGHSPEED;
1550 Physics_UpdateStats(this, maxspeed_mod);
1552 if (self.PlayerPhysplug)
1553 if (self.PlayerPhysplug())
1558 anticheat_physics();
1561 if (PM_check_specialcommand(buttons))
1566 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1567 self.parm_idlesince = time;
1570 int buttons_prev = self.buttons_old;
1571 self.buttons_old = buttons;
1572 self.movement_old = self.movement;
1573 self.v_angle_old = self.v_angle;
1575 PM_check_nickspam();
1579 if (IS_BOT_CLIENT(self))
1581 if (playerdemo_read())
1586 if (IS_PLAYER(self))
1589 bool not_allowed_to_move = false;
1591 if (time < game_starttime)
1592 not_allowed_to_move = true;
1595 if (not_allowed_to_move)
1597 self.velocity = '0 0 0';
1598 self.movetype = MOVETYPE_NONE;
1600 self.disableclientprediction = 2;
1604 else if (self.disableclientprediction == 2)
1606 if (self.movetype == MOVETYPE_NONE)
1607 self.movetype = MOVETYPE_WALK;
1608 self.disableclientprediction = 0;
1614 if (self.movetype == MOVETYPE_NONE)
1617 // when we get here, disableclientprediction cannot be 2
1618 self.disableclientprediction = 0;
1621 viewloc_PlayerPhysics();
1632 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1634 // conveyors: first fix velocity
1635 if (self.conveyor.state)
1636 self.velocity -= self.conveyor.movedir;
1639 MUTATOR_CALLHOOK(PlayerPhysics);
1645 // float forcedodge = 1;
1648 // PM_dodging_checkpressedkeys();
1651 // PM_ClientMovement_Move();
1656 if (!IS_PLAYER(self))
1658 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1659 if (!self.spectatorspeed)
1660 self.spectatorspeed = maxspeed_mod;
1661 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1663 if (self.lastclassname != "player")
1665 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1666 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1667 else if (self.impulse == 11)
1668 self.spectatorspeed = maxspeed_mod;
1669 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1670 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1671 else if (self.impulse >= 1 && self.impulse <= 9)
1672 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1673 } // otherwise just clear
1676 maxspeed_mod = self.spectatorspeed;
1679 float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1680 if(self.speed != spd)
1683 string temps = ftos(spd);
1684 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1685 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1686 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1687 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1690 if(self.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1692 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1693 stuffcmd(self, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1695 if(self.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1697 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1698 stuffcmd(self, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1704 // handle water here
1705 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1706 if(pointcontents(midpoint) == CONTENT_WATER)
1708 self.velocity = self.velocity * 0.5;
1711 //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1712 //{ self.velocity_z = 70; }
1718 if (!self.fixangle && !g_bugrigs)
1719 self.angles = '0 1 0' * self.v_angle.y;
1722 PM_check_hitground();
1727 if (IS_PLAYER(self))
1730 if (self.flags & FL_WATERJUMP)
1732 self.velocity_x = self.movedir_x;
1733 self.velocity_y = self.movedir_y;
1734 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1736 self.flags &= ~FL_WATERJUMP;
1737 self.teleport_time = 0;
1742 else if (g_bugrigs && IS_PLAYER(self))
1746 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS_STAT(self) & BUFF_FLIGHT.m_itemid))
1747 PM_fly(maxspeed_mod);
1749 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1750 PM_swim(maxspeed_mod);
1752 else if (time < self.ladder_time)
1753 PM_ladder(maxspeed_mod);
1755 else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1756 PM_jetpack(maxspeed_mod);
1758 else if (IS_ONGROUND(self))
1759 PM_walk(buttons_prev, maxspeed_mod);
1762 PM_air(buttons_prev, maxspeed_mod);
1767 if (IS_ONGROUND(self))
1768 self.lastground = time;
1770 // conveyors: then break velocity again
1771 if(self.conveyor.state)
1772 self.velocity += self.conveyor.movedir;
1774 self.lastflags = self.flags;
1776 self.lastclassname = self.classname;
1779 self.v_angle = oldv_angle;
1780 self.angles = oldangles;
1785 void SV_PlayerPhysics()
1787 void CSQC_ClientMovement_PlayerMove_Frame()
1794 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1795 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1796 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);