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