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