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