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