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