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