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