]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Attempt to optimise physics valid checking by using a custom word checker
[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 bool Physics_HasWord(string thelist, string theword)
11 {
12         FOREACH_WORD(thelist, it == theword,
13         {
14                 return true;
15         });
16
17         return false;
18 }
19
20 // client side physics
21 bool Physics_Valid(string thecvar)
22 {
23         return autocvar_g_physics_clientselect && thecvar != "" && thecvar && thecvar != "default" && Physics_HasWord(autocvar_g_physics_clientselect_options, thecvar);
24 }
25
26 float Physics_ClientOption(entity this, string option, float defaultval)
27 {
28         if(IS_REAL_CLIENT(this) && Physics_Valid(CS(this).cvar_cl_physics))
29         {
30                 string s = strcat("g_physics_", CS(this).cvar_cl_physics, "_", option);
31                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
32                         return cvar(s);
33         }
34         if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
35         {
36                 string s = strcat("g_physics_", autocvar_g_physics_clientselect_default, "_", option);
37                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
38                         return cvar(s);
39         }
40         return defaultval;
41 }
42
43 void Physics_UpdateStats(entity this)
44 {
45         // 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?)
46         STAT(MOVEVARS_HIGHSPEED, this) = autocvar_g_movement_highspeed;
47
48         MUTATOR_CALLHOOK(PlayerPhysics_UpdateStats, this);
49         float maxspd_mod = PHYS_HIGHSPEED(this);
50
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         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
57
58         STAT(PL_MIN, this) = autocvar_sv_player_mins;
59         STAT(PL_MAX, this) = autocvar_sv_player_maxs;
60         STAT(PL_VIEW_OFS, this) = autocvar_sv_player_viewoffset;
61         STAT(PL_CROUCH_MIN, this) = autocvar_sv_player_crouch_mins;
62         STAT(PL_CROUCH_MAX, this) = autocvar_sv_player_crouch_maxs;
63         STAT(PL_CROUCH_VIEW_OFS, this) = 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         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
70         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
71         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
72         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
73         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
74         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
75         STAT(MOVEVARS_AIRCONTROL_BACKWARDS, this) = Physics_ClientOption(this, "aircontrol_backwards", autocvar_sv_aircontrol_backwards);
76         STAT(MOVEVARS_AIRCONTROL_SIDEWARDS, this) = Physics_ClientOption(this, "aircontrol_sidewards", autocvar_sv_aircontrol_sidewards);
77         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
78         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
79         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
80         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
81         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
82         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
83         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
84         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
85         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
86         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
87         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
88         STAT(MOVEVARS_JUMPVELOCITY_CROUCH, this) = Physics_ClientOption(this, "jumpvelocity_crouch", autocvar_sv_jumpvelocity_crouch);
89         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
90 }
91 #endif
92
93 float IsMoveInDirection(vector mv, float ang) // key mix factor
94 {
95         if (mv_x == 0 && mv_y == 0)
96                 return 0; // avoid division by zero
97         ang -= RAD2DEG * atan2(mv_y, mv_x);
98         ang = remainder(ang, 360) / 45;
99         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
100 }
101
102 float GeomLerp(float a, float _lerp, float b)
103 {
104         return a == 0 ? (_lerp < 1 ? 0 : b)
105                 : b == 0 ? (_lerp > 0 ? 0 : a)
106                 : a * (fabs(b / a) ** _lerp);
107 }
108
109 void PM_ClientMovement_UpdateStatus(entity this)
110 {
111 #ifdef CSQC
112         if(!IS_PLAYER(this))
113                 return;
114
115         // set crouched
116         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
117         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
118         {
119                 entity wep = viewmodels[slot];
120                 if(wep.hook && !wasfreed(wep.hook))
121                 {
122                         do_crouch = false;
123                         break; // don't bother checking the others
124                 }
125         }
126         if(this.waterlevel >= WATERLEVEL_SWIMMING)
127                 do_crouch = false;
128         if(hud != HUD_NORMAL)
129                 do_crouch = false;
130         if(STAT(FROZEN, this))
131                 do_crouch = false;
132
133         if (do_crouch)
134         {
135                 // wants to crouch, this always works
136                 if (!IS_DUCKED(this)) SET_DUCKED(this);
137         }
138         else
139         {
140                 // wants to stand, if currently crouching we need to check for a low ceiling first
141                 if (IS_DUCKED(this))
142                 {
143                         tracebox(this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), this.origin, MOVE_NORMAL, this);
144                         if (!trace_startsolid) UNSET_DUCKED(this);
145                 }
146         }
147
148         if (IS_ONGROUND(this) || this.velocity.z <= 0 || PHYS_WATERJUMP_TIME(this) <= 0)
149                 PHYS_WATERJUMP_TIME(this) = 0;
150 #endif
151 }
152
153 void CPM_PM_Aircontrol(entity this, float dt, vector wishdir, float wishspeed)
154 {
155         float movity = IsMoveInDirection(PHYS_CS(this).movement, 0);
156         if(PHYS_AIRCONTROL_BACKWARDS(this))
157                 movity += IsMoveInDirection(PHYS_CS(this).movement, 180);
158         if(PHYS_AIRCONTROL_SIDEWARDS(this))
159         {
160                 movity += IsMoveInDirection(PHYS_CS(this).movement, 90);
161                 movity += IsMoveInDirection(PHYS_CS(this).movement, -90);
162         }
163
164         float k = 32 * (2 * movity - 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 *= (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_CROUCH(this) && IS_DUCKED(this)) ? PHYS_JUMPVELOCITY_CROUCH(this) : PHYS_JUMPVELOCITY(this));
318         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
319
320         if (MUTATOR_CALLHOOK(PlayerJump, this, mjumpheight, doublejump))
321                 return true;
322
323         mjumpheight = M_ARGV(1, float);
324         doublejump = M_ARGV(2, bool);
325
326         if (this.waterlevel >= WATERLEVEL_SWIMMING)
327         {
328                 if(this.viewloc)
329                 {
330                         doublejump = true;
331                         mjumpheight *= 0.7;
332                         track_jump = true;
333                 }
334                 else
335                 {
336                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
337                         return true;
338                 }
339         }
340
341         if (!doublejump)
342                 if (!IS_ONGROUND(this) && !IS_ONSLICK(this))
343                         return IS_JUMP_HELD(this);
344
345         if(PHYS_TRACK_CANJUMP(this))
346                 track_jump = true;
347
348         if (track_jump)
349                 if (IS_JUMP_HELD(this))
350                         return true;
351
352         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
353         // velocity bounds.  Final velocity is bound between (jumpheight *
354         // min + jumpheight) and (jumpheight * max + jumpheight);
355
356         if(PHYS_JUMPSPEEDCAP_MIN != "")
357         {
358                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
359
360                 if (this.velocity_z < minjumpspeed)
361                         mjumpheight += minjumpspeed - this.velocity_z;
362         }
363
364         if(PHYS_JUMPSPEEDCAP_MAX != "")
365         {
366                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
367                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
368
369                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(this)))
370                 {
371                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
372
373                         if (this.velocity_z > maxjumpspeed)
374                                 mjumpheight -= this.velocity_z - maxjumpspeed;
375                 }
376         }
377
378         if (!WAS_ONGROUND(this) && !WAS_ONSLICK(this))
379         {
380 #ifdef SVQC
381                 if(autocvar_speedmeter)
382                         LOG_TRACE("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")");
383 #endif
384                 if(this.lastground < time - 0.3)
385                 {
386                         float f = (1 - PHYS_FRICTION_ONLAND(this));
387                         this.velocity_x *= f;
388                         this.velocity_y *= f;
389                 }
390 #ifdef SVQC
391                 if(this.jumppadcount > 1)
392                         LOG_TRACE(ftos(this.jumppadcount), "x jumppad combo");
393                 this.jumppadcount = 0;
394 #endif
395         }
396
397         this.velocity_z += mjumpheight;
398
399         UNSET_ONGROUND(this);
400         UNSET_ONSLICK(this);
401         SET_JUMP_HELD(this);
402
403 #ifdef SVQC
404
405         this.oldvelocity_z = this.velocity_z;
406
407         animdecide_setaction(this, ANIMACTION_JUMP, true);
408
409         if (autocvar_g_jump_grunt)
410                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
411 #endif
412         return true;
413 }
414
415 void CheckWaterJump(entity this)
416 {
417 // check for a jump-out-of-water
418         makevectors(this.v_angle);
419         vector start = this.origin;
420         start_z += 8;
421         v_forward_z = 0;
422         normalize(v_forward);
423         vector end = start + v_forward*24;
424         traceline (start, end, true, this);
425         if (trace_fraction < 1)
426         {       // solid at waist
427                 start_z = start_z + this.maxs_z - 8;
428                 end = start + v_forward*24;
429                 this.movedir = trace_plane_normal * -50;
430                 traceline(start, end, true, this);
431                 if (trace_fraction == 1)
432                 {       // open at eye level
433                         this.velocity_z = 225;
434                         this.flags |= FL_WATERJUMP;
435                         SET_JUMP_HELD(this);
436                 #ifdef SVQC
437                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
438                 #elif defined(CSQC)
439                         PHYS_WATERJUMP_TIME(this) = 2;
440                 #endif
441                 }
442         }
443 }
444
445
446 #ifdef SVQC
447         #define JETPACK_JUMP(s) CS(s).cvar_cl_jetpack_jump
448 #elif defined(CSQC)
449         float autocvar_cl_jetpack_jump;
450         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
451 #endif
452 .float jetpack_stopped;
453 void CheckPlayerJump(entity this)
454 {
455 #ifdef SVQC
456         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
457 #endif
458         if (JETPACK_JUMP(this) < 2)
459                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
460
461         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
462         {
463                 bool playerjump = PlayerJump(this); // required
464
465                 bool air_jump = !playerjump || M_ARGV(2, bool);
466                 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
467                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
468
469                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
470                 else if (this.jetpack_stopped) { }
471                 else if (!has_fuel)
472                 {
473 #ifdef SVQC
474                         if (was_flying) // TODO: ran out of fuel message
475                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
476                         else if (activate)
477                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
478 #endif
479                         this.jetpack_stopped = true;
480                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
481                 }
482                 else if (activate && !PHYS_FROZEN(this))
483                         ITEMS_STAT(this) |= IT_USING_JETPACK;
484         }
485         else
486         {
487                 this.jetpack_stopped = false;
488                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
489         }
490         if (!PHYS_INPUT_BUTTON_JUMP(this))
491                 UNSET_JUMP_HELD(this);
492
493         if (this.waterlevel == WATERLEVEL_SWIMMING)
494                 CheckWaterJump(this);
495 }
496
497 #ifdef SVQC
498 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
499 .float specialcommand_pos;
500 void SpecialCommand(entity this)
501 {
502         if(autocvar_sv_cheats || this.maycheat)
503         {
504                 if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
505                         LOG_INFO("A hollow voice says \"Plugh\".");
506         }
507         else
508                 STAT(MOVEVARS_SPECIALCOMMAND, this) = true;
509 }
510 #endif
511
512 bool PM_check_specialcommand(entity this, int buttons)
513 {
514 #ifdef SVQC
515         string c;
516         if (!buttons)
517                 c = "x";
518         else if (buttons == 1)
519                 c = "1";
520         else if (buttons == 2)
521                 c = " ";
522         else if (buttons == 128)
523                 c = "s";
524         else if (buttons == 256)
525                 c = "w";
526         else if (buttons == 512)
527                 c = "a";
528         else if (buttons == 1024)
529                 c = "d";
530         else
531                 c = "?";
532
533         if (c == substring(specialcommand, CS(this).specialcommand_pos, 1))
534         {
535                 CS(this).specialcommand_pos += 1;
536                 if (CS(this).specialcommand_pos >= strlen(specialcommand))
537                 {
538                         CS(this).specialcommand_pos = 0;
539                         SpecialCommand(this);
540                         return true;
541                 }
542         }
543         else if (CS(this).specialcommand_pos && (c != substring(specialcommand, CS(this).specialcommand_pos - 1, 1)))
544                 CS(this).specialcommand_pos = 0;
545 #endif
546         return false;
547 }
548
549 void PM_check_nickspam(entity this)
550 {
551 #ifdef SVQC
552         if (time >= this.nickspamtime)
553                 return;
554         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
555         {
556                 // slight annoyance for nick change scripts
557                 PHYS_CS(this).movement = -1 * PHYS_CS(this).movement;
558                 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;
559
560                 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!
561                 {
562                         this.v_angle_x = random() * 360;
563                         this.v_angle_y = random() * 360;
564                         // at least I'm not forcing retardedview by also assigning to angles_z
565                         this.fixangle = true;
566                 }
567         }
568 #endif
569 }
570
571 void PM_check_punch(entity this, float dt)
572 {
573 #ifdef SVQC
574         if (this.punchangle != '0 0 0')
575         {
576                 float f = vlen(this.punchangle) - 10 * dt;
577                 if (f > 0)
578                         this.punchangle = normalize(this.punchangle) * f;
579                 else
580                         this.punchangle = '0 0 0';
581         }
582
583         if (this.punchvector != '0 0 0')
584         {
585                 float f = vlen(this.punchvector) - 30 * dt;
586                 if (f > 0)
587                         this.punchvector = normalize(this.punchvector) * f;
588                 else
589                         this.punchvector = '0 0 0';
590         }
591 #endif
592 }
593
594 // predict frozen movement, as frozen players CAN move in some cases
595 void PM_check_frozen(entity this)
596 {
597         if (!PHYS_FROZEN(this))
598                 return;
599         if (PHYS_DODGING_FROZEN(this)
600 #ifdef SVQC
601         && IS_REAL_CLIENT(this)
602 #endif
603         )
604         {
605                 PHYS_CS(this).movement_x = bound(-5, PHYS_CS(this).movement.x, 5);
606                 PHYS_CS(this).movement_y = bound(-5, PHYS_CS(this).movement.y, 5);
607                 PHYS_CS(this).movement_z = bound(-5, PHYS_CS(this).movement.z, 5);
608         }
609         else
610                 PHYS_CS(this).movement = '0 0 0';
611
612         vector midpoint = ((this.absmin + this.absmax) * 0.5);
613         if (pointcontents(midpoint) == CONTENT_WATER)
614         {
615                 this.velocity = this.velocity * 0.5;
616
617                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
618                         this.velocity_z = 200;
619         }
620 }
621
622 void PM_check_hitground(entity this)
623 {
624 #ifdef SVQC
625         if (!this.wasFlying) return;
626     this.wasFlying = false;
627     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
628     if (time < this.ladder_time) return;
629     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
630     {
631         .entity weaponentity = weaponentities[slot];
632         if(this.(weaponentity).hook)
633                 return;
634     }
635     this.nextstep = time + 0.3 + random() * 0.1;
636     trace_dphitq3surfaceflags = 0;
637     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
638     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
639     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
640         ? GS_FALL_METAL
641         : GS_FALL;
642     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
643     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
644 #endif
645 }
646
647 void PM_Footsteps(entity this)
648 {
649 #ifdef SVQC
650         if (!g_footsteps) return;
651         if (IS_DUCKED(this)) return;
652         if (time >= this.lastground + 0.2) return;
653         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
654         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
655         {
656                 this.nextstep = time + 0.3 + random() * 0.1;
657                 trace_dphitq3surfaceflags = 0;
658                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
659                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
660                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
661                         ? GS_STEP_METAL
662                         : GS_STEP;
663                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
664         }
665 #endif
666 }
667
668 void PM_check_slick(entity this)
669 {
670         if(!IS_ONGROUND(this))
671                 return;
672
673         if(!PHYS_SLICK_APPLYGRAVITY(this))
674                 return;
675
676         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
677         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
678         {
679                 UNSET_ONGROUND(this);
680                 SET_ONSLICK(this);
681         }
682         else
683                 UNSET_ONSLICK(this);
684 }
685
686 void PM_check_blocked(entity this)
687 {
688 #ifdef SVQC
689         if (!this.player_blocked)
690                 return;
691         PHYS_CS(this).movement = '0 0 0';
692         this.disableclientprediction = 1;
693 #endif
694 }
695
696 void PM_jetpack(entity this, float maxspd_mod, float dt)
697 {
698         //makevectors(this.v_angle.y * '0 1 0');
699         makevectors(this.v_angle);
700         vector wishvel = v_forward * PHYS_CS(this).movement_x
701                                         + v_right * PHYS_CS(this).movement_y;
702         // add remaining speed as Z component
703         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
704         // fix speedhacks :P
705         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
706         // add the unused velocity as up component
707         wishvel_z = 0;
708
709         // if (PHYS_INPUT_BUTTON_JUMP(this))
710                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
711
712         // it is now normalized, so...
713         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
714         float a_up = PHYS_JETPACK_ACCEL_UP(this);
715         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
716
717         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
718
719         wishvel_x *= a_side;
720         wishvel_y *= a_side;
721         wishvel_z *= a_up;
722         wishvel_z += a_add;
723
724         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(this)) { wishvel_z *= -1; }
725
726         float best = 0;
727         //////////////////////////////////////////////////////////////////////////////////////
728         // finding the maximum over all vectors of above form
729         // with wishvel having an absolute value of 1
730         //////////////////////////////////////////////////////////////////////////////////////
731         // we're finding the maximum over
732         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
733         // for z in the range from -1 to 1
734         //////////////////////////////////////////////////////////////////////////////////////
735         // maximum is EITHER attained at the single extreme point:
736         float a_diff = a_side * a_side - a_up * a_up;
737         float f;
738         if (a_diff != 0)
739         {
740                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
741                 if (f > -1 && f < 1) // can it be attained?
742                 {
743                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
744                         //print("middle\n");
745                 }
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("top\n");
753         }
754         // OR attained at z = -1:
755         f = (a_up - a_add) * (a_up - a_add);
756         if (f > best)
757         {
758                 best = f;
759                 //print("bottom\n");
760         }
761         best = sqrt(best);
762         //////////////////////////////////////////////////////////////////////////////////////
763
764         //print("best possible acceleration: ", ftos(best), "\n");
765
766         float fxy, fz;
767         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
768         if (wishvel_z - PHYS_GRAVITY(this) > 0)
769                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
770         else
771                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
772
773         float fvel;
774         fvel = vlen(wishvel);
775         wishvel_x *= fxy;
776         wishvel_y *= fxy;
777         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
778
779         fvel = min(1, vlen(wishvel) / best);
780         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
781                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
782         else
783                 f = 1;
784
785         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
786
787         if (f > 0 && wishvel != '0 0 0')
788         {
789                 this.velocity = this.velocity + wishvel * f * dt;
790                 UNSET_ONGROUND(this);
791
792 #ifdef SVQC
793                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
794                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
795
796                 ITEMS_STAT(this) |= IT_USING_JETPACK;
797
798                 // jetpack also inhibits health regeneration, but only for 1 second
799                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
800 #endif
801         }
802 }
803
804 // used for calculating airshots
805 bool IsFlying(entity this)
806 {
807         if(IS_ONGROUND(this))
808                 return false;
809         if(this.waterlevel >= WATERLEVEL_SWIMMING)
810                 return false;
811         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
812         if(trace_fraction < 1)
813                 return false;
814         return true;
815 }
816
817
818 void sys_phys_update(entity this, float dt);
819 #if defined(SVQC)
820 void SV_PlayerPhysics(entity this)
821 #elif defined(CSQC)
822 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
823 #endif
824 {
825 #ifdef SVQC
826         // needs to be called before physics are run!
827         if(IS_REAL_CLIENT(this))
828                 PM_UpdateButtons(this, CS(this));
829 #endif
830
831         sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
832
833 #ifdef SVQC
834         CS(this).pm_frametime = frametime;
835 #elif defined(CSQC)
836         if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
837                 this.csqcmodel_modelflags |= MF_ROCKET;
838         else
839                 this.csqcmodel_modelflags &= ~MF_ROCKET;
840 #endif
841 }