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