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