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