]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Remove engine side player movement prediction, and the extra mode without CSQC movety...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics / player.qc
1 #include "player.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, float defaultval)
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 defaultval;
31 }
32
33 void Physics_UpdateStats(entity this, float maxspd_mod)
34 {
35         STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
36         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
37                 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
38                 : 0;
39         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
40         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
41
42         // old stats
43         // fix some new settings
44         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
45         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
46         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
47         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
48         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
49         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
50         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
51         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
52         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
53         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
54         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
55         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
56         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
57         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
58         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
59         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
60         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
61         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
62         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
63         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
64 }
65 #endif
66
67 float IsMoveInDirection(vector mv, float ang) // key mix factor
68 {
69         if (mv_x == 0 && mv_y == 0)
70                 return 0; // avoid division by zero
71         ang -= RAD2DEG * atan2(mv_y, mv_x);
72         ang = remainder(ang, 360) / 45;
73         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
74 }
75
76 float GeomLerp(float a, float _lerp, float b)
77 {
78         return a == 0 ? (_lerp < 1 ? 0 : b)
79                 : b == 0 ? (_lerp > 0 ? 0 : a)
80                 : a * pow(fabs(b / a), _lerp);
81 }
82
83 #define unstick_offsets(X) \
84 /* 1 no nudge (just return the original if this test passes) */ \
85         X(' 0.000  0.000  0.000') \
86 /* 6 simple nudges */ \
87         X(' 0.000  0.000  0.125') X('0.000  0.000 -0.125') \
88         X('-0.125  0.000  0.000') X('0.125  0.000  0.000') \
89         X(' 0.000 -0.125  0.000') X('0.000  0.125  0.000') \
90 /* 4 diagonal flat nudges */ \
91         X('-0.125 -0.125  0.000') X('0.125 -0.125  0.000') \
92         X('-0.125  0.125  0.000') X('0.125  0.125  0.000') \
93 /* 8 diagonal upward nudges */ \
94         X('-0.125  0.000  0.125') X('0.125  0.000  0.125') \
95         X(' 0.000 -0.125  0.125') X('0.000  0.125  0.125') \
96         X('-0.125 -0.125  0.125') X('0.125 -0.125  0.125') \
97         X('-0.125  0.125  0.125') X('0.125  0.125  0.125') \
98 /* 8 diagonal downward nudges */ \
99         X('-0.125  0.000 -0.125') X('0.125  0.000 -0.125') \
100         X(' 0.000 -0.125 -0.125') X('0.000  0.125 -0.125') \
101         X('-0.125 -0.125 -0.125') X('0.125 -0.125 -0.125') \
102         X('-0.125  0.125 -0.125') X('0.125  0.125 -0.125') \
103 /**/
104
105 void PM_ClientMovement_Unstick(entity this)
106 {
107         #define X(unstick_offset) \
108         { \
109                 vector neworigin = unstick_offset + this.origin; \
110                 tracebox(neworigin, STAT(PL_CROUCH_MIN, NULL), STAT(PL_CROUCH_MAX, NULL), neworigin, MOVE_NORMAL, this); \
111                 if (!trace_startsolid) \
112                 { \
113                         setorigin(this, neworigin); \
114                         return; \
115                 } \
116         }
117         unstick_offsets(X);
118         #undef X
119 }
120
121 void PM_ClientMovement_UpdateStatus(entity this)
122 {
123 #ifdef CSQC
124         if(!IS_PLAYER(this))
125                 return;
126
127         // set crouched
128         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
129         if(this.hook && !wasfreed(this.hook))
130                 do_crouch = false;
131         if(this.waterlevel >= WATERLEVEL_SWIMMING)
132                 do_crouch = false;
133         if(hud != HUD_NORMAL)
134                 do_crouch = false;
135         if(STAT(FROZEN, this))
136                 do_crouch = false;
137         if((activeweapon == WEP_SHOCKWAVE || activeweapon == WEP_SHOTGUN) && viewmodel.animstate_startframe == viewmodel.anim_fire2_x && time < viewmodel.weapon_nextthink)
138                 do_crouch = false;
139
140         if (do_crouch)
141         {
142                 // wants to crouch, this always works
143                 if (!IS_DUCKED(this)) SET_DUCKED(this);
144         }
145         else
146         {
147                 // wants to stand, if currently crouching we need to check for a low ceiling first
148                 if (IS_DUCKED(this))
149                 {
150                         tracebox(this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), this.origin, MOVE_NORMAL, this);
151                         if (!trace_startsolid) UNSET_DUCKED(this);
152                 }
153         }
154
155         if (IS_ONGROUND(this) || this.velocity.z <= 0 || pmove_waterjumptime <= 0)
156                 pmove_waterjumptime = 0;
157 #endif
158 }
159
160 void CPM_PM_Aircontrol(entity this, vector wishdir, float wishspeed)
161 {
162         float k = 32 * (2 * IsMoveInDirection(this.movement, 0) - 1);
163         if (k <= 0)
164                 return;
165
166         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
167
168         float zspeed = this.velocity_z;
169         this.velocity_z = 0;
170         float xyspeed = vlen(this.velocity);
171         this.velocity = normalize(this.velocity);
172
173         float dot = this.velocity * wishdir;
174
175         if (dot > 0) // we can't change direction while slowing down
176         {
177                 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * PHYS_INPUT_TIMELENGTH;
178                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
179                 k *= PHYS_AIRCONTROL(this);
180                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
181         }
182
183         this.velocity = this.velocity * xyspeed;
184         this.velocity_z = zspeed;
185 }
186
187 float AdjustAirAccelQW(float accelqw, float factor)
188 {
189         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
190 }
191
192 // example config for alternate speed clamping:
193 //   sv_airaccel_qw 0.8
194 //   sv_airaccel_sideways_friction 0
195 //   prvm_globalset server speedclamp_mode 1
196 //     (or 2)
197 void PM_Accelerate(entity this, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
198 {
199         float speedclamp = stretchfactor > 0 ? stretchfactor
200         : accelqw < 0 ? 1 // full clamping, no stretch
201         : -1; // no clamping
202
203         accelqw = fabs(accelqw);
204
205         if (GAMEPLAYFIX_Q2AIRACCELERATE)
206                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
207
208         float vel_straight = this.velocity * wishdir;
209         float vel_z = this.velocity_z;
210         vector vel_xy = vec2(this.velocity);
211         vector vel_perpend = vel_xy - vel_straight * wishdir;
212
213         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
214
215         float vel_xy_current  = vlen(vel_xy);
216         if (speedlimit)
217                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
218         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
219         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
220         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
221         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
222
223         if (sidefric < 0 && (vel_perpend*vel_perpend))
224                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
225         {
226                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
227                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
228                 // assume: themin > 1
229                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
230                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
231                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
232                 // obviously, this cannot be
233                 if (themin <= 0)
234                         vel_perpend *= f;
235                 else
236                 {
237                         themin = sqrt(themin);
238                         vel_perpend *= max(themin, f);
239                 }
240         }
241         else
242                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
243
244         vel_xy = vel_straight * wishdir + vel_perpend;
245
246         if (speedclamp >= 0)
247         {
248                 float vel_xy_preclamp;
249                 vel_xy_preclamp = vlen(vel_xy);
250                 if (vel_xy_preclamp > 0) // prevent division by zero
251                 {
252                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
253                         if (vel_xy_current < vel_xy_preclamp)
254                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
255                 }
256         }
257
258         this.velocity = vel_xy + vel_z * '0 0 1';
259 }
260
261 void PM_AirAccelerate(entity this, vector wishdir, float wishspeed)
262 {
263         if (wishspeed == 0)
264                 return;
265
266         vector curvel = this.velocity;
267         curvel_z = 0;
268         float curspeed = vlen(curvel);
269
270         if (wishspeed > curspeed * 1.01)
271                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
272         else
273         {
274                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
275                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH;
276         }
277         vector wishvel = wishdir * wishspeed;
278         vector acceldir = wishvel - curvel;
279         float addspeed = vlen(acceldir);
280         acceldir = normalize(acceldir);
281
282         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
283
284         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
285         {
286                 vector curdir = normalize(curvel);
287                 float dot = acceldir * curdir;
288                 if (dot < 0)
289                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
290         }
291
292         this.velocity += accelspeed * acceldir;
293 }
294
295
296 /*
297 =============
298 PlayerJump
299
300 When you press the jump key
301 returns true if handled
302 =============
303 */
304 bool PlayerJump(entity this)
305 {
306         if (PHYS_FROZEN(this))
307                 return true; // no jumping in freezetag when frozen
308
309 #ifdef SVQC
310         if (this.player_blocked)
311                 return true; // no jumping while blocked
312 #endif
313
314         bool doublejump = false;
315         float mjumpheight = PHYS_JUMPVELOCITY(this);
316
317         if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
318                 return true;
319
320         mjumpheight = M_ARGV(1, float);
321         doublejump = M_ARGV(2, bool);
322
323         if (this.waterlevel >= WATERLEVEL_SWIMMING)
324         {
325                 if(this.viewloc)
326                 {
327                         doublejump = true;
328                         mjumpheight *= 0.7;
329                 }
330                 else
331                 {
332                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
333                         return true;
334                 }
335         }
336
337         if (!doublejump)
338                 if (!IS_ONGROUND(this))
339                         return IS_JUMP_HELD(this);
340
341         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
342         if(PHYS_TRACK_CANJUMP(this))
343                 track_jump = true;
344
345         if (track_jump)
346                 if (IS_JUMP_HELD(this))
347                         return true;
348
349         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
350         // velocity bounds.  Final velocity is bound between (jumpheight *
351         // min + jumpheight) and (jumpheight * max + jumpheight);
352
353         if(PHYS_JUMPSPEEDCAP_MIN != "")
354         {
355                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
356
357                 if (this.velocity_z < minjumpspeed)
358                         mjumpheight += minjumpspeed - this.velocity_z;
359         }
360
361         if(PHYS_JUMPSPEEDCAP_MAX != "")
362         {
363                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
364                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
365
366                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
367                 {
368                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
369
370                         if (this.velocity_z > maxjumpspeed)
371                                 mjumpheight -= this.velocity_z - maxjumpspeed;
372                 }
373         }
374
375         if (!WAS_ONGROUND(this))
376         {
377 #ifdef SVQC
378                 if(autocvar_speedmeter)
379                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
380 #endif
381                 if(this.lastground < time - 0.3)
382                 {
383                         float f = (1 - PHYS_FRICTION_ONLAND(this));
384                         this.velocity_x *= f;
385                         this.velocity_y *= f;
386                 }
387 #ifdef SVQC
388                 if(this.jumppadcount > 1)
389                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
390                 this.jumppadcount = 0;
391 #endif
392         }
393
394         this.velocity_z += mjumpheight;
395
396         UNSET_ONGROUND(this);
397         SET_JUMP_HELD(this);
398
399 #ifdef SVQC
400
401         this.oldvelocity_z = this.velocity_z;
402
403         animdecide_setaction(this, ANIMACTION_JUMP, true);
404
405         if (autocvar_g_jump_grunt)
406                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
407 #endif
408         return true;
409 }
410
411 void CheckWaterJump(entity this)
412 {
413 // check for a jump-out-of-water
414         makevectors(this.v_angle);
415         vector start = this.origin;
416         start_z += 8;
417         v_forward_z = 0;
418         normalize(v_forward);
419         vector end = start + v_forward*24;
420         traceline (start, end, true, this);
421         if (trace_fraction < 1)
422         {       // solid at waist
423                 start_z = start_z + this.maxs_z - 8;
424                 end = start + v_forward*24;
425                 this.movedir = trace_plane_normal * -50;
426                 traceline(start, end, true, this);
427                 if (trace_fraction == 1)
428                 {       // open at eye level
429                         this.velocity_z = 225;
430                         this.flags |= FL_WATERJUMP;
431                         SET_JUMP_HELD(this);
432                 #ifdef SVQC
433                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
434                 #elif defined(CSQC)
435                         pmove_waterjumptime = 2;
436                 #endif
437                 }
438         }
439 }
440
441
442 #ifdef SVQC
443         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
444 #elif defined(CSQC)
445         float autocvar_cl_jetpack_jump;
446         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
447 #endif
448 .float jetpack_stopped;
449 void CheckPlayerJump(entity this)
450 {
451 #ifdef SVQC
452         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
453 #endif
454         if (JETPACK_JUMP(this) < 2)
455                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
456
457         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
458         {
459                 bool playerjump = PlayerJump(this); // required
460
461                 bool air_jump = !playerjump || M_ARGV(2, bool);
462                 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
463                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
464
465                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
466                 else if (this.jetpack_stopped) { }
467                 else if (!has_fuel)
468                 {
469 #ifdef SVQC
470                         if (was_flying) // TODO: ran out of fuel message
471                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
472                         else if (activate)
473                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
474 #endif
475                         this.jetpack_stopped = true;
476                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
477                 }
478                 else if (activate && !PHYS_FROZEN(this))
479                         ITEMS_STAT(this) |= IT_USING_JETPACK;
480         }
481         else
482         {
483                 this.jetpack_stopped = false;
484                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
485         }
486         if (!PHYS_INPUT_BUTTON_JUMP(this))
487                 UNSET_JUMP_HELD(this);
488
489         if (this.waterlevel == WATERLEVEL_SWIMMING)
490                 CheckWaterJump(this);
491 }
492
493 float racecar_angle(float forward, float down)
494 {
495         if (forward < 0)
496         {
497                 forward = -forward;
498                 down = -down;
499         }
500
501         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
502
503         float angle_mult = forward / (800 + forward);
504
505         if (ret > 180)
506                 return ret * angle_mult + 360 * (1 - angle_mult);
507         else
508                 return ret * angle_mult;
509 }
510
511 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
512 .float specialcommand_pos;
513 void SpecialCommand(entity this)
514 {
515 #ifdef SVQC
516         if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
517                 LOG_INFO("A hollow voice says \"Plugh\".\n");
518 #endif
519 }
520
521 bool PM_check_specialcommand(entity this, int buttons)
522 {
523 #ifdef SVQC
524         string c;
525         if (!buttons)
526                 c = "x";
527         else if (buttons == 1)
528                 c = "1";
529         else if (buttons == 2)
530                 c = " ";
531         else if (buttons == 128)
532                 c = "s";
533         else if (buttons == 256)
534                 c = "w";
535         else if (buttons == 512)
536                 c = "a";
537         else if (buttons == 1024)
538                 c = "d";
539         else
540                 c = "?";
541
542         if (c == substring(specialcommand, this.specialcommand_pos, 1))
543         {
544                 this.specialcommand_pos += 1;
545                 if (this.specialcommand_pos >= strlen(specialcommand))
546                 {
547                         this.specialcommand_pos = 0;
548                         SpecialCommand(this);
549                         return true;
550                 }
551         }
552         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
553                 this.specialcommand_pos = 0;
554 #endif
555         return false;
556 }
557
558 void PM_check_nickspam(entity this)
559 {
560 #ifdef SVQC
561         if (time >= this.nickspamtime)
562                 return;
563         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
564         {
565                 // slight annoyance for nick change scripts
566                 this.movement = -1 * this.movement;
567                 PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_JUMP(this) = PHYS_INPUT_BUTTON_ATCK2(this) = PHYS_INPUT_BUTTON_ZOOM(this) = PHYS_INPUT_BUTTON_CROUCH(this) = PHYS_INPUT_BUTTON_HOOK(this) = PHYS_INPUT_BUTTON_USE(this) = false;
568
569                 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!
570                 {
571                         this.v_angle_x = random() * 360;
572                         this.v_angle_y = random() * 360;
573                         // at least I'm not forcing retardedview by also assigning to angles_z
574                         this.fixangle = true;
575                 }
576         }
577 #endif
578 }
579
580 void PM_check_punch(entity this)
581 {
582 #ifdef SVQC
583         if (this.punchangle != '0 0 0')
584         {
585                 float f = vlen(this.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
586                 if (f > 0)
587                         this.punchangle = normalize(this.punchangle) * f;
588                 else
589                         this.punchangle = '0 0 0';
590         }
591
592         if (this.punchvector != '0 0 0')
593         {
594                 float f = vlen(this.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
595                 if (f > 0)
596                         this.punchvector = normalize(this.punchvector) * f;
597                 else
598                         this.punchvector = '0 0 0';
599         }
600 #endif
601 }
602
603 // predict frozen movement, as frozen players CAN move in some cases
604 void PM_check_frozen(entity this)
605 {
606         if (!PHYS_FROZEN(this))
607                 return;
608         if (PHYS_DODGING_FROZEN(this)
609 #ifdef SVQC
610         && IS_REAL_CLIENT(this)
611 #endif
612         )
613         {
614                 this.movement_x = bound(-5, this.movement.x, 5);
615                 this.movement_y = bound(-5, this.movement.y, 5);
616                 this.movement_z = bound(-5, this.movement.z, 5);
617         }
618         else
619                 this.movement = '0 0 0';
620
621         vector midpoint = ((this.absmin + this.absmax) * 0.5);
622         if (pointcontents(midpoint) == CONTENT_WATER)
623         {
624                 this.velocity = this.velocity * 0.5;
625
626                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
627                         this.velocity_z = 200;
628         }
629 }
630
631 void PM_check_hitground(entity this)
632 {
633 #ifdef SVQC
634         if (!this.wasFlying) return;
635     this.wasFlying = false;
636     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
637     if (time < this.ladder_time) return;
638     if (this.hook) return;
639     this.nextstep = time + 0.3 + random() * 0.1;
640     trace_dphitq3surfaceflags = 0;
641     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
642     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
643     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
644         ? GS_FALL_METAL
645         : GS_FALL;
646     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
647     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
648 #endif
649 }
650
651 void PM_Footsteps(entity this)
652 {
653 #ifdef SVQC
654         if (!g_footsteps) return;
655         if (IS_DUCKED(this)) return;
656         if (time >= this.lastground + 0.2) return;
657         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
658         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
659         {
660                 this.nextstep = time + 0.3 + random() * 0.1;
661                 trace_dphitq3surfaceflags = 0;
662                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
663                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
664                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
665                         ? GS_STEP_METAL
666                         : GS_STEP;
667                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
668         }
669 #endif
670 }
671
672 void PM_check_blocked(entity this)
673 {
674 #ifdef SVQC
675         if (!this.player_blocked)
676                 return;
677         this.movement = '0 0 0';
678         this.disableclientprediction = 1;
679 #endif
680 }
681
682 void PM_fly(entity this, float maxspd_mod)
683 {
684         // noclipping or flying
685         UNSET_ONGROUND(this);
686
687         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
688         makevectors(this.v_angle);
689         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
690         vector wishvel = v_forward * this.movement.x
691                                         + v_right * this.movement.y
692                                         + '0 0 1' * this.movement.z;
693         // acceleration
694         vector wishdir = normalize(wishvel);
695         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
696 #ifdef SVQC
697         if(time >= PHYS_TELEPORT_TIME(this))
698 #elif defined(CSQC)
699         if(pmove_waterjumptime <= 0)
700 #endif
701                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
702 }
703
704 void PM_swim(entity this, float maxspd_mod)
705 {
706         // swimming
707         UNSET_ONGROUND(this);
708
709         float jump = PHYS_INPUT_BUTTON_JUMP(this);
710         // water jump only in certain situations
711         // this mimics quakeworld code
712         if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc)
713         {
714                 vector yawangles = '0 1 0' * this.v_angle.y;
715                 makevectors(yawangles);
716                 vector forward = v_forward;
717                 vector spot = this.origin + 24 * forward;
718                 spot_z += 8;
719                 traceline(spot, spot, MOVE_NOMONSTERS, this);
720                 if (trace_startsolid)
721                 {
722                         spot_z += 24;
723                         traceline(spot, spot, MOVE_NOMONSTERS, this);
724                         if (!trace_startsolid)
725                         {
726                                 this.velocity = forward * 50;
727                                 this.velocity_z = 310;
728                                 UNSET_ONGROUND(this);
729                                 SET_JUMP_HELD(this);
730                         }
731                 }
732         }
733         makevectors(this.v_angle);
734         float wishdown = this.movement.z;
735         if(PHYS_INPUT_BUTTON_CROUCH(this))
736                 wishdown = -PHYS_MAXSPEED(this);
737         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
738         vector wishvel = v_forward * this.movement.x
739                                         + v_right * this.movement.y
740                                         + '0 0 1' * wishdown;
741         if(this.viewloc)
742                 wishvel.z = -160; // drift anyway
743         else if (wishvel == '0 0 0')
744                 wishvel = '0 0 -60'; // drift towards bottom
745
746         vector wishdir = normalize(wishvel);
747         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod) * 0.7;
748
749 //      if (pmove_waterjumptime <= 0) // TODO: use
750     {
751                 // water friction
752                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this);
753                 f = min(max(0, f), 1);
754                 this.velocity *= f;
755
756                 f = wishspeed - this.velocity * wishdir;
757                 if (f > 0)
758                 {
759                         float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, f);
760                         this.velocity += accelspeed * wishdir;
761                 }
762
763                 // holding jump button swims upward slowly
764                 if (jump && !this.viewloc)
765                 {
766 #if 0
767                         if (this.watertype & CONTENT_LAVA)
768                                 this.velocity_z =  50;
769                         else if (this.watertype & CONTENT_SLIME)
770                                 this.velocity_z =  80;
771                         else
772                         {
773                                 if (IS_NEXUIZ_DERIVED(gamemode))
774 #endif
775                                 if(this.waterlevel >= WATERLEVEL_SUBMERGED)
776                                         this.velocity_z = PHYS_MAXSPEED(this);
777                                 else
778                                         this.velocity_z = 200;
779 #if 0
780                                 else
781                                         this.velocity_z = 100;
782                         }
783 #endif
784                 }
785         }
786         if(this.viewloc)
787         {
788                 const float addspeed = wishspeed - this.velocity * wishdir;
789                 if (addspeed > 0)
790                 {
791                         const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
792                         this.velocity += accelspeed * wishdir;
793                 }
794         }
795         else
796         {
797                 // water acceleration
798                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
799         }
800 }
801
802 .vector oldmovement;
803 void PM_ladder(entity this, float maxspd_mod)
804 {
805         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
806         UNSET_ONGROUND(this);
807
808         float g;
809         g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
810         if (PHYS_ENTGRAVITY(this))
811                 g *= PHYS_ENTGRAVITY(this);
812         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
813         {
814                 g *= 0.5;
815                 this.velocity_z += g;
816         }
817
818         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
819         makevectors(this.v_angle);
820         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
821         vector wishvel = v_forward * this.movement_x
822                                         + v_right * this.movement_y
823                                         + '0 0 1' * this.movement_z;
824         if(this.viewloc)
825                 wishvel.z = this.oldmovement.x;
826         this.velocity_z += g;
827         if (this.ladder_entity.classname == "func_water")
828         {
829                 float f = vlen(wishvel);
830                 if (f > this.ladder_entity.speed)
831                         wishvel *= (this.ladder_entity.speed / f);
832
833                 this.watertype = this.ladder_entity.skin;
834                 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
835                 if ((this.origin_z + this.view_ofs_z) < f)
836                         this.waterlevel = WATERLEVEL_SUBMERGED;
837                 else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f)
838                         this.waterlevel = WATERLEVEL_SWIMMING;
839                 else if ((this.origin_z + this.mins_z + 1) < f)
840                         this.waterlevel = WATERLEVEL_WETFEET;
841                 else
842                 {
843                         this.waterlevel = WATERLEVEL_NONE;
844                         this.watertype = CONTENT_EMPTY;
845                 }
846         }
847         // acceleration
848         vector wishdir = normalize(wishvel);
849         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
850 #ifdef SVQC
851         if(time >= PHYS_TELEPORT_TIME(this))
852 #elif defined(CSQC)
853         if(pmove_waterjumptime <= 0)
854 #endif
855                 // water acceleration
856                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this)*maxspd_mod, 1, 0, 0, 0);
857 }
858
859 void PM_jetpack(entity this, float maxspd_mod)
860 {
861         //makevectors(this.v_angle.y * '0 1 0');
862         makevectors(this.v_angle);
863         vector wishvel = v_forward * this.movement_x
864                                         + v_right * this.movement_y;
865         // add remaining speed as Z component
866         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
867         // fix speedhacks :P
868         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
869         // add the unused velocity as up component
870         wishvel_z = 0;
871
872         // if (PHYS_INPUT_BUTTON_JUMP(this))
873                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
874
875         // it is now normalized, so...
876         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
877         float a_up = PHYS_JETPACK_ACCEL_UP(this);
878         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
879
880         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
881
882         wishvel_x *= a_side;
883         wishvel_y *= a_side;
884         wishvel_z *= a_up;
885         wishvel_z += a_add;
886
887         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
888
889         float best = 0;
890         //////////////////////////////////////////////////////////////////////////////////////
891         // finding the maximum over all vectors of above form
892         // with wishvel having an absolute value of 1
893         //////////////////////////////////////////////////////////////////////////////////////
894         // we're finding the maximum over
895         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
896         // for z in the range from -1 to 1
897         //////////////////////////////////////////////////////////////////////////////////////
898         // maximum is EITHER attained at the single extreme point:
899         float a_diff = a_side * a_side - a_up * a_up;
900         float f;
901         if (a_diff != 0)
902         {
903                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
904                 if (f > -1 && f < 1) // can it be attained?
905                 {
906                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
907                         //print("middle\n");
908                 }
909         }
910         // OR attained at z = 1:
911         f = (a_up + a_add) * (a_up + a_add);
912         if (f > best)
913         {
914                 best = f;
915                 //print("top\n");
916         }
917         // OR attained at z = -1:
918         f = (a_up - a_add) * (a_up - a_add);
919         if (f > best)
920         {
921                 best = f;
922                 //print("bottom\n");
923         }
924         best = sqrt(best);
925         //////////////////////////////////////////////////////////////////////////////////////
926
927         //print("best possible acceleration: ", ftos(best), "\n");
928
929         float fxy, fz;
930         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
931         if (wishvel_z - PHYS_GRAVITY(this) > 0)
932                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
933         else
934                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
935
936         float fvel;
937         fvel = vlen(wishvel);
938         wishvel_x *= fxy;
939         wishvel_y *= fxy;
940         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
941
942         fvel = min(1, vlen(wishvel) / best);
943         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
944                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel));
945         else
946                 f = 1;
947
948         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
949
950         if (f > 0 && wishvel != '0 0 0')
951         {
952                 this.velocity = this.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
953                 UNSET_ONGROUND(this);
954
955 #ifdef SVQC
956                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
957                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel * f;
958
959                 ITEMS_STAT(this) |= IT_USING_JETPACK;
960
961                 // jetpack also inhibits health regeneration, but only for 1 second
962                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
963 #endif
964         }
965 }
966
967 void PM_walk(entity this, float maxspd_mod)
968 {
969         if (!WAS_ONGROUND(this))
970         {
971 #ifdef SVQC
972                 if (autocvar_speedmeter)
973                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
974 #endif
975                 if (this.lastground < time - 0.3)
976                         this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
977 #ifdef SVQC
978                 if (this.jumppadcount > 1)
979                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
980                 this.jumppadcount = 0;
981 #endif
982         }
983
984         // walking
985         makevectors(this.v_angle.y * '0 1 0');
986         const vector wishvel = v_forward * this.movement.x
987                                                 + v_right * this.movement.y;
988         // acceleration
989         const vector wishdir = normalize(wishvel);
990         float wishspeed = vlen(wishvel);
991         wishspeed = min(wishspeed, PHYS_MAXSPEED(this) * maxspd_mod);
992         if (IS_DUCKED(this)) wishspeed *= 0.5;
993
994         // apply edge friction
995         const float f2 = vlen2(vec2(this.velocity));
996         if (f2 > 0)
997         {
998                 trace_dphitq3surfaceflags = 0;
999                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
1000                 // TODO: apply edge friction
1001                 // apply ground friction
1002                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1003                         ? PHYS_FRICTION_SLICK(this)
1004                         : PHYS_FRICTION(this);
1005
1006                 float f = sqrt(f2);
1007                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
1008                 f = max(0, f);
1009                 this.velocity *= f;
1010                 /*
1011                    Mathematical analysis time!
1012
1013                    Our goal is to invert this mess.
1014
1015                    For the two cases we get:
1016                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
1017                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1018                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1019                    and
1020                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1021                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1022
1023                    These cases would be chosen ONLY if:
1024                         v0 < PHYS_STOPSPEED(this)
1025                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
1026                         v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1027                    and, respectively:
1028                         v0 >= PHYS_STOPSPEED(this)
1029                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
1030                         v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1031                  */
1032         }
1033         const float addspeed = wishspeed - this.velocity * wishdir;
1034         if (addspeed > 0)
1035         {
1036                 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1037                 this.velocity += accelspeed * wishdir;
1038         }
1039 }
1040
1041 void PM_air(entity this, float buttons_prev, float maxspd_mod)
1042 {
1043         makevectors(this.v_angle.y * '0 1 0');
1044         vector wishvel = v_forward * this.movement.x
1045                                         + v_right * this.movement.y;
1046         // acceleration
1047         vector wishdir = normalize(wishvel);
1048         float wishspeed = vlen(wishvel);
1049
1050 #ifdef SVQC
1051         if(time >= PHYS_TELEPORT_TIME(this))
1052 #elif defined(CSQC)
1053         if(pmove_waterjumptime <= 0)
1054 #endif
1055         {
1056                 float maxairspd = PHYS_MAXAIRSPEED(this) * min(maxspd_mod, 1);
1057
1058                 // apply air speed limit
1059                 float airaccelqw = PHYS_AIRACCEL_QW(this);
1060                 float wishspeed0 = wishspeed;
1061                 wishspeed = min(wishspeed, maxairspd);
1062                 if (IS_DUCKED(this))
1063                         wishspeed *= 0.5;
1064                 float airaccel = PHYS_AIRACCELERATE(this) * min(maxspd_mod, 1);
1065
1066                 float accelerating = (this.velocity * wishdir > 0);
1067                 float wishspeed2 = wishspeed;
1068
1069                 // CPM: air control
1070                 if (PHYS_AIRSTOPACCELERATE(this))
1071                 {
1072                         vector curdir = normalize(vec2(this.velocity));
1073                         airaccel += (PHYS_AIRSTOPACCELERATE(this)*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1074                 }
1075                 // note that for straight forward jumping:
1076                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1077                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1078                 // -->
1079                 // dv/dt = accel * maxspeed (when slow)
1080                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1081                 // log dv/dt = logaccel + logmaxspeed (when slow)
1082                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1083                 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
1084                 if (PHYS_MAXAIRSTRAFESPEED(this))
1085                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(this)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED(this)*maxspd_mod));
1086                 if (PHYS_AIRSTRAFEACCELERATE(this))
1087                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(this)*maxspd_mod);
1088                 if (PHYS_AIRSTRAFEACCEL_QW(this))
1089                         airaccelqw =
1090                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
1091                 *
1092                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
1093                 // !CPM
1094
1095                 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0)
1096                         PM_AirAccelerate(this, wishdir, wishspeed2);
1097                 else {
1098                     float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
1099                         PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
1100         }
1101
1102                 if (PHYS_AIRCONTROL(this))
1103                         CPM_PM_Aircontrol(this, wishdir, wishspeed2);
1104         }
1105 }
1106
1107 // used for calculating airshots
1108 bool IsFlying(entity this)
1109 {
1110         if(IS_ONGROUND(this))
1111                 return false;
1112         if(this.waterlevel >= WATERLEVEL_SWIMMING)
1113                 return false;
1114         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
1115         if(trace_fraction < 1)
1116                 return false;
1117         return true;
1118 }
1119
1120 void PM_Main(entity this)
1121 {
1122         int buttons = PHYS_INPUT_BUTTON_MASK(this);
1123 #ifdef CSQC
1124         this.items = STAT(ITEMS);
1125
1126         this.movement = PHYS_INPUT_MOVEVALUES(this);
1127
1128         this.spectatorspeed = STAT(SPECTATORSPEED);
1129
1130         this.team = myteam + 1; // is this correct?
1131         if (!(PHYS_INPUT_BUTTON_JUMP(this))) // !jump
1132                 UNSET_JUMP_HELD(this); // canjump = true
1133         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1134
1135         PM_ClientMovement_UpdateStatus(this);
1136 #endif
1137
1138         this.oldmovement = this.movement;
1139
1140
1141 #ifdef SVQC
1142         WarpZone_PlayerPhysics_FixVAngle(this);
1143 #endif
1144         float maxspeed_mod = 1;
1145         maxspeed_mod *= PHYS_HIGHSPEED(this);
1146
1147 #ifdef SVQC
1148         Physics_UpdateStats(this, maxspeed_mod);
1149
1150         if (this.PlayerPhysplug)
1151                 if (this.PlayerPhysplug(this))
1152                         return;
1153 #elif defined(CSQC)
1154         if(hud != HUD_NORMAL)
1155                 return; // no vehicle prediction (yet)
1156 #endif
1157
1158 #ifdef SVQC
1159         anticheat_physics(this);
1160 #endif
1161
1162         if (PM_check_specialcommand(this, buttons))
1163                 return;
1164 #ifdef SVQC
1165         if (sv_maxidle > 0)
1166         {
1167                 if (buttons != this.buttons_old || this.movement != this.movement_old || this.v_angle != this.v_angle_old)
1168                         this.parm_idlesince = time;
1169         }
1170 #endif
1171         int buttons_prev = this.buttons_old;
1172         this.buttons_old = buttons;
1173         this.movement_old = this.movement;
1174         this.v_angle_old = this.v_angle;
1175
1176         PM_check_nickspam(this);
1177
1178         PM_check_punch(this);
1179 #ifdef SVQC
1180         if (IS_BOT_CLIENT(this))
1181         {
1182                 if (playerdemo_read(this))
1183                         return;
1184                 bot_think(this);
1185         }
1186 #endif
1187
1188 #ifdef SVQC
1189         if (IS_PLAYER(this))
1190         {
1191                 const bool allowed_to_move = (time >= game_starttime);
1192                 if (!allowed_to_move)
1193                 {
1194                         this.velocity = '0 0 0';
1195                         set_movetype(this, MOVETYPE_NONE);
1196                         this.disableclientprediction = 2;
1197                 }
1198                 else if (this.disableclientprediction == 2)
1199                 {
1200                         if (this.move_movetype == MOVETYPE_NONE)
1201                                 set_movetype(this, MOVETYPE_WALK);
1202                         this.disableclientprediction = 0;
1203                 }
1204         }
1205 #endif
1206
1207 #ifdef SVQC
1208         if (this.move_movetype == MOVETYPE_NONE)
1209                 return;
1210
1211         // when we get here, disableclientprediction cannot be 2
1212         this.disableclientprediction = (this.move_qcphysics) ? -1 : 0;
1213 #endif
1214
1215         viewloc_PlayerPhysics(this);
1216
1217         PM_check_frozen(this);
1218
1219         PM_check_blocked(this);
1220
1221         maxspeed_mod = 1;
1222
1223         if (this.in_swamp)
1224                 maxspeed_mod *= this.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1225
1226         // conveyors: first fix velocity
1227         if (this.conveyor.state)
1228                 this.velocity -= this.conveyor.movedir;
1229
1230         MUTATOR_CALLHOOK(PlayerPhysics, this);
1231
1232         if (!IS_PLAYER(this))
1233         {
1234 #ifdef SVQC
1235                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1236                 if (!this.spectatorspeed)
1237                         this.spectatorspeed = maxspeed_mod;
1238                 if (this.impulse && this.impulse <= 19 || (this.impulse >= 200 && this.impulse <= 209) || (this.impulse >= 220 && this.impulse <= 229))
1239                 {
1240                         if (this.lastclassname != STR_PLAYER)
1241                         {
1242                                 if (this.impulse == 10 || this.impulse == 15 || this.impulse == 18 || (this.impulse >= 200 && this.impulse <= 209))
1243                                         this.spectatorspeed = bound(1, this.spectatorspeed + 0.5, 5);
1244                                 else if (this.impulse == 11)
1245                                         this.spectatorspeed = maxspeed_mod;
1246                                 else if (this.impulse == 12 || this.impulse == 16  || this.impulse == 19 || (this.impulse >= 220 && this.impulse <= 229))
1247                                         this.spectatorspeed = bound(1, this.spectatorspeed - 0.5, 5);
1248                                 else if (this.impulse >= 1 && this.impulse <= 9)
1249                                         this.spectatorspeed = 1 + 0.5 * (this.impulse - 1);
1250                         } // otherwise just clear
1251                         this.impulse = 0;
1252                 }
1253 #endif
1254                 maxspeed_mod = this.spectatorspeed;
1255         }
1256 #ifdef SVQC
1257
1258         float spd = max(PHYS_MAXSPEED(this), PHYS_MAXAIRSPEED(this)) * maxspeed_mod;
1259         if(this.speed != spd)
1260         {
1261                 this.speed = spd;
1262                 string temps = ftos(spd);
1263                 stuffcmd(this, strcat("cl_forwardspeed ", temps, "\n"));
1264                 stuffcmd(this, strcat("cl_backspeed ", temps, "\n"));
1265                 stuffcmd(this, strcat("cl_sidespeed ", temps, "\n"));
1266                 stuffcmd(this, strcat("cl_upspeed ", temps, "\n"));
1267         }
1268
1269         if(this.jumpspeedcap_min != autocvar_sv_jumpspeedcap_min)
1270         {
1271                 this.jumpspeedcap_min = autocvar_sv_jumpspeedcap_min;
1272                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_min \"%s\"\n", autocvar_sv_jumpspeedcap_min));
1273         }
1274         if(this.jumpspeedcap_max != autocvar_sv_jumpspeedcap_max)
1275         {
1276                 this.jumpspeedcap_max = autocvar_sv_jumpspeedcap_max;
1277                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_max \"%s\"\n", autocvar_sv_jumpspeedcap_max));
1278         }
1279 #endif
1280
1281         if(IS_DEAD(this))
1282         {
1283                 // handle water here
1284                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
1285                 if(pointcontents(midpoint) == CONTENT_WATER)
1286                 {
1287                         this.velocity = this.velocity * 0.5;
1288
1289                         // do we want this?
1290                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1291                                 //{ this.velocity_z = 70; }
1292                 }
1293                 goto end;
1294         }
1295
1296 #ifdef SVQC
1297         if (!this.fixangle)
1298                 this.angles = '0 1 0' * this.v_angle.y;
1299 #endif
1300
1301         if (IS_PLAYER(this) && IS_ONGROUND(this))
1302         {
1303                 PM_check_hitground(this);
1304                 PM_Footsteps(this);
1305         }
1306
1307 #ifdef SVQC
1308         if(IsFlying(this))
1309                 this.wasFlying = 1;
1310 #endif
1311
1312         if (IS_PLAYER(this))
1313                 CheckPlayerJump(this);
1314
1315         if (this.flags & FL_WATERJUMP)
1316         {
1317                 this.velocity_x = this.movedir.x;
1318                 this.velocity_y = this.movedir.y;
1319                 if (this.waterlevel == WATERLEVEL_NONE
1320                 #ifdef CSQC
1321                         || pmove_waterjumptime <= 0
1322                 #elif defined(SVQC)
1323                         || time > PHYS_TELEPORT_TIME(this)
1324                 #endif
1325                         )
1326                 {
1327                         this.flags &= ~FL_WATERJUMP;
1328                 #ifdef CSQC
1329                         pmove_waterjumptime = 0;
1330                 #elif defined(CSQC)
1331                         PHYS_TELEPORT_TIME(this) = 0;
1332                 #endif
1333                 }
1334         }
1335
1336         else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod))
1337                 { }
1338
1339 #ifdef SVQC
1340         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1341 #elif defined(CSQC)
1342         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1343 #endif
1344                 PM_fly(this, maxspeed_mod);
1345
1346         else if (this.waterlevel >= WATERLEVEL_SWIMMING)
1347                 PM_swim(this, maxspeed_mod);
1348
1349         else if (time < this.ladder_time)
1350                 PM_ladder(this, maxspeed_mod);
1351
1352         else if (ITEMS_STAT(this) & IT_USING_JETPACK)
1353                 PM_jetpack(this, maxspeed_mod);
1354
1355         else if (IS_ONGROUND(this))
1356                 PM_walk(this, maxspeed_mod);
1357
1358         else
1359                 PM_air(this, buttons_prev, maxspeed_mod);
1360
1361 LABEL(end)
1362         if (IS_ONGROUND(this))
1363                 this.lastground = time;
1364
1365         // conveyors: then break velocity again
1366         if(this.conveyor.state)
1367                 this.velocity += this.conveyor.movedir;
1368
1369         this.lastflags = this.flags;
1370
1371         this.lastclassname = this.classname;
1372 }
1373
1374 #if defined(SVQC)
1375 void SV_PlayerPhysics(entity this)
1376 #elif defined(CSQC)
1377 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
1378 #endif
1379 {
1380         PM_Main(this);
1381
1382 #ifdef SVQC
1383         this.pm_frametime = frametime;
1384 #endif
1385 }