Remove .move_* fields and MOVETYPE_PUSH logic (doesn't work)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics / player.qc
1 #include "player.qh"
2 #include "../triggers/include.qh"
3 #include "../viewloc.qh"
4
5 #ifdef SVQC
6
7 #include <server/miscfunctions.qh>
8 #include "../triggers/trigger/viewloc.qh"
9
10 // client side physics
11 bool Physics_Valid(string thecvar)
12 {
13         return autocvar_g_physics_clientselect && strhasword(autocvar_g_physics_clientselect_options, thecvar);
14 }
15
16 float Physics_ClientOption(entity this, string option, float defaultval)
17 {
18         if(Physics_Valid(this.cvar_cl_physics))
19         {
20                 string s = sprintf("g_physics_%s_%s", this.cvar_cl_physics, option);
21                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
22                         return cvar(s);
23         }
24         if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
25         {
26                 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
27                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
28                         return cvar(s);
29         }
30         return defaultval;
31 }
32
33 void Physics_UpdateStats(entity this, float maxspd_mod)
34 {
35         STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
36         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
37                 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
38                 : 0;
39         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
40         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
41
42         // old stats
43         // fix some new settings
44         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
45         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
46         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
47         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
48         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
49         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
50         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
51         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
52         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
53         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
54         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
55         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
56         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
57         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
58         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
59         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
60         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
61         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
62         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
63         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
64 }
65 #endif
66
67 float IsMoveInDirection(vector mv, float ang) // key mix factor
68 {
69         if (mv_x == 0 && mv_y == 0)
70                 return 0; // avoid division by zero
71         ang -= RAD2DEG * atan2(mv_y, mv_x);
72         ang = remainder(ang, 360) / 45;
73         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
74 }
75
76 float GeomLerp(float a, float _lerp, float b)
77 {
78         return a == 0 ? (_lerp < 1 ? 0 : b)
79                 : b == 0 ? (_lerp > 0 ? 0 : a)
80                 : a * pow(fabs(b / a), _lerp);
81 }
82
83 #define unstick_offsets(X) \
84 /* 1 no nudge (just return the original if this test passes) */ \
85         X(' 0.000  0.000  0.000') \
86 /* 6 simple nudges */ \
87         X(' 0.000  0.000  0.125') X('0.000  0.000 -0.125') \
88         X('-0.125  0.000  0.000') X('0.125  0.000  0.000') \
89         X(' 0.000 -0.125  0.000') X('0.000  0.125  0.000') \
90 /* 4 diagonal flat nudges */ \
91         X('-0.125 -0.125  0.000') X('0.125 -0.125  0.000') \
92         X('-0.125  0.125  0.000') X('0.125  0.125  0.000') \
93 /* 8 diagonal upward nudges */ \
94         X('-0.125  0.000  0.125') X('0.125  0.000  0.125') \
95         X(' 0.000 -0.125  0.125') X('0.000  0.125  0.125') \
96         X('-0.125 -0.125  0.125') X('0.125 -0.125  0.125') \
97         X('-0.125  0.125  0.125') X('0.125  0.125  0.125') \
98 /* 8 diagonal downward nudges */ \
99         X('-0.125  0.000 -0.125') X('0.125  0.000 -0.125') \
100         X(' 0.000 -0.125 -0.125') X('0.000  0.125 -0.125') \
101         X('-0.125 -0.125 -0.125') X('0.125 -0.125 -0.125') \
102         X('-0.125  0.125 -0.125') X('0.125  0.125 -0.125') \
103 /**/
104
105 void PM_ClientMovement_Unstick(entity this)
106 {
107         #define X(unstick_offset) \
108         { \
109                 vector neworigin = unstick_offset + this.origin; \
110                 tracebox(neworigin, STAT(PL_CROUCH_MIN, NULL), STAT(PL_CROUCH_MAX, NULL), neworigin, MOVE_NORMAL, this); \
111                 if (!trace_startsolid) \
112                 { \
113                         setorigin(this, neworigin); \
114                         return; \
115                 } \
116         }
117         unstick_offsets(X);
118         #undef X
119 }
120
121 void PM_ClientMovement_UpdateStatus(entity this, bool ground)
122 {
123 #ifdef CSQC
124         if(!IS_PLAYER(this))
125                 return;
126         // make sure player is not stuck
127         if(autocvar_cl_movement == 3)
128                 PM_ClientMovement_Unstick(this);
129
130         // set crouched
131         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
132         if(this.hook && !wasfreed(this.hook))
133                 do_crouch = false;
134         if(this.waterlevel >= WATERLEVEL_SWIMMING)
135                 do_crouch = false;
136         if(hud != HUD_NORMAL)
137                 do_crouch = false;
138         if(STAT(FROZEN, this))
139                 do_crouch = false;
140         if((activeweapon == WEP_SHOCKWAVE || activeweapon == WEP_SHOTGUN) && viewmodel.animstate_startframe == viewmodel.anim_fire2_x && time < viewmodel.weapon_nextthink)
141                 do_crouch = false;
142
143         if (do_crouch)
144         {
145                 // wants to crouch, this always works
146                 if (!IS_DUCKED(this)) SET_DUCKED(this);
147         }
148         else
149         {
150                 // wants to stand, if currently crouching we need to check for a low ceiling first
151                 if (IS_DUCKED(this))
152                 {
153                         tracebox(this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), this.origin, MOVE_NORMAL, this);
154                         if (!trace_startsolid) UNSET_DUCKED(this);
155                 }
156         }
157
158         // set onground
159         vector origin1 = this.origin + '0 0 1';
160         vector origin2 = this.origin - '0 0 1';
161
162         if (ground && autocvar_cl_movement == 3)
163         {
164                 tracebox(origin1, this.mins, this.maxs, origin2, MOVE_NORMAL, this);
165                 if (trace_fraction < 1.0 && trace_plane_normal.z > 0.7)
166                 {
167                         SET_ONGROUND(this);
168
169                         // this code actually "predicts" an impact; so let's clip velocity first
170                         this.velocity -= this.velocity * trace_plane_normal * trace_plane_normal;
171                 }
172                 else
173                         UNSET_ONGROUND(this);
174         }
175
176         if(autocvar_cl_movement == 3)
177         {
178                 // set watertype/waterlevel
179                 origin1 = this.origin;
180                 origin1.z += this.mins_z + 1;
181                 this.waterlevel = WATERLEVEL_NONE;
182
183                 int thepoint = pointcontents(origin1);
184
185                 this.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
186
187                 if (this.watertype)
188                 {
189                         this.waterlevel = WATERLEVEL_WETFEET;
190                         origin1.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
191                         thepoint = pointcontents(origin1);
192                         if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
193                         {
194                                 this.waterlevel = WATERLEVEL_SWIMMING;
195                                 origin1.z = this.origin.z + 22;
196                                 thepoint = pointcontents(origin1);
197                                 if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
198                                         this.waterlevel = WATERLEVEL_SUBMERGED;
199                         }
200                 }
201         }
202
203         if (IS_ONGROUND(this) || this.velocity.z <= 0 || pmove_waterjumptime <= 0)
204                 pmove_waterjumptime = 0;
205 #endif
206 }
207
208 void PM_ClientMovement_Move(entity this)
209 {
210 #ifdef CSQC
211
212         PM_ClientMovement_UpdateStatus(this, false);
213         if(autocvar_cl_movement == 1)
214                 return;
215
216         int bump;
217         float t;
218         float f;
219         vector neworigin;
220         vector currentorigin2;
221         vector neworigin2;
222         vector primalvelocity;
223
224         vector trace1_endpos = '0 0 0';
225         vector trace2_endpos = '0 0 0';
226         vector trace3_endpos = '0 0 0';
227         float trace1_fraction = 0;
228         float trace2_fraction = 0;
229         float trace3_fraction = 0;
230         vector trace1_plane_normal = '0 0 0';
231         vector trace2_plane_normal = '0 0 0';
232         vector trace3_plane_normal = '0 0 0';
233
234         primalvelocity = this.velocity;
235         for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (this.velocity * this.velocity) > 0; bump++)
236         {
237                 neworigin = this.origin + t * this.velocity;
238                 tracebox(this.origin, this.mins, this.maxs, neworigin, MOVE_NORMAL, this);
239                 trace1_endpos = trace_endpos;
240                 trace1_fraction = trace_fraction;
241                 trace1_plane_normal = trace_plane_normal;
242                 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
243                 {
244                         // may be a step or wall, try stepping up
245                         // first move forward at a higher level
246                         currentorigin2 = this.origin;
247                         currentorigin2_z += PHYS_STEPHEIGHT(this);
248                         neworigin2 = neworigin;
249                         neworigin2_z += PHYS_STEPHEIGHT(this);
250                         tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
251                         trace2_endpos = trace_endpos;
252                         trace2_fraction = trace_fraction;
253                         trace2_plane_normal = trace_plane_normal;
254                         if(!trace_startsolid)
255                         {
256                                 // then move down from there
257                                 currentorigin2 = trace2_endpos;
258                                 neworigin2 = trace2_endpos;
259                                 neworigin2_z = this.origin_z;
260                                 tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
261                                 trace3_endpos = trace_endpos;
262                                 trace3_fraction = trace_fraction;
263                                 trace3_plane_normal = trace_plane_normal;
264                                 // accept the new trace if it made some progress
265                                 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
266                                 {
267                                         trace1_endpos = trace2_endpos;
268                                         trace1_fraction = trace2_fraction;
269                                         trace1_plane_normal = trace2_plane_normal;
270                                         trace1_endpos = trace3_endpos;
271                                 }
272                         }
273                 }
274
275                 // check if it moved at all
276                 if(trace1_fraction >= 0.001)
277                         setorigin(this, trace1_endpos);
278
279                 // check if it moved all the way
280                 if(trace1_fraction == 1)
281                         break;
282
283                 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
284                 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
285                 // this got commented out in a change that supposedly makes the code match QW better
286                 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
287                 if(trace1_plane_normal_z > 0.7)
288                         SET_ONGROUND(this);
289
290                 t -= t * trace1_fraction;
291
292                 f = (this.velocity * trace1_plane_normal);
293                 this.velocity = this.velocity + -f * trace1_plane_normal;
294         }
295         if(PHYS_TELEPORT_TIME(this) > 0)
296                 this.velocity = primalvelocity;
297 #endif
298 }
299
300 void CPM_PM_Aircontrol(entity this, vector wishdir, float wishspeed)
301 {
302         float k = 32 * (2 * IsMoveInDirection(this.movement, 0) - 1);
303         if (k <= 0)
304                 return;
305
306         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
307
308         float zspeed = this.velocity_z;
309         this.velocity_z = 0;
310         float xyspeed = vlen(this.velocity);
311         this.velocity = normalize(this.velocity);
312
313         float dot = this.velocity * wishdir;
314
315         if (dot > 0) // we can't change direction while slowing down
316         {
317                 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * PHYS_INPUT_TIMELENGTH;
318                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
319                 k *= PHYS_AIRCONTROL(this);
320                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
321         }
322
323         this.velocity = this.velocity * xyspeed;
324         this.velocity_z = zspeed;
325 }
326
327 float AdjustAirAccelQW(float accelqw, float factor)
328 {
329         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
330 }
331
332 // example config for alternate speed clamping:
333 //   sv_airaccel_qw 0.8
334 //   sv_airaccel_sideways_friction 0
335 //   prvm_globalset server speedclamp_mode 1
336 //     (or 2)
337 void PM_Accelerate(entity this, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
338 {
339         float speedclamp = stretchfactor > 0 ? stretchfactor
340         : accelqw < 0 ? 1 // full clamping, no stretch
341         : -1; // no clamping
342
343         accelqw = fabs(accelqw);
344
345         if (GAMEPLAYFIX_Q2AIRACCELERATE)
346                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
347
348         float vel_straight = this.velocity * wishdir;
349         float vel_z = this.velocity_z;
350         vector vel_xy = vec2(this.velocity);
351         vector vel_perpend = vel_xy - vel_straight * wishdir;
352
353         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
354
355         float vel_xy_current  = vlen(vel_xy);
356         if (speedlimit)
357                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
358         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
359         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
360         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
361         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
362
363         if (sidefric < 0 && (vel_perpend*vel_perpend))
364                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
365         {
366                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
367                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
368                 // assume: themin > 1
369                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
370                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
371                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
372                 // obviously, this cannot be
373                 if (themin <= 0)
374                         vel_perpend *= f;
375                 else
376                 {
377                         themin = sqrt(themin);
378                         vel_perpend *= max(themin, f);
379                 }
380         }
381         else
382                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
383
384         vel_xy = vel_straight * wishdir + vel_perpend;
385
386         if (speedclamp >= 0)
387         {
388                 float vel_xy_preclamp;
389                 vel_xy_preclamp = vlen(vel_xy);
390                 if (vel_xy_preclamp > 0) // prevent division by zero
391                 {
392                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
393                         if (vel_xy_current < vel_xy_preclamp)
394                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
395                 }
396         }
397
398         this.velocity = vel_xy + vel_z * '0 0 1';
399 }
400
401 void PM_AirAccelerate(entity this, vector wishdir, float wishspeed)
402 {
403         if (wishspeed == 0)
404                 return;
405
406         vector curvel = this.velocity;
407         curvel_z = 0;
408         float curspeed = vlen(curvel);
409
410         if (wishspeed > curspeed * 1.01)
411                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
412         else
413         {
414                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
415                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH;
416         }
417         vector wishvel = wishdir * wishspeed;
418         vector acceldir = wishvel - curvel;
419         float addspeed = vlen(acceldir);
420         acceldir = normalize(acceldir);
421
422         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
423
424         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
425         {
426                 vector curdir = normalize(curvel);
427                 float dot = acceldir * curdir;
428                 if (dot < 0)
429                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
430         }
431
432         this.velocity += accelspeed * acceldir;
433 }
434
435
436 /*
437 =============
438 PlayerJump
439
440 When you press the jump key
441 returns true if handled
442 =============
443 */
444 bool PlayerJump(entity this)
445 {
446         if (PHYS_FROZEN(this))
447                 return true; // no jumping in freezetag when frozen
448
449 #ifdef SVQC
450         if (this.player_blocked)
451                 return true; // no jumping while blocked
452 #endif
453
454         bool doublejump = false;
455         float mjumpheight = PHYS_JUMPVELOCITY(this);
456
457         if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
458                 return true;
459
460         mjumpheight = M_ARGV(1, float);
461         doublejump = M_ARGV(2, bool);
462
463         if (this.waterlevel >= WATERLEVEL_SWIMMING)
464         {
465                 if(this.viewloc)
466                 {
467                         doublejump = true;
468                         mjumpheight *= 0.7;
469                 }
470                 else
471                 {
472                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
473                         return true;
474                 }
475         }
476
477         if (!doublejump)
478                 if (!IS_ONGROUND(this))
479                         return IS_JUMP_HELD(this);
480
481         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
482         if(PHYS_TRACK_CANJUMP(this))
483                 track_jump = true;
484
485         if (track_jump)
486                 if (IS_JUMP_HELD(this))
487                         return true;
488
489         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
490         // velocity bounds.  Final velocity is bound between (jumpheight *
491         // min + jumpheight) and (jumpheight * max + jumpheight);
492
493         if(PHYS_JUMPSPEEDCAP_MIN != "")
494         {
495                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
496
497                 if (this.velocity_z < minjumpspeed)
498                         mjumpheight += minjumpspeed - this.velocity_z;
499         }
500
501         if(PHYS_JUMPSPEEDCAP_MAX != "")
502         {
503                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
504                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
505
506                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
507                 {
508                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
509
510                         if (this.velocity_z > maxjumpspeed)
511                                 mjumpheight -= this.velocity_z - maxjumpspeed;
512                 }
513         }
514
515         if (!WAS_ONGROUND(this))
516         {
517 #ifdef SVQC
518                 if(autocvar_speedmeter)
519                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
520 #endif
521                 if(this.lastground < time - 0.3)
522                 {
523                         float f = (1 - PHYS_FRICTION_ONLAND(this));
524                         this.velocity_x *= f;
525                         this.velocity_y *= f;
526                 }
527 #ifdef SVQC
528                 if(this.jumppadcount > 1)
529                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
530                 this.jumppadcount = 0;
531 #endif
532         }
533
534         this.velocity_z += mjumpheight;
535
536         UNSET_ONGROUND(this);
537         SET_JUMP_HELD(this);
538
539 #ifdef SVQC
540
541         this.oldvelocity_z = this.velocity_z;
542
543         animdecide_setaction(this, ANIMACTION_JUMP, true);
544
545         if (autocvar_g_jump_grunt)
546                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
547 #endif
548         return true;
549 }
550
551 void CheckWaterJump(entity this)
552 {
553 // check for a jump-out-of-water
554         makevectors(this.v_angle);
555         vector start = this.origin;
556         start_z += 8;
557         v_forward_z = 0;
558         normalize(v_forward);
559         vector end = start + v_forward*24;
560         traceline (start, end, true, this);
561         if (trace_fraction < 1)
562         {       // solid at waist
563                 start_z = start_z + this.maxs_z - 8;
564                 end = start + v_forward*24;
565                 this.movedir = trace_plane_normal * -50;
566                 traceline(start, end, true, this);
567                 if (trace_fraction == 1)
568                 {       // open at eye level
569                         this.velocity_z = 225;
570                         this.flags |= FL_WATERJUMP;
571                         SET_JUMP_HELD(this);
572                 #ifdef SVQC
573                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
574                 #elif defined(CSQC)
575                         pmove_waterjumptime = 2;
576                 #endif
577                 }
578         }
579 }
580
581
582 #ifdef SVQC
583         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
584 #elif defined(CSQC)
585         float autocvar_cl_jetpack_jump;
586         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
587 #endif
588 .float jetpack_stopped;
589 void CheckPlayerJump(entity this)
590 {
591 #ifdef SVQC
592         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
593 #endif
594         if (JETPACK_JUMP(this) < 2)
595                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
596
597         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
598         {
599                 bool playerjump = PlayerJump(this); // required
600
601                 bool air_jump = !playerjump || M_ARGV(2, bool);
602                 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
603                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
604
605                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
606                 else if (this.jetpack_stopped) { }
607                 else if (!has_fuel)
608                 {
609 #ifdef SVQC
610                         if (was_flying) // TODO: ran out of fuel message
611                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
612                         else if (activate)
613                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
614 #endif
615                         this.jetpack_stopped = true;
616                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
617                 }
618                 else if (activate && !PHYS_FROZEN(this))
619                         ITEMS_STAT(this) |= IT_USING_JETPACK;
620         }
621         else
622         {
623                 this.jetpack_stopped = false;
624                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
625         }
626         if (!PHYS_INPUT_BUTTON_JUMP(this))
627                 UNSET_JUMP_HELD(this);
628
629         if (this.waterlevel == WATERLEVEL_SWIMMING)
630                 CheckWaterJump(this);
631 }
632
633 float racecar_angle(float forward, float down)
634 {
635         if (forward < 0)
636         {
637                 forward = -forward;
638                 down = -down;
639         }
640
641         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
642
643         float angle_mult = forward / (800 + forward);
644
645         if (ret > 180)
646                 return ret * angle_mult + 360 * (1 - angle_mult);
647         else
648                 return ret * angle_mult;
649 }
650
651 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
652 .float specialcommand_pos;
653 void SpecialCommand(entity this)
654 {
655 #ifdef SVQC
656         if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
657                 LOG_INFO("A hollow voice says \"Plugh\".\n");
658 #endif
659 }
660
661 bool PM_check_specialcommand(entity this, int buttons)
662 {
663 #ifdef SVQC
664         string c;
665         if (!buttons)
666                 c = "x";
667         else if (buttons == 1)
668                 c = "1";
669         else if (buttons == 2)
670                 c = " ";
671         else if (buttons == 128)
672                 c = "s";
673         else if (buttons == 256)
674                 c = "w";
675         else if (buttons == 512)
676                 c = "a";
677         else if (buttons == 1024)
678                 c = "d";
679         else
680                 c = "?";
681
682         if (c == substring(specialcommand, this.specialcommand_pos, 1))
683         {
684                 this.specialcommand_pos += 1;
685                 if (this.specialcommand_pos >= strlen(specialcommand))
686                 {
687                         this.specialcommand_pos = 0;
688                         SpecialCommand(this);
689                         return true;
690                 }
691         }
692         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
693                 this.specialcommand_pos = 0;
694 #endif
695         return false;
696 }
697
698 void PM_check_nickspam(entity this)
699 {
700 #ifdef SVQC
701         if (time >= this.nickspamtime)
702                 return;
703         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
704         {
705                 // slight annoyance for nick change scripts
706                 this.movement = -1 * this.movement;
707                 PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_JUMP(this) = PHYS_INPUT_BUTTON_ATCK2(this) = PHYS_INPUT_BUTTON_ZOOM(this) = PHYS_INPUT_BUTTON_CROUCH(this) = PHYS_INPUT_BUTTON_HOOK(this) = PHYS_INPUT_BUTTON_USE(this) = false;
708
709                 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!
710                 {
711                         this.v_angle_x = random() * 360;
712                         this.v_angle_y = random() * 360;
713                         // at least I'm not forcing retardedview by also assigning to angles_z
714                         this.fixangle = true;
715                 }
716         }
717 #endif
718 }
719
720 void PM_check_punch(entity this)
721 {
722 #ifdef SVQC
723         if (this.punchangle != '0 0 0')
724         {
725                 float f = vlen(this.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
726                 if (f > 0)
727                         this.punchangle = normalize(this.punchangle) * f;
728                 else
729                         this.punchangle = '0 0 0';
730         }
731
732         if (this.punchvector != '0 0 0')
733         {
734                 float f = vlen(this.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
735                 if (f > 0)
736                         this.punchvector = normalize(this.punchvector) * f;
737                 else
738                         this.punchvector = '0 0 0';
739         }
740 #endif
741 }
742
743 // predict frozen movement, as frozen players CAN move in some cases
744 void PM_check_frozen(entity this)
745 {
746         if (!PHYS_FROZEN(this))
747                 return;
748         if (PHYS_DODGING_FROZEN(this)
749 #ifdef SVQC
750         && IS_REAL_CLIENT(this)
751 #endif
752         )
753         {
754                 this.movement_x = bound(-5, this.movement.x, 5);
755                 this.movement_y = bound(-5, this.movement.y, 5);
756                 this.movement_z = bound(-5, this.movement.z, 5);
757         }
758         else
759                 this.movement = '0 0 0';
760
761         vector midpoint = ((this.absmin + this.absmax) * 0.5);
762         if (pointcontents(midpoint) == CONTENT_WATER)
763         {
764                 this.velocity = this.velocity * 0.5;
765
766                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
767                         this.velocity_z = 200;
768         }
769 }
770
771 void PM_check_hitground(entity this)
772 {
773 #ifdef SVQC
774         if (!this.wasFlying) return;
775     this.wasFlying = false;
776     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
777     if (time < this.ladder_time) return;
778     if (this.hook) return;
779     this.nextstep = time + 0.3 + random() * 0.1;
780     trace_dphitq3surfaceflags = 0;
781     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
782     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
783     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
784         ? GS_FALL_METAL
785         : GS_FALL;
786     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
787     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
788 #endif
789 }
790
791 void PM_Footsteps(entity this)
792 {
793 #ifdef SVQC
794         if (!g_footsteps) return;
795         if (IS_DUCKED(this)) return;
796         if (time >= this.lastground + 0.2) return;
797         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
798         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
799         {
800                 this.nextstep = time + 0.3 + random() * 0.1;
801                 trace_dphitq3surfaceflags = 0;
802                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
803                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
804                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
805                         ? GS_STEP_METAL
806                         : GS_STEP;
807                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
808         }
809 #endif
810 }
811
812 void PM_check_blocked(entity this)
813 {
814 #ifdef SVQC
815         if (!this.player_blocked)
816                 return;
817         this.movement = '0 0 0';
818         this.disableclientprediction = 1;
819 #endif
820 }
821
822 void PM_fly(entity this, float maxspd_mod)
823 {
824         // noclipping or flying
825         UNSET_ONGROUND(this);
826
827         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
828         makevectors(this.v_angle);
829         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
830         vector wishvel = v_forward * this.movement.x
831                                         + v_right * this.movement.y
832                                         + '0 0 1' * this.movement.z;
833         // acceleration
834         vector wishdir = normalize(wishvel);
835         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
836 #ifdef SVQC
837         if(time >= PHYS_TELEPORT_TIME(this))
838 #endif
839                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
840         PM_ClientMovement_Move(this);
841 }
842
843 void PM_swim(entity this, float maxspd_mod)
844 {
845         // swimming
846         UNSET_ONGROUND(this);
847
848         float jump = PHYS_INPUT_BUTTON_JUMP(this);
849         // water jump only in certain situations
850         // this mimics quakeworld code
851         if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc)
852         {
853                 vector yawangles = '0 1 0' * this.v_angle.y;
854                 makevectors(yawangles);
855                 vector forward = v_forward;
856                 vector spot = this.origin + 24 * forward;
857                 spot_z += 8;
858                 traceline(spot, spot, MOVE_NOMONSTERS, this);
859                 if (trace_startsolid)
860                 {
861                         spot_z += 24;
862                         traceline(spot, spot, MOVE_NOMONSTERS, this);
863                         if (!trace_startsolid)
864                         {
865                                 this.velocity = forward * 50;
866                                 this.velocity_z = 310;
867                         #ifdef CSQC
868                                 pmove_waterjumptime = 2;
869                         #endif
870                                 UNSET_ONGROUND(this);
871                                 SET_JUMP_HELD(this);
872                         }
873                 }
874         }
875         makevectors(this.v_angle);
876         float wishdown = this.movement.z;
877         if(PHYS_INPUT_BUTTON_CROUCH(this))
878                 wishdown = -PHYS_MAXSPEED(this);
879         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
880         vector wishvel = v_forward * this.movement.x
881                                         + v_right * this.movement.y
882                                         + '0 0 1' * wishdown;
883         if(this.viewloc)
884                 wishvel.z = -160; // drift anyway
885         else if (wishvel == '0 0 0')
886                 wishvel = '0 0 -60'; // drift towards bottom
887
888         vector wishdir = normalize(wishvel);
889         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod) * 0.7;
890
891 //      if (pmove_waterjumptime <= 0) // TODO: use
892     {
893                 // water friction
894                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this);
895                 f = min(max(0, f), 1);
896                 this.velocity *= f;
897
898                 f = wishspeed - this.velocity * wishdir;
899                 if (f > 0)
900                 {
901                         float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, f);
902                         this.velocity += accelspeed * wishdir;
903                 }
904
905                 // holding jump button swims upward slowly
906                 if (jump && !this.viewloc)
907                 {
908 #if 0
909                         if (this.watertype & CONTENT_LAVA)
910                                 this.velocity_z =  50;
911                         else if (this.watertype & CONTENT_SLIME)
912                                 this.velocity_z =  80;
913                         else
914                         {
915                                 if (IS_NEXUIZ_DERIVED(gamemode))
916 #endif
917                                 if(this.waterlevel >= WATERLEVEL_SUBMERGED)
918                                         this.velocity_z = PHYS_MAXSPEED(this);
919                                 else
920                                         this.velocity_z = 200;
921 #if 0
922                                 else
923                                         this.velocity_z = 100;
924                         }
925 #endif
926                 }
927         }
928         if(this.viewloc)
929         {
930                 const float addspeed = wishspeed - this.velocity * wishdir;
931                 if (addspeed > 0)
932                 {
933                         const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
934                         this.velocity += accelspeed * wishdir;
935                 }
936         }
937         else
938         {
939                 // water acceleration
940                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
941                 PM_ClientMovement_Move(this);
942         }
943 }
944
945 .vector oldmovement;
946 void PM_ladder(entity this, float maxspd_mod)
947 {
948         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
949         UNSET_ONGROUND(this);
950
951         float g;
952         g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
953         if (PHYS_ENTGRAVITY(this))
954                 g *= PHYS_ENTGRAVITY(this);
955         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
956         {
957                 g *= 0.5;
958                 this.velocity_z += g;
959         }
960
961         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
962         makevectors(this.v_angle);
963         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
964         vector wishvel = v_forward * this.movement_x
965                                         + v_right * this.movement_y
966                                         + '0 0 1' * this.movement_z;
967         if(this.viewloc)
968                 wishvel.z = this.oldmovement.x;
969         this.velocity_z += g;
970         if (this.ladder_entity.classname == "func_water")
971         {
972                 float f = vlen(wishvel);
973                 if (f > this.ladder_entity.speed)
974                         wishvel *= (this.ladder_entity.speed / f);
975
976                 this.watertype = this.ladder_entity.skin;
977                 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
978                 if ((this.origin_z + this.view_ofs_z) < f)
979                         this.waterlevel = WATERLEVEL_SUBMERGED;
980                 else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f)
981                         this.waterlevel = WATERLEVEL_SWIMMING;
982                 else if ((this.origin_z + this.mins_z + 1) < f)
983                         this.waterlevel = WATERLEVEL_WETFEET;
984                 else
985                 {
986                         this.waterlevel = WATERLEVEL_NONE;
987                         this.watertype = CONTENT_EMPTY;
988                 }
989         }
990         // acceleration
991         vector wishdir = normalize(wishvel);
992         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
993         if(time >= PHYS_TELEPORT_TIME(this))
994                 // water acceleration
995                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this)*maxspd_mod, 1, 0, 0, 0);
996         PM_ClientMovement_Move(this);
997 }
998
999 void PM_jetpack(entity this, float maxspd_mod)
1000 {
1001         //makevectors(this.v_angle.y * '0 1 0');
1002         makevectors(this.v_angle);
1003         vector wishvel = v_forward * this.movement_x
1004                                         + v_right * this.movement_y;
1005         // add remaining speed as Z component
1006         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
1007         // fix speedhacks :P
1008         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1009         // add the unused velocity as up component
1010         wishvel_z = 0;
1011
1012         // if (PHYS_INPUT_BUTTON_JUMP(this))
1013                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1014
1015         // it is now normalized, so...
1016         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
1017         float a_up = PHYS_JETPACK_ACCEL_UP(this);
1018         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
1019
1020         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
1021
1022         wishvel_x *= a_side;
1023         wishvel_y *= a_side;
1024         wishvel_z *= a_up;
1025         wishvel_z += a_add;
1026
1027         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
1028
1029         float best = 0;
1030         //////////////////////////////////////////////////////////////////////////////////////
1031         // finding the maximum over all vectors of above form
1032         // with wishvel having an absolute value of 1
1033         //////////////////////////////////////////////////////////////////////////////////////
1034         // we're finding the maximum over
1035         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1036         // for z in the range from -1 to 1
1037         //////////////////////////////////////////////////////////////////////////////////////
1038         // maximum is EITHER attained at the single extreme point:
1039         float a_diff = a_side * a_side - a_up * a_up;
1040         float f;
1041         if (a_diff != 0)
1042         {
1043                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1044                 if (f > -1 && f < 1) // can it be attained?
1045                 {
1046                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1047                         //print("middle\n");
1048                 }
1049         }
1050         // OR attained at z = 1:
1051         f = (a_up + a_add) * (a_up + a_add);
1052         if (f > best)
1053         {
1054                 best = f;
1055                 //print("top\n");
1056         }
1057         // OR attained at z = -1:
1058         f = (a_up - a_add) * (a_up - a_add);
1059         if (f > best)
1060         {
1061                 best = f;
1062                 //print("bottom\n");
1063         }
1064         best = sqrt(best);
1065         //////////////////////////////////////////////////////////////////////////////////////
1066
1067         //print("best possible acceleration: ", ftos(best), "\n");
1068
1069         float fxy, fz;
1070         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
1071         if (wishvel_z - PHYS_GRAVITY(this) > 0)
1072                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
1073         else
1074                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
1075
1076         float fvel;
1077         fvel = vlen(wishvel);
1078         wishvel_x *= fxy;
1079         wishvel_y *= fxy;
1080         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
1081
1082         fvel = min(1, vlen(wishvel) / best);
1083         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1084                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel));
1085         else
1086                 f = 1;
1087
1088         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1089
1090         if (f > 0 && wishvel != '0 0 0')
1091         {
1092                 this.velocity = this.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1093                 UNSET_ONGROUND(this);
1094
1095 #ifdef SVQC
1096                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1097                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel * f;
1098
1099                 ITEMS_STAT(this) |= IT_USING_JETPACK;
1100
1101                 // jetpack also inhibits health regeneration, but only for 1 second
1102                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1103 #endif
1104         }
1105
1106 #ifdef CSQC
1107         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1108         if(autocvar_cl_movement == 3)
1109         {
1110                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1111                         this.velocity_z -= g * 0.5;
1112                 else
1113                         this.velocity_z -= g;
1114         }
1115         PM_ClientMovement_Move(this);
1116         if(autocvar_cl_movement == 3)
1117         {
1118                 if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1119                         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1120                                 this.velocity_z -= g * 0.5;
1121         }
1122 #endif
1123 }
1124
1125 void PM_walk(entity this, float maxspd_mod)
1126 {
1127         if (!WAS_ONGROUND(this))
1128         {
1129 #ifdef SVQC
1130                 if (autocvar_speedmeter)
1131                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
1132 #endif
1133                 if (this.lastground < time - 0.3)
1134                         this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
1135 #ifdef SVQC
1136                 if (this.jumppadcount > 1)
1137                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
1138                 this.jumppadcount = 0;
1139 #endif
1140         }
1141
1142         // walking
1143         makevectors(this.v_angle.y * '0 1 0');
1144         const vector wishvel = v_forward * this.movement.x
1145                                                 + v_right * this.movement.y;
1146         // acceleration
1147         const vector wishdir = normalize(wishvel);
1148         float wishspeed = vlen(wishvel);
1149         wishspeed = min(wishspeed, PHYS_MAXSPEED(this) * maxspd_mod);
1150         if (IS_DUCKED(this)) wishspeed *= 0.5;
1151
1152         // apply edge friction
1153         const float f2 = vlen2(vec2(this.velocity));
1154         if (f2 > 0)
1155         {
1156                 trace_dphitq3surfaceflags = 0;
1157                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
1158                 // TODO: apply edge friction
1159                 // apply ground friction
1160                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1161                         ? PHYS_FRICTION_SLICK(this)
1162                         : PHYS_FRICTION(this);
1163
1164                 float f = sqrt(f2);
1165                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
1166                 f = max(0, f);
1167                 this.velocity *= f;
1168                 /*
1169                    Mathematical analysis time!
1170
1171                    Our goal is to invert this mess.
1172
1173                    For the two cases we get:
1174                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
1175                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1176                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1177                    and
1178                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1179                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1180
1181                    These cases would be chosen ONLY if:
1182                         v0 < PHYS_STOPSPEED(this)
1183                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
1184                         v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1185                    and, respectively:
1186                         v0 >= PHYS_STOPSPEED(this)
1187                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
1188                         v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1189                  */
1190         }
1191         const float addspeed = wishspeed - this.velocity * wishdir;
1192         if (addspeed > 0)
1193         {
1194                 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1195                 this.velocity += accelspeed * wishdir;
1196         }
1197 #ifdef CSQC
1198         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1199         if(autocvar_cl_movement == 3)
1200         {
1201                 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1202                         this.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1203         }
1204         if (vdist(this.velocity, >, 0))
1205                 PM_ClientMovement_Move(this);
1206         if(autocvar_cl_movement == 3)
1207         {
1208                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1209                         if (!IS_ONGROUND(this) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1210                                 this.velocity_z -= g * 0.5;
1211         }
1212 #endif
1213 }
1214
1215 void PM_air(entity this, float buttons_prev, float maxspd_mod)
1216 {
1217         makevectors(this.v_angle.y * '0 1 0');
1218         vector wishvel = v_forward * this.movement.x
1219                                         + v_right * this.movement.y;
1220         // acceleration
1221         vector wishdir = normalize(wishvel);
1222         float wishspeed = vlen(wishvel);
1223
1224 #ifdef SVQC
1225         if(time >= PHYS_TELEPORT_TIME(this))
1226 #elif defined(CSQC)
1227         if(pmove_waterjumptime <= 0)
1228 #endif
1229         {
1230                 float maxairspd = PHYS_MAXAIRSPEED(this) * min(maxspd_mod, 1);
1231
1232                 // apply air speed limit
1233                 float airaccelqw = PHYS_AIRACCEL_QW(this);
1234                 float wishspeed0 = wishspeed;
1235                 wishspeed = min(wishspeed, maxairspd);
1236                 if (IS_DUCKED(this))
1237                         wishspeed *= 0.5;
1238                 float airaccel = PHYS_AIRACCELERATE(this) * min(maxspd_mod, 1);
1239
1240                 float accelerating = (this.velocity * wishdir > 0);
1241                 float wishspeed2 = wishspeed;
1242
1243                 // CPM: air control
1244                 if (PHYS_AIRSTOPACCELERATE(this))
1245                 {
1246                         vector curdir = normalize(vec2(this.velocity));
1247                         airaccel += (PHYS_AIRSTOPACCELERATE(this)*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1248                 }
1249                 // note that for straight forward jumping:
1250                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1251                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1252                 // -->
1253                 // dv/dt = accel * maxspeed (when slow)
1254                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1255                 // log dv/dt = logaccel + logmaxspeed (when slow)
1256                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1257                 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
1258                 if (PHYS_MAXAIRSTRAFESPEED(this))
1259                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(this)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED(this)*maxspd_mod));
1260                 if (PHYS_AIRSTRAFEACCELERATE(this))
1261                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(this)*maxspd_mod);
1262                 if (PHYS_AIRSTRAFEACCEL_QW(this))
1263                         airaccelqw =
1264                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
1265                 *
1266                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
1267                 // !CPM
1268
1269                 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0)
1270                         PM_AirAccelerate(this, wishdir, wishspeed2);
1271                 else {
1272                     float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
1273                         PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
1274         }
1275
1276                 if (PHYS_AIRCONTROL(this))
1277                         CPM_PM_Aircontrol(this, wishdir, wishspeed2);
1278         }
1279 #ifdef CSQC
1280         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1281         if(autocvar_cl_movement == 3)
1282         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1283                 this.velocity_z -= g * 0.5;
1284         else
1285                 this.velocity_z -= g;
1286 #endif
1287         PM_ClientMovement_Move(this);
1288 #ifdef CSQC
1289         if(autocvar_cl_movement == 3)
1290         if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1291                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1292                         this.velocity_z -= g * 0.5;
1293 #endif
1294 }
1295
1296 // used for calculating airshots
1297 bool IsFlying(entity this)
1298 {
1299         if(IS_ONGROUND(this))
1300                 return false;
1301         if(this.waterlevel >= WATERLEVEL_SWIMMING)
1302                 return false;
1303         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
1304         if(trace_fraction < 1)
1305                 return false;
1306         return true;
1307 }
1308
1309 void PM_Main(entity this)
1310 {
1311         int buttons = PHYS_INPUT_BUTTON_MASK(this);
1312 #ifdef CSQC
1313         this.items = STAT(ITEMS);
1314
1315         this.movement = PHYS_INPUT_MOVEVALUES(this);
1316
1317         this.spectatorspeed = STAT(SPECTATORSPEED);
1318
1319         this.team = myteam + 1; // is this correct?
1320         if (!(PHYS_INPUT_BUTTON_JUMP(this))) // !jump
1321                 UNSET_JUMP_HELD(this); // canjump = true
1322         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1323
1324         PM_ClientMovement_UpdateStatus(this, true);
1325 #endif
1326
1327         this.oldmovement = this.movement;
1328
1329
1330 #ifdef SVQC
1331         WarpZone_PlayerPhysics_FixVAngle(this);
1332 #endif
1333         float maxspeed_mod = 1;
1334         maxspeed_mod *= PHYS_HIGHSPEED(this);
1335
1336 #ifdef SVQC
1337         Physics_UpdateStats(this, maxspeed_mod);
1338
1339         if (this.PlayerPhysplug)
1340                 if (this.PlayerPhysplug(this))
1341                         return;
1342 #elif defined(CSQC)
1343         if(hud != HUD_NORMAL)
1344                 return; // no vehicle prediction (yet)
1345 #endif
1346
1347 #ifdef SVQC
1348         anticheat_physics(this);
1349 #endif
1350
1351         if (PM_check_specialcommand(this, buttons))
1352                 return;
1353 #ifdef SVQC
1354         if (sv_maxidle > 0)
1355         {
1356                 if (buttons != this.buttons_old || this.movement != this.movement_old || this.v_angle != this.v_angle_old)
1357                         this.parm_idlesince = time;
1358         }
1359 #endif
1360         int buttons_prev = this.buttons_old;
1361         this.buttons_old = buttons;
1362         this.movement_old = this.movement;
1363         this.v_angle_old = this.v_angle;
1364
1365         PM_check_nickspam(this);
1366
1367         PM_check_punch(this);
1368 #ifdef SVQC
1369         if (IS_BOT_CLIENT(this))
1370         {
1371                 if (playerdemo_read(this))
1372                         return;
1373                 bot_think(this);
1374         }
1375 #endif
1376
1377 #ifdef SVQC
1378         if (IS_PLAYER(this))
1379         {
1380                 const bool allowed_to_move = (time >= game_starttime);
1381                 if (!allowed_to_move)
1382                 {
1383                         this.velocity = '0 0 0';
1384                         this.movetype = MOVETYPE_NONE;
1385                         this.disableclientprediction = 2;
1386                 }
1387                 else if (this.disableclientprediction == 2)
1388                 {
1389                         if (this.movetype == MOVETYPE_NONE)
1390                                 this.movetype = MOVETYPE_WALK;
1391                         this.disableclientprediction = 0;
1392                 }
1393         }
1394 #endif
1395
1396 #ifdef SVQC
1397         if (this.movetype == MOVETYPE_NONE)
1398                 return;
1399
1400         // when we get here, disableclientprediction cannot be 2
1401         this.disableclientprediction = 0;
1402 #endif
1403
1404         viewloc_PlayerPhysics(this);
1405
1406         PM_check_frozen(this);
1407
1408         PM_check_blocked(this);
1409
1410         maxspeed_mod = 1;
1411
1412         if (this.in_swamp)
1413                 maxspeed_mod *= this.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1414
1415         // conveyors: first fix velocity
1416         if (this.conveyor.state)
1417                 this.velocity -= this.conveyor.movedir;
1418
1419         MUTATOR_CALLHOOK(PlayerPhysics, this);
1420
1421         if (!IS_PLAYER(this))
1422         {
1423 #ifdef SVQC
1424                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1425                 if (!this.spectatorspeed)
1426                         this.spectatorspeed = maxspeed_mod;
1427                 if (this.impulse && this.impulse <= 19 || (this.impulse >= 200 && this.impulse <= 209) || (this.impulse >= 220 && this.impulse <= 229))
1428                 {
1429                         if (this.lastclassname != STR_PLAYER)
1430                         {
1431                                 if (this.impulse == 10 || this.impulse == 15 || this.impulse == 18 || (this.impulse >= 200 && this.impulse <= 209))
1432                                         this.spectatorspeed = bound(1, this.spectatorspeed + 0.5, 5);
1433                                 else if (this.impulse == 11)
1434                                         this.spectatorspeed = maxspeed_mod;
1435                                 else if (this.impulse == 12 || this.impulse == 16  || this.impulse == 19 || (this.impulse >= 220 && this.impulse <= 229))
1436                                         this.spectatorspeed = bound(1, this.spectatorspeed - 0.5, 5);
1437                                 else if (this.impulse >= 1 && this.impulse <= 9)
1438                                         this.spectatorspeed = 1 + 0.5 * (this.impulse - 1);
1439                         } // otherwise just clear
1440                         this.impulse = 0;
1441                 }
1442 #endif
1443                 maxspeed_mod = this.spectatorspeed;
1444         }
1445 #ifdef SVQC
1446
1447         float spd = max(PHYS_MAXSPEED(this), PHYS_MAXAIRSPEED(this)) * maxspeed_mod;
1448         if(this.speed != spd)
1449         {
1450                 this.speed = spd;
1451                 string temps = ftos(spd);
1452                 stuffcmd(this, strcat("cl_forwardspeed ", temps, "\n"));
1453                 stuffcmd(this, strcat("cl_backspeed ", temps, "\n"));
1454                 stuffcmd(this, strcat("cl_sidespeed ", temps, "\n"));
1455                 stuffcmd(this, strcat("cl_upspeed ", temps, "\n"));
1456         }
1457
1458         if(this.jumpspeedcap_min != autocvar_sv_jumpspeedcap_min)
1459         {
1460                 this.jumpspeedcap_min = autocvar_sv_jumpspeedcap_min;
1461                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_min \"%s\"\n", autocvar_sv_jumpspeedcap_min));
1462         }
1463         if(this.jumpspeedcap_max != autocvar_sv_jumpspeedcap_max)
1464         {
1465                 this.jumpspeedcap_max = autocvar_sv_jumpspeedcap_max;
1466                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_max \"%s\"\n", autocvar_sv_jumpspeedcap_max));
1467         }
1468 #endif
1469
1470         if(IS_DEAD(this))
1471         {
1472                 // handle water here
1473                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
1474                 if(pointcontents(midpoint) == CONTENT_WATER)
1475                 {
1476                         this.velocity = this.velocity * 0.5;
1477
1478                         // do we want this?
1479                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1480                                 //{ this.velocity_z = 70; }
1481                 }
1482                 goto end;
1483         }
1484
1485 #ifdef SVQC
1486         if (!this.fixangle)
1487                 this.angles = '0 1 0' * this.v_angle.y;
1488 #endif
1489
1490         if (IS_PLAYER(this) && IS_ONGROUND(this))
1491         {
1492                 PM_check_hitground(this);
1493                 PM_Footsteps(this);
1494         }
1495
1496 #ifdef SVQC
1497         if(IsFlying(this))
1498                 this.wasFlying = 1;
1499 #endif
1500
1501         if (IS_PLAYER(this))
1502                 CheckPlayerJump(this);
1503
1504         if (this.flags & FL_WATERJUMP)
1505         {
1506                 this.velocity_x = this.movedir.x;
1507                 this.velocity_y = this.movedir.y;
1508                 if (time > PHYS_TELEPORT_TIME(this) || this.waterlevel == WATERLEVEL_NONE
1509                 #ifdef CSQC
1510                         || pmove_waterjumptime <= 0
1511                 #endif
1512                         )
1513                 {
1514                         this.flags &= ~FL_WATERJUMP;
1515                         PHYS_TELEPORT_TIME(this) = 0;
1516                 #ifdef CSQC
1517                         pmove_waterjumptime = 0;
1518                 #endif
1519                 }
1520         }
1521
1522         else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod))
1523                 { }
1524
1525 #ifdef SVQC
1526         else if (this.movetype == MOVETYPE_NOCLIP || this.movetype == MOVETYPE_FLY || this.movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1527 #elif defined(CSQC)
1528         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1529 #endif
1530                 PM_fly(this, maxspeed_mod);
1531
1532         else if (this.waterlevel >= WATERLEVEL_SWIMMING)
1533                 PM_swim(this, maxspeed_mod);
1534
1535         else if (time < this.ladder_time)
1536                 PM_ladder(this, maxspeed_mod);
1537
1538         else if (ITEMS_STAT(this) & IT_USING_JETPACK)
1539                 PM_jetpack(this, maxspeed_mod);
1540
1541         else if (IS_ONGROUND(this))
1542                 PM_walk(this, maxspeed_mod);
1543
1544         else
1545                 PM_air(this, buttons_prev, maxspeed_mod);
1546
1547 LABEL(end)
1548         if (IS_ONGROUND(this))
1549                 this.lastground = time;
1550
1551         // conveyors: then break velocity again
1552         if(this.conveyor.state)
1553                 this.velocity += this.conveyor.movedir;
1554
1555         this.lastflags = this.flags;
1556
1557         this.lastclassname = this.classname;
1558 }
1559
1560 #if defined(SVQC)
1561 void SV_PlayerPhysics(entity this)
1562 #elif defined(CSQC)
1563 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
1564 #endif
1565 {
1566         PM_Main(this);
1567 }