]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
Fix slope sliding
[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                   cvar("sv_gameplayfix_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)                                       getstati(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 PM_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 void PM_ClientMovement_UpdateStatus()
272 {
273         // make sure player is not stuck
274         PM_ClientMovement_Unstick();
275
276         // set crouched
277         if (PHYS_INPUT_BUTTONS(self) & 16)
278         {
279                 // wants to crouch, this always works..
280                 if (!IS_DUCKED(self))
281                         SET_DUCKED(self);
282         }
283         else
284         {
285                 // wants to stand, if currently crouching we need to check for a
286                 // low ceiling first
287                 if (IS_DUCKED(self))
288                 {
289                         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
290                         if (!trace_startsolid)
291                                 UNSET_DUCKED(self);
292                 }
293         }
294
295         // set onground
296         vector origin1 = self.origin + '0 0 1';
297         vector origin2 = self.origin - '0 0 1';
298
299         tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
300         if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
301         {
302                 SET_ONGROUND(self);
303
304                 // this code actually "predicts" an impact; so let's clip velocity first
305                 float f = dotproduct(self.velocity, trace_plane_normal);
306                 if (f < 0) // only if moving downwards actually
307                         self.velocity -= f * trace_plane_normal;
308         }
309         else
310                 UNSET_ONGROUND(self);
311
312         // set watertype/waterlevel
313         origin1 = self.origin;
314         origin1_z += self.mins_z + 1;
315         self.waterlevel = WATERLEVEL_NONE;
316
317         self.watertype = (pointcontents(origin1) == CONTENT_WATER);
318
319         if(self.watertype)
320         {
321                 self.waterlevel = WATERLEVEL_WETFEET;
322                 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
323                 if(pointcontents(origin1) == CONTENT_WATER)
324                 {
325                         self.waterlevel = WATERLEVEL_SWIMMING;
326                         origin1_z = self.origin_z + 22;
327                         if(pointcontents(origin1) == CONTENT_WATER)
328                                 self.waterlevel = WATERLEVEL_SUBMERGED;
329                 }
330         }
331
332         if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
333                 pmove_waterjumptime = 0;
334 }
335
336 void PM_ClientMovement_Move()
337 {
338 #ifdef CSQC
339         float t = PHYS_INPUT_TIMELENGTH;
340         vector primalvelocity = self.velocity;
341         PM_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 #endif
408 }
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         PM_ClientMovement_Move();
1275         // water acceleration
1276         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1277 }
1278
1279 void PM_ladder(float maxspd_mod)
1280 {
1281 #ifdef SVQC
1282         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1283         UNSET_ONGROUND(self);
1284
1285         float g;
1286         g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1287         if (PHYS_ENTGRAVITY(self))
1288                 g *= PHYS_ENTGRAVITY(self);
1289         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1290         {
1291                 g *= 0.5;
1292                 self.velocity_z += g;
1293         }
1294
1295         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1296         makevectors(PHYS_INPUT_ANGLES(self));
1297         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1298         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1299                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y
1300                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1301         self.velocity_z += g;
1302         if (self.ladder_entity.classname == "func_water")
1303         {
1304                 float f = vlen(wishvel);
1305                 if (f > self.ladder_entity.speed)
1306                         wishvel *= (self.ladder_entity.speed / f);
1307
1308                 self.watertype = self.ladder_entity.skin;
1309                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1310                 if ((self.origin_z + self.view_ofs_z) < f)
1311                         self.waterlevel = WATERLEVEL_SUBMERGED;
1312                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1313                         self.waterlevel = WATERLEVEL_SWIMMING;
1314                 else if ((self.origin_z + self.mins_z + 1) < f)
1315                         self.waterlevel = WATERLEVEL_WETFEET;
1316                 else
1317                 {
1318                         self.waterlevel = WATERLEVEL_NONE;
1319                         self.watertype = CONTENT_EMPTY;
1320                 }
1321         }
1322         // acceleration
1323         vector wishdir = normalize(wishvel);
1324         float wishspeed = min(vlen(wishvel), self.stat_sv_maxspeed * maxspd_mod);
1325         if (time >= self.teleport_time)
1326                 // water acceleration
1327                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1328 #endif
1329 }
1330
1331 void PM_jetpack(float maxspd_mod)
1332 {
1333         //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1334         makevectors(PHYS_INPUT_ANGLES(self));
1335         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1336                                         + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1337         // add remaining speed as Z component
1338         float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1339         // fix speedhacks :P
1340         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1341         // add the unused velocity as up component
1342         wishvel_z = 0;
1343
1344         // if (self.BUTTON_JUMP)
1345                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1346
1347         // it is now normalized, so...
1348         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1349         float a_up = PHYS_JETPACK_ACCEL_UP;
1350         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1351
1352         wishvel_x *= a_side;
1353         wishvel_y *= a_side;
1354         wishvel_z *= a_up;
1355         wishvel_z += a_add;
1356
1357         float best = 0;
1358         //////////////////////////////////////////////////////////////////////////////////////
1359         // finding the maximum over all vectors of above form
1360         // with wishvel having an absolute value of 1
1361         //////////////////////////////////////////////////////////////////////////////////////
1362         // we're finding the maximum over
1363         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1364         // for z in the range from -1 to 1
1365         //////////////////////////////////////////////////////////////////////////////////////
1366         // maximum is EITHER attained at the single extreme point:
1367         float a_diff = a_side * a_side - a_up * a_up;
1368         float f;
1369         if (a_diff != 0)
1370         {
1371                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1372                 if (f > -1 && f < 1) // can it be attained?
1373                 {
1374                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1375                         //print("middle\n");
1376                 }
1377         }
1378         // OR attained at z = 1:
1379         f = (a_up + a_add) * (a_up + a_add);
1380         if (f > best)
1381         {
1382                 best = f;
1383                 //print("top\n");
1384         }
1385         // OR attained at z = -1:
1386         f = (a_up - a_add) * (a_up - a_add);
1387         if (f > best)
1388         {
1389                 best = f;
1390                 //print("bottom\n");
1391         }
1392         best = sqrt(best);
1393         //////////////////////////////////////////////////////////////////////////////////////
1394
1395         //print("best possible acceleration: ", ftos(best), "\n");
1396
1397         float fxy, fz;
1398         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1399         if (wishvel_z - PHYS_GRAVITY > 0)
1400                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1401         else
1402                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1403
1404         float fvel;
1405         fvel = vlen(wishvel);
1406         wishvel_x *= fxy;
1407         wishvel_y *= fxy;
1408         wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1409
1410         fvel = min(1, vlen(wishvel) / best);
1411         if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1412                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1413         else
1414                 f = 1;
1415
1416         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1417
1418         if (f > 0 && wishvel != '0 0 0')
1419         {
1420                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1421                 UNSET_ONGROUND(self);
1422
1423 #ifdef SVQC
1424                 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1425                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1426
1427                 self.items |= IT_USING_JETPACK;
1428
1429                 // jetpack also inhibits health regeneration, but only for 1 second
1430                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1431 #endif
1432         }
1433
1434 #ifdef CSQC
1435         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1436         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1437                 self.velocity_z -= g * 0.5;
1438         else
1439                 self.velocity_z -= g;
1440         PM_ClientMovement_Move();
1441         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1442                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1443                         self.velocity_z -= g * 0.5;
1444 #endif
1445 }
1446
1447 void PM_walk(float buttons_prev, float maxspd_mod)
1448 {
1449 #ifdef SVQC
1450         // we get here if we ran out of ammo
1451         if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1452                 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
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         // walking
1465         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1466         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1467                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1468         // acceleration
1469         vector wishdir = normalize(wishvel);
1470         float wishspeed = vlen(wishvel);
1471
1472         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1473         if (IS_DUCKED(self))
1474                 wishspeed *= 0.5;
1475
1476         // apply edge friction
1477         float f = vlen(vec2(self.velocity));
1478         if (f > 0)
1479         {
1480                 // TODO: apply edge friction
1481                 // apply ground friction
1482                 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1483                 f = max(0, f);
1484                 self.velocity *= f;
1485                 /*
1486                    Mathematical analysis time!
1487
1488                    Our goal is to invert this mess.
1489
1490                    For the two cases we get:
1491                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1492                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1493                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1494                    and
1495                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1496                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1497
1498                    These cases would be chosen ONLY if:
1499                         v0 < PHYS_STOPSPEED
1500                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1501                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1502                    and, respectively:
1503                         v0 >= PHYS_STOPSPEED
1504                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1505                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1506                  */
1507         }
1508         float addspeed = wishspeed - self.velocity * wishdir;
1509         if (addspeed > 0)
1510         {
1511                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1512                 self.velocity += accelspeed * wishdir;
1513         }
1514         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1515         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1516                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1517         if (self.velocity * self.velocity)
1518                 PM_ClientMovement_Move();
1519         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1520                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1521                         self.velocity_z -= g * 0.5;
1522 }
1523
1524 void PM_air(float buttons_prev, float maxspd_mod)
1525 {
1526 #ifdef SVQC
1527         // we get here if we ran out of ammo
1528         if ((ITEMS(self) & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && PHYS_AMMO_FUEL(self) < 0.01)
1529                 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1530 #endif
1531         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1532         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1533                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1534         // acceleration
1535         vector wishdir = normalize(wishvel);
1536         float wishspeed = vlen(wishvel);
1537
1538 #ifdef SVQC
1539         if (time >= self.teleport_time)
1540 #else
1541         if (pmove_waterjumptime <= 0)
1542 #endif
1543         {
1544                 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1545
1546                 // apply air speed limit
1547                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1548                 float wishspeed0 = wishspeed;
1549                 wishspeed = min(wishspeed, maxairspd);
1550                 if (IS_DUCKED(self))
1551                         wishspeed *= 0.5;
1552                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1553
1554                 float accelerating = (self.velocity * wishdir > 0);
1555                 float wishspeed2 = wishspeed;
1556
1557                 // CPM: air control
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 =
1578                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1579                 *
1580                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1581                 // !CPM
1582
1583                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1584                         PM_AirAccelerate(wishdir, wishspeed2);
1585                 else
1586                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1587
1588                 if (PHYS_AIRCONTROL)
1589                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1590         }
1591         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1592         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1593                 self.velocity_z -= g * 0.5;
1594         else
1595                 self.velocity_z -= g;
1596         PM_ClientMovement_Move();
1597         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1598                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1599                         self.velocity_z -= g * 0.5;
1600 }
1601
1602 // used for calculating airshots
1603 float PM_is_flying()
1604 {
1605         if (IS_ONGROUND(self))
1606                 return 0;
1607         if (self.waterlevel >= WATERLEVEL_SWIMMING)
1608                 return 0;
1609         traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1610         return trace_fraction >= 1;
1611 }
1612
1613 void PM_Main()
1614 {
1615 #ifdef CSQC
1616         //Con_Printf(" %f", PHYS_INPUT_TIMELENGTH);
1617         if (!(PHYS_INPUT_BUTTONS(self) & 2)) // !jump
1618                 UNSET_JUMP_HELD(self); // canjump = true
1619         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1620         PM_ClientMovement_UpdateStatus();
1621 #endif
1622
1623 #ifdef SVQC
1624         WarpZone_PlayerPhysics_FixVAngle();
1625 #endif
1626         float maxspeed_mod = 1;
1627         maxspeed_mod *= PM_check_keepaway();
1628         maxspeed_mod *= PHYS_HIGHSPEED;
1629
1630 #ifdef SVQC
1631         Physics_UpdateStats(maxspeed_mod);
1632
1633         if (self.PlayerPhysplug)
1634                 if (self.PlayerPhysplug())
1635                         return;
1636 #endif
1637
1638         PM_check_race_movetime();
1639 #ifdef SVQC
1640         anticheat_physics();
1641 #endif
1642         float buttons = PHYS_INPUT_BUTTONS(self);
1643
1644         if (PM_check_specialcommand(buttons))
1645                 return;
1646 #ifdef SVQC
1647         if (sv_maxidle > 0)
1648         {
1649                 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1650                         self.parm_idlesince = time;
1651         }
1652 #endif
1653         float buttons_prev = self.buttons_old;
1654         self.buttons_old = buttons;
1655         self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1656         self.v_angle_old = PHYS_INPUT_ANGLES(self);
1657
1658         PM_check_nickspam();
1659
1660         PM_check_punch();
1661 #ifdef SVQC
1662         if (IS_BOT_CLIENT(self))
1663         {
1664                 if (playerdemo_read())
1665                         return;
1666                 bot_think();
1667         }
1668
1669         self.items &= ~IT_USING_JETPACK;
1670
1671         if (IS_PLAYER(self))
1672 #endif
1673         {
1674 #ifdef SVQC
1675                 if (self.race_penalty)
1676                         if (time > self.race_penalty)
1677                                 self.race_penalty = 0;
1678 #endif
1679
1680                 float not_allowed_to_move = 0;
1681 #ifdef SVQC
1682                 if (self.race_penalty)
1683                         not_allowed_to_move = 1;
1684 #endif
1685 #ifdef SVQC
1686                 if (!autocvar_sv_ready_restart_after_countdown)
1687                         if (time < game_starttime)
1688                                 not_allowed_to_move = 1;
1689 #endif
1690
1691                 if (not_allowed_to_move)
1692                 {
1693                         self.velocity = '0 0 0';
1694                         self.movetype = MOVETYPE_NONE;
1695 #ifdef SVQC
1696                         self.disableclientprediction = 2;
1697 #endif
1698                 }
1699 #ifdef SVQC
1700                 else if (self.disableclientprediction == 2)
1701                 {
1702                         if (self.movetype == MOVETYPE_NONE)
1703                                 self.movetype = MOVETYPE_WALK;
1704                         self.disableclientprediction = 0;
1705                 }
1706 #endif
1707         }
1708
1709 #ifdef SVQC
1710         if (self.movetype == MOVETYPE_NONE)
1711                 return;
1712 #endif
1713
1714 #ifdef SVQC
1715         // when we get here, disableclientprediction cannot be 2
1716         self.disableclientprediction = 0;
1717         if (time < self.ladder_time)
1718                 self.disableclientprediction = 1;
1719 #endif
1720
1721         PM_check_spider();
1722
1723         PM_check_frozen();
1724
1725 #ifdef SVQC
1726         MUTATOR_CALLHOOK(PlayerPhysics);
1727 #endif
1728
1729         PM_check_blocked();
1730
1731         maxspeed_mod = 1;
1732
1733 #ifdef SVQC
1734         if (self.in_swamp) {
1735                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1736         }
1737 #endif
1738
1739 #ifdef SVQC
1740         // conveyors: first fix velocity
1741         if (self.conveyor.state)
1742                 self.velocity -= self.conveyor.movedir;
1743 #endif
1744
1745 #ifdef SVQC
1746         if (!IS_PLAYER(self))
1747         {
1748                 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1749                 if (!self.spectatorspeed)
1750                         self.spectatorspeed = maxspeed_mod;
1751                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1752                 {
1753                         if (self.lastclassname != "player")
1754                         {
1755                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1756                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1757                                 else if (self.impulse == 11)
1758                                         self.spectatorspeed = maxspeed_mod;
1759                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1760                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1761                                 else if (self.impulse >= 1 && self.impulse <= 9)
1762                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1763                         } // otherwise just clear
1764                         self.impulse = 0;
1765                 }
1766                 maxspeed_mod *= self.spectatorspeed;
1767         }
1768 #endif
1769
1770 #ifdef SVQC
1771         // if dead, behave differently
1772         // in CSQC, physics don't handle dead player
1773         if (self.deadflag)
1774                 goto end;
1775 #endif
1776
1777 #ifdef SVQC
1778         if (!self.fixangle && !g_bugrigs)
1779                 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1780 #endif
1781
1782 #ifdef SVQC
1783         if (IS_ONGROUND(self))
1784         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1785         if (self.wasFlying)
1786         {
1787                 self.wasFlying = 0;
1788                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1789                 if (time >= self.ladder_time)
1790                 if (!self.hook)
1791                 {
1792                         self.nextstep = time + 0.3 + random() * 0.1;
1793                         trace_dphitq3surfaceflags = 0;
1794                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1795                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1796                         {
1797                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1798                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1799                                 else
1800                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1801                         }
1802                 }
1803         }
1804 #endif
1805
1806         if (PM_is_flying())
1807                 self.wasFlying = 1;
1808
1809 #ifdef SVQC
1810         if (IS_PLAYER(self))
1811 #endif
1812                 CheckPlayerJump();
1813
1814
1815         if (self.flags & /* FL_WATERJUMP */ 2048)
1816         {
1817                 self.velocity_x = self.movedir_x;
1818                 self.velocity_y = self.movedir_y;
1819                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1820                 {
1821                         self.flags &= ~/* FL_WATERJUMP */ 2048;
1822                         self.teleport_time = 0;
1823                 }
1824         }
1825
1826 #ifdef SVQC
1827         else if (g_bugrigs && IS_PLAYER(self))
1828                 RaceCarPhysics();
1829 #endif
1830
1831         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1832                 PM_fly(maxspeed_mod);
1833
1834         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1835                 PM_swim(maxspeed_mod);
1836
1837 #ifdef SVQC
1838         else if (time < self.ladder_time)
1839                 PM_ladder(maxspeed_mod);
1840 #endif
1841
1842         else if ((ITEMS(self) & IT_JETPACK) && PHYS_BUTTON_HOOK(self) && (!PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) > 0 || (ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO)) && !PHYS_FROZEN(self))
1843                 PM_jetpack(maxspeed_mod);
1844
1845         else
1846         {
1847 #ifdef CSQC
1848                 // jump if on ground with jump button pressed but only if it has been
1849                 // released at least once since the last jump
1850                 if (PHYS_INPUT_BUTTONS(self) & 2)
1851                 {
1852                         if (IS_ONGROUND(self) && (!IS_JUMP_HELD(self) || !cvar("cl_movement_track_canjump")))
1853                         {
1854                                 self.velocity_z += PHYS_JUMPVELOCITY;
1855                                 UNSET_ONGROUND(self);
1856                                 SET_JUMP_HELD(self); // canjump = false
1857                         }
1858                 }
1859                 else
1860                         UNSET_JUMP_HELD(self); // canjump = true
1861 #endif
1862                 if (IS_ONGROUND(self))
1863                         PM_walk(buttons_prev, maxspeed_mod);
1864                 else
1865                         PM_air(buttons_prev, maxspeed_mod);
1866         }
1867
1868 #ifdef SVQC
1869         if (!IS_OBSERVER(self))
1870                 PM_check_race();
1871 #endif
1872         PM_check_vortex();
1873
1874 :end
1875         if (IS_ONGROUND(self))
1876                 self.lastground = time;
1877
1878 #ifdef SVQC
1879         // conveyors: then break velocity again
1880         if (self.conveyor.state)
1881                 self.velocity += self.conveyor.movedir;
1882 #endif
1883
1884         self.lastflags = self.flags;
1885         self.lastclassname = self.classname;
1886 }
1887
1888 void CSQC_ClientMovement_PlayerMove_Frame()
1889 {
1890         // if a move is more than 50ms, do it as two moves (matching qwsv)
1891         //Con_Printf("%i ", self.cmd.msec);
1892         if (PHYS_INPUT_TIMELENGTH > 0.0005)
1893         {
1894                 if (PHYS_INPUT_TIMELENGTH > 0.05)
1895                 {
1896                         PHYS_INPUT_TIMELENGTH /= 2;
1897                         PM_Main();
1898                 }
1899                 PM_Main();
1900         }
1901         else
1902                 // we REALLY need this handling to happen, even if the move is not executed
1903                 if (!(PHYS_INPUT_BUTTONS(self) & 2)) // !jump
1904                         UNSET_JUMP_HELD(self); // canjump = true
1905 }
1906
1907 #undef PHYS_INPUT_ANGLES
1908 #undef PHYS_INPUT_BUTTONS
1909 #undef PHYS_INPUT_TIMELENGTH
1910 #undef PHYS_INPUT_MOVEVALUES
1911
1912 #undef GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE
1913 #undef GAMEPLAYFIX_NOGRAVITYONGROUND
1914 #undef GAMEPLAYFIX_Q2AIRACCELERATE
1915
1916 #undef IS_DUCKED
1917 #undef SET_DUCKED
1918 #undef UNSET_DUCKED
1919
1920 #undef IS_JUMP_HELD
1921 #undef SET_JUMP_HELD
1922 #undef UNSET_JUMP_HELD
1923
1924 #undef IS_ONGROUND
1925 #undef SET_ONGROUND
1926 #undef UNSET_ONGROUND
1927
1928 #undef PHYS_ACCELERATE
1929 #undef PHYS_AIRACCEL_QW
1930 #undef PHYS_AIRACCEL_QW_STRETCHFACTOR
1931 #undef PHYS_AIRACCEL_SIDEWAYS_FRICTION
1932 #undef PHYS_AIRACCELERATE
1933 #undef PHYS_AIRCONTROL
1934 #undef PHYS_AIRCONTROL_PENALTY
1935 #undef PHYS_AIRCONTROL_POWER
1936 #undef PHYS_AIRSPEEDLIMIT_NONQW
1937 #undef PHYS_AIRSTOPACCELERATE
1938 #undef PHYS_AIRSTRAFEACCEL_QW
1939 #undef PHYS_AIRSTRAFEACCELERATE
1940 #undef PHYS_EDGEFRICTION
1941 #undef PHYS_ENTGRAVITY
1942 #undef PHYS_FRICTION
1943 #undef PHYS_GRAVITY
1944 #undef PHYS_HIGHSPEED
1945 #undef PHYS_JUMPVELOCITY
1946 #undef PHYS_MAXAIRSPEED
1947 #undef PHYS_MAXAIRSTRAFESPEED
1948 #undef PHYS_MAXSPEED
1949 #undef PHYS_STEPHEIGHT
1950 #undef PHYS_STOPSPEED
1951 #undef PHYS_WARSOWBUNNY_ACCEL
1952 #undef PHYS_WARSOWBUNNY_BACKTOSIDERATIO
1953 #undef PHYS_WARSOWBUNNY_AIRFORWARDACCEL
1954 #undef PHYS_WARSOWBUNNY_TOPSPEED
1955 #undef PHYS_WARSOWBUNNY_TURNACCEL
1956
1957 #ifdef SVQC
1958 // Entry point
1959 void SV_PlayerPhysics(void)
1960 {
1961         PM_Main();
1962 }
1963 #endif