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