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