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