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