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