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