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