2 #include "triggers/include.qh"
7 #include "../server/miscfunctions.qh"
8 #include "triggers/trigger/viewloc.qh"
10 // client side physics
11 bool Physics_Valid(string thecvar)
13 return autocvar_g_physics_clientselect && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 float Physics_ClientOption(entity this, string option)
18 if(Physics_Valid(this.cvar_cl_physics))
20 string s = sprintf("g_physics_%s_%s", this.cvar_cl_physics, option);
21 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
24 if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
26 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
27 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
30 return cvar(strcat("sv_", option));
33 void Physics_AddStats()
35 // static view offset and hitbox vectors
36 // networked for all you bandwidth pigs out there
37 addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
38 addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
39 addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
40 addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
41 addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
42 addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
44 addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
45 addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
46 addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
47 addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
48 addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
49 addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
50 addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
51 addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
52 addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
53 addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
54 addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
55 addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
57 // g_movementspeed hack
58 addstat(STAT_MOVEVARS_AIRSPEEDLIMIT_NONQW, AS_FLOAT, stat_sv_airspeedlimit_nonqw);
59 addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
60 addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
61 addstat(STAT_MOVEVARS_AIRSTRAFEACCEL_QW, AS_FLOAT, stat_sv_airstrafeaccel_qw);
62 addstat(STAT_MOVEVARS_HIGHSPEED, AS_FLOAT, stat_movement_highspeed);
65 addstat(STAT_JETPACK_ACCEL_SIDE, AS_FLOAT, stat_jetpack_accel_side);
66 addstat(STAT_JETPACK_ACCEL_UP, AS_FLOAT, stat_jetpack_accel_up);
67 addstat(STAT_JETPACK_ANTIGRAVITY, AS_FLOAT, stat_jetpack_antigravity);
68 addstat(STAT_JETPACK_FUEL, AS_FLOAT, stat_jetpack_fuel);
69 addstat(STAT_JETPACK_MAXSPEED_UP, AS_FLOAT, stat_jetpack_maxspeed_up);
70 addstat(STAT_JETPACK_MAXSPEED_SIDE, AS_FLOAT, stat_jetpack_maxspeed_side);
72 // hack to fix track_canjump
73 addstat(STAT_MOVEVARS_CL_TRACK_CANJUMP, AS_INT, cvar_cl_movement_track_canjump);
74 addstat(STAT_MOVEVARS_TRACK_CANJUMP, AS_INT, stat_sv_track_canjump);
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(entity this, float maxspd_mod)
111 this.stat_pl_view_ofs = PL_VIEW_OFS;
112 this.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
114 this.stat_pl_min = PL_MIN;
115 this.stat_pl_max = PL_MAX;
116 this.stat_pl_crouch_min = PL_CROUCH_MIN;
117 this.stat_pl_crouch_max = PL_CROUCH_MAX;
120 this.stat_sv_airaccel_qw = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw"), maxspd_mod);
121 if(Physics_ClientOption(this, "airstrafeaccel_qw"))
122 this.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw"), maxspd_mod);
124 this.stat_sv_airstrafeaccel_qw = 0;
125 this.stat_sv_airspeedlimit_nonqw = Physics_ClientOption(this, "airspeedlimit_nonqw") * maxspd_mod;
126 this.stat_sv_maxspeed = Physics_ClientOption(this, "maxspeed") * maxspd_mod; // also slow walking
127 this.stat_movement_highspeed = PHYS_HIGHSPEED; // TODO: remove this!
129 this.stat_jetpack_antigravity = PHYS_JETPACK_ANTIGRAVITY;
130 this.stat_jetpack_accel_up = PHYS_JETPACK_ACCEL_UP;
131 this.stat_jetpack_accel_side = PHYS_JETPACK_ACCEL_SIDE;
132 this.stat_jetpack_maxspeed_side = PHYS_JETPACK_MAXSPEED_SIDE;
133 this.stat_jetpack_maxspeed_up = PHYS_JETPACK_MAXSPEED_UP;
134 this.stat_jetpack_fuel = PHYS_JETPACK_FUEL;
136 this.stat_jumpspeedcap_disable_onramps = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
138 this.stat_sv_friction_on_land = PHYS_FRICTION_ONLAND;
139 this.stat_sv_friction_slick = PHYS_FRICTION_SLICK;
141 this.stat_gameplayfix_easierwaterjump = GAMEPLAYFIX_EASIERWATERJUMP;
145 // fix some new settings
146 this.stat_sv_airaccel_qw_stretchfactor = Physics_ClientOption(this, "airaccel_qw_stretchfactor");
147 this.stat_sv_maxairstrafespeed = Physics_ClientOption(this, "maxairstrafespeed");
148 this.stat_sv_maxairspeed = Physics_ClientOption(this, "maxairspeed");
149 this.stat_sv_airstrafeaccelerate = Physics_ClientOption(this, "airstrafeaccelerate");
150 this.stat_sv_warsowbunny_turnaccel = Physics_ClientOption(this, "warsowbunny_turnaccel");
151 this.stat_sv_airaccel_sideways_friction = Physics_ClientOption(this, "airaccel_sideways_friction");
152 this.stat_sv_aircontrol = Physics_ClientOption(this, "aircontrol");
153 this.stat_sv_aircontrol_power = Physics_ClientOption(this, "aircontrol_power");
154 this.stat_sv_aircontrol_penalty = Physics_ClientOption(this, "aircontrol_penalty");
155 this.stat_sv_warsowbunny_airforwardaccel = Physics_ClientOption(this, "warsowbunny_airforwardaccel");
156 this.stat_sv_warsowbunny_topspeed = Physics_ClientOption(this, "warsowbunny_topspeed");
157 this.stat_sv_warsowbunny_accel = Physics_ClientOption(this, "warsowbunny_accel");
158 this.stat_sv_warsowbunny_backtosideratio = Physics_ClientOption(this, "warsowbunny_backtosideratio");
159 this.stat_sv_friction = Physics_ClientOption(this, "friction");
160 this.stat_sv_accelerate = Physics_ClientOption(this, "accelerate");
161 this.stat_sv_stopspeed = Physics_ClientOption(this, "stopspeed");
162 this.stat_sv_airaccelerate = Physics_ClientOption(this, "airaccelerate");
163 this.stat_sv_airstopaccelerate = Physics_ClientOption(this, "airstopaccelerate");
164 this.stat_sv_jumpvelocity = Physics_ClientOption(this, "jumpvelocity");
166 this.stat_sv_track_canjump = Physics_ClientOption(this, "track_canjump");
168 this.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
172 float IsMoveInDirection(vector mv, float ang) // key mix factor
174 if (mv_x == 0 && mv_y == 0)
175 return 0; // avoid division by zero
176 ang -= RAD2DEG * atan2(mv_y, mv_x);
177 ang = remainder(ang, 360) / 45;
178 return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
181 float GeomLerp(float a, float lerp, float b)
183 return a == 0 ? (lerp < 1 ? 0 : b)
184 : b == 0 ? (lerp > 0 ? 0 : a)
185 : a * pow(fabs(b / a), lerp);
188 noref float pmove_waterjumptime;
190 #define unstick_offsets(X) \
191 /* 1 no nudge (just return the original if this test passes) */ \
192 X(' 0.000 0.000 0.000') \
193 /* 6 simple nudges */ \
194 X(' 0.000 0.000 0.125') X('0.000 0.000 -0.125') \
195 X('-0.125 0.000 0.000') X('0.125 0.000 0.000') \
196 X(' 0.000 -0.125 0.000') X('0.000 0.125 0.000') \
197 /* 4 diagonal flat nudges */ \
198 X('-0.125 -0.125 0.000') X('0.125 -0.125 0.000') \
199 X('-0.125 0.125 0.000') X('0.125 0.125 0.000') \
200 /* 8 diagonal upward nudges */ \
201 X('-0.125 0.000 0.125') X('0.125 0.000 0.125') \
202 X(' 0.000 -0.125 0.125') X('0.000 0.125 0.125') \
203 X('-0.125 -0.125 0.125') X('0.125 -0.125 0.125') \
204 X('-0.125 0.125 0.125') X('0.125 0.125 0.125') \
205 /* 8 diagonal downward nudges */ \
206 X('-0.125 0.000 -0.125') X('0.125 0.000 -0.125') \
207 X(' 0.000 -0.125 -0.125') X('0.000 0.125 -0.125') \
208 X('-0.125 -0.125 -0.125') X('0.125 -0.125 -0.125') \
209 X('-0.125 0.125 -0.125') X('0.125 0.125 -0.125') \
212 void PM_ClientMovement_Unstick(entity this)
214 #define X(unstick_offset) \
216 vector neworigin = unstick_offset + this.origin; \
217 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, this); \
218 if (!trace_startsolid) \
220 setorigin(this, neworigin); \
228 void PM_ClientMovement_UpdateStatus(entity this, bool ground)
230 // make sure player is not stuck
231 PM_ClientMovement_Unstick(this);
234 if (PHYS_INPUT_BUTTON_CROUCH(this))
236 // wants to crouch, this always works
237 if (!IS_DUCKED(this)) SET_DUCKED(this);
241 // wants to stand, if currently crouching we need to check for a low ceiling first
244 tracebox(this.origin, PL_MIN, PL_MAX, this.origin, MOVE_NORMAL, this);
245 if (!trace_startsolid) UNSET_DUCKED(this);
250 vector origin1 = this.origin + '0 0 1';
251 vector origin2 = this.origin - '0 0 1';
255 tracebox(origin1, this.mins, this.maxs, origin2, MOVE_NORMAL, this);
256 if (trace_fraction < 1.0 && trace_plane_normal.z > 0.7)
260 // this code actually "predicts" an impact; so let's clip velocity first
261 this.velocity -= this.velocity * trace_plane_normal * trace_plane_normal;
264 UNSET_ONGROUND(this);
267 // set watertype/waterlevel
268 origin1 = this.origin;
269 origin1.z += this.mins_z + 1;
270 this.waterlevel = WATERLEVEL_NONE;
272 int thepoint = pointcontents(origin1);
274 this.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
278 this.waterlevel = WATERLEVEL_WETFEET;
279 origin1.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
280 thepoint = pointcontents(origin1);
281 if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
283 this.waterlevel = WATERLEVEL_SWIMMING;
284 origin1.z = this.origin.z + 22;
285 thepoint = pointcontents(origin1);
286 if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
287 this.waterlevel = WATERLEVEL_SUBMERGED;
291 if (IS_ONGROUND(this) || this.velocity.z <= 0 || pmove_waterjumptime <= 0)
292 pmove_waterjumptime = 0;
295 void PM_ClientMovement_Move(entity this)
299 PM_ClientMovement_UpdateStatus(this, false);
300 if(autocvar_cl_movement == 3)
307 vector currentorigin2;
309 vector primalvelocity;
311 vector trace1_endpos = '0 0 0';
312 vector trace2_endpos = '0 0 0';
313 vector trace3_endpos = '0 0 0';
314 float trace1_fraction = 0;
315 float trace2_fraction = 0;
316 float trace3_fraction = 0;
317 vector trace1_plane_normal = '0 0 0';
318 vector trace2_plane_normal = '0 0 0';
319 vector trace3_plane_normal = '0 0 0';
321 primalvelocity = this.velocity;
322 for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (this.velocity * this.velocity) > 0; bump++)
324 neworigin = this.origin + t * this.velocity;
325 tracebox(this.origin, this.mins, this.maxs, neworigin, MOVE_NORMAL, this);
326 trace1_endpos = trace_endpos;
327 trace1_fraction = trace_fraction;
328 trace1_plane_normal = trace_plane_normal;
329 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
331 // may be a step or wall, try stepping up
332 // first move forward at a higher level
333 currentorigin2 = this.origin;
334 currentorigin2_z += PHYS_STEPHEIGHT;
335 neworigin2 = neworigin;
336 neworigin2_z += PHYS_STEPHEIGHT;
337 tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
338 trace2_endpos = trace_endpos;
339 trace2_fraction = trace_fraction;
340 trace2_plane_normal = trace_plane_normal;
341 if(!trace_startsolid)
343 // then move down from there
344 currentorigin2 = trace2_endpos;
345 neworigin2 = trace2_endpos;
346 neworigin2_z = this.origin_z;
347 tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
348 trace3_endpos = trace_endpos;
349 trace3_fraction = trace_fraction;
350 trace3_plane_normal = trace_plane_normal;
351 // accept the new trace if it made some progress
352 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
354 trace1_endpos = trace2_endpos;
355 trace1_fraction = trace2_fraction;
356 trace1_plane_normal = trace2_plane_normal;
357 trace1_endpos = trace3_endpos;
362 // check if it moved at all
363 if(trace1_fraction >= 0.001)
364 setorigin(this, trace1_endpos);
366 // check if it moved all the way
367 if(trace1_fraction == 1)
370 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
371 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
372 // this got commented out in a change that supposedly makes the code match QW better
373 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
374 if(trace1_plane_normal_z > 0.7)
377 t -= t * trace1_fraction;
379 f = (this.velocity * trace1_plane_normal);
380 this.velocity = this.velocity + -f * trace1_plane_normal;
382 if(pmove_waterjumptime > 0)
383 this.velocity = primalvelocity;
387 void CPM_PM_Aircontrol(entity this, vector wishdir, float wishspeed)
389 float k = 32 * (2 * IsMoveInDirection(this.movement, 0) - 1);
393 k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
395 float zspeed = this.velocity_z;
397 float xyspeed = vlen(this.velocity);
398 this.velocity = normalize(this.velocity);
400 float dot = this.velocity * wishdir;
402 if (dot > 0) // we can't change direction while slowing down
404 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * PHYS_INPUT_TIMELENGTH;
405 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
406 k *= PHYS_AIRCONTROL(this);
407 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
410 this.velocity = this.velocity * xyspeed;
411 this.velocity_z = zspeed;
414 float AdjustAirAccelQW(float accelqw, float factor)
416 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
419 // example config for alternate speed clamping:
420 // sv_airaccel_qw 0.8
421 // sv_airaccel_sideways_friction 0
422 // prvm_globalset server speedclamp_mode 1
424 void PM_Accelerate(entity this, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
426 float speedclamp = stretchfactor > 0 ? stretchfactor
427 : accelqw < 0 ? 1 // full clamping, no stretch
430 accelqw = fabs(accelqw);
432 if (GAMEPLAYFIX_Q2AIRACCELERATE)
433 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
435 float vel_straight = this.velocity * wishdir;
436 float vel_z = this.velocity_z;
437 vector vel_xy = vec2(this.velocity);
438 vector vel_perpend = vel_xy - vel_straight * wishdir;
440 float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
442 float vel_xy_current = vlen(vel_xy);
444 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
445 float vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
446 float vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
447 vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
448 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
450 if (sidefric < 0 && (vel_perpend*vel_perpend))
451 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
453 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
454 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
455 // assume: themin > 1
456 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
457 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
458 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
459 // obviously, this cannot be
464 themin = sqrt(themin);
465 vel_perpend *= max(themin, f);
469 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
471 vel_xy = vel_straight * wishdir + vel_perpend;
475 float vel_xy_preclamp;
476 vel_xy_preclamp = vlen(vel_xy);
477 if (vel_xy_preclamp > 0) // prevent division by zero
479 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
480 if (vel_xy_current < vel_xy_preclamp)
481 vel_xy *= (vel_xy_current / vel_xy_preclamp);
485 this.velocity = vel_xy + vel_z * '0 0 1';
488 void PM_AirAccelerate(entity this, vector wishdir, float wishspeed)
493 vector curvel = this.velocity;
495 float curspeed = vlen(curvel);
497 if (wishspeed > curspeed * 1.01)
498 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
501 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
502 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH;
504 vector wishvel = wishdir * wishspeed;
505 vector acceldir = wishvel - curvel;
506 float addspeed = vlen(acceldir);
507 acceldir = normalize(acceldir);
509 float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
511 if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
513 vector curdir = normalize(curvel);
514 float dot = acceldir * curdir;
516 acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
519 this.velocity += accelspeed * acceldir;
527 When you press the jump key
528 returns true if handled
531 bool PlayerJump(entity this)
533 if (PHYS_FROZEN(this))
534 return true; // no jumping in freezetag when frozen
537 if (this.player_blocked)
538 return true; // no jumping while blocked
541 bool doublejump = false;
542 float mjumpheight = PHYS_JUMPVELOCITY(this);
544 if (MUTATOR_CALLHOOK(PlayerJump, this, doublejump, mjumpheight))
547 doublejump = player_multijump;
548 mjumpheight = player_jumpheight;
550 if (this.waterlevel >= WATERLEVEL_SWIMMING)
552 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
557 if (!IS_ONGROUND(this))
558 return IS_JUMP_HELD(this);
560 bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
561 if(PHYS_TRACK_CANJUMP(this))
565 if (IS_JUMP_HELD(this))
568 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
569 // velocity bounds. Final velocity is bound between (jumpheight *
570 // min + jumpheight) and (jumpheight * max + jumpheight);
572 if(PHYS_JUMPSPEEDCAP_MIN != "")
574 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
576 if (this.velocity_z < minjumpspeed)
577 mjumpheight += minjumpspeed - this.velocity_z;
580 if(PHYS_JUMPSPEEDCAP_MAX != "")
582 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
583 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
585 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
587 float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
589 if (this.velocity_z > maxjumpspeed)
590 mjumpheight -= this.velocity_z - maxjumpspeed;
594 if (!WAS_ONGROUND(this))
597 if(autocvar_speedmeter)
598 LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
600 if(this.lastground < time - 0.3)
602 this.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
603 this.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
606 if(this.jumppadcount > 1)
607 LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
608 this.jumppadcount = 0;
612 this.velocity_z += mjumpheight;
614 UNSET_ONGROUND(this);
619 this.oldvelocity_z = this.velocity_z;
621 animdecide_setaction(this, ANIMACTION_JUMP, true);
623 if (autocvar_g_jump_grunt)
624 WITH(entity, this, this, PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND));
629 void CheckWaterJump(entity this)
631 // check for a jump-out-of-water
632 makevectors(this.v_angle);
633 vector start = this.origin;
636 normalize(v_forward);
637 vector end = start + v_forward*24;
638 traceline (start, end, true, this);
639 if (trace_fraction < 1)
641 start_z = start_z + this.maxs_z - 8;
642 end = start + v_forward*24;
643 this.movedir = trace_plane_normal * -50;
644 traceline(start, end, true, this);
645 if (trace_fraction == 1)
646 { // open at eye level
647 this.velocity_z = 225;
648 this.flags |= FL_WATERJUMP;
651 this.teleport_time = time + 2; // safety net
653 pmove_waterjumptime = time + 2;
661 #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
663 float autocvar_cl_jetpack_jump;
664 #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
666 .float jetpack_stopped;
667 void CheckPlayerJump(entity this)
670 float was_flying = ITEMS_STAT(this) & IT_USING_JETPACK;
672 if (JETPACK_JUMP(this) < 2)
673 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
675 if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
677 float air_jump = !PlayerJump(this) || player_multijump; // PlayerJump() has important side effects
678 float activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
679 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(this) || ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO;
681 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
682 else if (this.jetpack_stopped) { }
686 if (was_flying) // TODO: ran out of fuel message
687 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
689 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
691 this.jetpack_stopped = true;
692 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
694 else if (activate && !PHYS_FROZEN(this))
695 ITEMS_STAT(this) |= IT_USING_JETPACK;
699 this.jetpack_stopped = false;
700 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
702 if (!PHYS_INPUT_BUTTON_JUMP(this))
703 UNSET_JUMP_HELD(this);
705 if (this.waterlevel == WATERLEVEL_SWIMMING)
706 CheckWaterJump(this);
709 float racecar_angle(float forward, float down)
717 float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
719 float angle_mult = forward / (800 + forward);
722 return ret * angle_mult + 360 * (1 - angle_mult);
724 return ret * angle_mult;
727 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
728 .float specialcommand_pos;
729 void SpecialCommand()
732 if (!CheatImpulse(99))
733 LOG_INFO("A hollow voice says \"Plugh\".\n");
737 float PM_check_specialcommand(entity this, float buttons)
743 else if (buttons == 1)
745 else if (buttons == 2)
747 else if (buttons == 128)
749 else if (buttons == 256)
751 else if (buttons == 512)
753 else if (buttons == 1024)
758 if (c == substring(specialcommand, this.specialcommand_pos, 1))
760 this.specialcommand_pos += 1;
761 if (this.specialcommand_pos >= strlen(specialcommand))
763 this.specialcommand_pos = 0;
768 else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
769 this.specialcommand_pos = 0;
774 void PM_check_nickspam(entity this)
777 if (time >= this.nickspamtime)
779 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
781 // slight annoyance for nick change scripts
782 this.movement = -1 * this.movement;
783 this.BUTTON_ATCK = this.BUTTON_JUMP = this.BUTTON_ATCK2 = this.BUTTON_ZOOM = this.BUTTON_CROUCH = this.BUTTON_HOOK = this.BUTTON_USE = 0;
785 if (this.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
787 this.v_angle_x = random() * 360;
788 this.v_angle_y = random() * 360;
789 // at least I'm not forcing retardedview by also assigning to angles_z
790 this.fixangle = true;
796 void PM_check_punch(entity this)
799 if (this.punchangle != '0 0 0')
801 float f = vlen(this.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
803 this.punchangle = normalize(this.punchangle) * f;
805 this.punchangle = '0 0 0';
808 if (this.punchvector != '0 0 0')
810 float f = vlen(this.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
812 this.punchvector = normalize(this.punchvector) * f;
814 this.punchvector = '0 0 0';
819 // predict frozen movement, as frozen players CAN move in some cases
820 void PM_check_frozen(entity this)
822 if (!PHYS_FROZEN(this))
824 if (PHYS_DODGING_FROZEN
826 && IS_REAL_CLIENT(this)
830 this.movement_x = bound(-5, this.movement.x, 5);
831 this.movement_y = bound(-5, this.movement.y, 5);
832 this.movement_z = bound(-5, this.movement.z, 5);
835 this.movement = '0 0 0';
837 vector midpoint = ((this.absmin + this.absmax) * 0.5);
838 if (pointcontents(midpoint) == CONTENT_WATER)
840 this.velocity = this.velocity * 0.5;
842 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
843 this.velocity_z = 200;
847 void PM_check_hitground(entity this)
850 if (!IS_PLAYER(this)) return; // no fall sounds for observers thank you very much
851 if (!IS_ONGROUND(this)) return;
852 if (!this.wasFlying) return;
853 this.wasFlying = false;
854 if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
855 if (time < this.ladder_time) return;
856 if (this.hook) return;
857 this.nextstep = time + 0.3 + random() * 0.1;
858 trace_dphitq3surfaceflags = 0;
859 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
860 if ((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)) return;
861 entity fall = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS) ? GS_FALL_METAL : GS_FALL;
862 WITH(entity, self, this, GlobalSound(fall, CH_PLAYER, VOICETYPE_PLAYERSOUND));
866 void PM_check_blocked(entity this)
869 if (!this.player_blocked)
871 this.movement = '0 0 0';
872 this.disableclientprediction = 1;
876 void PM_fly(entity this, float maxspd_mod)
878 // noclipping or flying
879 UNSET_ONGROUND(this);
881 this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
882 makevectors(this.v_angle);
883 //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
884 vector wishvel = v_forward * this.movement.x
885 + v_right * this.movement.y
886 + '0 0 1' * this.movement.z;
888 vector wishdir = normalize(wishvel);
889 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
891 if (time >= this.teleport_time)
893 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
894 PM_ClientMovement_Move(this);
897 void PM_swim(entity this, float maxspd_mod)
900 UNSET_ONGROUND(this);
902 float jump = PHYS_INPUT_BUTTON_JUMP(this);
903 // water jump only in certain situations
904 // this mimics quakeworld code
905 if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180)
907 vector yawangles = '0 1 0' * this.v_angle.y;
908 makevectors(yawangles);
909 vector forward = v_forward;
910 vector spot = this.origin + 24 * forward;
912 traceline(spot, spot, MOVE_NOMONSTERS, this);
913 if (trace_startsolid)
916 traceline(spot, spot, MOVE_NOMONSTERS, this);
917 if (!trace_startsolid)
919 this.velocity = forward * 50;
920 this.velocity_z = 310;
921 pmove_waterjumptime = 2;
922 UNSET_ONGROUND(this);
927 makevectors(this.v_angle);
928 //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
929 vector wishvel = v_forward * this.movement.x
930 + v_right * this.movement.y
931 + '0 0 1' * this.movement.z;
932 if (wishvel == '0 0 0')
933 wishvel = '0 0 -60'; // drift towards bottom
935 vector wishdir = normalize(wishvel);
936 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod) * 0.7;
941 // if (pmove_waterjumptime <= 0) // TODO: use
944 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this);
945 f = min(max(0, f), 1);
948 f = wishspeed - this.velocity * wishdir;
951 float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, f);
952 this.velocity += accelspeed * wishdir;
955 // holding jump button swims upward slowly
959 if (this.watertype & CONTENT_LAVA)
960 this.velocity_z = 50;
961 else if (this.watertype & CONTENT_SLIME)
962 this.velocity_z = 80;
965 if (IS_NEXUIZ_DERIVED(gamemode))
967 this.velocity_z = 200;
970 this.velocity_z = 100;
975 // water acceleration
976 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
977 PM_ClientMovement_Move(this);
980 void PM_ladder(entity this, float maxspd_mod)
982 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
983 UNSET_ONGROUND(this);
986 g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
987 if (PHYS_ENTGRAVITY(this))
988 g *= PHYS_ENTGRAVITY(this);
989 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
992 this.velocity_z += g;
995 this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
996 makevectors(this.v_angle);
997 //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
998 vector wishvel = v_forward * this.movement_x
999 + v_right * this.movement_y
1000 + '0 0 1' * this.movement_z;
1001 this.velocity_z += g;
1002 if (this.ladder_entity.classname == "func_water")
1004 float f = vlen(wishvel);
1005 if (f > this.ladder_entity.speed)
1006 wishvel *= (this.ladder_entity.speed / f);
1008 this.watertype = this.ladder_entity.skin;
1009 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
1010 if ((this.origin_z + this.view_ofs_z) < f)
1011 this.waterlevel = WATERLEVEL_SUBMERGED;
1012 else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f)
1013 this.waterlevel = WATERLEVEL_SWIMMING;
1014 else if ((this.origin_z + this.mins_z + 1) < f)
1015 this.waterlevel = WATERLEVEL_WETFEET;
1018 this.waterlevel = WATERLEVEL_NONE;
1019 this.watertype = CONTENT_EMPTY;
1023 vector wishdir = normalize(wishvel);
1024 float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
1026 if (time >= this.teleport_time)
1028 // water acceleration
1029 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this)*maxspd_mod, 1, 0, 0, 0);
1030 PM_ClientMovement_Move(this);
1033 void PM_jetpack(entity this, float maxspd_mod)
1035 //makevectors(this.v_angle.y * '0 1 0');
1036 makevectors(this.v_angle);
1037 vector wishvel = v_forward * this.movement_x
1038 + v_right * this.movement_y;
1039 // add remaining speed as Z component
1040 float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
1041 // fix speedhacks :P
1042 wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1043 // add the unused velocity as up component
1046 // if (this.BUTTON_JUMP)
1047 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1049 // it is now normalized, so...
1050 float a_side = PHYS_JETPACK_ACCEL_SIDE;
1051 float a_up = PHYS_JETPACK_ACCEL_UP;
1052 float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1054 wishvel_x *= a_side;
1055 wishvel_y *= a_side;
1060 //////////////////////////////////////////////////////////////////////////////////////
1061 // finding the maximum over all vectors of above form
1062 // with wishvel having an absolute value of 1
1063 //////////////////////////////////////////////////////////////////////////////////////
1064 // we're finding the maximum over
1065 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1066 // for z in the range from -1 to 1
1067 //////////////////////////////////////////////////////////////////////////////////////
1068 // maximum is EITHER attained at the single extreme point:
1069 float a_diff = a_side * a_side - a_up * a_up;
1073 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1074 if (f > -1 && f < 1) // can it be attained?
1076 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1077 //print("middle\n");
1080 // OR attained at z = 1:
1081 f = (a_up + a_add) * (a_up + a_add);
1087 // OR attained at z = -1:
1088 f = (a_up - a_add) * (a_up - a_add);
1092 //print("bottom\n");
1095 //////////////////////////////////////////////////////////////////////////////////////
1097 //print("best possible acceleration: ", ftos(best), "\n");
1100 fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1101 if (wishvel_z - PHYS_GRAVITY > 0)
1102 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1104 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1107 fvel = vlen(wishvel);
1110 wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1112 fvel = min(1, vlen(wishvel) / best);
1113 if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1114 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1118 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1120 if (f > 0 && wishvel != '0 0 0')
1122 this.velocity = this.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1123 UNSET_ONGROUND(this);
1126 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1127 this.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1129 ITEMS_STAT(this) |= IT_USING_JETPACK;
1131 // jetpack also inhibits health regeneration, but only for 1 second
1132 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1137 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1138 if(autocvar_cl_movement != 3)
1140 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1141 this.velocity_z -= g * 0.5;
1143 this.velocity_z -= g;
1145 PM_ClientMovement_Move(this);
1146 if(autocvar_cl_movement != 3)
1148 if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1149 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1150 this.velocity_z -= g * 0.5;
1155 void PM_walk(entity this, float maxspd_mod)
1157 if (!WAS_ONGROUND(this))
1160 if (autocvar_speedmeter)
1161 LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
1163 if (this.lastground < time - 0.3)
1164 this.velocity *= (1 - PHYS_FRICTION_ONLAND);
1166 if (this.jumppadcount > 1)
1167 LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
1168 this.jumppadcount = 0;
1173 makevectors(this.v_angle.y * '0 1 0');
1174 const vector wishvel = v_forward * this.movement.x
1175 + v_right * this.movement.y;
1177 const vector wishdir = normalize(wishvel);
1178 float wishspeed = vlen(wishvel);
1179 wishspeed = min(wishspeed, PHYS_MAXSPEED(this) * maxspd_mod);
1180 if (IS_DUCKED(this)) wishspeed *= 0.5;
1182 // apply edge friction
1183 const float f2 = vlen2(vec2(this.velocity));
1186 trace_dphitq3surfaceflags = 0;
1187 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
1188 // TODO: apply edge friction
1189 // apply ground friction
1190 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1191 ? PHYS_FRICTION_SLICK
1192 : PHYS_FRICTION(this);
1195 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
1199 Mathematical analysis time!
1201 Our goal is to invert this mess.
1203 For the two cases we get:
1204 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
1205 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1206 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1208 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1209 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1211 These cases would be chosen ONLY if:
1212 v0 < PHYS_STOPSPEED(this)
1213 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
1214 v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1216 v0 >= PHYS_STOPSPEED(this)
1217 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
1218 v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1221 const float addspeed = wishspeed - this.velocity * wishdir;
1224 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1225 this.velocity += accelspeed * wishdir;
1228 const float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1229 if(autocvar_cl_movement != 3)
1231 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1232 this.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1234 if (vdist(this.velocity, >, 0))
1235 PM_ClientMovement_Move(this);
1236 if(autocvar_cl_movement != 3)
1238 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1239 if (!IS_ONGROUND(this) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1240 this.velocity_z -= g * 0.5;
1245 void PM_air(entity this, float buttons_prev, float maxspd_mod)
1247 makevectors(this.v_angle.y * '0 1 0');
1248 vector wishvel = v_forward * this.movement.x
1249 + v_right * this.movement.y;
1251 vector wishdir = normalize(wishvel);
1252 float wishspeed = vlen(wishvel);
1255 if (time >= this.teleport_time)
1257 if (pmove_waterjumptime <= 0)
1260 float maxairspd = PHYS_MAXAIRSPEED(this) * min(maxspd_mod, 1);
1262 // apply air speed limit
1263 float airaccelqw = PHYS_AIRACCEL_QW(this);
1264 float wishspeed0 = wishspeed;
1265 wishspeed = min(wishspeed, maxairspd);
1266 if (IS_DUCKED(this))
1268 float airaccel = PHYS_AIRACCELERATE(this) * min(maxspd_mod, 1);
1270 float accelerating = (this.velocity * wishdir > 0);
1271 float wishspeed2 = wishspeed;
1274 if (PHYS_AIRSTOPACCELERATE(this))
1276 vector curdir = normalize(vec2(this.velocity));
1277 airaccel += (PHYS_AIRSTOPACCELERATE(this)*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1279 // note that for straight forward jumping:
1280 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1281 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1283 // dv/dt = accel * maxspeed (when slow)
1284 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1285 // log dv/dt = logaccel + logmaxspeed (when slow)
1286 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1287 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
1288 if (PHYS_MAXAIRSTRAFESPEED(this))
1289 wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(this)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED(this)*maxspd_mod));
1290 if (PHYS_AIRSTRAFEACCELERATE(this))
1291 airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(this)*maxspd_mod);
1292 if (PHYS_AIRSTRAFEACCEL_QW(this))
1294 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
1296 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
1299 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0)
1300 PM_AirAccelerate(this, wishdir, wishspeed2);
1302 PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(this), PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(this));
1304 if (PHYS_AIRCONTROL(this))
1305 CPM_PM_Aircontrol(this, wishdir, wishspeed2);
1308 float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1309 if(autocvar_cl_movement != 3)
1310 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1311 this.velocity_z -= g * 0.5;
1313 this.velocity_z -= g;
1315 PM_ClientMovement_Move(this);
1317 if(autocvar_cl_movement != 3)
1318 if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1319 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1320 this.velocity_z -= g * 0.5;
1324 // used for calculating airshots
1325 bool IsFlying(entity this)
1327 if(IS_ONGROUND(this))
1329 if(this.waterlevel >= WATERLEVEL_SWIMMING)
1331 traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
1332 if(trace_fraction < 1)
1337 void PM_Main(entity this)
1339 int buttons = PHYS_INPUT_BUTTON_MASK(this);
1341 this.items = getstati(STAT_ITEMS, 0, 24);
1343 this.movement = PHYS_INPUT_MOVEVALUES(this);
1345 vector oldv_angle = this.v_angle;
1346 vector oldangles = this.angles; // we need to save these, as they're abused by other code
1347 this.v_angle = PHYS_INPUT_ANGLES(this);
1348 this.angles = PHYS_WORLD_ANGLES(this);
1350 this.team = myteam + 1; // is this correct?
1351 if (!(PHYS_INPUT_BUTTON_JUMP(this))) // !jump
1352 UNSET_JUMP_HELD(this); // canjump = true
1353 pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1355 PM_ClientMovement_UpdateStatus(this, true);
1360 WarpZone_PlayerPhysics_FixVAngle();
1362 float maxspeed_mod = 1;
1363 maxspeed_mod *= PHYS_HIGHSPEED;
1366 Physics_UpdateStats(this, maxspeed_mod);
1368 if (this.PlayerPhysplug)
1369 if (this.PlayerPhysplug())
1374 anticheat_physics(this);
1377 if (PM_check_specialcommand(this, buttons))
1382 if (buttons != this.buttons_old || this.movement != this.movement_old || this.v_angle != this.v_angle_old)
1383 this.parm_idlesince = time;
1386 int buttons_prev = this.buttons_old;
1387 this.buttons_old = buttons;
1388 this.movement_old = this.movement;
1389 this.v_angle_old = this.v_angle;
1391 PM_check_nickspam(this);
1393 PM_check_punch(this);
1395 if (IS_BOT_CLIENT(this))
1397 if (playerdemo_read(this))
1399 WITH(entity, self, this, bot_think());
1404 if (IS_PLAYER(this))
1406 const bool allowed_to_move = (time >= game_starttime);
1407 if (!allowed_to_move)
1409 this.velocity = '0 0 0';
1410 this.movetype = MOVETYPE_NONE;
1411 this.disableclientprediction = 2;
1413 else if (this.disableclientprediction == 2)
1415 if (this.movetype == MOVETYPE_NONE)
1416 this.movetype = MOVETYPE_WALK;
1417 this.disableclientprediction = 0;
1423 if (this.movetype == MOVETYPE_NONE)
1426 // when we get here, disableclientprediction cannot be 2
1427 this.disableclientprediction = 0;
1430 viewloc_PlayerPhysics(this);
1432 PM_check_frozen(this);
1434 PM_check_blocked(this);
1439 maxspeed_mod *= this.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1441 // conveyors: first fix velocity
1442 if (this.conveyor.state)
1443 this.velocity -= this.conveyor.movedir;
1445 MUTATOR_CALLHOOK(PlayerPhysics, this);
1448 if (!IS_PLAYER(this))
1450 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1451 if (!this.spectatorspeed)
1452 this.spectatorspeed = maxspeed_mod;
1453 if (this.impulse && this.impulse <= 19 || (this.impulse >= 200 && this.impulse <= 209) || (this.impulse >= 220 && this.impulse <= 229))
1455 if (this.lastclassname != "player")
1457 if (this.impulse == 10 || this.impulse == 15 || this.impulse == 18 || (this.impulse >= 200 && this.impulse <= 209))
1458 this.spectatorspeed = bound(1, this.spectatorspeed + 0.5, 5);
1459 else if (this.impulse == 11)
1460 this.spectatorspeed = maxspeed_mod;
1461 else if (this.impulse == 12 || this.impulse == 16 || this.impulse == 19 || (this.impulse >= 220 && this.impulse <= 229))
1462 this.spectatorspeed = bound(1, this.spectatorspeed - 0.5, 5);
1463 else if (this.impulse >= 1 && this.impulse <= 9)
1464 this.spectatorspeed = 1 + 0.5 * (this.impulse - 1);
1465 } // otherwise just clear
1468 maxspeed_mod = this.spectatorspeed;
1471 float spd = max(PHYS_MAXSPEED(this), PHYS_MAXAIRSPEED(this)) * maxspeed_mod;
1472 if(this.speed != spd)
1475 string temps = ftos(spd);
1476 stuffcmd(this, strcat("cl_forwardspeed ", temps, "\n"));
1477 stuffcmd(this, strcat("cl_backspeed ", temps, "\n"));
1478 stuffcmd(this, strcat("cl_sidespeed ", temps, "\n"));
1479 stuffcmd(this, strcat("cl_upspeed ", temps, "\n"));
1482 if(this.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1484 this.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1485 stuffcmd(this, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1487 if(this.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1489 this.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1490 stuffcmd(this, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1496 // handle water here
1497 vector midpoint = ((this.absmin + this.absmax) * 0.5);
1498 if(pointcontents(midpoint) == CONTENT_WATER)
1500 this.velocity = this.velocity * 0.5;
1503 //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1504 //{ this.velocity_z = 70; }
1511 this.angles = '0 1 0' * this.v_angle.y;
1514 PM_check_hitground(this);
1519 if (IS_PLAYER(this))
1520 CheckPlayerJump(this);
1522 if (this.flags & FL_WATERJUMP)
1524 this.velocity_x = this.movedir.x;
1525 this.velocity_y = this.movedir.y;
1526 if (time > this.teleport_time || this.waterlevel == WATERLEVEL_NONE)
1528 this.flags &= ~FL_WATERJUMP;
1529 this.teleport_time = 0;
1533 else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod))
1536 else if (this.movetype == MOVETYPE_NOCLIP || this.movetype == MOVETYPE_FLY || this.movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1537 PM_fly(this, maxspeed_mod);
1539 else if (this.waterlevel >= WATERLEVEL_SWIMMING)
1540 PM_swim(this, maxspeed_mod);
1542 else if (time < this.ladder_time)
1543 PM_ladder(this, maxspeed_mod);
1545 else if (ITEMS_STAT(this) & IT_USING_JETPACK)
1546 PM_jetpack(this, maxspeed_mod);
1548 else if (IS_ONGROUND(this))
1549 PM_walk(this, maxspeed_mod);
1552 PM_air(this, buttons_prev, maxspeed_mod);
1555 if (IS_ONGROUND(this))
1556 this.lastground = time;
1558 // conveyors: then break velocity again
1559 if(this.conveyor.state)
1560 this.velocity += this.conveyor.movedir;
1562 this.lastflags = this.flags;
1564 this.lastclassname = this.classname;
1567 this.v_angle = oldv_angle;
1568 this.angles = oldangles;
1573 void SV_PlayerPhysics()
1575 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
1584 ((this.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1585 (!(this.flags & FL_JUMPRELEASED) ? PMF_JUMP_HELD : 0) |
1586 ((this.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);