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