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