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