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