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