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