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