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