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