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