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