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