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