2 #include "triggers/trigger/swamp.qh"
3 #include "triggers/trigger/jumppads.qh"
8 #include "../server/miscfunctions.qh"
9 #include "triggers/trigger/viewloc.qh"
11 // client side physics
12 bool Physics_Valid(string thecvar)
14 if(!autocvar_g_physics_clientselect) { return false; }
16 string l = strcat(" ", autocvar_g_physics_clientselect_options, " ");
18 if(strstrofs(l, strcat(" ", thecvar, " "), 0) >= 0)
24 float Physics_ClientOption(entity pl, string option)
26 if(Physics_Valid(pl.cvar_cl_physics))
28 string s = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
29 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
32 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
34 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
35 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
38 return cvar(strcat("sv_", option));
41 void Physics_AddStats()
43 // static view offset and hitbox vectors
44 // networked for all you bandwidth pigs out there
45 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
46 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
47 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
48 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
49 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
50 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
52 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
53 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
54 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
55 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
56 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
57 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
58 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
59 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
60 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
61 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
62 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
63 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
65 // g_movementspeed hack
66 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
67 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
68 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
69 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
70 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
73 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
74 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
75 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
76 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
77 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
78 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
80 // hack to fix track_canjump
81 addstat(STAT_MOVEVARS_CL_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
82 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, stat_sv_track_canjump);
85 addstat(STAT_DOUBLEJUMP, AS_INT, stat_doublejump);
88 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, AS_INT, stat_jumpspeedcap_disable_onramps);
91 addstat(STAT_MOVEVARS_FRICTION_ONLAND, AS_FLOAT, stat_sv_friction_on_land);
92 addstat(STAT_MOVEVARS_FRICTION_SLICK, AS_FLOAT, stat_sv_friction_slick);
93 addstat(STAT_GAMEPLAYFIX_EASIERWATERJUMP, AS_INT, stat_gameplayfix_easierwaterjump);
96 addstat(STAT_MOVEVARS_JUMPVELOCITY, AS_FLOAT, stat_sv_jumpvelocity);
97 addstat(STAT_MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, AS_FLOAT, stat_sv_airaccel_qw_stretchfactor);
98 addstat(STAT_MOVEVARS_MAXAIRSTRAFESPEED, AS_FLOAT, stat_sv_maxairstrafespeed);
99 addstat(STAT_MOVEVARS_MAXAIRSPEED, AS_FLOAT, stat_sv_maxairspeed);
100 addstat(STAT_MOVEVARS_AIRSTRAFEACCELERATE, AS_FLOAT, stat_sv_airstrafeaccelerate);
101 addstat(STAT_MOVEVARS_WARSOWBUNNY_TURNACCEL, AS_FLOAT, stat_sv_warsowbunny_turnaccel);
102 addstat(STAT_MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, AS_FLOAT, stat_sv_airaccel_sideways_friction);
103 addstat(STAT_MOVEVARS_AIRCONTROL, AS_FLOAT, stat_sv_aircontrol);
104 addstat(STAT_MOVEVARS_AIRCONTROL_POWER, AS_FLOAT, stat_sv_aircontrol_power);
105 addstat(STAT_MOVEVARS_AIRCONTROL_PENALTY, AS_FLOAT, stat_sv_aircontrol_penalty);
106 addstat(STAT_MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, AS_FLOAT, stat_sv_warsowbunny_airforwardaccel);
107 addstat(STAT_MOVEVARS_WARSOWBUNNY_TOPSPEED, AS_FLOAT, stat_sv_warsowbunny_topspeed);
108 addstat(STAT_MOVEVARS_WARSOWBUNNY_ACCEL, AS_FLOAT, stat_sv_warsowbunny_accel);
109 addstat(STAT_MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, AS_FLOAT, stat_sv_warsowbunny_backtosideratio);
110 addstat(STAT_MOVEVARS_FRICTION, AS_FLOAT, stat_sv_friction);
111 addstat(STAT_MOVEVARS_ACCELERATE, AS_FLOAT, stat_sv_accelerate);
112 addstat(STAT_MOVEVARS_STOPSPEED, AS_FLOAT, stat_sv_stopspeed);
113 addstat(STAT_MOVEVARS_AIRACCELERATE, AS_FLOAT, stat_sv_airaccelerate);
114 addstat(STAT_MOVEVARS_AIRSTOPACCELERATE, AS_FLOAT, stat_sv_airstopaccelerate);
116 addstat(STAT_GAMEPLAYFIX_UPVELOCITYCLEARSONGROUND, AS_INT, stat_gameplayfix_upvelocityclearsonground);
119 void Physics_UpdateStats(float maxspd_mod)
122 self.stat_pl_view_ofs = PL_VIEW_OFS;
123 self.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
125 self.stat_pl_min = PL_MIN;
126 self.stat_pl_max = PL_MAX;
127 self.stat_pl_crouch_min = PL_CROUCH_MIN;
128 self.stat_pl_crouch_max = PL_CROUCH_MAX;
131 self.stat_sv_airaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airaccel_qw"), maxspd_mod);
132 if(Physics_ClientOption(self, "airstrafeaccel_qw"))
133 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airstrafeaccel_qw"), maxspd_mod);
135 self.stat_sv_airstrafeaccel_qw = 0;
136 self.stat_sv_airspeedlimit_nonqw = Physics_ClientOption(self, "airspeedlimit_nonqw") * maxspd_mod;
137 self.stat_sv_maxspeed = Physics_ClientOption(self, "maxspeed") * maxspd_mod; // also slow walking
138 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
140 self.stat_doublejump = PHYS_DOUBLEJUMP;
142 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
143 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
144 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
145 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
146 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
147 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
149 self.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
151 self.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
152 self.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
154 self.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
158 // fix some new settings
159 self.stat_sv_airaccel_qw_stretchfactor = Physics_ClientOption(self, "airaccel_qw_stretchfactor");
160 self.stat_sv_maxairstrafespeed = Physics_ClientOption(self, "maxairstrafespeed");
161 self.stat_sv_maxairspeed = Physics_ClientOption(self, "maxairspeed");
162 self.stat_sv_airstrafeaccelerate = Physics_ClientOption(self, "airstrafeaccelerate");
163 self.stat_sv_warsowbunny_turnaccel = Physics_ClientOption(self, "warsowbunny_turnaccel");
164 self.stat_sv_airaccel_sideways_friction = Physics_ClientOption(self, "airaccel_sideways_friction");
165 self.stat_sv_aircontrol = Physics_ClientOption(self, "aircontrol");
166 self.stat_sv_aircontrol_power = Physics_ClientOption(self, "aircontrol_power");
167 self.stat_sv_aircontrol_penalty = Physics_ClientOption(self, "aircontrol_penalty");
168 self.stat_sv_warsowbunny_airforwardaccel = Physics_ClientOption(self, "warsowbunny_airforwardaccel");
169 self.stat_sv_warsowbunny_topspeed = Physics_ClientOption(self, "warsowbunny_topspeed");
170 self.stat_sv_warsowbunny_accel = Physics_ClientOption(self, "warsowbunny_accel");
171 self.stat_sv_warsowbunny_backtosideratio = Physics_ClientOption(self, "warsowbunny_backtosideratio");
172 self.stat_sv_friction = Physics_ClientOption(self, "friction");
173 self.stat_sv_accelerate = Physics_ClientOption(self, "accelerate");
174 self.stat_sv_stopspeed = Physics_ClientOption(self, "stopspeed");
175 self.stat_sv_airaccelerate = Physics_ClientOption(self, "airaccelerate");
176 self.stat_sv_airstopaccelerate = Physics_ClientOption(self, "airstopaccelerate");
177 self.stat_sv_jumpvelocity = Physics_ClientOption(self, "jumpvelocity");
179 self.stat_sv_track_canjump = Physics_ClientOption(self, "track_canjump");
181 self.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
185 float IsMoveInDirection(vector mv, float ang) // key mix factor
187 if (mv_x == 0 && mv_y == 0)
188 return 0; // avoid division by zero
189 ang -= RAD2DEG * atan2(mv_y, mv_x);
190 ang = remainder(ang, 360) / 45;
191 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
194 float GeomLerp(float a, float lerp, float b)
196 return a == 0 ? (lerp < 1 ? 0 : b)
197 : b == 0 ? (lerp > 0 ? 0 : a)
198 : a * pow(fabs(b / a), lerp);
201 noref float pmove_waterjumptime;
203 const float unstick_count = 27;
204 vector unstick_offsets[unstick_count] =
206 // 1 no nudge (just return the original if this test passes)
209 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
210 '-0.125 0.000 0.000', '0.125 0.000 0.000',
211 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
212 // 4 diagonal flat nudges
213 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
214 '-0.125 0.125 0.000', '0.125 0.125 0.000',
215 // 8 diagonal upward nudges
216 '-0.125 0.000 0.125', '0.125 0.000 0.125',
217 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
218 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
219 '-0.125 0.125 0.125', '0.125 0.125 0.125',
220 // 8 diagonal downward nudges
221 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
222 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
223 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
224 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
227 void PM_ClientMovement_Unstick()
230 for (i = 0; i < unstick_count; i++)
232 vector neworigin = unstick_offsets[i] + self.origin;
233 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
234 if (!trace_startsolid)
236 setorigin(self, neworigin);
242 void PM_ClientMovement_UpdateStatus(bool ground)
244 // make sure player is not stuck
245 PM_ClientMovement_Unstick();
248 if (PHYS_INPUT_BUTTON_CROUCH(self))
250 // wants to crouch, this always works..
251 if (!IS_DUCKED(self))
256 // wants to stand, if currently crouching we need to check for a
260 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
261 if (!trace_startsolid)
267 vector origin1 = self.origin + '0 0 1';
268 vector origin2 = self.origin - '0 0 1';
272 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
273 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
277 // this code actually "predicts" an impact; so let's clip velocity first
278 float f = self.velocity * trace_plane_normal;
279 self.velocity -= f * trace_plane_normal;
282 UNSET_ONGROUND(self);
285 // set watertype/waterlevel
286 origin1 = self.origin;
287 origin1_z += self.mins_z + 1;
288 self.waterlevel = WATERLEVEL_NONE;
290 int thepoint = pointcontents(origin1);
292 self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
296 self.waterlevel = WATERLEVEL_WETFEET;
297 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
298 thepoint = pointcontents(origin1);
299 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
301 self.waterlevel = WATERLEVEL_SWIMMING;
302 origin1_z = self.origin_z + 22;
303 thepoint = pointcontents(origin1);
304 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
305 self.waterlevel = WATERLEVEL_SUBMERGED;
309 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
310 pmove_waterjumptime = 0;
313 void PM_ClientMovement_Move()
320 vector currentorigin2;
322 vector primalvelocity;
324 vector trace1_endpos = '0 0 0';
325 vector trace2_endpos = '0 0 0';
326 vector trace3_endpos = '0 0 0';
327 float trace1_fraction = 0;
328 float trace2_fraction = 0;
329 float trace3_fraction = 0;
330 vector trace1_plane_normal = '0 0 0';
331 vector trace2_plane_normal = '0 0 0';
332 vector trace3_plane_normal = '0 0 0';
335 PM_ClientMovement_UpdateStatus(false);
336 primalvelocity = self.velocity;
337 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
339 neworigin = self.origin + t * self.velocity;
340 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
341 trace1_endpos = trace_endpos;
342 trace1_fraction = trace_fraction;
343 trace1_plane_normal = trace_plane_normal;
344 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
346 // may be a step or wall, try stepping up
347 // first move forward at a higher level
348 currentorigin2 = self.origin;
349 currentorigin2_z += PHYS_STEPHEIGHT;
350 neworigin2 = neworigin;
351 neworigin2_z += PHYS_STEPHEIGHT;
352 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
353 trace2_endpos = trace_endpos;
354 trace2_fraction = trace_fraction;
355 trace2_plane_normal = trace_plane_normal;
356 if(!trace_startsolid)
358 // then move down from there
359 currentorigin2 = trace2_endpos;
360 neworigin2 = trace2_endpos;
361 neworigin2_z = self.origin_z;
362 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
363 trace3_endpos = trace_endpos;
364 trace3_fraction = trace_fraction;
365 trace3_plane_normal = trace_plane_normal;
366 // accept the new trace if it made some progress
367 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
369 trace1_endpos = trace2_endpos;
370 trace1_fraction = trace2_fraction;
371 trace1_plane_normal = trace2_plane_normal;
372 trace1_endpos = trace3_endpos;
377 // check if it moved at all
378 if(trace1_fraction >= 0.001)
379 setorigin(self, trace1_endpos);
381 // check if it moved all the way
382 if(trace1_fraction == 1)
385 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
386 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
387 // this got commented out in a change that supposedly makes the code match QW better
388 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
389 if(trace1_plane_normal_z > 0.7)
392 t -= t * trace1_fraction;
394 f = (self.velocity * trace1_plane_normal);
395 self.velocity = self.velocity + -f * trace1_plane_normal;
397 if(pmove_waterjumptime > 0)
398 self.velocity = primalvelocity;
402 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
404 float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
408 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(self), 1);
410 float zspeed = self.velocity_z;
412 float xyspeed = vlen(self.velocity);
413 self.velocity = normalize(self.velocity);
415 float dot = self.velocity * wishdir;
417 if (dot > 0) // we can't change direction while slowing down
419 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
420 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
421 k *= PHYS_AIRCONTROL;
422 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
425 self.velocity = self.velocity * xyspeed;
426 self.velocity_z = zspeed;
429 float AdjustAirAccelQW(float accelqw, float factor)
431 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
434 // example config for alternate speed clamping:
435 // sv_airaccel_qw 0.8
436 // sv_airaccel_sideways_friction 0
437 // prvm_globalset server speedclamp_mode 1
439 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
441 float speedclamp = stretchfactor > 0 ? stretchfactor
442 : accelqw < 0 ? 1 // full clamping, no stretch
445 accelqw = fabs(accelqw);
447 if (GAMEPLAYFIX_Q2AIRACCELERATE)
448 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
450 float vel_straight = self.velocity * wishdir;
451 float vel_z = self.velocity_z;
452 vector vel_xy = vec2(self.velocity);
453 vector vel_perpend = vel_xy - vel_straight * wishdir;
455 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
457 float vel_xy_current = vlen(vel_xy);
459 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
460 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
461 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
462 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
463 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
465 if (sidefric < 0 && (vel_perpend*vel_perpend))
466 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
468 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
469 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
471 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
472 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
473 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
474 // obviously, this cannot be
480 vel_perpend *= max(fmin, f);
484 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
486 vel_xy = vel_straight * wishdir + vel_perpend;
490 float vel_xy_preclamp;
491 vel_xy_preclamp = vlen(vel_xy);
492 if (vel_xy_preclamp > 0) // prevent division by zero
494 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
495 if (vel_xy_current < vel_xy_preclamp)
496 vel_xy *= (vel_xy_current / vel_xy_preclamp);
500 self.velocity = vel_xy + vel_z * '0 0 1';
503 void PM_AirAccelerate(vector wishdir, float wishspeed)
508 vector curvel = self.velocity;
510 float curspeed = vlen(curvel);
512 if (wishspeed > curspeed * 1.01)
513 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
516 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
517 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
519 vector wishvel = wishdir * wishspeed;
520 vector acceldir = wishvel - curvel;
521 float addspeed = vlen(acceldir);
522 acceldir = normalize(acceldir);
524 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
526 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
528 vector curdir = normalize(curvel);
529 float dot = acceldir * curdir;
531 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
534 self.velocity += accelspeed * acceldir;
542 When you press the jump key
543 returns true if handled
546 bool PlayerJump (void)
548 if (PHYS_FROZEN(self))
549 return true; // no jumping in freezetag when frozen
552 if (self.player_blocked)
553 return true; // no jumping while blocked
556 bool doublejump = false;
557 float mjumpheight = PHYS_JUMPVELOCITY;
559 player_multijump = doublejump;
560 player_jumpheight = mjumpheight;
563 if (MUTATOR_CALLHOOK(PlayerJump, doublejump, mjumpheight)
565 || PM_multijump_checkjump()
569 doublejump = player_multijump;
570 mjumpheight = player_jumpheight;
574 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
575 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
579 // we MUST clip velocity here!
581 f = self.velocity * trace_plane_normal;
583 self.velocity -= f * trace_plane_normal;
587 if (self.waterlevel >= WATERLEVEL_SWIMMING)
589 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
594 if (!IS_ONGROUND(self))
595 return IS_JUMP_HELD(self);
597 bool track_jump = PHYS_CL_TRACK_CANJUMP(self);
598 if(PHYS_TRACK_CANJUMP(self))
602 if (IS_JUMP_HELD(self))
605 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
606 // velocity bounds. Final velocity is bound between (jumpheight *
607 // min + jumpheight) and (jumpheight * max + jumpheight);
609 if(PHYS_JUMPSPEEDCAP_MIN != "")
611 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
613 if (self.velocity_z < minjumpspeed)
614 mjumpheight += minjumpspeed - self.velocity_z;
617 if(PHYS_JUMPSPEEDCAP_MAX != "")
619 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
620 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
622 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
624 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
626 if (self.velocity_z > maxjumpspeed)
627 mjumpheight -= self.velocity_z - maxjumpspeed;
631 if (!WAS_ONGROUND(self))
634 if(autocvar_speedmeter)
635 LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
637 if(self.lastground < time - 0.3)
639 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
640 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
643 if(self.jumppadcount > 1)
644 LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
645 self.jumppadcount = 0;
649 self.velocity_z += mjumpheight;
651 UNSET_ONGROUND(self);
656 self.oldvelocity_z = self.velocity_z;
658 animdecide_setaction(self, ANIMACTION_JUMP, true);
660 if (autocvar_g_jump_grunt)
661 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
666 void CheckWaterJump()
668 // check for a jump-out-of-water
669 makevectors(self.v_angle);
670 vector start = self.origin;
673 normalize(v_forward);
674 vector end = start + v_forward*24;
675 traceline (start, end, true, self);
676 if (trace_fraction < 1)
678 start_z = start_z + self.maxs_z - 8;
679 end = start + v_forward*24;
680 self.movedir = trace_plane_normal * -50;
681 traceline(start, end, true, self);
682 if (trace_fraction == 1)
683 { // open at eye level
684 self.velocity_z = 225;
685 self.flags |= FL_WATERJUMP;
688 self.teleport_time = time + 2; // safety net
690 pmove_waterjumptime = time + 2;
698 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
700 float autocvar_cl_jetpack_jump;
701 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
703 .float jetpack_stopped;
704 // Hack: shouldn't need to know about this
705 .float multijump_count;
706 void CheckPlayerJump()
709 float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
711 if (JETPACK_JUMP(self) < 2)
712 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
714 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
716 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
717 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
718 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
720 if (!(ITEMS_STAT(self) & ITEM_Jetpack.m_itemid)) { }
721 else if (self.jetpack_stopped) { }
725 if (was_flying) // TODO: ran out of fuel message
726 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
728 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
730 self.jetpack_stopped = true;
731 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
733 else if (activate && !PHYS_FROZEN(self))
734 ITEMS_STAT(self) |= IT_USING_JETPACK;
738 self.jetpack_stopped = false;
739 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
741 if (!PHYS_INPUT_BUTTON_JUMP(self))
742 UNSET_JUMP_HELD(self);
744 if (self.waterlevel == WATERLEVEL_SWIMMING)
748 float racecar_angle(float forward, float down)
756 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
758 float angle_mult = forward / (800 + forward);
761 return ret * angle_mult + 360 * (1 - angle_mult);
763 return ret * angle_mult;
766 void RaceCarPhysics()
769 // using this move type for "big rigs"
770 // the engine does not push the entity!
774 vector angles_save = self.angles;
775 float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
776 float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
778 if (g_bugrigs_reverse_speeding)
782 // back accel is DIGITAL
783 // to prevent speedhack
793 makevectors(self.angles); // new forward direction!
795 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
797 float myspeed = self.velocity * v_forward;
798 float upspeed = self.velocity * v_up;
800 // responsiveness factor for steering and acceleration
801 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
802 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
805 if (myspeed < 0 && g_bugrigs_reverse_spinning)
806 steerfactor = -myspeed * g_bugrigs_steer;
808 steerfactor = -myspeed * f * g_bugrigs_steer;
811 if (myspeed < 0 && g_bugrigs_reverse_speeding)
812 accelfactor = g_bugrigs_accel;
814 accelfactor = f * g_bugrigs_accel;
815 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
821 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
825 if (!g_bugrigs_reverse_speeding)
826 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
833 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
837 if (g_bugrigs_reverse_stopping)
840 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
843 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
844 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
846 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
847 makevectors(self.angles); // new forward direction!
849 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
851 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
855 float myspeed = vlen(self.velocity);
857 // responsiveness factor for steering and acceleration
858 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
859 float steerfactor = -myspeed * f;
860 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
862 rigvel = self.velocity;
863 makevectors(self.angles); // new forward direction!
866 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
867 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
868 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
869 //MAXIMA: solve(total_acceleration(v) = 0, v);
871 if (g_bugrigs_planar_movement)
873 vector rigvel_xy, neworigin, up;
876 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
877 rigvel_xy = vec2(rigvel);
879 if (g_bugrigs_planar_movement_car_jumping)
882 mt = MOVE_NOMONSTERS;
884 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
885 up = trace_endpos - self.origin;
887 // BUG RIGS: align the move to the surface instead of doing collision testing
889 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
892 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
894 if (trace_fraction < 0.5)
897 neworigin = self.origin;
900 neworigin = trace_endpos;
902 if (trace_fraction < 1)
904 // now set angles_x so that the car points parallel to the surface
905 self.angles = vectoangles(
906 '1 0 0' * v_forward_x * trace_plane_normal_z
908 '0 1 0' * v_forward_y * trace_plane_normal_z
910 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
916 // now set angles_x so that the car points forward, but is tilted in velocity direction
917 UNSET_ONGROUND(self);
920 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
921 self.movetype = MOVETYPE_NOCLIP;
925 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
926 self.velocity = rigvel;
927 self.movetype = MOVETYPE_FLY;
931 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
932 if (trace_fraction != 1)
934 self.angles = vectoangles2(
935 '1 0 0' * v_forward_x * trace_plane_normal_z
937 '0 1 0' * v_forward_y * trace_plane_normal_z
939 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
947 vel_local_x = v_forward * self.velocity;
948 vel_local_y = v_right * self.velocity;
949 vel_local_z = v_up * self.velocity;
951 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
952 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
956 vector vf1, vu1, smoothangles;
957 makevectors(self.angles);
958 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
963 makevectors(angles_save);
964 vf1 = vf1 + v_forward * (1 - f);
965 vu1 = vu1 + v_up * (1 - f);
966 smoothangles = vectoangles2(vf1, vu1);
967 self.angles_x = -smoothangles_x;
968 self.angles_z = smoothangles_z;
972 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
973 .float specialcommand_pos;
974 void SpecialCommand()
980 if (!CheatImpulse(99))
981 LOG_INFO("A hollow voice says \"Plugh\".\n");
986 void PM_check_race_movetime(void)
989 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
990 float f = floor(self.race_movetime_frac);
991 self.race_movetime_frac -= f;
992 self.race_movetime_count += f;
993 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
997 float PM_check_specialcommand(float buttons)
1003 else if (buttons == 1)
1005 else if (buttons == 2)
1007 else if (buttons == 128)
1009 else if (buttons == 256)
1011 else if (buttons == 512)
1013 else if (buttons == 1024)
1018 if (c == substring(specialcommand, self.specialcommand_pos, 1))
1020 self.specialcommand_pos += 1;
1021 if (self.specialcommand_pos >= strlen(specialcommand))
1023 self.specialcommand_pos = 0;
1028 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
1029 self.specialcommand_pos = 0;
1034 void PM_check_nickspam(void)
1037 if (time >= self.nickspamtime)
1039 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
1041 // slight annoyance for nick change scripts
1042 self.movement = -1 * self.movement;
1043 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
1045 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!
1047 self.v_angle_x = random() * 360;
1048 self.v_angle_y = random() * 360;
1049 // at least I'm not forcing retardedview by also assigning to angles_z
1050 self.fixangle = true;
1056 void PM_check_punch()
1059 if (self.punchangle != '0 0 0')
1061 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
1063 self.punchangle = normalize(self.punchangle) * f;
1065 self.punchangle = '0 0 0';
1068 if (self.punchvector != '0 0 0')
1070 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1072 self.punchvector = normalize(self.punchvector) * f;
1074 self.punchvector = '0 0 0';
1079 void PM_check_spider(void)
1082 if (time >= self.spider_slowness)
1084 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1085 PHYS_MAXAIRSPEED(self) *= 0.5;
1086 PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
1087 PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
1091 // predict frozen movement, as frozen players CAN move in some cases
1092 void PM_check_frozen(void)
1094 if (!PHYS_FROZEN(self))
1096 if (PHYS_DODGING_FROZEN
1098 && IS_REAL_CLIENT(self)
1102 self.movement_x = bound(-5, self.movement.x, 5);
1103 self.movement_y = bound(-5, self.movement.y, 5);
1104 self.movement_z = bound(-5, self.movement.z, 5);
1107 self.movement = '0 0 0';
1109 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1110 if (pointcontents(midpoint) == CONTENT_WATER)
1112 self.velocity = self.velocity * 0.5;
1114 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1115 self.velocity_z = 200;
1119 void PM_check_hitground()
1122 if (IS_ONGROUND(self))
1123 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1127 if (self.waterlevel < WATERLEVEL_SWIMMING)
1128 if (time >= self.ladder_time)
1131 self.nextstep = time + 0.3 + random() * 0.1;
1132 trace_dphitq3surfaceflags = 0;
1133 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1134 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1136 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1137 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1139 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1146 void PM_check_blocked(void)
1149 if (!self.player_blocked)
1151 self.movement = '0 0 0';
1152 self.disableclientprediction = 1;
1157 float speedaward_lastsent;
1158 float speedaward_lastupdate;
1160 void PM_check_race(void)
1163 if(!(g_cts || g_race))
1165 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1167 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1168 speedaward_holder = self.netname;
1169 speedaward_uid = self.crypto_idfp;
1170 speedaward_lastupdate = time;
1172 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1174 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1175 race_send_speedaward(MSG_ALL);
1176 speedaward_lastsent = speedaward_speed;
1177 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1179 speedaward_alltimebest = speedaward_speed;
1180 speedaward_alltimebest_holder = speedaward_holder;
1181 speedaward_alltimebest_uid = speedaward_uid;
1182 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1183 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1184 race_send_speedaward_alltimebest(MSG_ALL);
1190 void PM_check_vortex(void)
1194 float xyspeed = vlen(vec2(self.velocity));
1195 if (self.weapon == WEP_VORTEX.m_id && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1197 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1198 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1199 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1200 // add the extra charge
1201 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1206 void PM_fly(float maxspd_mod)
1208 // noclipping or flying
1209 UNSET_ONGROUND(self);
1211 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1212 makevectors(self.v_angle);
1213 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1214 vector wishvel = v_forward * self.movement.x
1215 + v_right * self.movement.y
1216 + '0 0 1' * self.movement.z;
1218 vector wishdir = normalize(wishvel);
1219 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1221 if (time >= self.teleport_time)
1223 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1224 PM_ClientMovement_Move();
1227 void PM_swim(float maxspd_mod)
1230 UNSET_ONGROUND(self);
1232 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1233 // water jump only in certain situations
1234 // this mimics quakeworld code
1235 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1237 vector yawangles = '0 1 0' * self.v_angle.y;
1238 makevectors(yawangles);
1239 vector forward = v_forward;
1240 vector spot = self.origin + 24 * forward;
1242 traceline(spot, spot, MOVE_NOMONSTERS, self);
1243 if (trace_startsolid)
1246 traceline(spot, spot, MOVE_NOMONSTERS, self);
1247 if (!trace_startsolid)
1249 self.velocity = forward * 50;
1250 self.velocity_z = 310;
1251 pmove_waterjumptime = 2;
1252 UNSET_ONGROUND(self);
1253 SET_JUMP_HELD(self);
1257 makevectors(self.v_angle);
1258 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1259 vector wishvel = v_forward * self.movement.x
1260 + v_right * self.movement.y
1261 + '0 0 1' * self.movement.z;
1262 if (wishvel == '0 0 0')
1263 wishvel = '0 0 -60'; // drift towards bottom
1265 vector wishdir = normalize(wishvel);
1266 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1268 if (IS_DUCKED(self))
1271 // if (pmove_waterjumptime <= 0) // TODO: use
1274 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1275 f = min(max(0, f), 1);
1278 f = wishspeed - self.velocity * wishdir;
1281 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1282 self.velocity += accelspeed * wishdir;
1285 // holding jump button swims upward slowly
1289 if (self.watertype & CONTENT_LAVA)
1290 self.velocity_z = 50;
1291 else if (self.watertype & CONTENT_SLIME)
1292 self.velocity_z = 80;
1295 if (IS_NEXUIZ_DERIVED(gamemode))
1297 self.velocity_z = 200;
1300 self.velocity_z = 100;
1305 // water acceleration
1306 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1307 PM_ClientMovement_Move();
1310 void PM_ladder(float maxspd_mod)
1312 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1313 UNSET_ONGROUND(self);
1316 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1317 if (PHYS_ENTGRAVITY(self))
1318 g *= PHYS_ENTGRAVITY(self);
1319 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1322 self.velocity_z += g;
1325 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1326 makevectors(self.v_angle);
1327 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1328 vector wishvel = v_forward * self.movement_x
1329 + v_right * self.movement_y
1330 + '0 0 1' * self.movement_z;
1331 self.velocity_z += g;
1332 if (self.ladder_entity.classname == "func_water")
1334 float f = vlen(wishvel);
1335 if (f > self.ladder_entity.speed)
1336 wishvel *= (self.ladder_entity.speed / f);
1338 self.watertype = self.ladder_entity.skin;
1339 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1340 if ((self.origin_z + self.view_ofs_z) < f)
1341 self.waterlevel = WATERLEVEL_SUBMERGED;
1342 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1343 self.waterlevel = WATERLEVEL_SWIMMING;
1344 else if ((self.origin_z + self.mins_z + 1) < f)
1345 self.waterlevel = WATERLEVEL_WETFEET;
1348 self.waterlevel = WATERLEVEL_NONE;
1349 self.watertype = CONTENT_EMPTY;
1353 vector wishdir = normalize(wishvel);
1354 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1356 if (time >= self.teleport_time)
1358 // water acceleration
1359 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1360 PM_ClientMovement_Move();
1363 void PM_jetpack(float maxspd_mod)
1365 //makevectors(self.v_angle.y * '0 1 0');
1366 makevectors(self.v_angle);
1367 vector wishvel = v_forward * self.movement_x
1368 + v_right * self.movement_y;
1369 // add remaining speed as Z component
1370 float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1371 // fix speedhacks :P
1372 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1373 // add the unused velocity as up component
1376 // if (self.BUTTON_JUMP)
1377 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1379 // it is now normalized, so...
1380 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1381 float a_up = PHYS_JETPACK_ACCEL_UP;
1382 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1384 wishvel_x *= a_side;
1385 wishvel_y *= a_side;
1390 //////////////////////////////////////////////////////////////////////////////////////
1391 // finding the maximum over all vectors of above form
1392 // with wishvel having an absolute value of 1
1393 //////////////////////////////////////////////////////////////////////////////////////
1394 // we're finding the maximum over
1395 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1396 // for z in the range from -1 to 1
1397 //////////////////////////////////////////////////////////////////////////////////////
1398 // maximum is EITHER attained at the single extreme point:
1399 float a_diff = a_side * a_side - a_up * a_up;
1403 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1404 if (f > -1 && f < 1) // can it be attained?
1406 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1407 //print("middle\n");
1410 // OR attained at z = 1:
1411 f = (a_up + a_add) * (a_up + a_add);
1417 // OR attained at z = -1:
1418 f = (a_up - a_add) * (a_up - a_add);
1422 //print("bottom\n");
1425 //////////////////////////////////////////////////////////////////////////////////////
1427 //print("best possible acceleration: ", ftos(best), "\n");
1430 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1431 if (wishvel_z - PHYS_GRAVITY > 0)
1432 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1434 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1437 fvel = vlen(wishvel);
1440 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1442 fvel = min(1, vlen(wishvel) / best);
1443 if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1444 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1448 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1450 if (f > 0 && wishvel != '0 0 0')
1452 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1453 UNSET_ONGROUND(self);
1456 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1457 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1459 ITEMS_STAT(self) |= IT_USING_JETPACK;
1461 // jetpack also inhibits health regeneration, but only for 1 second
1462 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1467 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1468 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1469 self.velocity_z -= g * 0.5;
1471 self.velocity_z -= g;
1472 PM_ClientMovement_Move();
1473 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1474 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1475 self.velocity_z -= g * 0.5;
1479 void PM_walk(float buttons_prev, float maxspd_mod)
1481 if (!WAS_ONGROUND(self))
1484 if (autocvar_speedmeter)
1485 LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1487 if (self.lastground < time - 0.3)
1488 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1490 if (self.jumppadcount > 1)
1491 LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1492 self.jumppadcount = 0;
1497 makevectors(self.v_angle.y * '0 1 0');
1498 vector wishvel = v_forward * self.movement.x
1499 + v_right * self.movement.y;
1501 vector wishdir = normalize(wishvel);
1502 float wishspeed = vlen(wishvel);
1504 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1505 if (IS_DUCKED(self))
1508 // apply edge friction
1509 float f = vlen(vec2(self.velocity));
1513 trace_dphitq3surfaceflags = 0;
1514 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1515 // TODO: apply edge friction
1516 // apply ground friction
1517 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1518 realfriction = PHYS_FRICTION_SLICK;
1520 realfriction = PHYS_FRICTION;
1522 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1526 Mathematical analysis time!
1528 Our goal is to invert this mess.
1530 For the two cases we get:
1531 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1532 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1533 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1535 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1536 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1538 These cases would be chosen ONLY if:
1540 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1541 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1543 v0 >= PHYS_STOPSPEED
1544 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1545 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1548 float addspeed = wishspeed - self.velocity * wishdir;
1551 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1552 self.velocity += accelspeed * wishdir;
1554 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1555 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1556 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1557 if (self.velocity * self.velocity)
1558 PM_ClientMovement_Move();
1559 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1560 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1561 self.velocity_z -= g * 0.5;
1564 void PM_air(float buttons_prev, float maxspd_mod)
1566 makevectors(self.v_angle.y * '0 1 0');
1567 vector wishvel = v_forward * self.movement.x
1568 + v_right * self.movement.y;
1570 vector wishdir = normalize(wishvel);
1571 float wishspeed = vlen(wishvel);
1574 if (time >= self.teleport_time)
1576 if (pmove_waterjumptime <= 0)
1579 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1581 // apply air speed limit
1582 float airaccelqw = PHYS_AIRACCEL_QW(self);
1583 float wishspeed0 = wishspeed;
1584 wishspeed = min(wishspeed, maxairspd);
1585 if (IS_DUCKED(self))
1587 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1589 float accelerating = (self.velocity * wishdir > 0);
1590 float wishspeed2 = wishspeed;
1593 if (PHYS_AIRSTOPACCELERATE)
1595 vector curdir = normalize(vec2(self.velocity));
1596 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1598 // note that for straight forward jumping:
1599 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1600 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1602 // dv/dt = accel * maxspeed (when slow)
1603 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1604 // log dv/dt = logaccel + logmaxspeed (when slow)
1605 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1606 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1607 if (PHYS_MAXAIRSTRAFESPEED)
1608 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1609 if (PHYS_AIRSTRAFEACCELERATE(self))
1610 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1611 if (PHYS_AIRSTRAFEACCEL_QW(self))
1613 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1615 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1618 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1619 PM_AirAccelerate(wishdir, wishspeed2);
1621 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1623 if (PHYS_AIRCONTROL)
1624 CPM_PM_Aircontrol(wishdir, wishspeed2);
1626 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1627 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1628 self.velocity_z -= g * 0.5;
1630 self.velocity_z -= g;
1631 PM_ClientMovement_Move();
1632 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1633 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1634 self.velocity_z -= g * 0.5;
1637 // used for calculating airshots
1638 bool IsFlying(entity a)
1642 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1644 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1645 if(trace_fraction < 1)
1652 int buttons = PHYS_INPUT_BUTTON_MASK(self);
1654 self.items = getstati(STAT_ITEMS, 0, 24);
1656 self.movement = PHYS_INPUT_MOVEVALUES(self);
1658 vector oldv_angle = self.v_angle;
1659 vector oldangles = self.angles; // we need to save these, as they're abused by other code
1660 self.v_angle = PHYS_INPUT_ANGLES(self);
1661 self.angles = PHYS_WORLD_ANGLES(self);
1663 self.team = myteam + 1; // is this correct?
1664 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1665 UNSET_JUMP_HELD(self); // canjump = true
1666 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1668 PM_ClientMovement_UpdateStatus(true);
1673 WarpZone_PlayerPhysics_FixVAngle();
1675 float maxspeed_mod = 1;
1676 maxspeed_mod *= PHYS_HIGHSPEED;
1679 Physics_UpdateStats(maxspeed_mod);
1681 if (self.PlayerPhysplug)
1682 if (self.PlayerPhysplug())
1686 PM_check_race_movetime();
1688 anticheat_physics();
1691 if (PM_check_specialcommand(buttons))
1696 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1697 self.parm_idlesince = time;
1700 int buttons_prev = self.buttons_old;
1701 self.buttons_old = buttons;
1702 self.movement_old = self.movement;
1703 self.v_angle_old = self.v_angle;
1705 PM_check_nickspam();
1709 if (IS_BOT_CLIENT(self))
1711 if (playerdemo_read())
1716 if (IS_PLAYER(self))
1720 if (self.race_penalty)
1721 if (time > self.race_penalty)
1722 self.race_penalty = 0;
1725 bool not_allowed_to_move = false;
1727 if (self.race_penalty)
1728 not_allowed_to_move = true;
1731 if (time < game_starttime)
1732 not_allowed_to_move = true;
1735 if (not_allowed_to_move)
1737 self.velocity = '0 0 0';
1738 self.movetype = MOVETYPE_NONE;
1740 self.disableclientprediction = 2;
1744 else if (self.disableclientprediction == 2)
1746 if (self.movetype == MOVETYPE_NONE)
1747 self.movetype = MOVETYPE_WALK;
1748 self.disableclientprediction = 0;
1754 if (self.movetype == MOVETYPE_NONE)
1757 // when we get here, disableclientprediction cannot be 2
1758 self.disableclientprediction = 0;
1761 viewloc_PlayerPhysics();
1772 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1774 // conveyors: first fix velocity
1775 if (self.conveyor.state)
1776 self.velocity -= self.conveyor.movedir;
1779 MUTATOR_CALLHOOK(PlayerPhysics);
1785 // float forcedodge = 1;
1788 // PM_dodging_checkpressedkeys();
1791 // PM_ClientMovement_Move();
1796 if (!IS_PLAYER(self))
1798 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1799 if (!self.spectatorspeed)
1800 self.spectatorspeed = maxspeed_mod;
1801 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1803 if (self.lastclassname != "player")
1805 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1806 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1807 else if (self.impulse == 11)
1808 self.spectatorspeed = maxspeed_mod;
1809 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1810 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1811 else if (self.impulse >= 1 && self.impulse <= 9)
1812 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1813 } // otherwise just clear
1816 maxspeed_mod = self.spectatorspeed;
1819 float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1820 if(self.speed != spd)
1823 string temps = ftos(spd);
1824 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1825 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1826 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1827 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1830 if(self.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1832 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1833 stuffcmd(self, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1835 if(self.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1837 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1838 stuffcmd(self, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1844 // handle water here
1845 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1846 if(pointcontents(midpoint) == CONTENT_WATER)
1848 self.velocity = self.velocity * 0.5;
1851 //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1852 //{ self.velocity_z = 70; }
1858 if (!self.fixangle && !g_bugrigs)
1859 self.angles = '0 1 0' * self.v_angle.y;
1862 PM_check_hitground();
1867 if (IS_PLAYER(self))
1870 if (self.flags & FL_WATERJUMP)
1872 self.velocity_x = self.movedir_x;
1873 self.velocity_y = self.movedir_y;
1874 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1876 self.flags &= ~FL_WATERJUMP;
1877 self.teleport_time = 0;
1882 else if (g_bugrigs && IS_PLAYER(self))
1886 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS_STAT(self) & BUFF_FLIGHT.m_itemid))
1887 PM_fly(maxspeed_mod);
1889 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1890 PM_swim(maxspeed_mod);
1892 else if (time < self.ladder_time)
1893 PM_ladder(maxspeed_mod);
1895 else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1896 PM_jetpack(maxspeed_mod);
1898 else if (IS_ONGROUND(self))
1899 PM_walk(buttons_prev, maxspeed_mod);
1902 PM_air(buttons_prev, maxspeed_mod);
1905 if (!IS_OBSERVER(self))
1911 if (IS_ONGROUND(self))
1912 self.lastground = time;
1914 // conveyors: then break velocity again
1915 if(self.conveyor.state)
1916 self.velocity += self.conveyor.movedir;
1918 self.lastflags = self.flags;
1920 self.lastclassname = self.classname;
1923 self.v_angle = oldv_angle;
1924 self.angles = oldangles;
1929 void SV_PlayerPhysics(void)
1931 void CSQC_ClientMovement_PlayerMove_Frame(void)
1938 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1939 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1940 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);