]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Some more defs.qh cleanup, update gameplay hash
[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 <common/mapobjects/defs.qh>
9 #include "../mapobjects/trigger/viewloc.qh"
10 #include <server/sv_main.qh>
11
12 // client side physics
13 bool Physics_Valid(string thecvar)
14 {
15         return thecvar != "" && thecvar && thecvar != "default" && strhasword(autocvar_g_physics_clientselect_options, thecvar);
16 }
17
18 float Physics_ClientOption(entity this, string option, float defaultval)
19 {
20         if(!autocvar_g_physics_clientselect)
21                 return defaultval;
22
23         if(IS_REAL_CLIENT(this) && Physics_Valid(CS(this).cvar_cl_physics))
24         {
25                 string s = strcat("g_physics_", CS(this).cvar_cl_physics, "_", option);
26                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
27                         return cvar(s);
28         }
29         if(autocvar_g_physics_clientselect_default && autocvar_g_physics_clientselect_default != "" && autocvar_g_physics_clientselect_default != "default")
30         {
31                 // NOTE: not using Physics_Valid here, so the default can be forced to something normally unavailable
32                 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
33                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
34                         return cvar(s);
35         }
36         return defaultval;
37 }
38
39 void Physics_UpdateStats(entity this)
40 {
41         // 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?)
42         STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
43
44         MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
45         float maxspd_mod = PHYS_HIGHSPEED(this) * ((this.swampslug.active == ACTIVE_ACTIVE) ? this.swampslug.swamp_slowdown : 1);
46         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
47         if (autocvar_g_movement_highspeed_q3_compat) {
48           STAT(MOVEVARS_AIRACCEL_QW, this) = Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw);
49           STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw);
50           STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw);
51         } else {
52           STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
53           STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
54             ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
55             : 0;
56           STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
57         }
58         bool q3dfcompat = autocvar_sv_q3defragcompat && autocvar_sv_q3defragcompat_changehitbox; // NOTE: these hitboxes are off by 1 due to engine differences
59         STAT(PL_MIN, this) = (q3dfcompat) ? '-15 -15 -20' : autocvar_sv_player_mins;
60         STAT(PL_MAX, this) = (q3dfcompat) ? '15 15 36' : autocvar_sv_player_maxs;
61         STAT(PL_VIEW_OFS, this) = (q3dfcompat) ? '0 0 30' : autocvar_sv_player_viewoffset;
62         STAT(PL_CROUCH_MIN, this) = (q3dfcompat) ? '-15 -15 -20' : autocvar_sv_player_crouch_mins;
63         STAT(PL_CROUCH_MAX, this) = (q3dfcompat) ? '15 15 20' : autocvar_sv_player_crouch_maxs;
64         STAT(PL_CROUCH_VIEW_OFS, this) = (q3dfcompat) ? '0 0 16' : autocvar_sv_player_crouch_viewoffset;
65
66         // old stats
67         // fix some new settings
68         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
69         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
70         if (autocvar_g_movement_highspeed_q3_compat) {
71           STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed) * maxspd_mod;
72         } else {
73           STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
74         }
75
76         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
77         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
78         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
79         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
80         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
81         STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
82         STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
83         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
84         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
85         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
86         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
87         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
88         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
89         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
90         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
91         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
92         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
93         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
94         STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
95         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
96
97         MUTATOR_CALLHOOK(PlayerPhysics_PostUpdateStats, this, maxspd_mod);
98 }
99 #endif
100
101 float IsMoveInDirection(vector mv, float ang) // key mix factor
102 {
103         if (mv_x == 0 && mv_y == 0)
104                 return 0; // avoid division by zero
105         ang -= RAD2DEG * atan2(mv_y, mv_x);
106         ang = remainder(ang, 360) / 45;
107         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
108 }
109
110 float GeomLerp(float a, float _lerp, float b)
111 {
112         return a == 0 ? (_lerp < 1 ? 0 : b)
113                 : b == 0 ? (_lerp > 0 ? 0 : a)
114                 : a * (fabs(b / a) ** _lerp);
115 }
116
117 void PM_ClientMovement_UpdateStatus(entity this)
118 {
119         if(!IS_PLAYER(this))
120                 return;
121
122         bool have_hook = false;
123         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
124         {
125         #if defined(CSQC)
126                 entity wepent = viewmodels[slot];
127         #elif defined(SVQC)
128                 .entity weaponentity = weaponentities[slot];
129                 entity wepent = this.(weaponentity);
130         #endif
131                 if(wepent.hook && !wasfreed(wepent.hook))
132                 {
133                         have_hook = true;
134                         break;
135                 }
136         }
137         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
138         if(this.viewloc && !(this.viewloc.spawnflags & VIEWLOC_FREEMOVE) && PHYS_CS(this).movement.x < 0)
139                 do_crouch = true;
140         if (have_hook) {
141                 do_crouch = false;
142         //} else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
143                 //do_crouch = false;
144         } else if (PHYS_INVEHICLE(this)) {
145                 do_crouch = false;
146         } else if (STAT(FROZEN, this) || IS_DEAD(this)) {
147                 do_crouch = false;
148     }
149
150     MUTATOR_CALLHOOK(PlayerCanCrouch, this, do_crouch);
151     do_crouch = M_ARGV(1, bool);
152
153         if (do_crouch) {
154                 if (!IS_DUCKED(this)) {
155                         SET_DUCKED(this);
156                         this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
157                         setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
158                         // setanim(this, this.anim_duck, false, true, true); // this anim is BROKEN anyway
159                 }
160         } else if (IS_DUCKED(this)) {
161         tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, false, this);
162         if (!trace_startsolid) {
163             UNSET_DUCKED(this);
164             this.view_ofs = STAT(PL_VIEW_OFS, this);
165             setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
166         }
167         }
168
169         _Movetype_CheckWater(this); // needs to be run on the client, might as well use the latest on the server too!
170 }
171
172 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
173 {
174         float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
175         if(PHYS_AIRCONTROL_BACKWARDS(this))
176                 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
177         if(PHYS_AIRCONTROL_SIDEWARDS(this))
178         {
179                 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
180                 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
181         }
182
183         float k = 32 * (2 * movity - 1);
184         if (k <= 0)
185                 return;
186
187         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
188
189         float zspeed = this.velocity_z;
190         this.velocity_z = 0;
191         float xyspeed = vlen(this.velocity);
192         this.velocity = normalize(this.velocity);
193
194         float dot = this.velocity * wishdir;
195
196         if (dot > 0) // we can't change direction while slowing down
197         {
198                 k *= (dot ** PHYS_AIRCONTROL_POWER(this)) * dt;
199                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
200                 k *= PHYS_AIRCONTROL(this);
201                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
202         }
203
204         this.velocity = this.velocity * xyspeed;
205         this.velocity_z = zspeed;
206 }
207
208 float AdjustAirAccelQW(float accelqw, float factor)
209 {
210         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
211 }
212
213 // example config for alternate speed clamping:
214 //   sv_airaccel_qw 0.8
215 //   sv_airaccel_sideways_friction 0
216 //   prvm_globalset server speedclamp_mode 1
217 //     (or 2)
218 void PM_Accelerate(entity this, float dt, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
219 {
220         float speedclamp = stretchfactor > 0 ? stretchfactor
221         : accelqw < 0 ? 1 // full clamping, no stretch
222         : -1; // no clamping
223
224         accelqw = fabs(accelqw);
225
226         if (GAMEPLAYFIX_Q2AIRACCELERATE)
227                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
228
229         float vel_straight = this.velocity * wishdir;
230         float vel_z = this.velocity_z;
231         vector vel_xy = vec2(this.velocity);
232         vector vel_perpend = vel_xy - vel_straight * wishdir;
233
234         float step = accel * dt * wishspeed0;
235
236         float vel_xy_current  = vlen(vel_xy);
237         if (speedlimit)
238                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
239         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
240         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
241         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
242         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
243
244         if (sidefric < 0 && (vel_perpend*vel_perpend))
245                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
246         {
247                 float f = max(0, 1 + dt * wishspeed * sidefric);
248                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
249                 // assume: themin > 1
250                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
251                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
252                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
253                 // obviously, this cannot be
254                 if (themin <= 0)
255                         vel_perpend *= f;
256                 else
257                 {
258                         themin = sqrt(themin);
259                         vel_perpend *= max(themin, f);
260                 }
261         }
262         else
263                 vel_perpend *= max(0, 1 - dt * wishspeed * sidefric);
264
265         vel_xy = vel_straight * wishdir + vel_perpend;
266
267         if (speedclamp >= 0)
268         {
269                 float vel_xy_preclamp;
270                 vel_xy_preclamp = vlen(vel_xy);
271                 if (vel_xy_preclamp > 0) // prevent division by zero
272                 {
273                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
274                         if (vel_xy_current < vel_xy_preclamp)
275                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
276                 }
277         }
278
279         this.velocity = vel_xy + vel_z * '0 0 1';
280 }
281
282 void PM_AirAccelerate(entity this, float dt, vector wishdir, float wishspeed)
283 {
284         if (wishspeed == 0)
285                 return;
286
287         vector curvel = this.velocity;
288         curvel_z = 0;
289         float curspeed = vlen(curvel);
290
291         if (wishspeed > curspeed * 1.01)
292                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * dt);
293         else
294         {
295                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
296                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * dt;
297         }
298         vector wishvel = wishdir * wishspeed;
299         vector acceldir = wishvel - curvel;
300         float addspeed = vlen(acceldir);
301         acceldir = normalize(acceldir);
302
303         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * dt);
304
305         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
306         {
307                 vector curdir = normalize(curvel);
308                 float dot = acceldir * curdir;
309                 if (dot < 0)
310                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
311         }
312
313         this.velocity += accelspeed * acceldir;
314 }
315
316
317 /*
318 =============
319 PlayerJump
320
321 When you press the jump key
322 returns true if handled
323 =============
324 */
325 bool PlayerJump(entity this)
326 {
327         if (PHYS_FROZEN(this))
328                 return true; // no jumping in freezetag when frozen
329
330         if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
331                 return true; // no jumping while typing
332
333 #ifdef SVQC
334         if (this.player_blocked)
335                 return true; // no jumping while blocked
336 #endif
337
338         bool doublejump = false;
339         float mjumpheight = ((PHYS_JUMPVELOCITY_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
340         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
341
342         if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
343                 return true;
344
345         mjumpheight = M_ARGV(1, float);
346         doublejump = M_ARGV(2, bool);
347
348         if (this.waterlevel >= WATERLEVEL_SWIMMING)
349         {
350                 if(this.viewloc)
351                 {
352                         doublejump = true;
353                         mjumpheight *= 0.7;
354                         track_jump = true;
355                 }
356                 else
357                 {
358                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
359                         return true;
360                 }
361         }
362
363         if (!doublejump)
364                 if (!IS_ONGROUND(this))
365                         return IS_JUMP_HELD(this);
366
367         if(PHYS_TRACK_CANJUMP(this))
368                 track_jump = true;
369
370         if (track_jump)
371                 if (IS_JUMP_HELD(this))
372                         return true;
373
374         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
375         // velocity bounds.  Final velocity is bound between (jumpheight *
376         // min + jumpheight) and (jumpheight * max + jumpheight);
377
378         if(PHYS_JUMPSPEEDCAP_MIN != "")
379         {
380                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
381
382                 if (this.velocity_z < minjumpspeed)
383                         mjumpheight += minjumpspeed - this.velocity_z;
384         }
385
386         if(PHYS_JUMPSPEEDCAP_MAX != "")
387         {
388                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
389                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
390
391                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
392                 {
393                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
394
395                         if (this.velocity_z > maxjumpspeed)
396                                 mjumpheight -= this.velocity_z - maxjumpspeed;
397                 }
398         }
399
400         if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
401         {
402 #ifdef SVQC
403                 if(autocvar_speedmeter)
404                         LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
405 #endif
406                 if(this.lastground < time - 0.3)
407                 {
408                         float f = (1 - PHYS_FRICTION_ONLAND(this));
409                         this.velocity_x *= f;
410                         this.velocity_y *= f;
411                 }
412 #ifdef SVQC
413                 if(this.jumppadcount > 1)
414                         LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
415                 this.jumppadcount = 0;
416 #endif
417         }
418
419         this.velocity_z += mjumpheight;
420
421         UNSET_ONGROUND(this);
422         UNSET_ONSLICK(this);
423         SET_JUMP_HELD(this);
424
425 #ifdef SVQC
426
427         this.oldvelocity_z = this.velocity_z;
428
429         animdecide_setaction(this, ANIMACTION_JUMP, true);
430
431         if (autocvar_g_jump_grunt)
432                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
433 #endif
434         return true;
435 }
436
437 void CheckWaterJump(entity this)
438 {
439 // check for a jump-out-of-water
440         makevectors(this.v_angle);
441         vector start = this.origin;
442         start_z += 8;
443         v_forward_z = 0;
444         normalize(v_forward);
445         vector end = start + v_forward*24;
446         traceline (start, end, true, this);
447         if (trace_fraction < 1)
448         {       // solid at waist
449                 start_z = start_z + this.maxs_z - 8;
450                 end = start + v_forward*24;
451                 this.movedir = trace_plane_normal * -50;
452                 traceline(start, end, true, this);
453                 if (trace_fraction == 1)
454                 {       // open at eye level
455                         this.velocity_z = 225;
456                         this.flags |= FL_WATERJUMP;
457                         SET_JUMP_HELD(this);
458                 }
459         }
460 }
461
462
463 #ifdef SVQC
464         #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
465 #elif defined(CSQC)
466         float autocvar_cl_jetpack_jump;
467         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
468 #endif
469 .float jetpack_stopped;
470 void CheckPlayerJump(entity this)
471 {
472 #ifdef SVQC
473         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
474 #endif
475         if (JETPACK_JUMP(this) < 2)
476                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
477
478         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
479         {
480                 bool playerjump = PlayerJump(this); // required
481
482                 bool air_jump = !playerjump || M_ARGV(2, bool);
483                 bool activate = (JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this)) || PHYS_INPUT_BUTTON_JETPACK(this);
484                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_AMMO);
485
486                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
487                 else if (this.jetpack_stopped) { }
488                 else if (!has_fuel)
489                 {
490 #ifdef SVQC
491                         if (was_flying) // TODO: ran out of fuel message
492                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
493                         else if (activate)
494                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
495 #endif
496                         this.jetpack_stopped = true;
497                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
498                 }
499                 else if (activate && !PHYS_FROZEN(this))
500                         ITEMS_STAT(this) |= IT_USING_JETPACK;
501         }
502         else
503         {
504                 this.jetpack_stopped = false;
505                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
506         }
507         if (!PHYS_INPUT_BUTTON_JUMP(this))
508                 UNSET_JUMP_HELD(this);
509
510         if (this.waterlevel == WATERLEVEL_SWIMMING)
511                 CheckWaterJump(this);
512 }
513
514 #ifdef SVQC
515 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
516 .float specialcommand_pos;
517 void SpecialCommand(entity this)
518 {
519         if(autocvar_sv_cheats || this.maycheat)
520         {
521                 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
522                         LOG_INFO("A hollow voice says \"Plugh\".");
523         }
524         else
525                 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
526 }
527 #endif
528
529 bool PM_check_specialcommand(entity this, int buttons)
530 {
531 #ifdef SVQC
532         string c;
533         if (!buttons)
534                 c = "x";
535         else if (buttons == 1)
536                 c = "1";
537         else if (buttons == 2)
538                 c = " ";
539         else if (buttons == 128)
540                 c = "s";
541         else if (buttons == 256)
542                 c = "w";
543         else if (buttons == 512)
544                 c = "a";
545         else if (buttons == 1024)
546                 c = "d";
547         else
548                 c = "?";
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 (!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 }