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