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