]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
Merge branch 'master' into TimePath/experiments/csqc_prediction
[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         // WEAPONTODO
1028         float xyspeed = vlen(vec2(self.velocity));
1029         if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1030         {
1031                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1032                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1033                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1034                 // add the extra charge
1035                 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * frametime);
1036         }
1037 #endif
1038 }
1039
1040 void PM_fly(float maxspd_mod)
1041 {
1042         // noclipping or flying
1043         UNSET_ONGROUND(self);
1044
1045         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1046         makevectors(PHYS_INPUT_ANGLES(self));
1047         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1048         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1049                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y
1050                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1051         // acceleration
1052         vector wishdir = normalize(wishvel);
1053         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1054         if (time >= self.teleport_time)
1055                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1056 }
1057
1058 void PM_swim(float maxspd_mod)
1059 {
1060         // swimming
1061         UNSET_ONGROUND(self);
1062
1063         float jump = PHYS_INPUT_BUTTON_JUMP(self);
1064         // water jump only in certain situations
1065         // this mimics quakeworld code
1066         if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1067         {
1068                 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1069                 makevectors(yawangles);
1070                 vector forward = v_forward;
1071                 vector spot = self.origin + 24 * forward;
1072                 spot_z += 8;
1073                 traceline(spot, spot, MOVE_NOMONSTERS, self);
1074                 if (trace_startsolid)
1075                 {
1076                         spot_z += 24;
1077                         traceline(spot, spot, MOVE_NOMONSTERS, self);
1078                         if (!trace_startsolid)
1079                         {
1080                                 self.velocity = forward * 50;
1081                                 self.velocity_z = 310;
1082                                 pmove_waterjumptime = 2;
1083                                 UNSET_ONGROUND(self);
1084                                 SET_JUMP_HELD(self);
1085                         }
1086                 }
1087         }
1088         makevectors(PHYS_INPUT_ANGLES(self));
1089         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1090         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1091                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y
1092                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1093         if (wishvel == '0 0 0')
1094                 wishvel = '0 0 -60'; // drift towards bottom
1095
1096         vector wishdir = normalize(wishvel);
1097         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1098
1099         if (IS_DUCKED(self))
1100         wishspeed *= 0.5;
1101
1102 //      if (pmove_waterjumptime <= 0) // TODO: use
1103     {
1104                 // water friction
1105                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1106                 f = min(max(0, f), 1);
1107                 self.velocity *= f;
1108
1109                 f = wishspeed - self.velocity * wishdir;
1110                 if (f > 0)
1111                 {
1112                         float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1113                         self.velocity += accelspeed * wishdir;
1114                 }
1115
1116                 // holding jump button swims upward slowly
1117                 if (jump)
1118                 {
1119 #if 0
1120                         if (self.watertype & CONTENT_LAVA)
1121                                 self.velocity_z =  50;
1122                         else if (self.watertype & CONTENT_SLIME)
1123                                 self.velocity_z =  80;
1124                         else
1125                         {
1126                                 if (IS_NEXUIZ_DERIVED(gamemode))
1127 #endif
1128                                         self.velocity_z = 200;
1129 #if 0
1130                                 else
1131                                         self.velocity_z = 100;
1132                         }
1133 #endif
1134                 }
1135         }
1136         PM_ClientMovement_Move();
1137         // water acceleration
1138         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1139 }
1140
1141 void PM_ladder(float maxspd_mod)
1142 {
1143         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1144         UNSET_ONGROUND(self);
1145
1146         float g;
1147         g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1148         if (PHYS_ENTGRAVITY(self))
1149                 g *= PHYS_ENTGRAVITY(self);
1150         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1151         {
1152                 g *= 0.5;
1153                 self.velocity_z += g;
1154         }
1155
1156         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1157         makevectors(PHYS_INPUT_ANGLES(self));
1158         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1159         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1160                                         + v_right * PHYS_INPUT_MOVEVALUES(self)_y
1161                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self)_z;
1162         self.velocity_z += g;
1163         if (self.ladder_entity.classname == "func_water")
1164         {
1165                 float f = vlen(wishvel);
1166                 if (f > self.ladder_entity.speed)
1167                         wishvel *= (self.ladder_entity.speed / f);
1168
1169                 self.watertype = self.ladder_entity.skin;
1170                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1171                 if ((self.origin_z + self.view_ofs_z) < f)
1172                         self.waterlevel = WATERLEVEL_SUBMERGED;
1173                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1174                         self.waterlevel = WATERLEVEL_SWIMMING;
1175                 else if ((self.origin_z + self.mins_z + 1) < f)
1176                         self.waterlevel = WATERLEVEL_WETFEET;
1177                 else
1178                 {
1179                         self.waterlevel = WATERLEVEL_NONE;
1180                         self.watertype = CONTENT_EMPTY;
1181                 }
1182         }
1183         // acceleration
1184         vector wishdir = normalize(wishvel);
1185         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1186         PM_ClientMovement_Move();
1187 #ifdef SVQC
1188         if (time >= self.teleport_time)
1189 #endif
1190                 // water acceleration
1191                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1192 }
1193
1194 void PM_jetpack(float maxspd_mod)
1195 {
1196         //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1197         makevectors(PHYS_INPUT_ANGLES(self));
1198         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1199                                         + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1200         // add remaining speed as Z component
1201         float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1202         // fix speedhacks :P
1203         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1204         // add the unused velocity as up component
1205         wishvel_z = 0;
1206
1207         // if (self.BUTTON_JUMP)
1208                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1209
1210         // it is now normalized, so...
1211         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1212         float a_up = PHYS_JETPACK_ACCEL_UP;
1213         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1214
1215         wishvel_x *= a_side;
1216         wishvel_y *= a_side;
1217         wishvel_z *= a_up;
1218         wishvel_z += a_add;
1219
1220         float best = 0;
1221         //////////////////////////////////////////////////////////////////////////////////////
1222         // finding the maximum over all vectors of above form
1223         // with wishvel having an absolute value of 1
1224         //////////////////////////////////////////////////////////////////////////////////////
1225         // we're finding the maximum over
1226         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1227         // for z in the range from -1 to 1
1228         //////////////////////////////////////////////////////////////////////////////////////
1229         // maximum is EITHER attained at the single extreme point:
1230         float a_diff = a_side * a_side - a_up * a_up;
1231         float f;
1232         if (a_diff != 0)
1233         {
1234                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1235                 if (f > -1 && f < 1) // can it be attained?
1236                 {
1237                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1238                         //print("middle\n");
1239                 }
1240         }
1241         // OR attained at z = 1:
1242         f = (a_up + a_add) * (a_up + a_add);
1243         if (f > best)
1244         {
1245                 best = f;
1246                 //print("top\n");
1247         }
1248         // OR attained at z = -1:
1249         f = (a_up - a_add) * (a_up - a_add);
1250         if (f > best)
1251         {
1252                 best = f;
1253                 //print("bottom\n");
1254         }
1255         best = sqrt(best);
1256         //////////////////////////////////////////////////////////////////////////////////////
1257
1258         //print("best possible acceleration: ", ftos(best), "\n");
1259
1260         float fxy, fz;
1261         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1262         if (wishvel_z - PHYS_GRAVITY > 0)
1263                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1264         else
1265                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1266
1267         float fvel;
1268         fvel = vlen(wishvel);
1269         wishvel_x *= fxy;
1270         wishvel_y *= fxy;
1271         wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1272
1273         fvel = min(1, vlen(wishvel) / best);
1274         if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1275                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1276         else
1277                 f = 1;
1278
1279         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1280
1281         if (f > 0 && wishvel != '0 0 0')
1282         {
1283                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1284                 UNSET_ONGROUND(self);
1285
1286 #ifdef SVQC
1287                 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1288                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1289
1290                 self.items |= IT_USING_JETPACK;
1291
1292                 // jetpack also inhibits health regeneration, but only for 1 second
1293                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1294 #endif
1295         }
1296
1297 #ifdef CSQC
1298         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1299         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1300                 self.velocity_z -= g * 0.5;
1301         else
1302                 self.velocity_z -= g;
1303         PM_ClientMovement_Move();
1304         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1305                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1306                         self.velocity_z -= g * 0.5;
1307 #endif
1308 }
1309
1310 void PM_walk(float buttons_prev, float maxspd_mod)
1311 {
1312 #ifdef SVQC
1313         // we get here if we ran out of ammo
1314         if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1315                 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1316         if (!(self.lastflags & FL_ONGROUND))
1317         {
1318                 if (autocvar_speedmeter)
1319                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1320                 if (self.lastground < time - 0.3)
1321                         self.velocity *= (1 - autocvar_sv_friction_on_land);
1322                 if (self.jumppadcount > 1)
1323                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1324                 self.jumppadcount = 0;
1325         }
1326 #endif
1327         // walking
1328         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1329         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1330                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1331         // acceleration
1332         vector wishdir = normalize(wishvel);
1333         float wishspeed = vlen(wishvel);
1334
1335         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1336         if (IS_DUCKED(self))
1337                 wishspeed *= 0.5;
1338
1339         // apply edge friction
1340         float f = vlen(vec2(self.velocity));
1341         if (f > 0)
1342         {
1343                 // TODO: apply edge friction
1344                 // apply ground friction
1345                 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1346                 f = max(0, f);
1347                 self.velocity *= f;
1348                 /*
1349                    Mathematical analysis time!
1350
1351                    Our goal is to invert this mess.
1352
1353                    For the two cases we get:
1354                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1355                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1356                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1357                    and
1358                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1359                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1360
1361                    These cases would be chosen ONLY if:
1362                         v0 < PHYS_STOPSPEED
1363                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1364                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1365                    and, respectively:
1366                         v0 >= PHYS_STOPSPEED
1367                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1368                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1369                  */
1370         }
1371         float addspeed = wishspeed - self.velocity * wishdir;
1372         if (addspeed > 0)
1373         {
1374                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1375                 self.velocity += accelspeed * wishdir;
1376         }
1377         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1378         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1379                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1380         if (self.velocity * self.velocity)
1381                 PM_ClientMovement_Move();
1382         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1383                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1384                         self.velocity_z -= g * 0.5;
1385 }
1386
1387 void PM_air(float buttons_prev, float maxspd_mod)
1388 {
1389 #ifdef SVQC
1390         // we get here if we ran out of ammo
1391         if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && PHYS_AMMO_FUEL(self) < 0.01)
1392                 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1393 #endif
1394         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1395         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1396                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1397         // acceleration
1398         vector wishdir = normalize(wishvel);
1399         float wishspeed = vlen(wishvel);
1400
1401 #ifdef SVQC
1402         if (time >= self.teleport_time)
1403 #else
1404         if (pmove_waterjumptime <= 0)
1405 #endif
1406         {
1407                 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1408
1409                 // apply air speed limit
1410                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1411                 float wishspeed0 = wishspeed;
1412                 wishspeed = min(wishspeed, maxairspd);
1413                 if (IS_DUCKED(self))
1414                         wishspeed *= 0.5;
1415                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1416
1417                 float accelerating = (self.velocity * wishdir > 0);
1418                 float wishspeed2 = wishspeed;
1419
1420                 // CPM: air control
1421                 if (PHYS_AIRSTOPACCELERATE)
1422                 {
1423                         vector curdir = normalize(vec2(self.velocity));
1424                         airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1425                 }
1426                 // note that for straight forward jumping:
1427                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1428                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1429                 // -->
1430                 // dv/dt = accel * maxspeed (when slow)
1431                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1432                 // log dv/dt = logaccel + logmaxspeed (when slow)
1433                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1434                 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1435                 if (PHYS_MAXAIRSTRAFESPEED)
1436                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1437                 if (PHYS_AIRSTRAFEACCELERATE)
1438                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1439                 if (PHYS_AIRSTRAFEACCEL_QW(self))
1440                         airaccelqw =
1441                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1442                 *
1443                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1444                 // !CPM
1445
1446                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1447                         PM_AirAccelerate(wishdir, wishspeed2);
1448                 else
1449                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1450
1451                 if (PHYS_AIRCONTROL)
1452                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1453         }
1454         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1455         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1456                 self.velocity_z -= g * 0.5;
1457         else
1458                 self.velocity_z -= g;
1459         PM_ClientMovement_Move();
1460         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1461                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1462                         self.velocity_z -= g * 0.5;
1463 }
1464
1465 // used for calculating airshots
1466 float PM_is_flying()
1467 {
1468         if (IS_ONGROUND(self))
1469                 return 0;
1470         if (self.waterlevel >= WATERLEVEL_SWIMMING)
1471                 return 0;
1472         traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1473         return trace_fraction >= 1;
1474 }
1475
1476 void PM_Main()
1477 {
1478         float buttons = PHYS_INPUT_BUTTON_MASK(self);
1479 #ifdef CSQC
1480         //Con_Printf(" %f", PHYS_INPUT_TIMELENGTH);
1481         if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1482                 UNSET_JUMP_HELD(self); // canjump = true
1483         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1484         PM_ClientMovement_UpdateStatus();
1485 #endif
1486
1487 #ifdef SVQC
1488         WarpZone_PlayerPhysics_FixVAngle();
1489 #endif
1490         float maxspeed_mod = 1;
1491         maxspeed_mod *= PM_check_keepaway();
1492         maxspeed_mod *= PHYS_HIGHSPEED;
1493
1494 #ifdef SVQC
1495         Physics_UpdateStats(maxspeed_mod);
1496
1497         if (self.PlayerPhysplug)
1498                 if (self.PlayerPhysplug())
1499                         return;
1500 #endif
1501
1502         PM_check_race_movetime();
1503 #ifdef SVQC
1504         anticheat_physics();
1505 #endif
1506
1507         if (PM_check_specialcommand(buttons))
1508                 return;
1509 #ifdef SVQC
1510         if (sv_maxidle > 0)
1511         {
1512                 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1513                         self.parm_idlesince = time;
1514         }
1515 #endif
1516         float buttons_prev = self.buttons_old;
1517         self.buttons_old = buttons;
1518         self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1519         self.v_angle_old = PHYS_INPUT_ANGLES(self);
1520
1521         PM_check_nickspam();
1522
1523         PM_check_punch();
1524 #ifdef SVQC
1525         if (IS_BOT_CLIENT(self))
1526         {
1527                 if (playerdemo_read())
1528                         return;
1529                 bot_think();
1530         }
1531
1532         self.items &= ~IT_USING_JETPACK;
1533
1534         if (IS_PLAYER(self))
1535 #endif
1536         {
1537 #ifdef SVQC
1538                 if (self.race_penalty)
1539                         if (time > self.race_penalty)
1540                                 self.race_penalty = 0;
1541 #endif
1542
1543                 float not_allowed_to_move = 0;
1544 #ifdef SVQC
1545                 if (self.race_penalty)
1546                         not_allowed_to_move = 1;
1547 #endif
1548 #ifdef SVQC
1549                 if (!autocvar_sv_ready_restart_after_countdown)
1550                         if (time < game_starttime)
1551                                 not_allowed_to_move = 1;
1552 #endif
1553
1554                 if (not_allowed_to_move)
1555                 {
1556                         self.velocity = '0 0 0';
1557                         self.movetype = MOVETYPE_NONE;
1558 #ifdef SVQC
1559                         self.disableclientprediction = 2;
1560 #endif
1561                 }
1562 #ifdef SVQC
1563                 else if (self.disableclientprediction == 2)
1564                 {
1565                         if (self.movetype == MOVETYPE_NONE)
1566                                 self.movetype = MOVETYPE_WALK;
1567                         self.disableclientprediction = 0;
1568                 }
1569 #endif
1570         }
1571
1572 #ifdef SVQC
1573         if (self.movetype == MOVETYPE_NONE)
1574                 return;
1575
1576         // when we get here, disableclientprediction cannot be 2
1577         self.disableclientprediction = 0;
1578 #endif
1579
1580         PM_check_spider();
1581
1582         PM_check_frozen();
1583
1584         PM_check_blocked();
1585
1586         maxspeed_mod = 1;
1587
1588 #ifdef SVQC
1589         if (self.in_swamp) {
1590                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1591         }
1592 #endif
1593
1594 #ifdef SVQC
1595         // conveyors: first fix velocity
1596         if (self.conveyor.state)
1597                 self.velocity -= self.conveyor.movedir;
1598 #endif
1599
1600 #ifdef SVQC
1601         MUTATOR_CALLHOOK(PlayerPhysics);
1602 #endif
1603 //      float forcedodge = 1;
1604 //      if(forcedodge) {
1605 //#ifdef CSQC
1606 //              PM_dodging_checkpressedkeys();
1607 //#endif
1608 //              PM_dodging();
1609 //              PM_ClientMovement_Move();
1610 //              return;
1611 //      }
1612
1613 #ifdef SVQC
1614         if (!IS_PLAYER(self))
1615         {
1616                 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1617                 if (!self.spectatorspeed)
1618                         self.spectatorspeed = maxspeed_mod;
1619                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1620                 {
1621                         if (self.lastclassname != "player")
1622                         {
1623                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1624                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1625                                 else if (self.impulse == 11)
1626                                         self.spectatorspeed = maxspeed_mod;
1627                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1628                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1629                                 else if (self.impulse >= 1 && self.impulse <= 9)
1630                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1631                         } // otherwise just clear
1632                         self.impulse = 0;
1633                 }
1634                 maxspeed_mod *= self.spectatorspeed;
1635         }
1636 #endif
1637
1638 #ifdef SVQC
1639         // if dead, behave differently
1640         // in CSQC, physics don't handle dead player
1641         if (self.deadflag)
1642                 goto end;
1643 #endif
1644
1645 #ifdef SVQC
1646         if (!self.fixangle && !g_bugrigs)
1647                 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1648 #endif
1649
1650 #ifdef SVQC
1651         if (IS_ONGROUND(self))
1652         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1653         if (self.wasFlying)
1654         {
1655                 self.wasFlying = 0;
1656                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1657                 if (time >= self.ladder_time)
1658                 if (!self.hook)
1659                 {
1660                         self.nextstep = time + 0.3 + random() * 0.1;
1661                         trace_dphitq3surfaceflags = 0;
1662                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1663                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1664                         {
1665                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1666                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1667                                 else
1668                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1669                         }
1670                 }
1671         }
1672 #endif
1673
1674         if (PM_is_flying())
1675                 self.wasFlying = 1;
1676
1677 #ifdef SVQC
1678         if (IS_PLAYER(self))
1679 #endif
1680                 CheckPlayerJump();
1681
1682
1683         if (self.flags & /* FL_WATERJUMP */ 2048)
1684         {
1685                 self.velocity_x = self.movedir_x;
1686                 self.velocity_y = self.movedir_y;
1687                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1688                 {
1689                         self.flags &= ~/* FL_WATERJUMP */ 2048;
1690                         self.teleport_time = 0;
1691                 }
1692         }
1693
1694 #ifdef SVQC
1695         else if (g_bugrigs && IS_PLAYER(self))
1696                 RaceCarPhysics();
1697 #endif
1698
1699         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1700                 PM_fly(maxspeed_mod);
1701
1702         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1703                 PM_swim(maxspeed_mod);
1704
1705         else if (time < self.ladder_time)
1706                 PM_ladder(maxspeed_mod);
1707
1708         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))
1709                 PM_jetpack(maxspeed_mod);
1710
1711         else
1712         {
1713 #ifdef CSQC
1714                 // jump if on ground with jump button pressed but only if it has been
1715                 // released at least once since the last jump
1716                 if (PHYS_INPUT_BUTTON_JUMP(self))
1717                 {
1718                         if (IS_ONGROUND(self) && (!IS_JUMP_HELD(self) || !cvar("cl_movement_track_canjump")))
1719                         {
1720                                 self.velocity_z += PHYS_JUMPVELOCITY;
1721                                 UNSET_ONGROUND(self);
1722                                 SET_JUMP_HELD(self); // canjump = false
1723                         }
1724                 }
1725                 else
1726                         UNSET_JUMP_HELD(self); // canjump = true
1727 #endif
1728                 if (IS_ONGROUND(self))
1729                         PM_walk(buttons_prev, maxspeed_mod);
1730                 else
1731                         PM_air(buttons_prev, maxspeed_mod);
1732         }
1733
1734 #ifdef SVQC
1735         if (!IS_OBSERVER(self))
1736                 PM_check_race();
1737 #endif
1738         PM_check_vortex();
1739
1740 :end
1741         if (IS_ONGROUND(self))
1742                 self.lastground = time;
1743
1744 #ifdef SVQC
1745         // conveyors: then break velocity again
1746         if (self.conveyor.state)
1747                 self.velocity += self.conveyor.movedir;
1748 #endif
1749         self.lastflags = self.flags;
1750         self.lastclassname = self.classname;
1751 }
1752
1753 void CSQC_ClientMovement_PlayerMove_Frame()
1754 {
1755         // if a move is more than 50ms, do it as two moves (matching qwsv)
1756         //Con_Printf("%i ", self.cmd.msec);
1757         if (PHYS_INPUT_TIMELENGTH > 0.0005)
1758         {
1759                 if (PHYS_INPUT_TIMELENGTH > 0.05)
1760                 {
1761                         PHYS_INPUT_TIMELENGTH /= 2;
1762                         PM_Main();
1763                 }
1764                 PM_Main();
1765         }
1766         else
1767                 // we REALLY need this handling to happen, even if the move is not executed
1768                 if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1769                         UNSET_JUMP_HELD(self); // canjump = true
1770 }
1771
1772 #ifdef SVQC
1773 // Entry point
1774 void SV_PlayerPhysics(void)
1775 {
1776         PM_Main();
1777 }
1778 #endif