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