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