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