2 #include "triggers/trigger/swamp.qh"
3 #include "triggers/trigger/jumppads.qh"
7 #include "../server/miscfunctions.qh"
10 bool Physics_Valid(string thecvar)
12 if(!autocvar_g_physics_clientselect) { return false; }
14 string l = strcat(" ", autocvar_g_physics_clientselect_options, " ");
16 if(strstrofs(l, strcat(" ", thecvar, " "), 0) >= 0)
22 float Physics_ClientOption(entity pl, string option)
24 if(Physics_Valid(pl.cvar_cl_physics))
26 string var = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
27 if(cvar_type(var) & CVAR_TYPEFLAG_EXISTS)
30 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
32 string var = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
33 if(cvar_type(var) & CVAR_TYPEFLAG_EXISTS)
36 return cvar(strcat("sv_", option));
39 void Physics_AddStats()
41 // static view offset and hitbox vectors
42 // networked for all you bandwidth pigs out there
43 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
44 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
45 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
46 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
47 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
48 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
50 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
51 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
52 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
53 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
54 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
55 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
56 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
57 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
58 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
59 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
60 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
61 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
63 // g_movementspeed hack
64 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
65 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
66 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
67 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
68 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
71 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
72 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
73 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
74 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
75 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
76 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
78 // hack to fix track_canjump
79 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
82 addstat(STAT_DOUBLEJUMP, AS_INT, stat_doublejump);
85 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
86 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_MIN, AS_FLOAT, stat_jumpspeedcap_min);
87 addstat(STAT_MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, AS_INT, stat_jumpspeedcap_disable_onramps);
90 addstat(STAT_MOVEVARS_FRICTION_ONLAND, AS_FLOAT, stat_sv_friction_on_land);
91 addstat(STAT_MOVEVARS_FRICTION_SLICK, AS_FLOAT, stat_sv_friction_slick);
92 addstat(STAT_GAMEPLAYFIX_EASIERWATERJUMP, AS_INT, stat_gameplayfix_easierwaterjump);
95 addstat(STAT_MOVEVARS_JUMPVELOCITY, AS_FLOAT, stat_sv_jumpvelocity);
96 addstat(STAT_MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, AS_FLOAT, stat_sv_airaccel_qw_stretchfactor);
97 addstat(STAT_MOVEVARS_MAXAIRSTRAFESPEED, AS_FLOAT, stat_sv_maxairstrafespeed);
98 addstat(STAT_MOVEVARS_MAXAIRSPEED, AS_FLOAT, stat_sv_maxairspeed);
99 addstat(STAT_MOVEVARS_AIRSTRAFEACCELERATE, AS_FLOAT, stat_sv_airstrafeaccelerate);
100 addstat(STAT_MOVEVARS_WARSOWBUNNY_TURNACCEL, AS_FLOAT, stat_sv_warsowbunny_turnaccel);
101 addstat(STAT_MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, AS_FLOAT, stat_sv_airaccel_sideways_friction);
102 addstat(STAT_MOVEVARS_AIRCONTROL, AS_FLOAT, stat_sv_aircontrol);
103 addstat(STAT_MOVEVARS_AIRCONTROL_POWER, AS_FLOAT, stat_sv_aircontrol_power);
104 addstat(STAT_MOVEVARS_AIRCONTROL_PENALTY, AS_FLOAT, stat_sv_aircontrol_penalty);
105 addstat(STAT_MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, AS_FLOAT, stat_sv_warsowbunny_airforwardaccel);
106 addstat(STAT_MOVEVARS_WARSOWBUNNY_TOPSPEED, AS_FLOAT, stat_sv_warsowbunny_topspeed);
107 addstat(STAT_MOVEVARS_WARSOWBUNNY_ACCEL, AS_FLOAT, stat_sv_warsowbunny_accel);
108 addstat(STAT_MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, AS_FLOAT, stat_sv_warsowbunny_backtosideratio);
109 addstat(STAT_MOVEVARS_FRICTION, AS_FLOAT, stat_sv_friction);
110 addstat(STAT_MOVEVARS_ACCELERATE, AS_FLOAT, stat_sv_accelerate);
111 addstat(STAT_MOVEVARS_STOPSPEED, AS_FLOAT, stat_sv_stopspeed);
112 addstat(STAT_MOVEVARS_AIRACCELERATE, AS_FLOAT, stat_sv_airaccelerate);
113 addstat(STAT_MOVEVARS_AIRSTOPACCELERATE, AS_FLOAT, stat_sv_airstopaccelerate);
115 addstat(STAT_GAMEPLAYFIX_UPVELOCITYCLEARSONGROUND, AS_INT, stat_gameplayfix_upvelocityclearsonground);
118 void Physics_UpdateStats(float maxspd_mod)
121 self.stat_pl_view_ofs = PL_VIEW_OFS;
122 self.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
124 self.stat_pl_min = PL_MIN;
125 self.stat_pl_max = PL_MAX;
126 self.stat_pl_crouch_min = PL_CROUCH_MIN;
127 self.stat_pl_crouch_max = PL_CROUCH_MAX;
130 self.stat_sv_airaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airaccel_qw"), maxspd_mod);
131 if(Physics_ClientOption(self, "airstrafeaccel_qw"))
132 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airstrafeaccel_qw"), maxspd_mod);
134 self.stat_sv_airstrafeaccel_qw = 0;
135 self.stat_sv_airspeedlimit_nonqw = Physics_ClientOption(self, "airspeedlimit_nonqw") * maxspd_mod;
136 self.stat_sv_maxspeed = Physics_ClientOption(self, "maxspeed") * maxspd_mod; // also slow walking
137 self.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
139 self.stat_doublejump = PHYS_DOUBLEJUMP;
141 self.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
142 self.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
143 self.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
144 self.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
145 self.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
146 self.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
148 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
149 self.stat_jumpspeedcap_max = PHYS_JUMPSPEEDCAP_MAX;
150 self.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
152 self.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
153 self.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
155 self.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
159 // fix some new settings
160 self.stat_sv_airaccel_qw_stretchfactor = Physics_ClientOption(self, "airaccel_qw_stretchfactor");
161 self.stat_sv_maxairstrafespeed = Physics_ClientOption(self, "maxairstrafespeed");
162 self.stat_sv_maxairspeed = Physics_ClientOption(self, "maxairspeed");
163 self.stat_sv_airstrafeaccelerate = Physics_ClientOption(self, "airstrafeaccelerate");
164 self.stat_sv_warsowbunny_turnaccel = Physics_ClientOption(self, "warsowbunny_turnaccel");
165 self.stat_sv_airaccel_sideways_friction = Physics_ClientOption(self, "airaccel_sideways_friction");
166 self.stat_sv_aircontrol = Physics_ClientOption(self, "aircontrol");
167 self.stat_sv_aircontrol_power = Physics_ClientOption(self, "aircontrol_power");
168 self.stat_sv_aircontrol_penalty = Physics_ClientOption(self, "aircontrol_penalty");
169 self.stat_sv_warsowbunny_airforwardaccel = Physics_ClientOption(self, "warsowbunny_airforwardaccel");
170 self.stat_sv_warsowbunny_topspeed = Physics_ClientOption(self, "warsowbunny_topspeed");
171 self.stat_sv_warsowbunny_accel = Physics_ClientOption(self, "warsowbunny_accel");
172 self.stat_sv_warsowbunny_backtosideratio = Physics_ClientOption(self, "warsowbunny_backtosideratio");
173 self.stat_sv_friction = Physics_ClientOption(self, "friction");
174 self.stat_sv_accelerate = Physics_ClientOption(self, "accelerate");
175 self.stat_sv_stopspeed = Physics_ClientOption(self, "stopspeed");
176 self.stat_sv_airaccelerate = Physics_ClientOption(self, "airaccelerate");
177 self.stat_sv_airstopaccelerate = Physics_ClientOption(self, "airstopaccelerate");
178 self.stat_sv_jumpvelocity = Physics_ClientOption(self, "jumpvelocity");
180 self.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
184 float IsMoveInDirection(vector mv, float ang) // key mix factor
186 if (mv_x == 0 && mv_y == 0)
187 return 0; // avoid division by zero
188 ang -= RAD2DEG * atan2(mv_y, mv_x);
189 ang = remainder(ang, 360) / 45;
190 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
193 float GeomLerp(float a, float lerp, float b)
195 return a == 0 ? (lerp < 1 ? 0 : b)
196 : b == 0 ? (lerp > 0 ? 0 : a)
197 : a * pow(fabs(b / a), lerp);
200 noref float pmove_waterjumptime;
202 const float unstick_count = 27;
203 vector unstick_offsets[unstick_count] =
205 // 1 no nudge (just return the original if this test passes)
208 ' 0.000 0.000 0.125', '0.000 0.000 -0.125',
209 '-0.125 0.000 0.000', '0.125 0.000 0.000',
210 ' 0.000 -0.125 0.000', '0.000 0.125 0.000',
211 // 4 diagonal flat nudges
212 '-0.125 -0.125 0.000', '0.125 -0.125 0.000',
213 '-0.125 0.125 0.000', '0.125 0.125 0.000',
214 // 8 diagonal upward nudges
215 '-0.125 0.000 0.125', '0.125 0.000 0.125',
216 ' 0.000 -0.125 0.125', '0.000 0.125 0.125',
217 '-0.125 -0.125 0.125', '0.125 -0.125 0.125',
218 '-0.125 0.125 0.125', '0.125 0.125 0.125',
219 // 8 diagonal downward nudges
220 '-0.125 0.000 -0.125', '0.125 0.000 -0.125',
221 ' 0.000 -0.125 -0.125', '0.000 0.125 -0.125',
222 '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
223 '-0.125 0.125 -0.125', '0.125 0.125 -0.125',
226 void PM_ClientMovement_Unstick()
229 for (i = 0; i < unstick_count; i++)
231 vector neworigin = unstick_offsets[i] + self.origin;
232 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
233 if (!trace_startsolid)
235 setorigin(self, neworigin);
241 void PM_ClientMovement_UpdateStatus(bool ground)
243 // make sure player is not stuck
244 PM_ClientMovement_Unstick();
247 if (PHYS_INPUT_BUTTON_CROUCH(self))
249 // wants to crouch, this always works..
250 if (!IS_DUCKED(self))
255 // wants to stand, if currently crouching we need to check for a
259 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
260 if (!trace_startsolid)
266 vector origin1 = self.origin + '0 0 1';
267 vector origin2 = self.origin - '0 0 1';
271 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
272 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
276 // this code actually "predicts" an impact; so let's clip velocity first
277 float f = self.velocity * trace_plane_normal;
278 self.velocity -= f * trace_plane_normal;
281 UNSET_ONGROUND(self);
284 // set watertype/waterlevel
285 origin1 = self.origin;
286 origin1_z += self.mins_z + 1;
287 self.waterlevel = WATERLEVEL_NONE;
289 int thepoint = pointcontents(origin1);
291 self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
295 self.waterlevel = WATERLEVEL_WETFEET;
296 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
297 thepoint = pointcontents(origin1);
298 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
300 self.waterlevel = WATERLEVEL_SWIMMING;
301 origin1_z = self.origin_z + 22;
302 thepoint = pointcontents(origin1);
303 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
304 self.waterlevel = WATERLEVEL_SUBMERGED;
308 if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
309 pmove_waterjumptime = 0;
312 void PM_ClientMovement_Move()
319 vector currentorigin2;
321 vector primalvelocity;
323 vector trace1_endpos = '0 0 0';
324 vector trace2_endpos = '0 0 0';
325 vector trace3_endpos = '0 0 0';
326 float trace1_fraction = 0;
327 float trace2_fraction = 0;
328 float trace3_fraction = 0;
329 vector trace1_plane_normal = '0 0 0';
330 vector trace2_plane_normal = '0 0 0';
331 vector trace3_plane_normal = '0 0 0';
334 PM_ClientMovement_UpdateStatus(false);
335 primalvelocity = self.velocity;
336 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
338 neworigin = self.origin + t * self.velocity;
339 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
340 trace1_endpos = trace_endpos;
341 trace1_fraction = trace_fraction;
342 trace1_plane_normal = trace_plane_normal;
343 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
345 // may be a step or wall, try stepping up
346 // first move forward at a higher level
347 currentorigin2 = self.origin;
348 currentorigin2_z += PHYS_STEPHEIGHT;
349 neworigin2 = neworigin;
350 neworigin2_z += PHYS_STEPHEIGHT;
351 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
352 trace2_endpos = trace_endpos;
353 trace2_fraction = trace_fraction;
354 trace2_plane_normal = trace_plane_normal;
355 if(!trace_startsolid)
357 // then move down from there
358 currentorigin2 = trace2_endpos;
359 neworigin2 = trace2_endpos;
360 neworigin2_z = self.origin_z;
361 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
362 trace3_endpos = trace_endpos;
363 trace3_fraction = trace_fraction;
364 trace3_plane_normal = trace_plane_normal;
365 // accept the new trace if it made some progress
366 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
368 trace1_endpos = trace2_endpos;
369 trace1_fraction = trace2_fraction;
370 trace1_plane_normal = trace2_plane_normal;
371 trace1_endpos = trace3_endpos;
376 // check if it moved at all
377 if(trace1_fraction >= 0.001)
378 setorigin(self, trace1_endpos);
380 // check if it moved all the way
381 if(trace1_fraction == 1)
384 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
385 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
386 // this got commented out in a change that supposedly makes the code match QW better
387 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
388 if(trace1_plane_normal_z > 0.7)
391 t -= t * trace1_fraction;
393 f = (self.velocity * trace1_plane_normal);
394 self.velocity = self.velocity + -f * trace1_plane_normal;
396 if(pmove_waterjumptime > 0)
397 self.velocity = primalvelocity;
401 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
403 float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
407 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(self), 1);
409 float zspeed = self.velocity_z;
411 float xyspeed = vlen(self.velocity);
412 self.velocity = normalize(self.velocity);
414 float dot = self.velocity * wishdir;
416 if (dot > 0) // we can't change direction while slowing down
418 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
419 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
420 k *= PHYS_AIRCONTROL;
421 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
424 self.velocity = self.velocity * xyspeed;
425 self.velocity_z = zspeed;
428 float AdjustAirAccelQW(float accelqw, float factor)
430 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
433 // example config for alternate speed clamping:
434 // sv_airaccel_qw 0.8
435 // sv_airaccel_sideways_friction 0
436 // prvm_globalset server speedclamp_mode 1
438 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
440 float speedclamp = stretchfactor > 0 ? stretchfactor
441 : accelqw < 0 ? 1 // full clamping, no stretch
444 accelqw = fabs(accelqw);
446 if (GAMEPLAYFIX_Q2AIRACCELERATE)
447 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
449 float vel_straight = self.velocity * wishdir;
450 float vel_z = self.velocity_z;
451 vector vel_xy = vec2(self.velocity);
452 vector vel_perpend = vel_xy - vel_straight * wishdir;
454 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
456 float vel_xy_current = vlen(vel_xy);
458 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
459 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
460 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
461 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
462 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
464 if (sidefric < 0 && (vel_perpend*vel_perpend))
465 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
467 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
468 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
470 // 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_xy * vel_xy
473 // obviously, this cannot be
479 vel_perpend *= max(fmin, f);
483 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
485 vel_xy = vel_straight * wishdir + vel_perpend;
489 float vel_xy_preclamp;
490 vel_xy_preclamp = vlen(vel_xy);
491 if (vel_xy_preclamp > 0) // prevent division by zero
493 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
494 if (vel_xy_current < vel_xy_preclamp)
495 vel_xy *= (vel_xy_current / vel_xy_preclamp);
499 self.velocity = vel_xy + vel_z * '0 0 1';
502 void PM_AirAccelerate(vector wishdir, float wishspeed)
507 vector curvel = self.velocity;
509 float curspeed = vlen(curvel);
511 if (wishspeed > curspeed * 1.01)
512 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
515 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
516 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
518 vector wishvel = wishdir * wishspeed;
519 vector acceldir = wishvel - curvel;
520 float addspeed = vlen(acceldir);
521 acceldir = normalize(acceldir);
523 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
525 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
527 vector curdir = normalize(curvel);
528 float dot = acceldir * curdir;
530 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
533 self.velocity += accelspeed * acceldir;
541 When you press the jump key
542 returns true if handled
545 bool PlayerJump (void)
547 if (PHYS_FROZEN(self))
548 return true; // no jumping in freezetag when frozen
551 if (self.player_blocked)
552 return true; // no jumping while blocked
555 bool doublejump = false;
556 float mjumpheight = PHYS_JUMPVELOCITY;
558 player_multijump = doublejump;
559 player_jumpheight = mjumpheight;
561 if (MUTATOR_CALLHOOK(PlayerJump))
563 if(PM_multijump_checkjump())
567 doublejump = player_multijump;
568 mjumpheight = player_jumpheight;
572 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
573 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
577 // we MUST clip velocity here!
579 f = self.velocity * trace_plane_normal;
581 self.velocity -= f * trace_plane_normal;
585 if (self.waterlevel >= WATERLEVEL_SWIMMING)
587 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
592 if (!IS_ONGROUND(self))
593 return IS_JUMP_HELD(self);
595 if (PHYS_TRACK_CANJUMP(self))
596 if (IS_JUMP_HELD(self))
599 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
600 // velocity bounds. Final velocity is bound between (jumpheight *
601 // min + jumpheight) and (jumpheight * max + jumpheight);
603 if(PHYS_JUMPSPEEDCAP_MIN)
605 float minjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MIN;
607 if (self.velocity_z < minjumpspeed)
608 mjumpheight += minjumpspeed - self.velocity_z;
611 if(PHYS_JUMPSPEEDCAP_MAX)
613 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
614 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
616 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
618 float maxjumpspeed = mjumpheight * PHYS_JUMPSPEEDCAP_MAX;
620 if (self.velocity_z > maxjumpspeed)
621 mjumpheight -= self.velocity_z - maxjumpspeed;
625 if (!WAS_ONGROUND(self))
628 if(autocvar_speedmeter)
629 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
631 if(self.lastground < time - 0.3)
633 self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
634 self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
637 if(self.jumppadcount > 1)
638 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
639 self.jumppadcount = 0;
643 self.velocity_z += mjumpheight;
645 UNSET_ONGROUND(self);
650 self.oldvelocity_z = self.velocity_z;
652 animdecide_setaction(self, ANIMACTION_JUMP, true);
654 if (autocvar_g_jump_grunt)
655 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
660 void CheckWaterJump()
662 // check for a jump-out-of-water
663 makevectors(self.v_angle);
664 vector start = self.origin;
667 normalize(v_forward);
668 vector end = start + v_forward*24;
669 traceline (start, end, true, self);
670 if (trace_fraction < 1)
672 start_z = start_z + self.maxs_z - 8;
673 end = start + v_forward*24;
674 self.movedir = trace_plane_normal * -50;
675 traceline(start, end, true, self);
676 if (trace_fraction == 1)
677 { // open at eye level
678 self.velocity_z = 225;
679 self.flags |= FL_WATERJUMP;
682 self.teleport_time = time + 2; // safety net
684 pmove_waterjumptime = time + 2;
692 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
694 float autocvar_cl_jetpack_jump;
695 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
697 .float jetpack_stopped;
698 // Hack: shouldn't need to know about this
699 .float multijump_count;
700 void CheckPlayerJump()
703 float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
705 if (JETPACK_JUMP(self) < 2)
706 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
708 if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
710 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
711 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
712 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
714 if (!(ITEMS_STAT(self) & IT_JETPACK)) { }
715 else if (self.jetpack_stopped) { }
719 if (was_flying) // TODO: ran out of fuel message
720 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
722 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
724 self.jetpack_stopped = true;
725 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
727 else if (activate && !PHYS_FROZEN(self))
728 ITEMS_STAT(self) |= IT_USING_JETPACK;
732 self.jetpack_stopped = false;
733 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
735 if (!PHYS_INPUT_BUTTON_JUMP(self))
736 UNSET_JUMP_HELD(self);
738 if (self.waterlevel == WATERLEVEL_SWIMMING)
742 float racecar_angle(float forward, float down)
750 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
752 float angle_mult = forward / (800 + forward);
755 return ret * angle_mult + 360 * (1 - angle_mult);
757 return ret * angle_mult;
760 void RaceCarPhysics()
763 // using this move type for "big rigs"
764 // the engine does not push the entity!
768 vector angles_save = self.angles;
769 float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
770 float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
772 if (g_bugrigs_reverse_speeding)
776 // back accel is DIGITAL
777 // to prevent speedhack
787 makevectors(self.angles); // new forward direction!
789 if (IS_ONGROUND(self) || g_bugrigs_air_steering)
791 float myspeed = self.velocity * v_forward;
792 float upspeed = self.velocity * v_up;
794 // responsiveness factor for steering and acceleration
795 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
796 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
799 if (myspeed < 0 && g_bugrigs_reverse_spinning)
800 steerfactor = -myspeed * g_bugrigs_steer;
802 steerfactor = -myspeed * f * g_bugrigs_steer;
805 if (myspeed < 0 && g_bugrigs_reverse_speeding)
806 accelfactor = g_bugrigs_accel;
808 accelfactor = f * g_bugrigs_accel;
809 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
815 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
819 if (!g_bugrigs_reverse_speeding)
820 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
827 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
831 if (g_bugrigs_reverse_stopping)
834 myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
837 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
838 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
840 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
841 makevectors(self.angles); // new forward direction!
843 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
845 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
849 float myspeed = vlen(self.velocity);
851 // responsiveness factor for steering and acceleration
852 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
853 float steerfactor = -myspeed * f;
854 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
856 rigvel = self.velocity;
857 makevectors(self.angles); // new forward direction!
860 rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
861 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
862 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
863 //MAXIMA: solve(total_acceleration(v) = 0, v);
865 if (g_bugrigs_planar_movement)
867 vector rigvel_xy, neworigin, up;
870 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
871 rigvel_xy = vec2(rigvel);
873 if (g_bugrigs_planar_movement_car_jumping)
876 mt = MOVE_NOMONSTERS;
878 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
879 up = trace_endpos - self.origin;
881 // BUG RIGS: align the move to the surface instead of doing collision testing
883 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
886 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
888 if (trace_fraction < 0.5)
891 neworigin = self.origin;
894 neworigin = trace_endpos;
896 if (trace_fraction < 1)
898 // now set angles_x so that the car points parallel to the surface
899 self.angles = vectoangles(
900 '1 0 0' * v_forward_x * trace_plane_normal_z
902 '0 1 0' * v_forward_y * trace_plane_normal_z
904 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
910 // now set angles_x so that the car points forward, but is tilted in velocity direction
911 UNSET_ONGROUND(self);
914 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
915 self.movetype = MOVETYPE_NOCLIP;
919 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
920 self.velocity = rigvel;
921 self.movetype = MOVETYPE_FLY;
925 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
926 if (trace_fraction != 1)
928 self.angles = vectoangles2(
929 '1 0 0' * v_forward_x * trace_plane_normal_z
931 '0 1 0' * v_forward_y * trace_plane_normal_z
933 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
941 vel_local_x = v_forward * self.velocity;
942 vel_local_y = v_right * self.velocity;
943 vel_local_z = v_up * self.velocity;
945 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
946 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
950 vector vf1, vu1, smoothangles;
951 makevectors(self.angles);
952 float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
957 makevectors(angles_save);
958 vf1 = vf1 + v_forward * (1 - f);
959 vu1 = vu1 + v_up * (1 - f);
960 smoothangles = vectoangles2(vf1, vu1);
961 self.angles_x = -smoothangles_x;
962 self.angles_z = smoothangles_z;
966 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
967 .float specialcommand_pos;
968 void SpecialCommand()
974 if (!CheatImpulse(99))
975 print("A hollow voice says \"Plugh\".\n");
980 float PM_check_keepaway(void)
983 return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
989 void PM_check_race_movetime(void)
992 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
993 float f = floor(self.race_movetime_frac);
994 self.race_movetime_frac -= f;
995 self.race_movetime_count += f;
996 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
1000 float PM_check_specialcommand(float buttons)
1006 else if (buttons == 1)
1008 else if (buttons == 2)
1010 else if (buttons == 128)
1012 else if (buttons == 256)
1014 else if (buttons == 512)
1016 else if (buttons == 1024)
1021 if (c == substring(specialcommand, self.specialcommand_pos, 1))
1023 self.specialcommand_pos += 1;
1024 if (self.specialcommand_pos >= strlen(specialcommand))
1026 self.specialcommand_pos = 0;
1031 else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
1032 self.specialcommand_pos = 0;
1037 void PM_check_nickspam(void)
1040 if (time >= self.nickspamtime)
1042 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
1044 // slight annoyance for nick change scripts
1045 self.movement = -1 * self.movement;
1046 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
1048 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!
1050 self.v_angle_x = random() * 360;
1051 self.v_angle_y = random() * 360;
1052 // at least I'm not forcing retardedview by also assigning to angles_z
1053 self.fixangle = true;
1059 void PM_check_punch()
1062 if (self.punchangle != '0 0 0')
1064 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
1066 self.punchangle = normalize(self.punchangle) * f;
1068 self.punchangle = '0 0 0';
1071 if (self.punchvector != '0 0 0')
1073 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1075 self.punchvector = normalize(self.punchvector) * f;
1077 self.punchvector = '0 0 0';
1082 void PM_check_spider(void)
1085 if (time >= self.spider_slowness)
1087 PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1088 PHYS_MAXAIRSPEED(self) *= 0.5;
1089 PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
1090 PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
1094 // predict frozen movement, as frozen players CAN move in some cases
1095 void PM_check_frozen(void)
1097 if (!PHYS_FROZEN(self))
1099 if (PHYS_DODGING_FROZEN
1101 && IS_REAL_CLIENT(self)
1105 self.movement_x = bound(-5, self.movement.x, 5);
1106 self.movement_y = bound(-5, self.movement.y, 5);
1107 self.movement_z = bound(-5, self.movement.z, 5);
1110 self.movement = '0 0 0';
1112 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1113 if (pointcontents(midpoint) == CONTENT_WATER)
1115 self.velocity = self.velocity * 0.5;
1117 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1118 self.velocity_z = 200;
1122 void PM_check_hitground()
1125 if (IS_ONGROUND(self))
1126 if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1130 if (self.waterlevel < WATERLEVEL_SWIMMING)
1131 if (time >= self.ladder_time)
1134 self.nextstep = time + 0.3 + random() * 0.1;
1135 trace_dphitq3surfaceflags = 0;
1136 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1137 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1139 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1140 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1142 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1149 void PM_check_blocked(void)
1152 if (!self.player_blocked)
1154 self.movement = '0 0 0';
1155 self.disableclientprediction = 1;
1160 float speedaward_lastsent;
1161 float speedaward_lastupdate;
1163 void PM_check_race(void)
1166 if(!(g_cts || g_race))
1168 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1170 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1171 speedaward_holder = self.netname;
1172 speedaward_uid = self.crypto_idfp;
1173 speedaward_lastupdate = time;
1175 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1177 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1178 race_send_speedaward(MSG_ALL);
1179 speedaward_lastsent = speedaward_speed;
1180 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1182 speedaward_alltimebest = speedaward_speed;
1183 speedaward_alltimebest_holder = speedaward_holder;
1184 speedaward_alltimebest_uid = speedaward_uid;
1185 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1186 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1187 race_send_speedaward_alltimebest(MSG_ALL);
1193 void PM_check_vortex(void)
1197 float xyspeed = vlen(vec2(self.velocity));
1198 if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1200 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1201 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1202 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1203 // add the extra charge
1204 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1209 void PM_fly(float maxspd_mod)
1211 // noclipping or flying
1212 UNSET_ONGROUND(self);
1214 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1215 makevectors(self.v_angle);
1216 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1217 vector wishvel = v_forward * self.movement.x
1218 + v_right * self.movement.y
1219 + '0 0 1' * self.movement.z;
1221 vector wishdir = normalize(wishvel);
1222 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1224 if (time >= self.teleport_time)
1226 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1227 PM_ClientMovement_Move();
1230 void PM_swim(float maxspd_mod)
1233 UNSET_ONGROUND(self);
1235 float jump = PHYS_INPUT_BUTTON_JUMP(self);
1236 // water jump only in certain situations
1237 // this mimics quakeworld code
1238 if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1240 vector yawangles = '0 1 0' * self.v_angle.y;
1241 makevectors(yawangles);
1242 vector forward = v_forward;
1243 vector spot = self.origin + 24 * forward;
1245 traceline(spot, spot, MOVE_NOMONSTERS, self);
1246 if (trace_startsolid)
1249 traceline(spot, spot, MOVE_NOMONSTERS, self);
1250 if (!trace_startsolid)
1252 self.velocity = forward * 50;
1253 self.velocity_z = 310;
1254 pmove_waterjumptime = 2;
1255 UNSET_ONGROUND(self);
1256 SET_JUMP_HELD(self);
1260 makevectors(self.v_angle);
1261 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1262 vector wishvel = v_forward * self.movement.x
1263 + v_right * self.movement.y
1264 + '0 0 1' * self.movement.z;
1265 if (wishvel == '0 0 0')
1266 wishvel = '0 0 -60'; // drift towards bottom
1268 vector wishdir = normalize(wishvel);
1269 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1271 if (IS_DUCKED(self))
1274 // if (pmove_waterjumptime <= 0) // TODO: use
1277 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1278 f = min(max(0, f), 1);
1281 f = wishspeed - self.velocity * wishdir;
1284 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1285 self.velocity += accelspeed * wishdir;
1288 // holding jump button swims upward slowly
1292 if (self.watertype & CONTENT_LAVA)
1293 self.velocity_z = 50;
1294 else if (self.watertype & CONTENT_SLIME)
1295 self.velocity_z = 80;
1298 if (IS_NEXUIZ_DERIVED(gamemode))
1300 self.velocity_z = 200;
1303 self.velocity_z = 100;
1308 // water acceleration
1309 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1310 PM_ClientMovement_Move();
1313 void PM_ladder(float maxspd_mod)
1315 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1316 UNSET_ONGROUND(self);
1319 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1320 if (PHYS_ENTGRAVITY(self))
1321 g *= PHYS_ENTGRAVITY(self);
1322 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1325 self.velocity_z += g;
1328 self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1329 makevectors(self.v_angle);
1330 //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1331 vector wishvel = v_forward * self.movement_x
1332 + v_right * self.movement_y
1333 + '0 0 1' * self.movement_z;
1334 self.velocity_z += g;
1335 if (self.ladder_entity.classname == "func_water")
1337 float f = vlen(wishvel);
1338 if (f > self.ladder_entity.speed)
1339 wishvel *= (self.ladder_entity.speed / f);
1341 self.watertype = self.ladder_entity.skin;
1342 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1343 if ((self.origin_z + self.view_ofs_z) < f)
1344 self.waterlevel = WATERLEVEL_SUBMERGED;
1345 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1346 self.waterlevel = WATERLEVEL_SWIMMING;
1347 else if ((self.origin_z + self.mins_z + 1) < f)
1348 self.waterlevel = WATERLEVEL_WETFEET;
1351 self.waterlevel = WATERLEVEL_NONE;
1352 self.watertype = CONTENT_EMPTY;
1356 vector wishdir = normalize(wishvel);
1357 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1359 if (time >= self.teleport_time)
1361 // water acceleration
1362 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1363 PM_ClientMovement_Move();
1366 void PM_jetpack(float maxspd_mod)
1368 //makevectors(self.v_angle.y * '0 1 0');
1369 makevectors(self.v_angle);
1370 vector wishvel = v_forward * self.movement_x
1371 + v_right * self.movement_y;
1372 // add remaining speed as Z component
1373 float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1374 // fix speedhacks :P
1375 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1376 // add the unused velocity as up component
1379 // if (self.BUTTON_JUMP)
1380 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1382 // it is now normalized, so...
1383 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1384 float a_up = PHYS_JETPACK_ACCEL_UP;
1385 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1387 wishvel_x *= a_side;
1388 wishvel_y *= a_side;
1393 //////////////////////////////////////////////////////////////////////////////////////
1394 // finding the maximum over all vectors of above form
1395 // with wishvel having an absolute value of 1
1396 //////////////////////////////////////////////////////////////////////////////////////
1397 // we're finding the maximum over
1398 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1399 // for z in the range from -1 to 1
1400 //////////////////////////////////////////////////////////////////////////////////////
1401 // maximum is EITHER attained at the single extreme point:
1402 float a_diff = a_side * a_side - a_up * a_up;
1406 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1407 if (f > -1 && f < 1) // can it be attained?
1409 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1410 //print("middle\n");
1413 // OR attained at z = 1:
1414 f = (a_up + a_add) * (a_up + a_add);
1420 // OR attained at z = -1:
1421 f = (a_up - a_add) * (a_up - a_add);
1425 //print("bottom\n");
1428 //////////////////////////////////////////////////////////////////////////////////////
1430 //print("best possible acceleration: ", ftos(best), "\n");
1433 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1434 if (wishvel_z - PHYS_GRAVITY > 0)
1435 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1437 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1440 fvel = vlen(wishvel);
1443 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1445 fvel = min(1, vlen(wishvel) / best);
1446 if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1447 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1451 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1453 if (f > 0 && wishvel != '0 0 0')
1455 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1456 UNSET_ONGROUND(self);
1459 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1460 self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1462 ITEMS_STAT(self) |= IT_USING_JETPACK;
1464 // jetpack also inhibits health regeneration, but only for 1 second
1465 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1470 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1471 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1472 self.velocity_z -= g * 0.5;
1474 self.velocity_z -= g;
1475 PM_ClientMovement_Move();
1476 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1477 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1478 self.velocity_z -= g * 0.5;
1482 void PM_walk(float buttons_prev, float maxspd_mod)
1484 if (!WAS_ONGROUND(self))
1487 if (autocvar_speedmeter)
1488 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1490 if (self.lastground < time - 0.3)
1491 self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1493 if (self.jumppadcount > 1)
1494 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1495 self.jumppadcount = 0;
1500 makevectors(self.v_angle.y * '0 1 0');
1501 vector wishvel = v_forward * self.movement.x
1502 + v_right * self.movement.y;
1504 vector wishdir = normalize(wishvel);
1505 float wishspeed = vlen(wishvel);
1507 wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1508 if (IS_DUCKED(self))
1511 // apply edge friction
1512 float f = vlen(vec2(self.velocity));
1516 trace_dphitq3surfaceflags = 0;
1517 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1518 // TODO: apply edge friction
1519 // apply ground friction
1520 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1521 realfriction = PHYS_FRICTION_SLICK;
1523 realfriction = PHYS_FRICTION;
1525 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1529 Mathematical analysis time!
1531 Our goal is to invert this mess.
1533 For the two cases we get:
1534 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1535 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1536 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1538 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1539 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1541 These cases would be chosen ONLY if:
1543 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1544 v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1546 v0 >= PHYS_STOPSPEED
1547 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1548 v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1551 float addspeed = wishspeed - self.velocity * wishdir;
1554 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1555 self.velocity += accelspeed * wishdir;
1557 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1558 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1559 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1560 if (self.velocity * self.velocity)
1561 PM_ClientMovement_Move();
1562 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1563 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1564 self.velocity_z -= g * 0.5;
1567 void PM_air(float buttons_prev, float maxspd_mod)
1569 makevectors(self.v_angle.y * '0 1 0');
1570 vector wishvel = v_forward * self.movement.x
1571 + v_right * self.movement.y;
1573 vector wishdir = normalize(wishvel);
1574 float wishspeed = vlen(wishvel);
1577 if (time >= self.teleport_time)
1579 if (pmove_waterjumptime <= 0)
1582 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1584 // apply air speed limit
1585 float airaccelqw = PHYS_AIRACCEL_QW(self);
1586 float wishspeed0 = wishspeed;
1587 wishspeed = min(wishspeed, maxairspd);
1588 if (IS_DUCKED(self))
1590 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1592 float accelerating = (self.velocity * wishdir > 0);
1593 float wishspeed2 = wishspeed;
1596 if (PHYS_AIRSTOPACCELERATE)
1598 vector curdir = normalize(vec2(self.velocity));
1599 airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1601 // note that for straight forward jumping:
1602 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1603 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1605 // dv/dt = accel * maxspeed (when slow)
1606 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1607 // log dv/dt = logaccel + logmaxspeed (when slow)
1608 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1609 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1610 if (PHYS_MAXAIRSTRAFESPEED)
1611 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1612 if (PHYS_AIRSTRAFEACCELERATE(self))
1613 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1614 if (PHYS_AIRSTRAFEACCEL_QW(self))
1616 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1618 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1621 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1622 PM_AirAccelerate(wishdir, wishspeed2);
1624 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1626 if (PHYS_AIRCONTROL)
1627 CPM_PM_Aircontrol(wishdir, wishspeed2);
1629 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1630 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1631 self.velocity_z -= g * 0.5;
1633 self.velocity_z -= g;
1634 PM_ClientMovement_Move();
1635 if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1636 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1637 self.velocity_z -= g * 0.5;
1640 // used for calculating airshots
1641 bool IsFlying(entity a)
1645 if(a.waterlevel >= WATERLEVEL_SWIMMING)
1647 traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1648 if(trace_fraction < 1)
1655 int buttons = PHYS_INPUT_BUTTON_MASK(self);
1657 self.items = getstati(STAT_ITEMS, 0, 24);
1659 self.movement = PHYS_INPUT_MOVEVALUES(self);
1661 vector oldv_angle = self.v_angle;
1662 vector oldangles = self.angles; // we need to save these, as they're abused by other code
1663 self.v_angle = PHYS_INPUT_ANGLES(self);
1664 self.angles = PHYS_WORLD_ANGLES(self);
1666 self.team = myteam + 1; // is this correct?
1667 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1668 UNSET_JUMP_HELD(self); // canjump = true
1669 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1671 PM_ClientMovement_UpdateStatus(true);
1676 WarpZone_PlayerPhysics_FixVAngle();
1678 float maxspeed_mod = 1;
1679 maxspeed_mod *= PM_check_keepaway();
1680 maxspeed_mod *= PHYS_HIGHSPEED;
1683 Physics_UpdateStats(maxspeed_mod);
1685 if (self.PlayerPhysplug)
1686 if (self.PlayerPhysplug())
1690 PM_check_race_movetime();
1692 anticheat_physics();
1695 if (PM_check_specialcommand(buttons))
1700 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1701 self.parm_idlesince = time;
1704 int buttons_prev = self.buttons_old;
1705 self.buttons_old = buttons;
1706 self.movement_old = self.movement;
1707 self.v_angle_old = self.v_angle;
1709 PM_check_nickspam();
1713 if (IS_BOT_CLIENT(self))
1715 if (playerdemo_read())
1720 if (IS_PLAYER(self))
1724 if (self.race_penalty)
1725 if (time > self.race_penalty)
1726 self.race_penalty = 0;
1729 bool not_allowed_to_move = false;
1731 if (self.race_penalty)
1732 not_allowed_to_move = true;
1735 if (time < game_starttime)
1736 not_allowed_to_move = true;
1739 if (not_allowed_to_move)
1741 self.velocity = '0 0 0';
1742 self.movetype = MOVETYPE_NONE;
1744 self.disableclientprediction = 2;
1748 else if (self.disableclientprediction == 2)
1750 if (self.movetype == MOVETYPE_NONE)
1751 self.movetype = MOVETYPE_WALK;
1752 self.disableclientprediction = 0;
1758 if (self.movetype == MOVETYPE_NONE)
1761 // when we get here, disableclientprediction cannot be 2
1762 self.disableclientprediction = 0;
1774 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1776 // conveyors: first fix velocity
1777 if (self.conveyor.state)
1778 self.velocity -= self.conveyor.movedir;
1781 MUTATOR_CALLHOOK(PlayerPhysics);
1787 // float forcedodge = 1;
1790 // PM_dodging_checkpressedkeys();
1793 // PM_ClientMovement_Move();
1798 if (!IS_PLAYER(self))
1800 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1801 if (!self.spectatorspeed)
1802 self.spectatorspeed = maxspeed_mod;
1803 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1805 if (self.lastclassname != "player")
1807 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1808 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1809 else if (self.impulse == 11)
1810 self.spectatorspeed = maxspeed_mod;
1811 else if (self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1812 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1813 else if (self.impulse >= 1 && self.impulse <= 9)
1814 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1815 } // otherwise just clear
1818 maxspeed_mod = self.spectatorspeed;
1821 float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1822 if(self.speed != spd)
1825 string temps = ftos(spd);
1826 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1827 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1828 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1829 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1835 // handle water here
1836 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1837 if(pointcontents(midpoint) == CONTENT_WATER)
1839 self.velocity = self.velocity * 0.5;
1842 //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1843 //{ self.velocity_z = 70; }
1849 if (!self.fixangle && !g_bugrigs)
1850 self.angles = '0 1 0' * self.v_angle.y;
1853 PM_check_hitground();
1858 if (IS_PLAYER(self))
1861 if (self.flags & FL_WATERJUMP)
1863 self.velocity_x = self.movedir_x;
1864 self.velocity_y = self.movedir_y;
1865 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1867 self.flags &= ~FL_WATERJUMP;
1868 self.teleport_time = 0;
1873 else if (g_bugrigs && IS_PLAYER(self))
1877 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS(self) & BUFF_FLIGHT))
1878 PM_fly(maxspeed_mod);
1880 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1881 PM_swim(maxspeed_mod);
1883 else if (time < self.ladder_time)
1884 PM_ladder(maxspeed_mod);
1886 else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1887 PM_jetpack(maxspeed_mod);
1889 else if (IS_ONGROUND(self))
1890 PM_walk(buttons_prev, maxspeed_mod);
1893 PM_air(buttons_prev, maxspeed_mod);
1896 if (!IS_OBSERVER(self))
1902 if (IS_ONGROUND(self))
1903 self.lastground = time;
1905 // conveyors: then break velocity again
1906 if(self.conveyor.state)
1907 self.velocity += self.conveyor.movedir;
1909 self.lastflags = self.flags;
1911 self.lastclassname = self.classname;
1914 self.v_angle = oldv_angle;
1915 self.angles = oldangles;
1920 void SV_PlayerPhysics(void)
1922 void CSQC_ClientMovement_PlayerMove_Frame(void)
1929 ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1930 (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1931 ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);