Enforce repressing key to jump if player is in view location and underwater
[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         STAT(PL_MIN, this) = autocvar_sv_player_mins;
43         STAT(PL_MAX, this) = autocvar_sv_player_maxs;
44         STAT(PL_VIEW_OFS, this) = autocvar_sv_player_viewoffset;
45         STAT(PL_CROUCH_MIN, this) = autocvar_sv_player_crouch_mins;
46         STAT(PL_CROUCH_MAX, this) = autocvar_sv_player_crouch_maxs;
47         STAT(PL_CROUCH_VIEW_OFS, this) = autocvar_sv_player_crouch_viewoffset;
48
49         // old stats
50         // fix some new settings
51         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
52         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
53         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
54         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
55         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
56         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
57         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
58         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
59         STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
60         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
61         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
62         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
63         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
64         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
65         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
66         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
67         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
68         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
69         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
70         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
71         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
72 }
73 #endif
74
75 float IsMoveInDirection(vector mv, float ang) // key mix factor
76 {
77         if (mv_x == 0 && mv_y == 0)
78                 return 0; // avoid division by zero
79         ang -= RAD2DEG * atan2(mv_y, mv_x);
80         ang = remainder(ang, 360) / 45;
81         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
82 }
83
84 float GeomLerp(float a, float _lerp, float b)
85 {
86         return a == 0 ? (_lerp < 1 ? 0 : b)
87                 : b == 0 ? (_lerp > 0 ? 0 : a)
88                 : a * pow(fabs(b / a), _lerp);
89 }
90
91 void PM_ClientMovement_UpdateStatus(entity this)
92 {
93 #ifdef CSQC
94         if(!IS_PLAYER(this))
95                 return;
96
97         // set crouched
98         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
99         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
100         {
101                 entity wep = viewmodels[slot];
102                 if(wep.hook && !wasfreed(wep.hook))
103                 {
104                         do_crouch = false;
105                         break; // don't bother checking the others
106                 }
107         }
108         if(this.waterlevel >= WATERLEVEL_SWIMMING)
109                 do_crouch = false;
110         if(hud != HUD_NORMAL)
111                 do_crouch = false;
112         if(STAT(FROZEN, this))
113                 do_crouch = false;
114
115         if (do_crouch)
116         {
117                 // wants to crouch, this always works
118                 if (!IS_DUCKED(this)) SET_DUCKED(this);
119         }
120         else
121         {
122                 // wants to stand, if currently crouching we need to check for a low ceiling first
123                 if (IS_DUCKED(this))
124                 {
125                         tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
126                         if (!trace_startsolid) UNSET_DUCKED(this);
127                 }
128         }
129
130         if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
131                 PHYS_WATERJUMP_TIME(this) = 0;
132 #endif
133 }
134
135 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
136 {
137         float movity = IsMoveInDirection(this.movement, 0);
138         if(PHYS_AIRCONTROL_BACKWARDS(this))
139                 movity += IsMoveInDirection(this.movement, 180);
140
141         float k = 32 * (2 * movity - 1);
142         if (k <= 0)
143                 return;
144
145         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
146
147         float zspeed = this.velocity_z;
148         this.velocity_z = 0;
149         float xyspeed = vlen(this.velocity);
150         this.velocity = normalize(this.velocity);
151
152         float dot = this.velocity * wishdir;
153
154         if (dot > 0) // we can't change direction while slowing down
155         {
156                 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * dt;
157                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
158                 k *= PHYS_AIRCONTROL(this);
159                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
160         }
161
162         this.velocity = this.velocity * xyspeed;
163         this.velocity_z = zspeed;
164 }
165
166 float AdjustAirAccelQW(float accelqw, float factor)
167 {
168         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
169 }
170
171 // example config for alternate speed clamping:
172 //   sv_airaccel_qw 0.8
173 //   sv_airaccel_sideways_friction 0
174 //   prvm_globalset server speedclamp_mode 1
175 //     (or 2)
176 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
177 {
178         float speedclamp = stretchfactor > 0 ? stretchfactor
179         : accelqw < 0 ? 1 // full clamping, no stretch
180         : -1; // no clamping
181
182         accelqw = fabs(accelqw);
183
184         if (GAMEPLAYFIX_Q2AIRACCELERATE)
185                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
186
187         float vel_straight = this.velocity * wishdir;
188         float vel_z = this.velocity_z;
189         vector vel_xy = vec2(this.velocity);
190         vector vel_perpend = vel_xy - vel_straight * wishdir;
191
192         float step = accel * dt * wishspeed0;
193
194         float vel_xy_current  = vlen(vel_xy);
195         if (speedlimit)
196                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
197         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
198         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
199         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
200         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
201
202         if (sidefric < 0 && (vel_perpend*vel_perpend))
203                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
204         {
205                 float f = max(0, 1 + dt * wishspeed * sidefric);
206                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
207                 // assume: themin > 1
208                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
209                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
210                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
211                 // obviously, this cannot be
212                 if (themin <= 0)
213                         vel_perpend *= f;
214                 else
215                 {
216                         themin = sqrt(themin);
217                         vel_perpend *= max(themin, f);
218                 }
219         }
220         else
221                 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
222
223         vel_xy = vel_straight * wishdir + vel_perpend;
224
225         if (speedclamp >= 0)
226         {
227                 float vel_xy_preclamp;
228                 vel_xy_preclamp = vlen(vel_xy);
229                 if (vel_xy_preclamp > 0) // prevent division by zero
230                 {
231                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
232                         if (vel_xy_current < vel_xy_preclamp)
233                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
234                 }
235         }
236
237         this.velocity = vel_xy + vel_z * '0 0 1';
238 }
239
240 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
241 {
242         if (wishspeed == 0)
243                 return;
244
245         vector curvel = this.velocity;
246         curvel_z = 0;
247         float curspeed = vlen(curvel);
248
249         if (wishspeed > curspeed * 1.01)
250                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
251         else
252         {
253                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
254                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
255         }
256         vector wishvel = wishdir * wishspeed;
257         vector acceldir = wishvel - curvel;
258         float addspeed = vlen(acceldir);
259         acceldir = normalize(acceldir);
260
261         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
262
263         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
264         {
265                 vector curdir = normalize(curvel);
266                 float dot = acceldir * curdir;
267                 if (dot < 0)
268                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
269         }
270
271         this.velocity += accelspeed * acceldir;
272 }
273
274
275 /*
276 =============
277 PlayerJump
278
279 When you press the jump key
280 returns true if handled
281 =============
282 */
283 bool PlayerJump(entity this)
284 {
285         if (PHYS_FROZEN(this))
286                 return true; // no jumping in freezetag when frozen
287
288 #ifdef SVQC
289         if (this.player_blocked)
290                 return true; // no jumping while blocked
291 #endif
292
293         bool doublejump = false;
294         float mjumpheight = PHYS_JUMPVELOCITY(this);
295         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
296
297         if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
298                 return true;
299
300         mjumpheight = M_ARGV(1, float);
301         doublejump = M_ARGV(2, bool);
302
303         if (this.waterlevel >= WATERLEVEL_SWIMMING)
304         {
305                 if(this.viewloc)
306                 {
307                         doublejump = true;
308                         mjumpheight *= 0.7;
309                         track_jump = true;
310                 }
311                 else
312                 {
313                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
314                         return true;
315                 }
316         }
317
318         if (!doublejump)
319                 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
320                         return IS_JUMP_HELD(this);
321
322         if(PHYS_TRACK_CANJUMP(this))
323                 track_jump = true;
324
325         if (track_jump)
326                 if (IS_JUMP_HELD(this))
327                         return true;
328
329         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
330         // velocity bounds.  Final velocity is bound between (jumpheight *
331         // min + jumpheight) and (jumpheight * max + jumpheight);
332
333         if(PHYS_JUMPSPEEDCAP_MIN != "")
334         {
335                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
336
337                 if (this.velocity_z < minjumpspeed)
338                         mjumpheight += minjumpspeed - this.velocity_z;
339         }
340
341         if(PHYS_JUMPSPEEDCAP_MAX != "")
342         {
343                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
344                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
345
346                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
347                 {
348                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
349
350                         if (this.velocity_z > maxjumpspeed)
351                                 mjumpheight -= this.velocity_z - maxjumpspeed;
352                 }
353         }
354
355         if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
356         {
357 #ifdef SVQC
358                 if(autocvar_speedmeter)
359                         LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
360 #endif
361                 if(this.lastground < time - 0.3)
362                 {
363                         float f = (1 - PHYS_FRICTION_ONLAND(this));
364                         this.velocity_x *= f;
365                         this.velocity_y *= f;
366                 }
367 #ifdef SVQC
368                 if(this.jumppadcount > 1)
369                         LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
370                 this.jumppadcount = 0;
371 #endif
372         }
373
374         this.velocity_z += mjumpheight;
375
376         UNSET_ONGROUND(this);
377         UNSET_ONSLICK(this);
378         SET_JUMP_HELD(this);
379
380 #ifdef SVQC
381
382         this.oldvelocity_z = this.velocity_z;
383
384         animdecide_setaction(this, ANIMACTION_JUMP, true);
385
386         if (autocvar_g_jump_grunt)
387                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
388 #endif
389         return true;
390 }
391
392 void CheckWaterJump(entity this)
393 {
394 // check for a jump-out-of-water
395         makevectors(this.v_angle);
396         vector start = this.origin;
397         start_z += 8;
398         v_forward_z = 0;
399         normalize(v_forward);
400         vector end = start + v_forward*24;
401         traceline (start, end, true, this);
402         if (trace_fraction < 1)
403         {       // solid at waist
404                 start_z = start_z + this.maxs_z - 8;
405                 end = start + v_forward*24;
406                 this.movedir = trace_plane_normal * -50;
407                 traceline(start, end, true, this);
408                 if (trace_fraction == 1)
409                 {       // open at eye level
410                         this.velocity_z = 225;
411                         this.flags |= FL_WATERJUMP;
412                         SET_JUMP_HELD(this);
413                 #ifdef SVQC
414                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
415                 #elif defined(CSQC)
416                         PHYS_WATERJUMP_TIME(this) = 2;
417                 #endif
418                 }
419         }
420 }
421
422
423 #ifdef SVQC
424         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
425 #elif defined(CSQC)
426         float autocvar_cl_jetpack_jump;
427         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
428 #endif
429 .float jetpack_stopped;
430 void CheckPlayerJump(entity this)
431 {
432 #ifdef SVQC
433         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
434 #endif
435         if (JETPACK_JUMP(this) < 2)
436                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
437
438         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
439         {
440                 bool playerjump = PlayerJump(this); // required
441
442                 bool air_jump = !playerjump || M_ARGV(2, bool);
443                 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
444                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
445
446                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
447                 else if (this.jetpack_stopped) { }
448                 else if (!has_fuel)
449                 {
450 #ifdef SVQC
451                         if (was_flying) // TODO: ran out of fuel message
452                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
453                         else if (activate)
454                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
455 #endif
456                         this.jetpack_stopped = true;
457                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
458                 }
459                 else if (activate && !PHYS_FROZEN(this))
460                         ITEMS_STAT(this) |= IT_USING_JETPACK;
461         }
462         else
463         {
464                 this.jetpack_stopped = false;
465                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
466         }
467         if (!PHYS_INPUT_BUTTON_JUMP(this))
468                 UNSET_JUMP_HELD(this);
469
470         if (this.waterlevel == WATERLEVEL_SWIMMING)
471                 CheckWaterJump(this);
472 }
473
474 float racecar_angle(float forward, float down)
475 {
476         if (forward < 0)
477         {
478                 forward = -forward;
479                 down = -down;
480         }
481
482         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
483
484         float angle_mult = forward / (800 + forward);
485
486         if (ret > 180)
487                 return ret * angle_mult + 360 * (1 - angle_mult);
488         else
489                 return ret * angle_mult;
490 }
491
492 #ifdef SVQC
493 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
494 .float specialcommand_pos;
495 void SpecialCommand(entity this)
496 {
497         if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
498                 LOG_INFO("A hollow voice says \"Plugh\".\n");
499 }
500 #endif
501
502 bool PM_check_specialcommand(entity this, int buttons)
503 {
504 #ifdef SVQC
505         string c;
506         if (!buttons)
507                 c = "x";
508         else if (buttons == 1)
509                 c = "1";
510         else if (buttons == 2)
511                 c = " ";
512         else if (buttons == 128)
513                 c = "s";
514         else if (buttons == 256)
515                 c = "w";
516         else if (buttons == 512)
517                 c = "a";
518         else if (buttons == 1024)
519                 c = "d";
520         else
521                 c = "?";
522
523         if (c == substring(specialcommand, this.specialcommand_pos, 1))
524         {
525                 this.specialcommand_pos += 1;
526                 if (this.specialcommand_pos >= strlen(specialcommand))
527                 {
528                         this.specialcommand_pos = 0;
529                         SpecialCommand(this);
530                         return true;
531                 }
532         }
533         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
534                 this.specialcommand_pos = 0;
535 #endif
536         return false;
537 }
538
539 void PM_check_nickspam(entity this)
540 {
541 #ifdef SVQC
542         if (time >= this.nickspamtime)
543                 return;
544         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
545         {
546                 // slight annoyance for nick change scripts
547                 this.movement = -1 * this.movement;
548                 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;
549
550                 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!
551                 {
552                         this.v_angle_x = random() * 360;
553                         this.v_angle_y = random() * 360;
554                         // at least I'm not forcing retardedview by also assigning to angles_z
555                         this.fixangle = true;
556                 }
557         }
558 #endif
559 }
560
561 void PM_check_punch(entity this, float dt)
562 {
563 #ifdef SVQC
564         if (this.punchangle != '0 0 0')
565         {
566                 float f = vlen(this.punchangle) - 10 * dt;
567                 if (f > 0)
568                         this.punchangle = normalize(this.punchangle) * f;
569                 else
570                         this.punchangle = '0 0 0';
571         }
572
573         if (this.punchvector != '0 0 0')
574         {
575                 float f = vlen(this.punchvector) - 30 * dt;
576                 if (f > 0)
577                         this.punchvector = normalize(this.punchvector) * f;
578                 else
579                         this.punchvector = '0 0 0';
580         }
581 #endif
582 }
583
584 // predict frozen movement, as frozen players CAN move in some cases
585 void PM_check_frozen(entity this)
586 {
587         if (!PHYS_FROZEN(this))
588                 return;
589         if (PHYS_DODGING_FROZEN(this)
590 #ifdef SVQC
591         && IS_REAL_CLIENT(this)
592 #endif
593         )
594         {
595                 this.movement_x = bound(-5, this.movement.x, 5);
596                 this.movement_y = bound(-5, this.movement.y, 5);
597                 this.movement_z = bound(-5, this.movement.z, 5);
598         }
599         else
600                 this.movement = '0 0 0';
601
602         vector midpoint = ((this.absmin + this.absmax) * 0.5);
603         if (pointcontents(midpoint) == CONTENT_WATER)
604         {
605                 this.velocity = this.velocity * 0.5;
606
607                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
608                         this.velocity_z = 200;
609         }
610 }
611
612 void PM_check_hitground(entity this)
613 {
614 #ifdef SVQC
615         if (!this.wasFlying) return;
616     this.wasFlying = false;
617     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
618     if (time < this.ladder_time) return;
619     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
620     {
621         .entity weaponentity = weaponentities[slot];
622         if(this.(weaponentity).hook)
623                 return;
624     }
625     this.nextstep = time + 0.3 + random() * 0.1;
626     trace_dphitq3surfaceflags = 0;
627     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
628     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
629     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
630         ? GS_FALL_METAL
631         : GS_FALL;
632     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
633     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
634 #endif
635 }
636
637 void PM_Footsteps(entity this)
638 {
639 #ifdef SVQC
640         if (!g_footsteps) return;
641         if (IS_DUCKED(this)) return;
642         if (time >= this.lastground + 0.2) return;
643         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
644         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
645         {
646                 this.nextstep = time + 0.3 + random() * 0.1;
647                 trace_dphitq3surfaceflags = 0;
648                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
649                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
650                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
651                         ? GS_STEP_METAL
652                         : GS_STEP;
653                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
654         }
655 #endif
656 }
657
658 void PM_check_slick(entity this)
659 {
660         if(!IS_ONGROUND(this))
661                 return;
662
663         if(!PHYS_SLICK_APPLYGRAVITY(this))
664                 return;
665
666         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
667         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
668         {
669                 UNSET_ONGROUND(this);
670                 SET_ONSLICK(this);
671         }
672         else
673                 UNSET_ONSLICK(this);
674 }
675
676 void PM_check_blocked(entity this)
677 {
678 #ifdef SVQC
679         if (!this.player_blocked)
680                 return;
681         this.movement = '0 0 0';
682         this.disableclientprediction = 1;
683 #endif
684 }
685
686 void PM_jetpack(entity this, float maxspd_mod, float dt)
687 {
688         //makevectors(this.v_angle.y * '0 1 0');
689         makevectors(this.v_angle);
690         vector wishvel = v_forward * this.movement_x
691                                         + v_right * this.movement_y;
692         // add remaining speed as Z component
693         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
694         // fix speedhacks :P
695         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
696         // add the unused velocity as up component
697         wishvel_z = 0;
698
699         // if (PHYS_INPUT_BUTTON_JUMP(this))
700                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
701
702         // it is now normalized, so...
703         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
704         float a_up = PHYS_JETPACK_ACCEL_UP(this);
705         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
706
707         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
708
709         wishvel_x *= a_side;
710         wishvel_y *= a_side;
711         wishvel_z *= a_up;
712         wishvel_z += a_add;
713
714         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
715
716         float best = 0;
717         //////////////////////////////////////////////////////////////////////////////////////
718         // finding the maximum over all vectors of above form
719         // with wishvel having an absolute value of 1
720         //////////////////////////////////////////////////////////////////////////////////////
721         // we're finding the maximum over
722         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
723         // for z in the range from -1 to 1
724         //////////////////////////////////////////////////////////////////////////////////////
725         // maximum is EITHER attained at the single extreme point:
726         float a_diff = a_side * a_side - a_up * a_up;
727         float f;
728         if (a_diff != 0)
729         {
730                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
731                 if (f > -1 && f < 1) // can it be attained?
732                 {
733                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
734                         //print("middle\n");
735                 }
736         }
737         // OR attained at z = 1:
738         f = (a_up + a_add) * (a_up + a_add);
739         if (f > best)
740         {
741                 best = f;
742                 //print("top\n");
743         }
744         // OR attained at z = -1:
745         f = (a_up - a_add) * (a_up - a_add);
746         if (f > best)
747         {
748                 best = f;
749                 //print("bottom\n");
750         }
751         best = sqrt(best);
752         //////////////////////////////////////////////////////////////////////////////////////
753
754         //print("best possible acceleration: ", ftos(best), "\n");
755
756         float fxy, fz;
757         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
758         if (wishvel_z - PHYS_GRAVITY(this) > 0)
759                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
760         else
761                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
762
763         float fvel;
764         fvel = vlen(wishvel);
765         wishvel_x *= fxy;
766         wishvel_y *= fxy;
767         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
768
769         fvel = min(1, vlen(wishvel) / best);
770         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
771                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
772         else
773                 f = 1;
774
775         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
776
777         if (f > 0 && wishvel != '0 0 0')
778         {
779                 this.velocity = this.velocity + wishvel * f * dt;
780                 UNSET_ONGROUND(this);
781
782 #ifdef SVQC
783                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
784                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
785
786                 ITEMS_STAT(this) |= IT_USING_JETPACK;
787
788                 // jetpack also inhibits health regeneration, but only for 1 second
789                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
790 #endif
791         }
792 }
793
794 // used for calculating airshots
795 bool IsFlying(entity this)
796 {
797         if(IS_ONGROUND(this))
798                 return false;
799         if(this.waterlevel >= WATERLEVEL_SWIMMING)
800                 return false;
801         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
802         if(trace_fraction < 1)
803                 return false;
804         return true;
805 }
806
807
808 void sys_phys_update(entity this, float dt);
809 #if defined(SVQC)
810 void SV_PlayerPhysics(entity this)
811 #elif defined(CSQC)
812 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
813 #endif
814 {
815         sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
816
817 #ifdef SVQC
818         this.pm_frametime = frametime;
819 #elif defined(CSQC)
820         if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
821                 this.csqcmodel_modelflags |= MF_ROCKET;
822         else
823                 this.csqcmodel_modelflags &= ~MF_ROCKET;
824 #endif
825 }