]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
Merge branches 'TimePath/oo', 'TimePath/buffsys', 'TimePath/gametypesys'
[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 var = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
29                 if(cvar_type(var) & CVAR_TYPEFLAG_EXISTS)
30                         return cvar(var);
31         }
32         if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
33         {
34                 string var = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
35                 if(cvar_type(var) & CVAR_TYPEFLAG_EXISTS)
36                         return cvar(var);
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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
556         player_multijump = doublejump;
557         player_jumpheight = mjumpheight;
558 #ifdef SVQC
559         if (MUTATOR_CALLHOOK(PlayerJump))
560 #elif defined(CSQC)
561         if(PM_multijump_checkjump())
562 #endif
563                 return true;
564
565         doublejump = player_multijump;
566         mjumpheight = player_jumpheight;
567
568         if (PHYS_DOUBLEJUMP)
569         {
570                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
571                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
572                 {
573                         doublejump = true;
574
575                         // we MUST clip velocity here!
576                         float f;
577                         f = self.velocity * trace_plane_normal;
578                         if (f < 0)
579                                 self.velocity -= f * trace_plane_normal;
580                 }
581         }
582
583         if (self.waterlevel >= WATERLEVEL_SWIMMING)
584         {
585                 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
586                 return true;
587         }
588
589         if (!doublejump)
590                 if (!IS_ONGROUND(self))
591                         return IS_JUMP_HELD(self);
592
593         if (PHYS_TRACK_CANJUMP(self))
594                 if (IS_JUMP_HELD(self))
595                         return true;
596
597         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
598         // velocity bounds.  Final velocity is bound between (jumpheight *
599         // min + jumpheight) and (jumpheight * max + jumpheight);
600
601         if(PHYS_JUMPSPEEDCAP_MIN != "")
602         {
603                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
604
605                 if (self.velocity_z < minjumpspeed)
606                         mjumpheight += minjumpspeed - self.velocity_z;
607         }
608
609         if(PHYS_JUMPSPEEDCAP_MAX != "")
610         {
611                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
612                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
613
614                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
615                 {
616                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
617
618                         if (self.velocity_z > maxjumpspeed)
619                                 mjumpheight -= self.velocity_z - maxjumpspeed;
620                 }
621         }
622
623         if (!WAS_ONGROUND(self))
624         {
625 #ifdef SVQC
626                 if(autocvar_speedmeter)
627                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
628 #endif
629                 if(self.lastground < time - 0.3)
630                 {
631                         self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
632                         self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
633                 }
634 #ifdef SVQC
635                 if(self.jumppadcount > 1)
636                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
637                 self.jumppadcount = 0;
638 #endif
639         }
640
641         self.velocity_z += mjumpheight;
642
643         UNSET_ONGROUND(self);
644         SET_JUMP_HELD(self);
645
646 #ifdef SVQC
647
648         self.oldvelocity_z = self.velocity_z;
649
650         animdecide_setaction(self, ANIMACTION_JUMP, true);
651
652         if (autocvar_g_jump_grunt)
653                 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
654 #endif
655         return true;
656 }
657
658 void CheckWaterJump()
659 {
660 // check for a jump-out-of-water
661         makevectors(self.v_angle);
662         vector start = self.origin;
663         start_z += 8;
664         v_forward_z = 0;
665         normalize(v_forward);
666         vector end = start + v_forward*24;
667         traceline (start, end, true, self);
668         if (trace_fraction < 1)
669         {       // solid at waist
670                 start_z = start_z + self.maxs_z - 8;
671                 end = start + v_forward*24;
672                 self.movedir = trace_plane_normal * -50;
673                 traceline(start, end, true, self);
674                 if (trace_fraction == 1)
675                 {       // open at eye level
676                         self.velocity_z = 225;
677                         self.flags |= FL_WATERJUMP;
678                         SET_JUMP_HELD(self);
679 #ifdef SVQC
680                         self.teleport_time = time + 2;  // safety net
681 #elif defined(CSQC)
682                         pmove_waterjumptime = time + 2;
683 #endif
684                 }
685         }
686 }
687
688
689 #ifdef SVQC
690         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
691 #elif defined(CSQC)
692         float autocvar_cl_jetpack_jump;
693         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
694 #endif
695 .float jetpack_stopped;
696 // Hack: shouldn't need to know about this
697 .float multijump_count;
698 void CheckPlayerJump()
699 {
700 #ifdef SVQC
701         float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
702 #endif
703         if (JETPACK_JUMP(self) < 2)
704                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
705
706         if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
707         {
708                 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
709                 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
710                 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
711
712                 if (!(ITEMS_STAT(self) & IT_JETPACK)) { }
713                 else if (self.jetpack_stopped) { }
714                 else if (!has_fuel)
715                 {
716 #ifdef SVQC
717                         if (was_flying) // TODO: ran out of fuel message
718                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
719                         else if (activate)
720                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
721 #endif
722                         self.jetpack_stopped = true;
723                         ITEMS_STAT(self) &= ~IT_USING_JETPACK;
724                 }
725                 else if (activate && !PHYS_FROZEN(self))
726                         ITEMS_STAT(self) |= IT_USING_JETPACK;
727         }
728         else
729         {
730                 self.jetpack_stopped = false;
731                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
732         }
733         if (!PHYS_INPUT_BUTTON_JUMP(self))
734                 UNSET_JUMP_HELD(self);
735
736         if (self.waterlevel == WATERLEVEL_SWIMMING)
737                 CheckWaterJump();
738 }
739
740 float racecar_angle(float forward, float down)
741 {
742         if (forward < 0)
743         {
744                 forward = -forward;
745                 down = -down;
746         }
747
748         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
749
750         float angle_mult = forward / (800 + forward);
751
752         if (ret > 180)
753                 return ret * angle_mult + 360 * (1 - angle_mult);
754         else
755                 return ret * angle_mult;
756 }
757
758 void RaceCarPhysics()
759 {
760 #ifdef SVQC
761         // using this move type for "big rigs"
762         // the engine does not push the entity!
763
764         vector rigvel;
765
766         vector angles_save = self.angles;
767         float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
768         float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
769
770         if (g_bugrigs_reverse_speeding)
771         {
772                 if (accel < 0)
773                 {
774                         // back accel is DIGITAL
775                         // to prevent speedhack
776                         if (accel < -0.5)
777                                 accel = -1;
778                         else
779                                 accel = 0;
780                 }
781         }
782
783         self.angles_x = 0;
784         self.angles_z = 0;
785         makevectors(self.angles); // new forward direction!
786
787         if (IS_ONGROUND(self) || g_bugrigs_air_steering)
788         {
789                 float myspeed = self.velocity * v_forward;
790                 float upspeed = self.velocity * v_up;
791
792                 // responsiveness factor for steering and acceleration
793                 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
794                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
795
796                 float steerfactor;
797                 if (myspeed < 0 && g_bugrigs_reverse_spinning)
798                         steerfactor = -myspeed * g_bugrigs_steer;
799                 else
800                         steerfactor = -myspeed * f * g_bugrigs_steer;
801
802                 float accelfactor;
803                 if (myspeed < 0 && g_bugrigs_reverse_speeding)
804                         accelfactor = g_bugrigs_accel;
805                 else
806                         accelfactor = f * g_bugrigs_accel;
807                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
808
809                 if (accel < 0)
810                 {
811                         if (myspeed > 0)
812                         {
813                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
814                         }
815                         else
816                         {
817                                 if (!g_bugrigs_reverse_speeding)
818                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
819                         }
820                 }
821                 else
822                 {
823                         if (myspeed >= 0)
824                         {
825                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
826                         }
827                         else
828                         {
829                                 if (g_bugrigs_reverse_stopping)
830                                         myspeed = 0;
831                                 else
832                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
833                         }
834                 }
835                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
836                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
837
838                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
839                 makevectors(self.angles); // new forward direction!
840
841                 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
842
843                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
844         }
845         else
846         {
847                 float myspeed = vlen(self.velocity);
848
849                 // responsiveness factor for steering and acceleration
850                 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
851                 float steerfactor = -myspeed * f;
852                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
853
854                 rigvel = self.velocity;
855                 makevectors(self.angles); // new forward direction!
856         }
857
858         rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
859         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
860         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
861         //MAXIMA: solve(total_acceleration(v) = 0, v);
862
863         if (g_bugrigs_planar_movement)
864         {
865                 vector rigvel_xy, neworigin, up;
866                 float mt;
867
868                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
869                 rigvel_xy = vec2(rigvel);
870
871                 if (g_bugrigs_planar_movement_car_jumping)
872                         mt = MOVE_NORMAL;
873                 else
874                         mt = MOVE_NOMONSTERS;
875
876                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
877                 up = trace_endpos - self.origin;
878
879                 // BUG RIGS: align the move to the surface instead of doing collision testing
880                 // can we move?
881                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
882
883                 // align to surface
884                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
885
886                 if (trace_fraction < 0.5)
887                 {
888                         trace_fraction = 1;
889                         neworigin = self.origin;
890                 }
891                 else
892                         neworigin = trace_endpos;
893
894                 if (trace_fraction < 1)
895                 {
896                         // now set angles_x so that the car points parallel to the surface
897                         self.angles = vectoangles(
898                                         '1 0 0' * v_forward_x * trace_plane_normal_z
899                                         +
900                                         '0 1 0' * v_forward_y * trace_plane_normal_z
901                                         +
902                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
903                                         );
904                         SET_ONGROUND(self);
905                 }
906                 else
907                 {
908                         // now set angles_x so that the car points forward, but is tilted in velocity direction
909                         UNSET_ONGROUND(self);
910                 }
911
912                 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
913                 self.movetype = MOVETYPE_NOCLIP;
914         }
915         else
916         {
917                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
918                 self.velocity = rigvel;
919                 self.movetype = MOVETYPE_FLY;
920         }
921
922         trace_fraction = 1;
923         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
924         if (trace_fraction != 1)
925         {
926                 self.angles = vectoangles2(
927                                 '1 0 0' * v_forward_x * trace_plane_normal_z
928                                 +
929                                 '0 1 0' * v_forward_y * trace_plane_normal_z
930                                 +
931                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
932                                 trace_plane_normal
933                                 );
934         }
935         else
936         {
937                 vector vel_local;
938
939                 vel_local_x = v_forward * self.velocity;
940                 vel_local_y = v_right * self.velocity;
941                 vel_local_z = v_up * self.velocity;
942
943                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
944                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
945         }
946
947         // smooth the angles
948         vector vf1, vu1, smoothangles;
949         makevectors(self.angles);
950         float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
951         if (f == 0)
952                 f = 1;
953         vf1 = v_forward * f;
954         vu1 = v_up * f;
955         makevectors(angles_save);
956         vf1 = vf1 + v_forward * (1 - f);
957         vu1 = vu1 + v_up * (1 - f);
958         smoothangles = vectoangles2(vf1, vu1);
959         self.angles_x = -smoothangles_x;
960         self.angles_z =  smoothangles_z;
961 #endif
962 }
963
964 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
965 .float specialcommand_pos;
966 void SpecialCommand()
967 {
968 #ifdef SVQC
969 #ifdef TETRIS
970         TetrisImpulse();
971 #else
972         if (!CheatImpulse(99))
973                 print("A hollow voice says \"Plugh\".\n");
974 #endif
975 #endif
976 }
977
978 float PM_check_keepaway(void)
979 {
980 #ifdef SVQC
981         return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
982 #else
983         return 1;
984 #endif
985 }
986
987 void PM_check_race_movetime(void)
988 {
989 #ifdef SVQC
990         self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
991         float f = floor(self.race_movetime_frac);
992         self.race_movetime_frac -= f;
993         self.race_movetime_count += f;
994         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
995 #endif
996 }
997
998 float PM_check_specialcommand(float buttons)
999 {
1000 #ifdef SVQC
1001         string c;
1002         if (!buttons)
1003                 c = "x";
1004         else if (buttons == 1)
1005                 c = "1";
1006         else if (buttons == 2)
1007                 c = " ";
1008         else if (buttons == 128)
1009                 c = "s";
1010         else if (buttons == 256)
1011                 c = "w";
1012         else if (buttons == 512)
1013                 c = "a";
1014         else if (buttons == 1024)
1015                 c = "d";
1016         else
1017                 c = "?";
1018
1019         if (c == substring(specialcommand, self.specialcommand_pos, 1))
1020         {
1021                 self.specialcommand_pos += 1;
1022                 if (self.specialcommand_pos >= strlen(specialcommand))
1023                 {
1024                         self.specialcommand_pos = 0;
1025                         SpecialCommand();
1026                         return true;
1027                 }
1028         }
1029         else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
1030                 self.specialcommand_pos = 0;
1031 #endif
1032         return false;
1033 }
1034
1035 void PM_check_nickspam(void)
1036 {
1037 #ifdef SVQC
1038         if (time >= self.nickspamtime)
1039                 return;
1040         if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
1041         {
1042                 // slight annoyance for nick change scripts
1043                 self.movement = -1 * self.movement;
1044                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
1045
1046                 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!
1047                 {
1048                         self.v_angle_x = random() * 360;
1049                         self.v_angle_y = random() * 360;
1050                         // at least I'm not forcing retardedview by also assigning to angles_z
1051                         self.fixangle = true;
1052                 }
1053         }
1054 #endif
1055 }
1056
1057 void PM_check_punch()
1058 {
1059 #ifdef SVQC
1060         if (self.punchangle != '0 0 0')
1061         {
1062                 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
1063                 if (f > 0)
1064                         self.punchangle = normalize(self.punchangle) * f;
1065                 else
1066                         self.punchangle = '0 0 0';
1067         }
1068
1069         if (self.punchvector != '0 0 0')
1070         {
1071                 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1072                 if (f > 0)
1073                         self.punchvector = normalize(self.punchvector) * f;
1074                 else
1075                         self.punchvector = '0 0 0';
1076         }
1077 #endif
1078 }
1079
1080 void PM_check_spider(void)
1081 {
1082 #ifdef SVQC
1083         if (time >= self.spider_slowness)
1084                 return;
1085         PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1086         PHYS_MAXAIRSPEED(self) *= 0.5;
1087         PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
1088         PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
1089 #endif
1090 }
1091
1092 // predict frozen movement, as frozen players CAN move in some cases
1093 void PM_check_frozen(void)
1094 {
1095         if (!PHYS_FROZEN(self))
1096                 return;
1097         if (PHYS_DODGING_FROZEN
1098 #ifdef SVQC
1099         && IS_REAL_CLIENT(self)
1100 #endif
1101         )
1102         {
1103                 self.movement_x = bound(-5, self.movement.x, 5);
1104                 self.movement_y = bound(-5, self.movement.y, 5);
1105                 self.movement_z = bound(-5, self.movement.z, 5);
1106         }
1107         else
1108                 self.movement = '0 0 0';
1109
1110         vector midpoint = ((self.absmin + self.absmax) * 0.5);
1111         if (pointcontents(midpoint) == CONTENT_WATER)
1112         {
1113                 self.velocity = self.velocity * 0.5;
1114
1115                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1116                         self.velocity_z = 200;
1117         }
1118 }
1119
1120 void PM_check_hitground()
1121 {
1122 #ifdef SVQC
1123         if (IS_ONGROUND(self))
1124         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1125         if (self.wasFlying)
1126         {
1127                 self.wasFlying = 0;
1128                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1129                 if (time >= self.ladder_time)
1130                 if (!self.hook)
1131                 {
1132                         self.nextstep = time + 0.3 + random() * 0.1;
1133                         trace_dphitq3surfaceflags = 0;
1134                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1135                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1136                         {
1137                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1138                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1139                                 else
1140                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1141                         }
1142                 }
1143         }
1144 #endif
1145 }
1146
1147 void PM_check_blocked(void)
1148 {
1149 #ifdef SVQC
1150         if (!self.player_blocked)
1151                 return;
1152         self.movement = '0 0 0';
1153         self.disableclientprediction = 1;
1154 #endif
1155 }
1156
1157 #ifdef SVQC
1158 float speedaward_lastsent;
1159 float speedaward_lastupdate;
1160 #endif
1161 void PM_check_race(void)
1162 {
1163 #ifdef SVQC
1164         if(!(g_cts || g_race))
1165                 return;
1166         if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1167         {
1168                 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1169                 speedaward_holder = self.netname;
1170                 speedaward_uid = self.crypto_idfp;
1171                 speedaward_lastupdate = time;
1172         }
1173         if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1174         {
1175                 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1176                 race_send_speedaward(MSG_ALL);
1177                 speedaward_lastsent = speedaward_speed;
1178                 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1179                 {
1180                         speedaward_alltimebest = speedaward_speed;
1181                         speedaward_alltimebest_holder = speedaward_holder;
1182                         speedaward_alltimebest_uid = speedaward_uid;
1183                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1184                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1185                         race_send_speedaward_alltimebest(MSG_ALL);
1186                 }
1187         }
1188 #endif
1189 }
1190
1191 void PM_check_vortex(void)
1192 {
1193 #ifdef SVQC
1194         // WEAPONTODO
1195         float xyspeed = vlen(vec2(self.velocity));
1196         if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1197         {
1198                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1199                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1200                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1201                 // add the extra charge
1202                 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1203         }
1204 #endif
1205 }
1206
1207 void PM_fly(float maxspd_mod)
1208 {
1209         // noclipping or flying
1210         UNSET_ONGROUND(self);
1211
1212         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1213         makevectors(self.v_angle);
1214         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1215         vector wishvel = v_forward * self.movement.x
1216                                         + v_right * self.movement.y
1217                                         + '0 0 1' * self.movement.z;
1218         // acceleration
1219         vector wishdir = normalize(wishvel);
1220         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1221 #ifdef SVQC
1222         if (time >= self.teleport_time)
1223 #endif
1224                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1225         PM_ClientMovement_Move();
1226 }
1227
1228 void PM_swim(float maxspd_mod)
1229 {
1230         // swimming
1231         UNSET_ONGROUND(self);
1232
1233         float jump = PHYS_INPUT_BUTTON_JUMP(self);
1234         // water jump only in certain situations
1235         // this mimics quakeworld code
1236         if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1237         {
1238                 vector yawangles = '0 1 0' * self.v_angle.y;
1239                 makevectors(yawangles);
1240                 vector forward = v_forward;
1241                 vector spot = self.origin + 24 * forward;
1242                 spot_z += 8;
1243                 traceline(spot, spot, MOVE_NOMONSTERS, self);
1244                 if (trace_startsolid)
1245                 {
1246                         spot_z += 24;
1247                         traceline(spot, spot, MOVE_NOMONSTERS, self);
1248                         if (!trace_startsolid)
1249                         {
1250                                 self.velocity = forward * 50;
1251                                 self.velocity_z = 310;
1252                                 pmove_waterjumptime = 2;
1253                                 UNSET_ONGROUND(self);
1254                                 SET_JUMP_HELD(self);
1255                         }
1256                 }
1257         }
1258         makevectors(self.v_angle);
1259         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1260         vector wishvel = v_forward * self.movement.x
1261                                         + v_right * self.movement.y
1262                                         + '0 0 1' * self.movement.z;
1263         if (wishvel == '0 0 0')
1264                 wishvel = '0 0 -60'; // drift towards bottom
1265
1266         vector wishdir = normalize(wishvel);
1267         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1268
1269         if (IS_DUCKED(self))
1270         wishspeed *= 0.5;
1271
1272 //      if (pmove_waterjumptime <= 0) // TODO: use
1273     {
1274                 // water friction
1275                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1276                 f = min(max(0, f), 1);
1277                 self.velocity *= f;
1278
1279                 f = wishspeed - self.velocity * wishdir;
1280                 if (f > 0)
1281                 {
1282                         float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1283                         self.velocity += accelspeed * wishdir;
1284                 }
1285
1286                 // holding jump button swims upward slowly
1287                 if (jump)
1288                 {
1289 #if 0
1290                         if (self.watertype & CONTENT_LAVA)
1291                                 self.velocity_z =  50;
1292                         else if (self.watertype & CONTENT_SLIME)
1293                                 self.velocity_z =  80;
1294                         else
1295                         {
1296                                 if (IS_NEXUIZ_DERIVED(gamemode))
1297 #endif
1298                                         self.velocity_z = 200;
1299 #if 0
1300                                 else
1301                                         self.velocity_z = 100;
1302                         }
1303 #endif
1304                 }
1305         }
1306         // water acceleration
1307         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1308         PM_ClientMovement_Move();
1309 }
1310
1311 void PM_ladder(float maxspd_mod)
1312 {
1313         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1314         UNSET_ONGROUND(self);
1315
1316         float g;
1317         g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1318         if (PHYS_ENTGRAVITY(self))
1319                 g *= PHYS_ENTGRAVITY(self);
1320         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1321         {
1322                 g *= 0.5;
1323                 self.velocity_z += g;
1324         }
1325
1326         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1327         makevectors(self.v_angle);
1328         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1329         vector wishvel = v_forward * self.movement_x
1330                                         + v_right * self.movement_y
1331                                         + '0 0 1' * self.movement_z;
1332         self.velocity_z += g;
1333         if (self.ladder_entity.classname == "func_water")
1334         {
1335                 float f = vlen(wishvel);
1336                 if (f > self.ladder_entity.speed)
1337                         wishvel *= (self.ladder_entity.speed / f);
1338
1339                 self.watertype = self.ladder_entity.skin;
1340                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1341                 if ((self.origin_z + self.view_ofs_z) < f)
1342                         self.waterlevel = WATERLEVEL_SUBMERGED;
1343                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1344                         self.waterlevel = WATERLEVEL_SWIMMING;
1345                 else if ((self.origin_z + self.mins_z + 1) < f)
1346                         self.waterlevel = WATERLEVEL_WETFEET;
1347                 else
1348                 {
1349                         self.waterlevel = WATERLEVEL_NONE;
1350                         self.watertype = CONTENT_EMPTY;
1351                 }
1352         }
1353         // acceleration
1354         vector wishdir = normalize(wishvel);
1355         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1356 #ifdef SVQC
1357         if (time >= self.teleport_time)
1358 #endif
1359                 // water acceleration
1360                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1361         PM_ClientMovement_Move();
1362 }
1363
1364 void PM_jetpack(float maxspd_mod)
1365 {
1366         //makevectors(self.v_angle.y * '0 1 0');
1367         makevectors(self.v_angle);
1368         vector wishvel = v_forward * self.movement_x
1369                                         + v_right * self.movement_y;
1370         // add remaining speed as Z component
1371         float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1372         // fix speedhacks :P
1373         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1374         // add the unused velocity as up component
1375         wishvel_z = 0;
1376
1377         // if (self.BUTTON_JUMP)
1378                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1379
1380         // it is now normalized, so...
1381         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1382         float a_up = PHYS_JETPACK_ACCEL_UP;
1383         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1384
1385         wishvel_x *= a_side;
1386         wishvel_y *= a_side;
1387         wishvel_z *= a_up;
1388         wishvel_z += a_add;
1389
1390         float best = 0;
1391         //////////////////////////////////////////////////////////////////////////////////////
1392         // finding the maximum over all vectors of above form
1393         // with wishvel having an absolute value of 1
1394         //////////////////////////////////////////////////////////////////////////////////////
1395         // we're finding the maximum over
1396         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1397         // for z in the range from -1 to 1
1398         //////////////////////////////////////////////////////////////////////////////////////
1399         // maximum is EITHER attained at the single extreme point:
1400         float a_diff = a_side * a_side - a_up * a_up;
1401         float f;
1402         if (a_diff != 0)
1403         {
1404                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1405                 if (f > -1 && f < 1) // can it be attained?
1406                 {
1407                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1408                         //print("middle\n");
1409                 }
1410         }
1411         // OR attained at z = 1:
1412         f = (a_up + a_add) * (a_up + a_add);
1413         if (f > best)
1414         {
1415                 best = f;
1416                 //print("top\n");
1417         }
1418         // OR attained at z = -1:
1419         f = (a_up - a_add) * (a_up - a_add);
1420         if (f > best)
1421         {
1422                 best = f;
1423                 //print("bottom\n");
1424         }
1425         best = sqrt(best);
1426         //////////////////////////////////////////////////////////////////////////////////////
1427
1428         //print("best possible acceleration: ", ftos(best), "\n");
1429
1430         float fxy, fz;
1431         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1432         if (wishvel_z - PHYS_GRAVITY > 0)
1433                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1434         else
1435                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1436
1437         float fvel;
1438         fvel = vlen(wishvel);
1439         wishvel_x *= fxy;
1440         wishvel_y *= fxy;
1441         wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1442
1443         fvel = min(1, vlen(wishvel) / best);
1444         if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1445                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1446         else
1447                 f = 1;
1448
1449         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1450
1451         if (f > 0 && wishvel != '0 0 0')
1452         {
1453                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1454                 UNSET_ONGROUND(self);
1455
1456 #ifdef SVQC
1457                 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1458                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1459
1460                 ITEMS_STAT(self) |= IT_USING_JETPACK;
1461
1462                 // jetpack also inhibits health regeneration, but only for 1 second
1463                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1464 #endif
1465         }
1466
1467 #ifdef CSQC
1468         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1469         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1470                 self.velocity_z -= g * 0.5;
1471         else
1472                 self.velocity_z -= g;
1473         PM_ClientMovement_Move();
1474         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1475                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1476                         self.velocity_z -= g * 0.5;
1477 #endif
1478 }
1479
1480 void PM_walk(float buttons_prev, float maxspd_mod)
1481 {
1482         if (!WAS_ONGROUND(self))
1483         {
1484 #ifdef SVQC
1485                 if (autocvar_speedmeter)
1486                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1487 #endif
1488                 if (self.lastground < time - 0.3)
1489                         self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1490 #ifdef SVQC
1491                 if (self.jumppadcount > 1)
1492                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1493                 self.jumppadcount = 0;
1494 #endif
1495         }
1496
1497         // walking
1498         makevectors(self.v_angle.y * '0 1 0');
1499         vector wishvel = v_forward * self.movement.x
1500                                         + v_right * self.movement.y;
1501         // acceleration
1502         vector wishdir = normalize(wishvel);
1503         float wishspeed = vlen(wishvel);
1504
1505         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1506         if (IS_DUCKED(self))
1507                 wishspeed *= 0.5;
1508
1509         // apply edge friction
1510         float f = vlen(vec2(self.velocity));
1511         if (f > 0)
1512         {
1513                 float realfriction;
1514                 trace_dphitq3surfaceflags = 0;
1515                 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1516                 // TODO: apply edge friction
1517                 // apply ground friction
1518                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1519                         realfriction = PHYS_FRICTION_SLICK;
1520                 else
1521                         realfriction = PHYS_FRICTION;
1522
1523                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1524                 f = max(0, f);
1525                 self.velocity *= f;
1526                 /*
1527                    Mathematical analysis time!
1528
1529                    Our goal is to invert this mess.
1530
1531                    For the two cases we get:
1532                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1533                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1534                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1535                    and
1536                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1537                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1538
1539                    These cases would be chosen ONLY if:
1540                         v0 < PHYS_STOPSPEED
1541                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1542                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1543                    and, respectively:
1544                         v0 >= PHYS_STOPSPEED
1545                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1546                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1547                  */
1548         }
1549         float addspeed = wishspeed - self.velocity * wishdir;
1550         if (addspeed > 0)
1551         {
1552                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1553                 self.velocity += accelspeed * wishdir;
1554         }
1555         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1556         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1557                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1558         if (self.velocity * self.velocity)
1559                 PM_ClientMovement_Move();
1560         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1561                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1562                         self.velocity_z -= g * 0.5;
1563 }
1564
1565 void PM_air(float buttons_prev, float maxspd_mod)
1566 {
1567         makevectors(self.v_angle.y * '0 1 0');
1568         vector wishvel = v_forward * self.movement.x
1569                                         + v_right * self.movement.y;
1570         // acceleration
1571         vector wishdir = normalize(wishvel);
1572         float wishspeed = vlen(wishvel);
1573
1574 #ifdef SVQC
1575         if (time >= self.teleport_time)
1576 #else
1577         if (pmove_waterjumptime <= 0)
1578 #endif
1579         {
1580                 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1581
1582                 // apply air speed limit
1583                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1584                 float wishspeed0 = wishspeed;
1585                 wishspeed = min(wishspeed, maxairspd);
1586                 if (IS_DUCKED(self))
1587                         wishspeed *= 0.5;
1588                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1589
1590                 float accelerating = (self.velocity * wishdir > 0);
1591                 float wishspeed2 = wishspeed;
1592
1593                 // CPM: air control
1594                 if (PHYS_AIRSTOPACCELERATE)
1595                 {
1596                         vector curdir = normalize(vec2(self.velocity));
1597                         airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1598                 }
1599                 // note that for straight forward jumping:
1600                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1601                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1602                 // -->
1603                 // dv/dt = accel * maxspeed (when slow)
1604                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1605                 // log dv/dt = logaccel + logmaxspeed (when slow)
1606                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1607                 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1608                 if (PHYS_MAXAIRSTRAFESPEED)
1609                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1610                 if (PHYS_AIRSTRAFEACCELERATE(self))
1611                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1612                 if (PHYS_AIRSTRAFEACCEL_QW(self))
1613                         airaccelqw =
1614                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1615                 *
1616                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1617                 // !CPM
1618
1619                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1620                         PM_AirAccelerate(wishdir, wishspeed2);
1621                 else
1622                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1623
1624                 if (PHYS_AIRCONTROL)
1625                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1626         }
1627         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1628         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1629                 self.velocity_z -= g * 0.5;
1630         else
1631                 self.velocity_z -= g;
1632         PM_ClientMovement_Move();
1633         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1634                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1635                         self.velocity_z -= g * 0.5;
1636 }
1637
1638 // used for calculating airshots
1639 bool IsFlying(entity a)
1640 {
1641         if(IS_ONGROUND(a))
1642                 return false;
1643         if(a.waterlevel >= WATERLEVEL_SWIMMING)
1644                 return false;
1645         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1646         if(trace_fraction < 1)
1647                 return false;
1648         return true;
1649 }
1650
1651 void PM_Main()
1652 {
1653         int buttons = PHYS_INPUT_BUTTON_MASK(self);
1654 #ifdef CSQC
1655         self.items = getstati(STAT_ITEMS, 0, 24);
1656
1657         self.movement = PHYS_INPUT_MOVEVALUES(self);
1658
1659         vector oldv_angle = self.v_angle;
1660         vector oldangles = self.angles; // we need to save these, as they're abused by other code
1661         self.v_angle = PHYS_INPUT_ANGLES(self);
1662         self.angles = PHYS_WORLD_ANGLES(self);
1663
1664         self.team = myteam + 1; // is this correct?
1665         if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1666                 UNSET_JUMP_HELD(self); // canjump = true
1667         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1668
1669         PM_ClientMovement_UpdateStatus(true);
1670 #endif
1671         
1672
1673 #ifdef SVQC
1674         WarpZone_PlayerPhysics_FixVAngle();
1675 #endif
1676         float maxspeed_mod = 1;
1677         maxspeed_mod *= PM_check_keepaway();
1678         maxspeed_mod *= PHYS_HIGHSPEED;
1679
1680 #ifdef SVQC
1681         Physics_UpdateStats(maxspeed_mod);
1682
1683         if (self.PlayerPhysplug)
1684                 if (self.PlayerPhysplug())
1685                         return;
1686 #endif
1687
1688         PM_check_race_movetime();
1689 #ifdef SVQC
1690         anticheat_physics();
1691 #endif
1692
1693         if (PM_check_specialcommand(buttons))
1694                 return;
1695 #ifdef SVQC
1696         if (sv_maxidle > 0)
1697         {
1698                 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1699                         self.parm_idlesince = time;
1700         }
1701 #endif
1702         int buttons_prev = self.buttons_old;
1703         self.buttons_old = buttons;
1704         self.movement_old = self.movement;
1705         self.v_angle_old = self.v_angle;
1706
1707         PM_check_nickspam();
1708
1709         PM_check_punch();
1710 #ifdef SVQC
1711         if (IS_BOT_CLIENT(self))
1712         {
1713                 if (playerdemo_read())
1714                         return;
1715                 bot_think();
1716         }
1717
1718         if (IS_PLAYER(self))
1719 #endif
1720         {
1721 #ifdef SVQC
1722                 if (self.race_penalty)
1723                         if (time > self.race_penalty)
1724                                 self.race_penalty = 0;
1725 #endif
1726
1727                 bool not_allowed_to_move = false;
1728 #ifdef SVQC
1729                 if (self.race_penalty)
1730                         not_allowed_to_move = true;
1731 #endif
1732 #ifdef SVQC
1733                 if (time < game_starttime)
1734                         not_allowed_to_move = true;
1735 #endif
1736
1737                 if (not_allowed_to_move)
1738                 {
1739                         self.velocity = '0 0 0';
1740                         self.movetype = MOVETYPE_NONE;
1741 #ifdef SVQC
1742                         self.disableclientprediction = 2;
1743 #endif
1744                 }
1745 #ifdef SVQC
1746                 else if (self.disableclientprediction == 2)
1747                 {
1748                         if (self.movetype == MOVETYPE_NONE)
1749                                 self.movetype = MOVETYPE_WALK;
1750                         self.disableclientprediction = 0;
1751                 }
1752 #endif
1753         }
1754
1755 #ifdef SVQC
1756         if (self.movetype == MOVETYPE_NONE)
1757                 return;
1758
1759         // when we get here, disableclientprediction cannot be 2
1760         self.disableclientprediction = 0;
1761 #endif
1762
1763         viewloc_PlayerPhysics();
1764
1765         PM_check_spider();
1766
1767         PM_check_frozen();
1768
1769         PM_check_blocked();
1770
1771         maxspeed_mod = 1;
1772
1773         if (self.in_swamp)
1774                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1775
1776         // conveyors: first fix velocity
1777         if (self.conveyor.state)
1778                 self.velocity -= self.conveyor.movedir;
1779
1780 #ifdef SVQC
1781         MUTATOR_CALLHOOK(PlayerPhysics);
1782 #endif
1783 #ifdef CSQC
1784         PM_multijump();
1785 #endif
1786
1787 //      float forcedodge = 1;
1788 //      if(forcedodge) {
1789 //#ifdef CSQC
1790 //              PM_dodging_checkpressedkeys();
1791 //#endif
1792 //              PM_dodging();
1793 //              PM_ClientMovement_Move();
1794 //              return;
1795 //      }
1796
1797 #ifdef SVQC
1798         if (!IS_PLAYER(self))
1799         {
1800                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1801                 if (!self.spectatorspeed)
1802                         self.spectatorspeed = maxspeed_mod;
1803                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1804                 {
1805                         if (self.lastclassname != "player")
1806                         {
1807                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1808                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1809                                 else if (self.impulse == 11)
1810                                         self.spectatorspeed = maxspeed_mod;
1811                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1812                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1813                                 else if (self.impulse >= 1 && self.impulse <= 9)
1814                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1815                         } // otherwise just clear
1816                         self.impulse = 0;
1817                 }
1818                 maxspeed_mod = self.spectatorspeed;
1819         }
1820
1821         float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1822         if(self.speed != spd)
1823         {
1824                 self.speed = spd;
1825                 string temps = ftos(spd);
1826                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1827                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1828                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1829                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1830         }
1831
1832         if(self.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1833         {
1834                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1835                 stuffcmd(self, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1836         }
1837         if(self.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1838         {
1839                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1840                 stuffcmd(self, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1841         }
1842 #endif
1843
1844         if(PHYS_DEAD(self))
1845         {
1846                 // handle water here
1847                 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1848                 if(pointcontents(midpoint) == CONTENT_WATER)
1849                 {
1850                         self.velocity = self.velocity * 0.5;
1851
1852                         // do we want this?
1853                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1854                                 //{ self.velocity_z = 70; }
1855                 }
1856                 goto end;
1857         }
1858
1859 #ifdef SVQC
1860         if (!self.fixangle && !g_bugrigs)
1861                 self.angles = '0 1 0' * self.v_angle.y;
1862 #endif
1863
1864         PM_check_hitground();
1865
1866         if(IsFlying(self))
1867                 self.wasFlying = 1;
1868
1869         if (IS_PLAYER(self))
1870                 CheckPlayerJump();
1871
1872         if (self.flags & FL_WATERJUMP)
1873         {
1874                 self.velocity_x = self.movedir_x;
1875                 self.velocity_y = self.movedir_y;
1876                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1877                 {
1878                         self.flags &= ~FL_WATERJUMP;
1879                         self.teleport_time = 0;
1880                 }
1881         }
1882
1883 #ifdef SVQC
1884         else if (g_bugrigs && IS_PLAYER(self))
1885                 RaceCarPhysics();
1886 #endif
1887
1888         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS_STAT(self) & BUFF_FLIGHT.m_itemid))
1889                 PM_fly(maxspeed_mod);
1890
1891         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1892                 PM_swim(maxspeed_mod);
1893
1894         else if (time < self.ladder_time)
1895                 PM_ladder(maxspeed_mod);
1896
1897         else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1898                 PM_jetpack(maxspeed_mod);
1899
1900         else if (IS_ONGROUND(self))
1901                 PM_walk(buttons_prev, maxspeed_mod);
1902
1903         else
1904                 PM_air(buttons_prev, maxspeed_mod);
1905
1906 #ifdef SVQC
1907         if (!IS_OBSERVER(self))
1908                 PM_check_race();
1909 #endif
1910         PM_check_vortex();
1911
1912 :end
1913         if (IS_ONGROUND(self))
1914                 self.lastground = time;
1915
1916         // conveyors: then break velocity again
1917         if(self.conveyor.state)
1918                 self.velocity += self.conveyor.movedir;
1919
1920         self.lastflags = self.flags;
1921
1922         self.lastclassname = self.classname;
1923
1924 #ifdef CSQC
1925         self.v_angle = oldv_angle;
1926         self.angles = oldangles;
1927 #endif
1928 }
1929
1930 #ifdef SVQC
1931 void SV_PlayerPhysics(void)
1932 #elif defined(CSQC)
1933 void CSQC_ClientMovement_PlayerMove_Frame(void)
1934 #endif
1935 {
1936         PM_Main();
1937
1938 #ifdef CSQC
1939         self.pmove_flags = 
1940                         ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1941                         (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1942                         ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);
1943 #endif
1944 }