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