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