]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Merge branch 'terencehill/vec2_optimizations' into 'master'
[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(this.movement, 0);
139         if(PHYS_AIRCONTROL_BACKWARDS(this))
140                 movity += IsMoveInDirection(this.movement, 180);
141         if(PHYS_AIRCONTROL_SIDEWARDS(this))
142         {
143                 movity += IsMoveInDirection(this.movement, 90);
144                 movity += IsMoveInDirection(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 float racecar_angle(float forward, float down)
481 {
482         if (forward < 0)
483         {
484                 forward = -forward;
485                 down = -down;
486         }
487
488         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
489
490         float angle_mult = forward / (800 + forward);
491
492         if (ret > 180)
493                 return ret * angle_mult + 360 * (1 - angle_mult);
494         else
495                 return ret * angle_mult;
496 }
497
498 #ifdef SVQC
499 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
500 .float specialcommand_pos;
501 void SpecialCommand(entity this)
502 {
503         if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
504                 LOG_INFO("A hollow voice says \"Plugh\".\n");
505 }
506 #endif
507
508 bool PM_check_specialcommand(entity this, int buttons)
509 {
510 #ifdef SVQC
511         string c;
512         if (!buttons)
513                 c = "x";
514         else if (buttons == 1)
515                 c = "1";
516         else if (buttons == 2)
517                 c = " ";
518         else if (buttons == 128)
519                 c = "s";
520         else if (buttons == 256)
521                 c = "w";
522         else if (buttons == 512)
523                 c = "a";
524         else if (buttons == 1024)
525                 c = "d";
526         else
527                 c = "?";
528
529         if (c == substring(specialcommand, this.specialcommand_pos, 1))
530         {
531                 this.specialcommand_pos += 1;
532                 if (this.specialcommand_pos >= strlen(specialcommand))
533                 {
534                         this.specialcommand_pos = 0;
535                         SpecialCommand(this);
536                         return true;
537                 }
538         }
539         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
540                 this.specialcommand_pos = 0;
541 #endif
542         return false;
543 }
544
545 void PM_check_nickspam(entity this)
546 {
547 #ifdef SVQC
548         if (time >= this.nickspamtime)
549                 return;
550         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
551         {
552                 // slight annoyance for nick change scripts
553                 this.movement = -1 * this.movement;
554                 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;
555
556                 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!
557                 {
558                         this.v_angle_x = random() * 360;
559                         this.v_angle_y = random() * 360;
560                         // at least I'm not forcing retardedview by also assigning to angles_z
561                         this.fixangle = true;
562                 }
563         }
564 #endif
565 }
566
567 void PM_check_punch(entity this, float dt)
568 {
569 #ifdef SVQC
570         if (this.punchangle != '0 0 0')
571         {
572                 float f = vlen(this.punchangle) - 10 * dt;
573                 if (f > 0)
574                         this.punchangle = normalize(this.punchangle) * f;
575                 else
576                         this.punchangle = '0 0 0';
577         }
578
579         if (this.punchvector != '0 0 0')
580         {
581                 float f = vlen(this.punchvector) - 30 * dt;
582                 if (f > 0)
583                         this.punchvector = normalize(this.punchvector) * f;
584                 else
585                         this.punchvector = '0 0 0';
586         }
587 #endif
588 }
589
590 // predict frozen movement, as frozen players CAN move in some cases
591 void PM_check_frozen(entity this)
592 {
593         if (!PHYS_FROZEN(this))
594                 return;
595         if (PHYS_DODGING_FROZEN(this)
596 #ifdef SVQC
597         && IS_REAL_CLIENT(this)
598 #endif
599         )
600         {
601                 this.movement_x = bound(-5, this.movement.x, 5);
602                 this.movement_y = bound(-5, this.movement.y, 5);
603                 this.movement_z = bound(-5, this.movement.z, 5);
604         }
605         else
606                 this.movement = '0 0 0';
607
608         vector midpoint = ((this.absmin + this.absmax) * 0.5);
609         if (pointcontents(midpoint) == CONTENT_WATER)
610         {
611                 this.velocity = this.velocity * 0.5;
612
613                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
614                         this.velocity_z = 200;
615         }
616 }
617
618 void PM_check_hitground(entity this)
619 {
620 #ifdef SVQC
621         if (!this.wasFlying) return;
622     this.wasFlying = false;
623     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
624     if (time < this.ladder_time) return;
625     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
626     {
627         .entity weaponentity = weaponentities[slot];
628         if(this.(weaponentity).hook)
629                 return;
630     }
631     this.nextstep = time + 0.3 + random() * 0.1;
632     trace_dphitq3surfaceflags = 0;
633     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
634     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
635     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
636         ? GS_FALL_METAL
637         : GS_FALL;
638     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
639     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
640 #endif
641 }
642
643 void PM_Footsteps(entity this)
644 {
645 #ifdef SVQC
646         if (!g_footsteps) return;
647         if (IS_DUCKED(this)) return;
648         if (time >= this.lastground + 0.2) return;
649         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
650         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
651         {
652                 this.nextstep = time + 0.3 + random() * 0.1;
653                 trace_dphitq3surfaceflags = 0;
654                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
655                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
656                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
657                         ? GS_STEP_METAL
658                         : GS_STEP;
659                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
660         }
661 #endif
662 }
663
664 void PM_check_slick(entity this)
665 {
666         if(!IS_ONGROUND(this))
667                 return;
668
669         if(!PHYS_SLICK_APPLYGRAVITY(this))
670                 return;
671
672         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
673         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
674         {
675                 UNSET_ONGROUND(this);
676                 SET_ONSLICK(this);
677         }
678         else
679                 UNSET_ONSLICK(this);
680 }
681
682 void PM_check_blocked(entity this)
683 {
684 #ifdef SVQC
685         if (!this.player_blocked)
686                 return;
687         this.movement = '0 0 0';
688         this.disableclientprediction = 1;
689 #endif
690 }
691
692 void PM_jetpack(entity this, float maxspd_mod, float dt)
693 {
694         //makevectors(this.v_angle.y * '0 1 0');
695         makevectors(this.v_angle);
696         vector wishvel = v_forward * this.movement_x
697                                         + v_right * this.movement_y;
698         // add remaining speed as Z component
699         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
700         // fix speedhacks :P
701         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
702         // add the unused velocity as up component
703         wishvel_z = 0;
704
705         // if (PHYS_INPUT_BUTTON_JUMP(this))
706                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
707
708         // it is now normalized, so...
709         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
710         float a_up = PHYS_JETPACK_ACCEL_UP(this);
711         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
712
713         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
714
715         wishvel_x *= a_side;
716         wishvel_y *= a_side;
717         wishvel_z *= a_up;
718         wishvel_z += a_add;
719
720         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
721
722         float best = 0;
723         //////////////////////////////////////////////////////////////////////////////////////
724         // finding the maximum over all vectors of above form
725         // with wishvel having an absolute value of 1
726         //////////////////////////////////////////////////////////////////////////////////////
727         // we're finding the maximum over
728         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
729         // for z in the range from -1 to 1
730         //////////////////////////////////////////////////////////////////////////////////////
731         // maximum is EITHER attained at the single extreme point:
732         float a_diff = a_side * a_side - a_up * a_up;
733         float f;
734         if (a_diff != 0)
735         {
736                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
737                 if (f > -1 && f < 1) // can it be attained?
738                 {
739                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
740                         //print("middle\n");
741                 }
742         }
743         // OR attained at z = 1:
744         f = (a_up + a_add) * (a_up + a_add);
745         if (f > best)
746         {
747                 best = f;
748                 //print("top\n");
749         }
750         // OR attained at z = -1:
751         f = (a_up - a_add) * (a_up - a_add);
752         if (f > best)
753         {
754                 best = f;
755                 //print("bottom\n");
756         }
757         best = sqrt(best);
758         //////////////////////////////////////////////////////////////////////////////////////
759
760         //print("best possible acceleration: ", ftos(best), "\n");
761
762         float fxy, fz;
763         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
764         if (wishvel_z - PHYS_GRAVITY(this) > 0)
765                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
766         else
767                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
768
769         float fvel;
770         fvel = vlen(wishvel);
771         wishvel_x *= fxy;
772         wishvel_y *= fxy;
773         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
774
775         fvel = min(1, vlen(wishvel) / best);
776         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
777                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * dt * fvel));
778         else
779                 f = 1;
780
781         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
782
783         if (f > 0 && wishvel != '0 0 0')
784         {
785                 this.velocity = this.velocity + wishvel * f * dt;
786                 UNSET_ONGROUND(this);
787
788 #ifdef SVQC
789                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
790                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * dt * fvel * f;
791
792                 ITEMS_STAT(this) |= IT_USING_JETPACK;
793
794                 // jetpack also inhibits health regeneration, but only for 1 second
795                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
796 #endif
797         }
798 }
799
800 // used for calculating airshots
801 bool IsFlying(entity this)
802 {
803         if(IS_ONGROUND(this))
804                 return false;
805         if(this.waterlevel >= WATERLEVEL_SWIMMING)
806                 return false;
807         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
808         if(trace_fraction < 1)
809                 return false;
810         return true;
811 }
812
813
814 void sys_phys_update(entity this, float dt);
815 #if defined(SVQC)
816 void SV_PlayerPhysics(entity this)
817 #elif defined(CSQC)
818 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
819 #endif
820 {
821         sys_phys_update(this, PHYS_INPUT_TIMELENGTH);
822
823 #ifdef SVQC
824         this.pm_frametime = frametime;
825 #elif defined(CSQC)
826         if((ITEMS_STAT(this) & IT_USING_JETPACK) && !IS_DEAD(this) && !intermission)
827                 this.csqcmodel_modelflags |= MF_ROCKET;
828         else
829                 this.csqcmodel_modelflags &= ~MF_ROCKET;
830 #endif
831 }