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