]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Resolve conflicts 2: Merge branch 'master' into bones_was_here/q3compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics / player.qc
1 #include "player.qh"
2
3 #include <common/mapobjects/_mod.qh>
4 #include <common/viewloc.qh>
5
6 #ifdef SVQC
7 #include <common/mapobjects/defs.qh>
8 #include <common/mapobjects/trigger/viewloc.qh>
9 #include <server/client.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_CVAR(this).cvar_cl_physics))
23         {
24                 string s = strcat("g_physics_", CS_CVAR(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;
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         animdecide_setaction(this, ANIMACTION_JUMP, true);
426
427         if (autocvar_g_jump_grunt)
428                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
429 #endif
430         return true;
431 }
432
433 void CheckWaterJump(entity this)
434 {
435 // check for a jump-out-of-water
436         makevectors(this.v_angle);
437         vector start = this.origin;
438         start_z += 8;
439         v_forward_z = 0;
440         normalize(v_forward);
441         vector end = start + v_forward*24;
442         traceline (start, end, true, this);
443         if (trace_fraction < 1)
444         {       // solid at waist
445                 start_z = start_z + this.maxs_z - 8;
446                 end = start + v_forward*24;
447                 this.movedir = trace_plane_normal * -50;
448                 traceline(start, end, true, this);
449                 if (trace_fraction == 1)
450                 {       // open at eye level
451                         this.velocity_z = 225;
452                         this.flags |= FL_WATERJUMP;
453                         this.teleport_time = time + 2; // safety net
454                         SET_JUMP_HELD(this);
455                 }
456         }
457 }
458
459
460 #ifdef SVQC
461         #define JETPACK_JUMP(s) CS_CVAR(s).cvar_cl_jetpack_jump
462 #elif defined(CSQC)
463         float autocvar_cl_jetpack_jump;
464         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
465 #endif
466 .float jetpack_stopped;
467 void CheckPlayerJump(entity this)
468 {
469 #ifdef SVQC
470         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
471 #endif
472         if (JETPACK_JUMP(this) < 2)
473                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
474
475         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
476         {
477                 bool playerjump = PlayerJump(this); // required
478
479                 bool air_jump = !playerjump || M_ARGV(2, bool);
480                 bool activate = (JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this)) || PHYS_INPUT_BUTTON_JETPACK(this);
481                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_AMMO);
482
483                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
484                 else if (this.jetpack_stopped) { }
485                 else if (!has_fuel)
486                 {
487 #ifdef SVQC
488                         if (was_flying) // TODO: ran out of fuel message
489                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
490                         else if (activate)
491                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
492 #endif
493                         this.jetpack_stopped = true;
494                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
495                 }
496                 else if (activate && !PHYS_FROZEN(this))
497                         ITEMS_STAT(this) |= IT_USING_JETPACK;
498         }
499         else
500         {
501                 this.jetpack_stopped = false;
502                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
503         }
504         if (!PHYS_INPUT_BUTTON_JUMP(this))
505                 UNSET_JUMP_HELD(this);
506
507         if (this.waterlevel == WATERLEVEL_SWIMMING)
508                 CheckWaterJump(this);
509 }
510
511 #ifdef SVQC
512 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
513 .float specialcommand_pos;
514 void SpecialCommand(entity this)
515 {
516         if(autocvar_sv_cheats || this.maycheat)
517         {
518                 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
519                         LOG_INFO("A hollow voice says \"Plugh\".");
520         }
521 }
522 #endif
523
524 bool PM_check_specialcommand(entity this, int buttons)
525 {
526 #ifdef SVQC
527         string c;
528         switch (buttons)
529         {
530                 // buttons mapped in PHYS_INPUT_BUTTON_MASK
531                 case 0: c = "x"; break;
532                 case BIT(0): c = "1"; break;
533                 case BIT(2): c = " "; break;
534                 case BIT(7): c = "s"; break;
535                 case BIT(8): c = "w"; break;
536                 case BIT(9): c = "a"; break;
537                 case BIT(10): c = "d"; break;
538                 default: c = "?";
539         }
540
541         if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
542         {
543                 CS(this).specialcommand_pos += 1;
544                 if (CS(this).specialcommand_pos >= strlen(specialcommand))
545                 {
546                         CS(this).specialcommand_pos = 0;
547                         SpecialCommand(this);
548                         return true;
549                 }
550         }
551         else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
552                 CS(this).specialcommand_pos = 0;
553 #endif
554         return false;
555 }
556
557 void PM_check_nickspam(entity this)
558 {
559 #ifdef SVQC
560         if (time >= this.nickspamtime)
561                 return;
562         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
563         {
564                 // slight annoyance for nick change scripts
565                 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
566                 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;
567
568                 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!
569                 {
570                         this.v_angle_x = random() * 360;
571                         this.v_angle_y = random() * 360;
572                         // at least I'm not forcing retardedview by also assigning to angles_z
573                         this.fixangle = true;
574                 }
575         }
576 #endif
577 }
578
579 void PM_check_punch(entity this, float dt)
580 {
581 #ifdef SVQC
582         if (this.punchangle != '0 0 0')
583         {
584                 float f = vlen(this.punchangle) - 10 * dt;
585                 if (f > 0)
586                         this.punchangle = normalize(this.punchangle) * f;
587                 else
588                         this.punchangle = '0 0 0';
589         }
590
591         if (this.punchvector != '0 0 0')
592         {
593                 float f = vlen(this.punchvector) - 30 * dt;
594                 if (f > 0)
595                         this.punchvector = normalize(this.punchvector) * f;
596                 else
597                         this.punchvector = '0 0 0';
598         }
599 #endif
600 }
601
602 // predict frozen movement, as frozen players CAN move in some cases
603 void PM_check_frozen(entity this)
604 {
605         if (!PHYS_FROZEN(this))
606                 return;
607         if (PHYS_DODGING_FROZEN(this) && IS_CLIENT(this))
608         {
609                 // bind movement to a very slow speed so dodging can use .movement for directional calculations
610                 PHYS_CS(this).movement_x = bound(-2, PHYS_CS(this).movement.x, 2);
611                 PHYS_CS(this).movement_y = bound(-2, PHYS_CS(this).movement.y, 2);
612                 PHYS_CS(this).movement_z = bound(-2, PHYS_CS(this).movement.z, 2);
613         }
614         else
615                 PHYS_CS(this).movement = '0 0 0';
616 }
617
618 void PM_check_hitground(entity this)
619 {
620 #ifdef SVQC
621         if (!this.wasFlying) return;
622     this.wasFlying = false;
623     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
624     if (this.ladder_entity) return;
625     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
626     {
627         .entity weaponentity = weaponentities[slot];
628         if(this.(weaponentity).hook)
629                 return;
630     }
631     this.nextstep = time + 0.3 + random() * 0.1;
632     trace_dphitq3surfaceflags = 0;
633     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
634     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
635     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
636         ? GS_FALL_METAL
637         : GS_FALL;
638     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
639     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
640 #endif
641 }
642
643 void PM_Footsteps(entity this)
644 {
645 #ifdef SVQC
646         if (!autocvar_g_footsteps) return;
647         if (IS_DUCKED(this)) return;
648         if (time >= this.lastground + 0.2) return;
649         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
650         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
651         {
652                 this.nextstep = time + 0.3 + random() * 0.1;
653                 trace_dphitq3surfaceflags = 0;
654                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
655                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
656                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
657                         ? GS_STEP_METAL
658                         : GS_STEP;
659                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
660         }
661 #endif
662 }
663
664 void PM_check_slick(entity this)
665 {
666         if(!IS_ONGROUND(this))
667                 return;
668
669         trace_dphitq3surfaceflags = 0;
670         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
671         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
672                 SET_ONSLICK(this);
673         else
674                 UNSET_ONSLICK(this);
675 }
676
677 void PM_check_blocked(entity this)
678 {
679         if(PHYS_INPUT_BUTTON_CHAT(this) || PHYS_INPUT_BUTTON_MINIGAME(this))
680                 PHYS_CS(this).movement = '0 0 0';
681 #ifdef SVQC
682         if (!this.player_blocked)
683                 return;
684         PHYS_CS(this).movement = '0 0 0';
685         this.disableclientprediction = 1;
686 #endif
687 }
688
689 void PM_jetpack(entity this, float maxspd_mod, float dt)
690 {
691         //makevectors(this.v_angle.y * '0 1 0');
692         makevectors(this.v_angle);
693         vector wishvel = v_forward * PHYS_CS(this).movement_x
694                                         + v_right * PHYS_CS(this).movement_y;
695         // add remaining speed as Z component
696         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
697         // fix speedhacks :P
698         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
699         // add the unused velocity as up component
700         wishvel_z = 0;
701
702         // if (PHYS_INPUT_BUTTON_JUMP(this))
703                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
704
705         // it is now normalized, so...
706         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
707         float a_up = PHYS_JETPACK_ACCEL_UP(this);
708         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
709
710         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
711
712         wishvel_x *= a_side;
713         wishvel_y *= a_side;
714         wishvel_z *= a_up;
715         wishvel_z += a_add;
716
717         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
718
719         float best = 0;
720         //////////////////////////////////////////////////////////////////////////////////////
721         // finding the maximum over all vectors of above form
722         // with wishvel having an absolute value of 1
723         //////////////////////////////////////////////////////////////////////////////////////
724         // we're finding the maximum over
725         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
726         // for z in the range from -1 to 1
727         //////////////////////////////////////////////////////////////////////////////////////
728         // maximum is EITHER attained at the single extreme point:
729         float a_diff = a_side * a_side - a_up * a_up;
730         float f;
731         if (a_diff != 0)
732         {
733                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
734                 if (f > -1 && f < 1) // can it be attained?
735                 {
736                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
737                         //print("middle\n");
738                 }
739         }
740         // OR attained at z = 1:
741         f = (a_up + a_add) * (a_up + a_add);
742         if (f > best)
743         {
744                 best = f;
745                 //print("top\n");
746         }
747         // OR attained at z = -1:
748         f = (a_up - a_add) * (a_up - a_add);
749         if (f > best)
750         {
751                 best = f;
752                 //print("bottom\n");
753         }
754         best = sqrt(best);
755         //////////////////////////////////////////////////////////////////////////////////////
756
757         //print("best possible acceleration: ", ftos(best), "\n");
758
759         float fxy, fz;
760         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
761         if (wishvel_z - PHYS_GRAVITY(this) > 0)
762                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
763         else
764                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
765
766         float fvel;
767         fvel = vlen(wishvel);
768         wishvel_x *= fxy;
769         wishvel_y *= fxy;
770         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
771
772         fvel = min(1, vlen(wishvel) / best);
773         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
774                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
775         else
776                 f = 1;
777
778         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
779
780         if (f > 0 && wishvel != '0 0 0')
781         {
782                 this.velocity = this.velocity + wishvel * f * dt;
783                 UNSET_ONGROUND(this);
784
785 #ifdef SVQC
786                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_AMMO))
787                         TakeResource(this, RES_FUEL, PHYS_JETPACK_FUEL(this) * dt * fvel * f);
788
789                 ITEMS_STAT(this) |= IT_USING_JETPACK;
790
791                 // jetpack also inhibits health regeneration, but only for 1 second
792                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
793 #endif
794         }
795 }
796
797 // used for calculating airshots
798 bool IsFlying(entity this)
799 {
800         if(IS_ONGROUND(this))
801                 return false;
802         if(this.waterlevel >= WATERLEVEL_SWIMMING)
803                 return false;
804         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 24', MOVE_NORMAL, this);
805         //traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
806         if(trace_fraction < 1)
807                 return false;
808         return true;
809 }
810
811
812 void sys_phys_update(entity this, float dt);
813 #if defined(SVQC)
814 void SV_PlayerPhysics(entity this)
815 #elif defined(CSQC)
816 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
817 #endif
818 {
819 #ifdef SVQC
820         // needs to be called before physics are run!
821         if(IS_REAL_CLIENT(this))
822                 PM_UpdateButtons(this, CS(this));
823 #endif
824
825         sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
826
827 #ifdef SVQC
828         CS(this).pm_frametime = frametime;
829 #elif defined(CSQC)
830         if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
831                 this.csqcmodel_modelflags |= MF_ROCKET;
832         else
833                 this.csqcmodel_modelflags &= ~MF_ROCKET;
834 #endif
835 }