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