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 return autocvar_g_physics_clientselect && checkinlist(thecvar, autocvar_g_physics_clientselect_options);
17 float Physics_ClientOption(entity pl, string option)
19 if (Physics_Valid(pl.cvar_cl_physics))
21 string var = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
22 if (cvar_type(var) & 1)
25 return cvar(strcat("sv_", option));
28 void Physics_AddStats()
30 // static view offset and hitbox vectors
31 // networked for all you bandwidth pigs out there
32 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
33 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
34 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
35 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
36 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
37 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
39 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
40 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
41 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
42 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
43 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
44 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
45 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
46 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
47 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
48 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
49 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
50 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
52 // g_movementspeed hack
53 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
54 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
55 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
56 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
57 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
60 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
61 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
62 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
63 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
64 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
65 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
66 addstat(STAT_JETPACK_REVERSE_THRUST, AS_INT, stat_jetpack_reverse_thrust);
68 // hack to fix track_canjump
69 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
72 addstat(STAT_DOUBLEJUMP, AS_INT, stat_doublejump);
75 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
76 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
77 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, AS_INT, stat_jumpspeedcap_disable_onramps);
80 addstat(STAT_MOVEVARS_FRICTION_ONLAND, AS_FLOAT, stat_sv_friction_on_land);
81 addstat(STAT_MOVEVARS_FRICTION_SLICK, AS_FLOAT, stat_sv_friction_slick);
82 addstat(STAT_GAMEPLAYFIX_EASIERWATERJUMP, AS_INT, stat_gameplayfix_easierwaterjump);
85 addstat(STAT_MOVEVARS_JUMPVELOCITY, AS_FLOAT, stat_sv_jumpvelocity);
86 addstat(STAT_MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, AS_FLOAT, stat_sv_airaccel_qw_stretchfactor);
87 addstat(STAT_MOVEVARS_MAXAIRSTRAFESPEED, AS_FLOAT, stat_sv_maxairstrafespeed);
88 addstat(STAT_MOVEVARS_MAXAIRSPEED, AS_FLOAT, stat_sv_maxairspeed);
89 addstat(STAT_MOVEVARS_AIRSTRAFEACCELERATE, AS_FLOAT, stat_sv_airstrafeaccelerate);
90 addstat(STAT_MOVEVARS_WARSOWBUNNY_TURNACCEL, AS_FLOAT, stat_sv_warsowbunny_turnaccel);
91 addstat(STAT_MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, AS_FLOAT, stat_sv_airaccel_sideways_friction);
92 addstat(STAT_MOVEVARS_AIRCONTROL, AS_FLOAT, stat_sv_aircontrol);
93 addstat(STAT_MOVEVARS_AIRCONTROL_POWER, AS_FLOAT, stat_sv_aircontrol_power);
94 addstat(STAT_MOVEVARS_AIRCONTROL_PENALTY, AS_FLOAT, stat_sv_aircontrol_penalty);
95 addstat(STAT_MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, AS_FLOAT, stat_sv_warsowbunny_airforwardaccel);
96 addstat(STAT_MOVEVARS_WARSOWBUNNY_TOPSPEED, AS_FLOAT, stat_sv_warsowbunny_topspeed);
97 addstat(STAT_MOVEVARS_WARSOWBUNNY_ACCEL, AS_FLOAT, stat_sv_warsowbunny_accel);
98 addstat(STAT_MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, AS_FLOAT, stat_sv_warsowbunny_backtosideratio);
99 addstat(STAT_MOVEVARS_FRICTION, AS_FLOAT, stat_sv_friction);
100 addstat(STAT_MOVEVARS_ACCELERATE, AS_FLOAT, stat_sv_accelerate);
101 addstat(STAT_MOVEVARS_STOPSPEED, AS_FLOAT, stat_sv_stopspeed);
102 addstat(STAT_MOVEVARS_AIRACCELERATE, AS_FLOAT, stat_sv_airaccelerate);
103 addstat(STAT_MOVEVARS_AIRSTOPACCELERATE, AS_FLOAT, stat_sv_airstopaccelerate);
105 addstat(STAT_GAMEPLAYFIX_UPVELOCITYCLEARSONGROUND, AS_INT, stat_gameplayfix_upvelocityclearsonground);
108 void Physics_UpdateStats(float maxspd_mod)
111 self.stat_pl_view_ofs = PL_VIEW_OFS;
112 self.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
114 self.stat_pl_min = PL_MIN;
115 self.stat_pl_max = PL_MAX;
116 self.stat_pl_crouch_min = PL_CROUCH_MIN;
117 self.stat_pl_crouch_max = PL_CROUCH_MAX;
120 self.stat_sv_airaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airaccel_qw"), maxspd_mod);
121 if(Physics_ClientOption(self, "airstrafeaccel_qw"))
122 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airstrafeaccel_qw"), maxspd_mod);
124 self.stat_sv_airstrafeaccel_qw = 0;
125 self.stat_sv_airspeedlimit_nonqw = Physics_ClientOption(self, "airspeedlimit_nonqw") * maxspd_mod;
126 self.stat_sv_maxspeed = Physics_ClientOption(self, "maxspeed") * maxspd_mod; // also slow walking
127 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
129 self.stat_doublejump = PHYS_DOUBLEJUMP;
131 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
132 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
133 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
134 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
135 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
136 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
137 self.stat_jetpack_reverse_thrust = PHYS_JETPACK_REVERSE_THRUST;
139 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
140 self.stat_jumpspeedcap_max = PHYS_JUMPSPEEDCAP_MAX;
141 self.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
143 self.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
144 self.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
146 self.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
150 // fix some new settings
151 self.stat_sv_airaccel_qw_stretchfactor = Physics_ClientOption(self, "airaccel_qw_stretchfactor");
152 self.stat_sv_maxairstrafespeed = Physics_ClientOption(self, "maxairstrafespeed");
153 self.stat_sv_maxairspeed = Physics_ClientOption(self, "maxairspeed");
154 self.stat_sv_airstrafeaccelerate = Physics_ClientOption(self, "airstrafeaccelerate");
155 self.stat_sv_warsowbunny_turnaccel = Physics_ClientOption(self, "warsowbunny_turnaccel");
156 self.stat_sv_airaccel_sideways_friction = Physics_ClientOption(self, "airaccel_sideways_friction");
157 self.stat_sv_aircontrol = Physics_ClientOption(self, "aircontrol");
158 self.stat_sv_aircontrol_power = Physics_ClientOption(self, "aircontrol_power");
159 self.stat_sv_aircontrol_penalty = Physics_ClientOption(self, "aircontrol_penalty");
160 self.stat_sv_warsowbunny_airforwardaccel = Physics_ClientOption(self, "warsowbunny_airforwardaccel");
161 self.stat_sv_warsowbunny_topspeed = Physics_ClientOption(self, "warsowbunny_topspeed");
162 self.stat_sv_warsowbunny_accel = Physics_ClientOption(self, "warsowbunny_accel");
163 self.stat_sv_warsowbunny_backtosideratio = Physics_ClientOption(self, "warsowbunny_backtosideratio");
164 self.stat_sv_friction = Physics_ClientOption(self, "friction");
165 self.stat_sv_accelerate = Physics_ClientOption(self, "accelerate");
166 self.stat_sv_stopspeed = Physics_ClientOption(self, "stopspeed");
167 self.stat_sv_airaccelerate = Physics_ClientOption(self, "airaccelerate");
168 self.stat_sv_airstopaccelerate = Physics_ClientOption(self, "airstopaccelerate");
169 self.stat_sv_jumpvelocity = Physics_ClientOption(self, "jumpvelocity");
171 self.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
175 float IsMoveInDirection(vector mv, float ang) // key mix factor
177 if (mv_x == 0 && mv_y == 0)
178 return 0; // avoid division by zero
179 ang -= RAD2DEG * atan2(mv_y, mv_x);
180 ang = remainder(ang, 360) / 45;
181 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
184 float GeomLerp(float a, float lerp, float b)
186 return a == 0 ? (lerp < 1 ? 0 : b)
187 : b == 0 ? (lerp > 0 ? 0 : a)
188 : a * pow(fabs(b / a), lerp);
191 noref float pmove_waterjumptime;
193 const float unstick_count = 27;
194 vector unstick_offsets[unstick_count] =
196 // 1 no nudge (just return the original if this test passes)
199 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
200 '-0.125 0.000 0.000', '0.125 0.000 0.000',
201 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
202 // 4 diagonal flat nudges
203 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
204 '-0.125 0.125 0.000', '0.125 0.125 0.000',
205 // 8 diagonal upward nudges
206 '-0.125 0.000 0.125', '0.125 0.000 0.125',
207 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
208 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
209 '-0.125 0.125 0.125', '0.125 0.125 0.125',
210 // 8 diagonal downward nudges
211 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
212 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
213 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
214 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
217 void PM_ClientMovement_Unstick()
220 for (i = 0; i < unstick_count; i++)
222 vector neworigin = unstick_offsets[i] + self.origin;
223 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
224 if (!trace_startsolid)
226 setorigin(self, neworigin);
232 void PM_ClientMovement_UpdateStatus(bool ground)
234 // make sure player is not stuck
235 PM_ClientMovement_Unstick();
238 if (PHYS_INPUT_BUTTON_CROUCH(self))
240 // wants to crouch, this always works..
241 if (!IS_DUCKED(self))
246 // wants to stand, if currently crouching we need to check for a
250 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
251 if (!trace_startsolid)
257 vector origin1 = self.origin + '0 0 1';
258 vector origin2 = self.origin - '0 0 1';
262 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
263 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
267 // this code actually "predicts" an impact; so let's clip velocity first
268 float f = self.velocity * trace_plane_normal;
269 self.velocity -= f * trace_plane_normal;
272 UNSET_ONGROUND(self);
275 // set watertype/waterlevel
276 origin1 = self.origin;
277 origin1_z += self.mins_z + 1;
278 self.waterlevel = WATERLEVEL_NONE;
280 int thepoint = pointcontents(origin1);
282 self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
286 self.waterlevel = WATERLEVEL_WETFEET;
287 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
288 thepoint = pointcontents(origin1);
289 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
291 self.waterlevel = WATERLEVEL_SWIMMING;
292 origin1_z = self.origin_z + 22;
293 thepoint = pointcontents(origin1);
294 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
295 self.waterlevel = WATERLEVEL_SUBMERGED;
299 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
300 pmove_waterjumptime = 0;
303 void PM_ClientMovement_Move()
310 vector currentorigin2;
312 vector primalvelocity;
314 vector trace1_endpos = '0 0 0';
315 vector trace2_endpos = '0 0 0';
316 vector trace3_endpos = '0 0 0';
317 float trace1_fraction = 0;
318 float trace2_fraction = 0;
319 float trace3_fraction = 0;
320 vector trace1_plane_normal = '0 0 0';
321 vector trace2_plane_normal = '0 0 0';
322 vector trace3_plane_normal = '0 0 0';
325 PM_ClientMovement_UpdateStatus(false);
326 primalvelocity = self.velocity;
327 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
329 neworigin = self.origin + t * self.velocity;
330 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
331 trace1_endpos = trace_endpos;
332 trace1_fraction = trace_fraction;
333 trace1_plane_normal = trace_plane_normal;
334 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
336 // may be a step or wall, try stepping up
337 // first move forward at a higher level
338 currentorigin2 = self.origin;
339 currentorigin2_z += PHYS_STEPHEIGHT;
340 neworigin2 = neworigin;
341 neworigin2_z += PHYS_STEPHEIGHT;
342 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
343 trace2_endpos = trace_endpos;
344 trace2_fraction = trace_fraction;
345 trace2_plane_normal = trace_plane_normal;
346 if(!trace_startsolid)
348 // then move down from there
349 currentorigin2 = trace2_endpos;
350 neworigin2 = trace2_endpos;
351 neworigin2_z = self.origin_z;
352 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
353 trace3_endpos = trace_endpos;
354 trace3_fraction = trace_fraction;
355 trace3_plane_normal = trace_plane_normal;
356 // accept the new trace if it made some progress
357 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
359 trace1_endpos = trace2_endpos;
360 trace1_fraction = trace2_fraction;
361 trace1_plane_normal = trace2_plane_normal;
362 trace1_endpos = trace3_endpos;
367 // check if it moved at all
368 if(trace1_fraction >= 0.001)
369 setorigin(self, trace1_endpos);
371 // check if it moved all the way
372 if(trace1_fraction == 1)
375 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
376 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
377 // this got commented out in a change that supposedly makes the code match QW better
378 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
379 if(trace1_plane_normal_z > 0.7)
382 t -= t * trace1_fraction;
384 f = (self.velocity * trace1_plane_normal);
385 self.velocity = self.velocity + -f * trace1_plane_normal;
387 if(pmove_waterjumptime > 0)
388 self.velocity = primalvelocity;
392 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
394 float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
398 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED, 1);
400 float zspeed = self.velocity_z;
402 float xyspeed = vlen(self.velocity);
403 self.velocity = normalize(self.velocity);
405 float dot = self.velocity * wishdir;
407 if (dot > 0) // we can't change direction while slowing down
409 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
410 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
411 k *= PHYS_AIRCONTROL;
412 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
415 self.velocity = self.velocity * xyspeed;
416 self.velocity_z = zspeed;
419 float AdjustAirAccelQW(float accelqw, float factor)
421 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
424 // example config for alternate speed clamping:
425 // sv_airaccel_qw 0.8
426 // sv_airaccel_sideways_friction 0
427 // prvm_globalset server speedclamp_mode 1
429 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
431 float speedclamp = stretchfactor > 0 ? stretchfactor
432 : accelqw < 0 ? 1 // full clamping, no stretch
435 accelqw = fabs(accelqw);
437 if (GAMEPLAYFIX_Q2AIRACCELERATE)
438 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
440 float vel_straight = self.velocity * wishdir;
441 float vel_z = self.velocity_z;
442 vector vel_xy = vec2(self.velocity);
443 vector vel_perpend = vel_xy - vel_straight * wishdir;
445 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
447 float vel_xy_current = vlen(vel_xy);
449 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
450 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
451 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
452 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
453 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
455 if (sidefric < 0 && (vel_perpend*vel_perpend))
456 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
458 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
459 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
461 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
462 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
463 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
464 // obviously, this cannot be
470 vel_perpend *= max(fmin, f);
474 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
476 vel_xy = vel_straight * wishdir + vel_perpend;
480 float vel_xy_preclamp;
481 vel_xy_preclamp = vlen(vel_xy);
482 if (vel_xy_preclamp > 0) // prevent division by zero
484 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
485 if (vel_xy_current < vel_xy_preclamp)
486 vel_xy *= (vel_xy_current / vel_xy_preclamp);
490 self.velocity = vel_xy + vel_z * '0 0 1';
493 void PM_AirAccelerate(vector wishdir, float wishspeed)
498 vector curvel = self.velocity;
500 float curspeed = vlen(curvel);
502 if (wishspeed > curspeed * 1.01)
503 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
506 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
507 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
509 vector wishvel = wishdir * wishspeed;
510 vector acceldir = wishvel - curvel;
511 float addspeed = vlen(acceldir);
512 acceldir = normalize(acceldir);
514 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
516 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
518 vector curdir = normalize(curvel);
519 float dot = acceldir * curdir;
521 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
524 self.velocity += accelspeed * acceldir;
532 When you press the jump key
533 returns true if handled
536 float PlayerJump (void)
538 if (PHYS_FROZEN(self))
539 return true; // no jumping in freezetag when frozen
542 if (self.player_blocked)
543 return true; // no jumping while blocked
546 float doublejump = false;
547 float mjumpheight = PHYS_JUMPVELOCITY;
549 player_multijump = doublejump;
550 player_jumpheight = mjumpheight;
552 if (MUTATOR_CALLHOOK(PlayerJump))
554 if(PM_multijump_checkjump())
558 doublejump = player_multijump;
559 mjumpheight = player_jumpheight;
563 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
564 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
568 // we MUST clip velocity here!
570 f = self.velocity * trace_plane_normal;
572 self.velocity -= f * trace_plane_normal;
576 if (self.waterlevel >= WATERLEVEL_SWIMMING)
578 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
583 if (!IS_ONGROUND(self))
584 return IS_JUMP_HELD(self);
586 if (PHYS_TRACK_CANJUMP(self))
587 if (IS_JUMP_HELD(self))
590 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
591 // velocity bounds. Final velocity is bound between (jumpheight *
592 // min + jumpheight) and (jumpheight * max + jumpheight);
594 if(PHYS_JUMPSPEEDCAP_MIN)
596 float minjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MIN;
598 if (self.velocity_z < minjumpspeed)
599 mjumpheight += minjumpspeed - self.velocity_z;
602 if(PHYS_JUMPSPEEDCAP_MAX)
604 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
605 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
607 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
609 float maxjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MAX;
611 if (self.velocity_z > maxjumpspeed)
612 mjumpheight -= self.velocity_z - maxjumpspeed;
616 if (!WAS_ONGROUND(self))
619 if(autocvar_speedmeter)
620 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
622 if(self.lastground < time - 0.3)
624 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
625 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
628 if(self.jumppadcount > 1)
629 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
630 self.jumppadcount = 0;
634 self.velocity_z += mjumpheight;
636 UNSET_ONGROUND(self);
641 self.oldvelocity_z = self.velocity_z;
643 animdecide_setaction(self, ANIMACTION_JUMP, true);
645 if (autocvar_g_jump_grunt)
646 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
651 void CheckWaterJump()
653 // check for a jump-out-of-water
654 makevectors(self.v_angle);
655 vector start = self.origin;
658 normalize(v_forward);
659 vector end = start + v_forward*24;
660 traceline (start, end, true, self);
661 if (trace_fraction < 1)
663 start_z = start_z + self.maxs_z - 8;
664 end = start + v_forward*24;
665 self.movedir = trace_plane_normal * -50;
666 traceline(start, end, true, self);
667 if (trace_fraction == 1)
668 { // open at eye level
669 self.velocity_z = 225;
670 self.flags |= FL_WATERJUMP;
672 self.teleport_time = time + 2; // safety net
679 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
681 float autocvar_cl_jetpack_jump;
682 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
684 .float jetpack_stopped;
685 // Hack: shouldn't need to know about this
686 .float multijump_count;
687 void CheckPlayerJump()
690 float was_flying = ITEMS(self) & IT_USING_JETPACK;
692 if (JETPACK_JUMP(self) < 2)
693 ITEMS(self) &= ~IT_USING_JETPACK;
695 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
697 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
698 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
699 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO;
701 if (!(ITEMS(self) & IT_JETPACK)) { }
702 else if (self.jetpack_stopped) { }
706 if (was_flying) // TODO: ran out of fuel message
707 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
709 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
711 self.jetpack_stopped = true;
712 ITEMS(self) &= ~IT_USING_JETPACK;
714 else if (activate && !PHYS_FROZEN(self))
715 ITEMS(self) |= IT_USING_JETPACK;
719 self.jetpack_stopped = false;
720 ITEMS(self) &= ~IT_USING_JETPACK;
722 if (!PHYS_INPUT_BUTTON_JUMP(self))
723 UNSET_JUMP_HELD(self);
725 if (self.waterlevel == WATERLEVEL_SWIMMING)
729 float racecar_angle(float forward, float down)
737 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
739 float angle_mult = forward / (800 + forward);
742 return ret * angle_mult + 360 * (1 - angle_mult);
744 return ret * angle_mult;
747 void RaceCarPhysics()
750 // using this move type for "big rigs"
751 // the engine does not push the entity!
755 vector angles_save = self.angles;
756 float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
757 float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
759 if (g_bugrigs_reverse_speeding)
763 // back accel is DIGITAL
764 // to prevent speedhack
774 makevectors(self.angles); // new forward direction!
776 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
778 float myspeed = self.velocity * v_forward;
779 float upspeed = self.velocity * v_up;
781 // responsiveness factor for steering and acceleration
782 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
783 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
786 if (myspeed < 0 && g_bugrigs_reverse_spinning)
787 steerfactor = -myspeed * g_bugrigs_steer;
789 steerfactor = -myspeed * f * g_bugrigs_steer;
792 if (myspeed < 0 && g_bugrigs_reverse_speeding)
793 accelfactor = g_bugrigs_accel;
795 accelfactor = f * g_bugrigs_accel;
796 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
802 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
806 if (!g_bugrigs_reverse_speeding)
807 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
814 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
818 if (g_bugrigs_reverse_stopping)
821 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
824 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
825 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
827 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
828 makevectors(self.angles); // new forward direction!
830 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
832 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
836 float myspeed = vlen(self.velocity);
838 // responsiveness factor for steering and acceleration
839 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
840 float steerfactor = -myspeed * f;
841 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
843 rigvel = self.velocity;
844 makevectors(self.angles); // new forward direction!
847 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
848 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
849 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
850 //MAXIMA: solve(total_acceleration(v) = 0, v);
852 if (g_bugrigs_planar_movement)
854 vector rigvel_xy, neworigin, up;
857 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
858 rigvel_xy = vec2(rigvel);
860 if (g_bugrigs_planar_movement_car_jumping)
863 mt = MOVE_NOMONSTERS;
865 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
866 up = trace_endpos - self.origin;
868 // BUG RIGS: align the move to the surface instead of doing collision testing
870 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
873 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
875 if (trace_fraction < 0.5)
878 neworigin = self.origin;
881 neworigin = trace_endpos;
883 if (trace_fraction < 1)
885 // now set angles_x so that the car points parallel to the surface
886 self.angles = vectoangles(
887 '1 0 0' * v_forward_x * trace_plane_normal_z
889 '0 1 0' * v_forward_y * trace_plane_normal_z
891 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
897 // now set angles_x so that the car points forward, but is tilted in velocity direction
898 UNSET_ONGROUND(self);
901 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
902 self.movetype = MOVETYPE_NOCLIP;
906 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
907 self.velocity = rigvel;
908 self.movetype = MOVETYPE_FLY;
912 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
913 if (trace_fraction != 1)
915 self.angles = vectoangles2(
916 '1 0 0' * v_forward_x * trace_plane_normal_z
918 '0 1 0' * v_forward_y * trace_plane_normal_z
920 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
928 vel_local_x = v_forward * self.velocity;
929 vel_local_y = v_right * self.velocity;
930 vel_local_z = v_up * self.velocity;
932 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
933 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
937 vector vf1, vu1, smoothangles;
938 makevectors(self.angles);
939 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
944 makevectors(angles_save);
945 vf1 = vf1 + v_forward * (1 - f);
946 vu1 = vu1 + v_up * (1 - f);
947 smoothangles = vectoangles2(vf1, vu1);
948 self.angles_x = -smoothangles_x;
949 self.angles_z = smoothangles_z;
953 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
954 .float specialcommand_pos;
955 void SpecialCommand()
961 if (!CheatImpulse(99))
962 print("A hollow voice says \"Plugh\".\n");
967 float PM_check_keepaway(void)
970 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
976 void PM_check_race_movetime(void)
979 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
980 float f = floor(self.race_movetime_frac);
981 self.race_movetime_frac -= f;
982 self.race_movetime_count += f;
983 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
987 float PM_check_specialcommand(float buttons)
993 else if (buttons == 1)
995 else if (buttons == 2)
997 else if (buttons == 128)
999 else if (buttons == 256)
1001 else if (buttons == 512)
1003 else if (buttons == 1024)
1008 if (c == substring(specialcommand, self.specialcommand_pos, 1))
1010 self.specialcommand_pos += 1;
1011 if (self.specialcommand_pos >= strlen(specialcommand))
1013 self.specialcommand_pos = 0;
1018 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
1019 self.specialcommand_pos = 0;
1024 void PM_check_nickspam(void)
1027 if (time >= self.nickspamtime)
1029 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
1031 // slight annoyance for nick change scripts
1032 self.movement = -1 * self.movement;
1033 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
1035 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!
1037 self.v_angle_x = random() * 360;
1038 self.v_angle_y = random() * 360;
1039 // at least I'm not forcing retardedview by also assigning to angles_z
1040 self.fixangle = true;
1046 void PM_check_punch()
1049 if (self.punchangle != '0 0 0')
1051 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
1053 self.punchangle = normalize(self.punchangle) * f;
1055 self.punchangle = '0 0 0';
1058 if (self.punchvector != '0 0 0')
1060 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1062 self.punchvector = normalize(self.punchvector) * f;
1064 self.punchvector = '0 0 0';
1069 void PM_check_spider(void)
1072 if (time >= self.spider_slowness)
1074 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1075 self.stat_sv_airspeedlimit_nonqw *= 0.5;
1079 // predict frozen movement, as frozen players CAN move in some cases
1080 void PM_check_frozen(void)
1082 if (!PHYS_FROZEN(self))
1084 if (PHYS_DODGING_FROZEN
1086 && IS_REAL_CLIENT(self)
1090 self.movement_x = bound(-5, self.movement.x, 5);
1091 self.movement_y = bound(-5, self.movement.y, 5);
1092 self.movement_z = bound(-5, self.movement.z, 5);
1095 self.movement = '0 0 0';
1097 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1098 if (pointcontents(midpoint) == CONTENT_WATER)
1100 self.velocity = self.velocity * 0.5;
1102 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1103 self.velocity_z = 200;
1107 void PM_check_hitground()
1110 if (IS_ONGROUND(self))
1111 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1115 if (self.waterlevel < WATERLEVEL_SWIMMING)
1116 if (time >= self.ladder_time)
1119 self.nextstep = time + 0.3 + random() * 0.1;
1120 trace_dphitq3surfaceflags = 0;
1121 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1122 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1124 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1125 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1127 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1134 void PM_check_blocked(void)
1137 if (!self.player_blocked)
1139 self.movement = '0 0 0';
1140 self.disableclientprediction = 1;
1144 void PM_check_race(void)
1147 if(!(g_cts || g_race))
1149 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1151 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1152 speedaward_holder = self.netname;
1153 speedaward_uid = self.crypto_idfp;
1154 speedaward_lastupdate = time;
1156 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1158 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1159 race_send_speedaward(MSG_ALL);
1160 speedaward_lastsent = speedaward_speed;
1161 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1163 speedaward_alltimebest = speedaward_speed;
1164 speedaward_alltimebest_holder = speedaward_holder;
1165 speedaward_alltimebest_uid = speedaward_uid;
1166 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1167 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1168 race_send_speedaward_alltimebest(MSG_ALL);
1174 void PM_check_vortex(void)
1178 float xyspeed = vlen(vec2(self.velocity));
1179 if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1181 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1182 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1183 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1184 // add the extra charge
1185 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1190 void PM_fly(float maxspd_mod)
1192 // noclipping or flying
1193 UNSET_ONGROUND(self);
1195 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1196 makevectors(self.v_angle);
1197 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1198 vector wishvel = v_forward * self.movement.x
1199 + v_right * self.movement.y
1200 + '0 0 1' * self.movement.z;
1202 vector wishdir = normalize(wishvel);
1203 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1205 if (time >= self.teleport_time)
1207 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1208 PM_ClientMovement_Move();
1211 void PM_swim(float maxspd_mod)
1214 UNSET_ONGROUND(self);
1216 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1217 // water jump only in certain situations
1218 // this mimics quakeworld code
1219 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1221 vector yawangles = '0 1 0' * self.v_angle.y;
1222 makevectors(yawangles);
1223 vector forward = v_forward;
1224 vector spot = self.origin + 24 * forward;
1226 traceline(spot, spot, MOVE_NOMONSTERS, self);
1227 if (trace_startsolid)
1230 traceline(spot, spot, MOVE_NOMONSTERS, self);
1231 if (!trace_startsolid)
1233 self.velocity = forward * 50;
1234 self.velocity_z = 310;
1235 pmove_waterjumptime = 2;
1236 UNSET_ONGROUND(self);
1237 SET_JUMP_HELD(self);
1241 makevectors(self.v_angle);
1242 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1243 vector wishvel = v_forward * self.movement.x
1244 + v_right * self.movement.y
1245 + '0 0 1' * self.movement.z;
1246 if (wishvel == '0 0 0')
1247 wishvel = '0 0 -60'; // drift towards bottom
1249 vector wishdir = normalize(wishvel);
1250 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1252 if (IS_DUCKED(self))
1255 // if (pmove_waterjumptime <= 0) // TODO: use
1258 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1259 f = min(max(0, f), 1);
1262 f = wishspeed - self.velocity * wishdir;
1265 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1266 self.velocity += accelspeed * wishdir;
1269 // holding jump button swims upward slowly
1273 if (self.watertype & CONTENT_LAVA)
1274 self.velocity_z = 50;
1275 else if (self.watertype & CONTENT_SLIME)
1276 self.velocity_z = 80;
1279 if (IS_NEXUIZ_DERIVED(gamemode))
1281 self.velocity_z = 200;
1284 self.velocity_z = 100;
1289 // water acceleration
1290 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1291 PM_ClientMovement_Move();
1294 void PM_ladder(float maxspd_mod)
1296 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1297 UNSET_ONGROUND(self);
1300 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1301 if (PHYS_ENTGRAVITY(self))
1302 g *= PHYS_ENTGRAVITY(self);
1303 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1306 self.velocity_z += g;
1309 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1310 makevectors(self.v_angle);
1311 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1312 vector wishvel = v_forward * self.movement_x
1313 + v_right * self.movement_y
1314 + '0 0 1' * self.movement_z;
1315 self.velocity_z += g;
1316 if (self.ladder_entity.classname == "func_water")
1318 float f = vlen(wishvel);
1319 if (f > self.ladder_entity.speed)
1320 wishvel *= (self.ladder_entity.speed / f);
1322 self.watertype = self.ladder_entity.skin;
1323 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1324 if ((self.origin_z + self.view_ofs_z) < f)
1325 self.waterlevel = WATERLEVEL_SUBMERGED;
1326 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1327 self.waterlevel = WATERLEVEL_SWIMMING;
1328 else if ((self.origin_z + self.mins_z + 1) < f)
1329 self.waterlevel = WATERLEVEL_WETFEET;
1332 self.waterlevel = WATERLEVEL_NONE;
1333 self.watertype = CONTENT_EMPTY;
1337 vector wishdir = normalize(wishvel);
1338 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1340 if (time >= self.teleport_time)
1342 // water acceleration
1343 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1344 PM_ClientMovement_Move();
1347 void PM_jetpack(float maxspd_mod)
1349 //makevectors(self.v_angle.y * '0 1 0');
1350 makevectors(self.v_angle);
1351 vector wishvel = v_forward * self.movement_x
1352 + v_right * self.movement_y;
1353 // add remaining speed as Z component
1354 float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1355 // fix speedhacks :P
1356 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1357 // add the unused velocity as up component
1360 // if (self.BUTTON_JUMP)
1361 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1363 // it is now normalized, so...
1364 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1365 float a_up = PHYS_JETPACK_ACCEL_UP;
1366 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1368 if(PHYS_JETPACK_REVERSE_THRUST && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST; }
1370 wishvel_x *= a_side;
1371 wishvel_y *= a_side;
1375 if(PHYS_JETPACK_REVERSE_THRUST && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
1378 //////////////////////////////////////////////////////////////////////////////////////
1379 // finding the maximum over all vectors of above form
1380 // with wishvel having an absolute value of 1
1381 //////////////////////////////////////////////////////////////////////////////////////
1382 // we're finding the maximum over
1383 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1384 // for z in the range from -1 to 1
1385 //////////////////////////////////////////////////////////////////////////////////////
1386 // maximum is EITHER attained at the single extreme point:
1387 float a_diff = a_side * a_side - a_up * a_up;
1391 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1392 if (f > -1 && f < 1) // can it be attained?
1394 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1395 //print("middle\n");
1398 // OR attained at z = 1:
1399 f = (a_up + a_add) * (a_up + a_add);
1405 // OR attained at z = -1:
1406 f = (a_up - a_add) * (a_up - a_add);
1410 //print("bottom\n");
1413 //////////////////////////////////////////////////////////////////////////////////////
1415 //print("best possible acceleration: ", ftos(best), "\n");
1418 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1419 if (wishvel_z - PHYS_GRAVITY > 0)
1420 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1422 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1425 fvel = vlen(wishvel);
1428 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1430 fvel = min(1, vlen(wishvel) / best);
1431 if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1432 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1436 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1438 if (f > 0 && wishvel != '0 0 0')
1440 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1441 UNSET_ONGROUND(self);
1444 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1445 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1447 ITEMS(self) |= IT_USING_JETPACK;
1449 // jetpack also inhibits health regeneration, but only for 1 second
1450 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1455 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1456 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1457 self.velocity_z -= g * 0.5;
1459 self.velocity_z -= g;
1460 PM_ClientMovement_Move();
1461 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1462 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1463 self.velocity_z -= g * 0.5;
1467 void PM_walk(float buttons_prev, float maxspd_mod)
1469 if (!WAS_ONGROUND(self))
1472 if (autocvar_speedmeter)
1473 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1475 if (self.lastground < time - 0.3)
1476 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1478 if (self.jumppadcount > 1)
1479 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1480 self.jumppadcount = 0;
1485 makevectors(self.v_angle.y * '0 1 0');
1486 vector wishvel = v_forward * self.movement.x
1487 + v_right * self.movement.y;
1489 vector wishdir = normalize(wishvel);
1490 float wishspeed = vlen(wishvel);
1492 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1493 if (IS_DUCKED(self))
1496 // apply edge friction
1497 float f = vlen(vec2(self.velocity));
1501 trace_dphitq3surfaceflags = 0;
1502 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1503 // TODO: apply edge friction
1504 // apply ground friction
1505 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1506 realfriction = PHYS_FRICTION_SLICK;
1508 realfriction = PHYS_FRICTION;
1510 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1514 Mathematical analysis time!
1516 Our goal is to invert this mess.
1518 For the two cases we get:
1519 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1520 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1521 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1523 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1524 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1526 These cases would be chosen ONLY if:
1528 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1529 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1531 v0 >= PHYS_STOPSPEED
1532 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1533 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1536 float addspeed = wishspeed - self.velocity * wishdir;
1539 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1540 self.velocity += accelspeed * wishdir;
1542 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1543 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1544 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1545 if (self.velocity * self.velocity)
1546 PM_ClientMovement_Move();
1547 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1548 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1549 self.velocity_z -= g * 0.5;
1552 void PM_air(float buttons_prev, float maxspd_mod)
1554 makevectors(self.v_angle.y * '0 1 0');
1555 vector wishvel = v_forward * self.movement.x
1556 + v_right * self.movement.y;
1558 vector wishdir = normalize(wishvel);
1559 float wishspeed = vlen(wishvel);
1562 if (time >= self.teleport_time)
1564 if (pmove_waterjumptime <= 0)
1567 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1569 // apply air speed limit
1570 float airaccelqw = PHYS_AIRACCEL_QW(self);
1571 float wishspeed0 = wishspeed;
1572 wishspeed = min(wishspeed, maxairspd);
1573 if (IS_DUCKED(self))
1575 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1577 float accelerating = (self.velocity * wishdir > 0);
1578 float wishspeed2 = wishspeed;
1581 if (PHYS_AIRSTOPACCELERATE)
1583 vector curdir = normalize(vec2(self.velocity));
1584 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1586 // note that for straight forward jumping:
1587 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1588 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1590 // dv/dt = accel * maxspeed (when slow)
1591 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1592 // log dv/dt = logaccel + logmaxspeed (when slow)
1593 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1594 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1595 if (PHYS_MAXAIRSTRAFESPEED)
1596 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1597 if (PHYS_AIRSTRAFEACCELERATE)
1598 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1599 if (PHYS_AIRSTRAFEACCEL_QW(self))
1601 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1603 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1606 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1607 PM_AirAccelerate(wishdir, wishspeed2);
1609 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1611 if (PHYS_AIRCONTROL)
1612 CPM_PM_Aircontrol(wishdir, wishspeed2);
1614 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1615 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1616 self.velocity_z -= g * 0.5;
1618 self.velocity_z -= g;
1619 PM_ClientMovement_Move();
1620 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1621 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1622 self.velocity_z -= g * 0.5;
1625 // used for calculating airshots
1626 bool IsFlying(entity a)
1630 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1632 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1633 if(trace_fraction < 1)
1639 float autocvar_cl_forwardspeed;
1640 float autocvar_cl_sidespeed;
1645 float buttons = PHYS_INPUT_BUTTON_MASK(self);
1647 self.items = getstati(STAT_ITEMS, 0, 24);
1649 self.movement = PHYS_INPUT_MOVEVALUES(self);
1651 self.v_angle = PHYS_INPUT_ANGLES(self);
1652 self.angles = PHYS_WORLD_ANGLES(self);
1654 if(PHYS_MAXSPEED(self) != autocvar_cl_forwardspeed || PHYS_MAXSPEED(self) != autocvar_cl_sidespeed)
1656 cvar_set("cl_forwardspeed", ftos(PHYS_MAXSPEED(self)));
1657 cvar_set("cl_sidespeed", ftos(PHYS_MAXSPEED(self)));
1660 self.team = myteam + 1; // is this correct?
1661 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1662 UNSET_JUMP_HELD(self); // canjump = true
1663 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1665 PM_ClientMovement_UpdateStatus(true);
1670 WarpZone_PlayerPhysics_FixVAngle();
1672 float maxspeed_mod = 1;
1673 maxspeed_mod *= PM_check_keepaway();
1674 maxspeed_mod *= PHYS_HIGHSPEED;
1677 Physics_UpdateStats(maxspeed_mod);
1679 if (self.PlayerPhysplug)
1680 if (self.PlayerPhysplug())
1684 PM_check_race_movetime();
1686 anticheat_physics();
1689 if (PM_check_specialcommand(buttons))
1694 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1695 self.parm_idlesince = time;
1698 float buttons_prev = self.buttons_old;
1699 self.buttons_old = buttons;
1700 self.movement_old = self.movement;
1701 self.v_angle_old = self.v_angle;
1703 PM_check_nickspam();
1707 if (IS_BOT_CLIENT(self))
1709 if (playerdemo_read())
1714 if (IS_PLAYER(self))
1718 if (self.race_penalty)
1719 if (time > self.race_penalty)
1720 self.race_penalty = 0;
1723 float not_allowed_to_move = 0;
1725 if (self.race_penalty)
1726 not_allowed_to_move = 1;
1729 if (time < game_starttime)
1730 not_allowed_to_move = 1;
1733 if (not_allowed_to_move)
1735 self.velocity = '0 0 0';
1736 self.movetype = MOVETYPE_NONE;
1738 self.disableclientprediction = 2;
1742 else if (self.disableclientprediction == 2)
1744 if (self.movetype == MOVETYPE_NONE)
1745 self.movetype = MOVETYPE_WALK;
1746 self.disableclientprediction = 0;
1752 if ( self.discomode )
1755 self.BUTTON_JUMP = 1;
1757 self.angles_y = time*180;
1758 self.velocity = randomvec() * 80;
1759 self.fixangle = true;
1762 if (self.movetype == MOVETYPE_NONE)
1765 // when we get here, disableclientprediction cannot be 2
1766 self.disableclientprediction = 0;
1769 viewloc_PlayerPhysics();
1780 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1782 // conveyors: first fix velocity
1783 if (self.conveyor.state)
1784 self.velocity -= self.conveyor.movedir;
1787 MUTATOR_CALLHOOK(PlayerPhysics);
1793 // float forcedodge = 1;
1796 // PM_dodging_checkpressedkeys();
1799 // PM_ClientMovement_Move();
1804 if (!IS_PLAYER(self))
1806 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1807 if (!self.spectatorspeed)
1808 self.spectatorspeed = maxspeed_mod;
1809 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1811 if (self.lastclassname != "player")
1813 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1814 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1815 else if (self.impulse == 11)
1816 self.spectatorspeed = maxspeed_mod;
1817 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1818 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1819 else if (self.impulse >= 1 && self.impulse <= 9)
1820 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1821 } // otherwise just clear
1824 maxspeed_mod = self.spectatorspeed;
1832 if (!self.fixangle && !g_bugrigs)
1833 self.angles = '0 1 0' * self.v_angle.y;
1836 PM_check_hitground();
1841 if (IS_PLAYER(self))
1844 if (self.flags & FL_WATERJUMP)
1846 self.velocity_x = self.movedir_x;
1847 self.velocity_y = self.movedir_y;
1848 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1850 self.flags &= ~FL_WATERJUMP;
1851 self.teleport_time = 0;
1856 else if (g_bugrigs && IS_PLAYER(self))
1860 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS(self) & BUFF_FLIGHT))
1861 PM_fly(maxspeed_mod);
1863 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1864 PM_swim(maxspeed_mod);
1866 else if (time < self.ladder_time)
1867 PM_ladder(maxspeed_mod);
1869 else if (ITEMS(self) & IT_USING_JETPACK)
1870 PM_jetpack(maxspeed_mod);
1872 else if (IS_ONGROUND(self))
1873 PM_walk(buttons_prev, maxspeed_mod);
1876 PM_air(buttons_prev, maxspeed_mod);
1879 if (!IS_OBSERVER(self))
1885 if (IS_ONGROUND(self))
1886 self.lastground = time;
1888 // conveyors: then break velocity again
1889 if(self.conveyor.state)
1890 self.velocity += self.conveyor.movedir;
1892 self.lastflags = self.flags;
1894 self.lastclassname = self.classname;
1898 void SV_PlayerPhysics(void)
1900 void CSQC_ClientMovement_PlayerMove_Frame(void)
1907 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1908 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1909 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);