]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/player.qc
Merge branch 'sev/hud_ctf_update' into 'master'
[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 && strhasword(autocvar_g_physics_clientselect_options, thecvar);
14 }
15
16 float Physics_ClientOption(entity this, string option, float defaultval)
17 {
18         if(Physics_Valid(this.cvar_cl_physics))
19         {
20                 string s = sprintf("g_physics_%s_%s", 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)
25         {
26                 string s = sprintf("g_physics_%s_%s", 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, float maxspd_mod)
34 {
35         STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw", autocvar_sv_airaccel_qw), maxspd_mod);
36         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw))
37                 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw", autocvar_sv_airstrafeaccel_qw), maxspd_mod)
38                 : 0;
39         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw", autocvar_sv_airspeedlimit_nonqw) * maxspd_mod;
40         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed", autocvar_sv_maxspeed) * maxspd_mod; // also slow walking
41
42         // old stats
43         // fix some new settings
44         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor", autocvar_sv_airaccel_qw_stretchfactor);
45         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed", autocvar_sv_maxairstrafespeed);
46         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed", autocvar_sv_maxairspeed);
47         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate", autocvar_sv_airstrafeaccelerate);
48         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel", autocvar_sv_warsowbunny_turnaccel);
49         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction", autocvar_sv_airaccel_sideways_friction);
50         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol", autocvar_sv_aircontrol);
51         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power", autocvar_sv_aircontrol_power);
52         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty", autocvar_sv_aircontrol_penalty);
53         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel", autocvar_sv_warsowbunny_airforwardaccel);
54         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed", autocvar_sv_warsowbunny_topspeed);
55         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel", autocvar_sv_warsowbunny_accel);
56         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio", autocvar_sv_warsowbunny_backtosideratio);
57         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction", autocvar_sv_friction);
58         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate", autocvar_sv_accelerate);
59         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed", autocvar_sv_stopspeed);
60         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate", autocvar_sv_airaccelerate);
61         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate", autocvar_sv_airstopaccelerate);
62         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity", autocvar_sv_jumpvelocity);
63         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump", autocvar_sv_track_canjump);
64 }
65 #endif
66
67 float IsMoveInDirection(vector mv, float ang) // key mix factor
68 {
69         if (mv_x == 0 && mv_y == 0)
70                 return 0; // avoid division by zero
71         ang -= RAD2DEG * atan2(mv_y, mv_x);
72         ang = remainder(ang, 360) / 45;
73         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
74 }
75
76 float GeomLerp(float a, float _lerp, float b)
77 {
78         return a == 0 ? (_lerp < 1 ? 0 : b)
79                 : b == 0 ? (_lerp > 0 ? 0 : a)
80                 : a * pow(fabs(b / a), _lerp);
81 }
82
83 #define unstick_offsets(X) \
84 /* 1 no nudge (just return the original if this test passes) */ \
85         X(' 0.000  0.000  0.000') \
86 /* 6 simple nudges */ \
87         X(' 0.000  0.000  0.125') X('0.000  0.000 -0.125') \
88         X('-0.125  0.000  0.000') X('0.125  0.000  0.000') \
89         X(' 0.000 -0.125  0.000') X('0.000  0.125  0.000') \
90 /* 4 diagonal flat nudges */ \
91         X('-0.125 -0.125  0.000') X('0.125 -0.125  0.000') \
92         X('-0.125  0.125  0.000') X('0.125  0.125  0.000') \
93 /* 8 diagonal upward nudges */ \
94         X('-0.125  0.000  0.125') X('0.125  0.000  0.125') \
95         X(' 0.000 -0.125  0.125') X('0.000  0.125  0.125') \
96         X('-0.125 -0.125  0.125') X('0.125 -0.125  0.125') \
97         X('-0.125  0.125  0.125') X('0.125  0.125  0.125') \
98 /* 8 diagonal downward nudges */ \
99         X('-0.125  0.000 -0.125') X('0.125  0.000 -0.125') \
100         X(' 0.000 -0.125 -0.125') X('0.000  0.125 -0.125') \
101         X('-0.125 -0.125 -0.125') X('0.125 -0.125 -0.125') \
102         X('-0.125  0.125 -0.125') X('0.125  0.125 -0.125') \
103 /**/
104
105 void PM_ClientMovement_Unstick(entity this)
106 {
107         #define X(unstick_offset) \
108         { \
109                 vector neworigin = unstick_offset + this.origin; \
110                 tracebox(neworigin, STAT(PL_CROUCH_MIN, NULL), STAT(PL_CROUCH_MAX, NULL), neworigin, MOVE_NORMAL, this); \
111                 if (!trace_startsolid) \
112                 { \
113                         setorigin(this, neworigin); \
114                         return; \
115                 } \
116         }
117         unstick_offsets(X);
118         #undef X
119 }
120
121 void PM_ClientMovement_UpdateStatus(entity this)
122 {
123 #ifdef CSQC
124         if(!IS_PLAYER(this))
125                 return;
126
127         // set crouched
128         bool do_crouch = PHYS_INPUT_BUTTON_CROUCH(this);
129         if(this.hook && !wasfreed(this.hook))
130                 do_crouch = false;
131         if(this.waterlevel >= WATERLEVEL_SWIMMING)
132                 do_crouch = false;
133         if(hud != HUD_NORMAL)
134                 do_crouch = false;
135         if(STAT(FROZEN, this))
136                 do_crouch = false;
137         if((activeweapon.spawnflags & WEP_TYPE_MELEE_PRI) && viewmodel.animstate_startframe == viewmodel.anim_fire1_x && time < viewmodel.weapon_nextthink)
138                 do_crouch = false;
139         if((activeweapon.spawnflags & WEP_TYPE_MELEE_SEC) && viewmodel.animstate_startframe == viewmodel.anim_fire2_x && time < viewmodel.weapon_nextthink)
140                 do_crouch = false;
141
142         if (do_crouch)
143         {
144                 // wants to crouch, this always works
145                 if (!IS_DUCKED(this)) SET_DUCKED(this);
146         }
147         else
148         {
149                 // wants to stand, if currently crouching we need to check for a low ceiling first
150                 if (IS_DUCKED(this))
151                 {
152                         tracebox(this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), this.origin, MOVE_NORMAL, this);
153                         if (!trace_startsolid) UNSET_DUCKED(this);
154                 }
155         }
156
157         if (IS_ONGROUND(this) || this.velocity.z <= 0 || pmove_waterjumptime <= 0)
158                 pmove_waterjumptime = 0;
159 #endif
160 }
161
162 void CPM_PM_Aircontrol(entity this, vector wishdir, float wishspeed)
163 {
164         float k = 32 * (2 * IsMoveInDirection(this.movement, 0) - 1);
165         if (k <= 0)
166                 return;
167
168         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
169
170         float zspeed = this.velocity_z;
171         this.velocity_z = 0;
172         float xyspeed = vlen(this.velocity);
173         this.velocity = normalize(this.velocity);
174
175         float dot = this.velocity * wishdir;
176
177         if (dot > 0) // we can't change direction while slowing down
178         {
179                 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * PHYS_INPUT_TIMELENGTH;
180                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
181                 k *= PHYS_AIRCONTROL(this);
182                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
183         }
184
185         this.velocity = this.velocity * xyspeed;
186         this.velocity_z = zspeed;
187 }
188
189 float AdjustAirAccelQW(float accelqw, float factor)
190 {
191         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
192 }
193
194 // example config for alternate speed clamping:
195 //   sv_airaccel_qw 0.8
196 //   sv_airaccel_sideways_friction 0
197 //   prvm_globalset server speedclamp_mode 1
198 //     (or 2)
199 void PM_Accelerate(entity this, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
200 {
201         float speedclamp = stretchfactor > 0 ? stretchfactor
202         : accelqw < 0 ? 1 // full clamping, no stretch
203         : -1; // no clamping
204
205         accelqw = fabs(accelqw);
206
207         if (GAMEPLAYFIX_Q2AIRACCELERATE)
208                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
209
210         float vel_straight = this.velocity * wishdir;
211         float vel_z = this.velocity_z;
212         vector vel_xy = vec2(this.velocity);
213         vector vel_perpend = vel_xy - vel_straight * wishdir;
214
215         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
216
217         float vel_xy_current  = vlen(vel_xy);
218         if (speedlimit)
219                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
220         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
221         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
222         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
223         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
224
225         if (sidefric < 0 && (vel_perpend*vel_perpend))
226                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
227         {
228                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
229                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
230                 // assume: themin > 1
231                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
232                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
233                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
234                 // obviously, this cannot be
235                 if (themin <= 0)
236                         vel_perpend *= f;
237                 else
238                 {
239                         themin = sqrt(themin);
240                         vel_perpend *= max(themin, f);
241                 }
242         }
243         else
244                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
245
246         vel_xy = vel_straight * wishdir + vel_perpend;
247
248         if (speedclamp >= 0)
249         {
250                 float vel_xy_preclamp;
251                 vel_xy_preclamp = vlen(vel_xy);
252                 if (vel_xy_preclamp > 0) // prevent division by zero
253                 {
254                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
255                         if (vel_xy_current < vel_xy_preclamp)
256                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
257                 }
258         }
259
260         this.velocity = vel_xy + vel_z * '0 0 1';
261 }
262
263 void PM_AirAccelerate(entity this, vector wishdir, float wishspeed)
264 {
265         if (wishspeed == 0)
266                 return;
267
268         vector curvel = this.velocity;
269         curvel_z = 0;
270         float curspeed = vlen(curvel);
271
272         if (wishspeed > curspeed * 1.01)
273                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
274         else
275         {
276                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
277                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH;
278         }
279         vector wishvel = wishdir * wishspeed;
280         vector acceldir = wishvel - curvel;
281         float addspeed = vlen(acceldir);
282         acceldir = normalize(acceldir);
283
284         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
285
286         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
287         {
288                 vector curdir = normalize(curvel);
289                 float dot = acceldir * curdir;
290                 if (dot < 0)
291                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
292         }
293
294         this.velocity += accelspeed * acceldir;
295 }
296
297
298 /*
299 =============
300 PlayerJump
301
302 When you press the jump key
303 returns true if handled
304 =============
305 */
306 bool PlayerJump(entity this)
307 {
308         if (PHYS_FROZEN(this))
309                 return true; // no jumping in freezetag when frozen
310
311 #ifdef SVQC
312         if (this.player_blocked)
313                 return true; // no jumping while blocked
314 #endif
315
316         bool doublejump = false;
317         float mjumpheight = PHYS_JUMPVELOCITY(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                 }
332                 else
333                 {
334                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
335                         return true;
336                 }
337         }
338
339         if (!doublejump)
340                 if (!IS_ONGROUND(this))
341                         return IS_JUMP_HELD(this);
342
343         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
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))
378         {
379 #ifdef SVQC
380                 if(autocvar_speedmeter)
381                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
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(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
392                 this.jumppadcount = 0;
393 #endif
394         }
395
396         this.velocity_z += mjumpheight;
397
398         UNSET_ONGROUND(this);
399         SET_JUMP_HELD(this);
400
401 #ifdef SVQC
402
403         this.oldvelocity_z = this.velocity_z;
404
405         animdecide_setaction(this, ANIMACTION_JUMP, true);
406
407         if (autocvar_g_jump_grunt)
408                 PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
409 #endif
410         return true;
411 }
412
413 void CheckWaterJump(entity this)
414 {
415 // check for a jump-out-of-water
416         makevectors(this.v_angle);
417         vector start = this.origin;
418         start_z += 8;
419         v_forward_z = 0;
420         normalize(v_forward);
421         vector end = start + v_forward*24;
422         traceline (start, end, true, this);
423         if (trace_fraction < 1)
424         {       // solid at waist
425                 start_z = start_z + this.maxs_z - 8;
426                 end = start + v_forward*24;
427                 this.movedir = trace_plane_normal * -50;
428                 traceline(start, end, true, this);
429                 if (trace_fraction == 1)
430                 {       // open at eye level
431                         this.velocity_z = 225;
432                         this.flags |= FL_WATERJUMP;
433                         SET_JUMP_HELD(this);
434                 #ifdef SVQC
435                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
436                 #elif defined(CSQC)
437                         pmove_waterjumptime = 2;
438                 #endif
439                 }
440         }
441 }
442
443
444 #ifdef SVQC
445         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
446 #elif defined(CSQC)
447         float autocvar_cl_jetpack_jump;
448         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
449 #endif
450 .float jetpack_stopped;
451 void CheckPlayerJump(entity this)
452 {
453 #ifdef SVQC
454         bool was_flying = boolean(ITEMS_STAT(this) & IT_USING_JETPACK);
455 #endif
456         if (JETPACK_JUMP(this) < 2)
457                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
458
459         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
460         {
461                 bool playerjump = PlayerJump(this); // required
462
463                 bool air_jump = !playerjump || M_ARGV(2, bool);
464                 bool activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
465                 bool has_fuel = !PHYS_JETPACK_FUEL(this) || PHYS_AMMO_FUEL(this) || (ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO);
466
467                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
468                 else if (this.jetpack_stopped) { }
469                 else if (!has_fuel)
470                 {
471 #ifdef SVQC
472                         if (was_flying) // TODO: ran out of fuel message
473                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
474                         else if (activate)
475                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
476 #endif
477                         this.jetpack_stopped = true;
478                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
479                 }
480                 else if (activate && !PHYS_FROZEN(this))
481                         ITEMS_STAT(this) |= IT_USING_JETPACK;
482         }
483         else
484         {
485                 this.jetpack_stopped = false;
486                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
487         }
488         if (!PHYS_INPUT_BUTTON_JUMP(this))
489                 UNSET_JUMP_HELD(this);
490
491         if (this.waterlevel == WATERLEVEL_SWIMMING)
492                 CheckWaterJump(this);
493 }
494
495 float racecar_angle(float forward, float down)
496 {
497         if (forward < 0)
498         {
499                 forward = -forward;
500                 down = -down;
501         }
502
503         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
504
505         float angle_mult = forward / (800 + forward);
506
507         if (ret > 180)
508                 return ret * angle_mult + 360 * (1 - angle_mult);
509         else
510                 return ret * angle_mult;
511 }
512
513 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
514 .float specialcommand_pos;
515 void SpecialCommand(entity this)
516 {
517 #ifdef SVQC
518         if (!CheatImpulse(this, CHIMPULSE_GIVE_ALL.impulse))
519                 LOG_INFO("A hollow voice says \"Plugh\".\n");
520 #endif
521 }
522
523 bool PM_check_specialcommand(entity this, int buttons)
524 {
525 #ifdef SVQC
526         string c;
527         if (!buttons)
528                 c = "x";
529         else if (buttons == 1)
530                 c = "1";
531         else if (buttons == 2)
532                 c = " ";
533         else if (buttons == 128)
534                 c = "s";
535         else if (buttons == 256)
536                 c = "w";
537         else if (buttons == 512)
538                 c = "a";
539         else if (buttons == 1024)
540                 c = "d";
541         else
542                 c = "?";
543
544         if (c == substring(specialcommand, this.specialcommand_pos, 1))
545         {
546                 this.specialcommand_pos += 1;
547                 if (this.specialcommand_pos >= strlen(specialcommand))
548                 {
549                         this.specialcommand_pos = 0;
550                         SpecialCommand(this);
551                         return true;
552                 }
553         }
554         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
555                 this.specialcommand_pos = 0;
556 #endif
557         return false;
558 }
559
560 void PM_check_nickspam(entity this)
561 {
562 #ifdef SVQC
563         if (time >= this.nickspamtime)
564                 return;
565         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
566         {
567                 // slight annoyance for nick change scripts
568                 this.movement = -1 * this.movement;
569                 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;
570
571                 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!
572                 {
573                         this.v_angle_x = random() * 360;
574                         this.v_angle_y = random() * 360;
575                         // at least I'm not forcing retardedview by also assigning to angles_z
576                         this.fixangle = true;
577                 }
578         }
579 #endif
580 }
581
582 void PM_check_punch(entity this)
583 {
584 #ifdef SVQC
585         if (this.punchangle != '0 0 0')
586         {
587                 float f = vlen(this.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
588                 if (f > 0)
589                         this.punchangle = normalize(this.punchangle) * f;
590                 else
591                         this.punchangle = '0 0 0';
592         }
593
594         if (this.punchvector != '0 0 0')
595         {
596                 float f = vlen(this.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
597                 if (f > 0)
598                         this.punchvector = normalize(this.punchvector) * f;
599                 else
600                         this.punchvector = '0 0 0';
601         }
602 #endif
603 }
604
605 // predict frozen movement, as frozen players CAN move in some cases
606 void PM_check_frozen(entity this)
607 {
608         if (!PHYS_FROZEN(this))
609                 return;
610         if (PHYS_DODGING_FROZEN(this)
611 #ifdef SVQC
612         && IS_REAL_CLIENT(this)
613 #endif
614         )
615         {
616                 this.movement_x = bound(-5, this.movement.x, 5);
617                 this.movement_y = bound(-5, this.movement.y, 5);
618                 this.movement_z = bound(-5, this.movement.z, 5);
619         }
620         else
621                 this.movement = '0 0 0';
622
623         vector midpoint = ((this.absmin + this.absmax) * 0.5);
624         if (pointcontents(midpoint) == CONTENT_WATER)
625         {
626                 this.velocity = this.velocity * 0.5;
627
628                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
629                         this.velocity_z = 200;
630         }
631 }
632
633 void PM_check_hitground(entity this)
634 {
635 #ifdef SVQC
636         if (!this.wasFlying) return;
637     this.wasFlying = false;
638     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
639     if (time < this.ladder_time) return;
640     if (this.hook) return;
641     this.nextstep = time + 0.3 + random() * 0.1;
642     trace_dphitq3surfaceflags = 0;
643     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
644     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
645     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
646         ? GS_FALL_METAL
647         : GS_FALL;
648     float vol = ((IS_DUCKED(this)) ? VOL_MUFFLED : VOL_BASE);
649     GlobalSound(this, gs, CH_PLAYER, vol, VOICETYPE_PLAYERSOUND);
650 #endif
651 }
652
653 void PM_Footsteps(entity this)
654 {
655 #ifdef SVQC
656         if (!g_footsteps) return;
657         if (IS_DUCKED(this)) return;
658         if (time >= this.lastground + 0.2) return;
659         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
660         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
661         {
662                 this.nextstep = time + 0.3 + random() * 0.1;
663                 trace_dphitq3surfaceflags = 0;
664                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
665                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
666                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
667                         ? GS_STEP_METAL
668                         : GS_STEP;
669                 GlobalSound(this, gs, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
670         }
671 #endif
672 }
673
674 void PM_check_blocked(entity this)
675 {
676 #ifdef SVQC
677         if (!this.player_blocked)
678                 return;
679         this.movement = '0 0 0';
680         this.disableclientprediction = 1;
681 #endif
682 }
683
684 void PM_fly(entity this, float maxspd_mod)
685 {
686         // noclipping or flying
687         UNSET_ONGROUND(this);
688
689         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
690         makevectors(this.v_angle);
691         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
692         vector wishvel = v_forward * this.movement.x
693                                         + v_right * this.movement.y
694                                         + '0 0 1' * this.movement.z;
695         // acceleration
696         vector wishdir = normalize(wishvel);
697         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
698 #ifdef SVQC
699         if(time >= PHYS_TELEPORT_TIME(this))
700 #elif defined(CSQC)
701         if(pmove_waterjumptime <= 0)
702 #endif
703                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
704 }
705
706 void PM_swim(entity this, float maxspd_mod)
707 {
708         // swimming
709         UNSET_ONGROUND(this);
710
711         float jump = PHYS_INPUT_BUTTON_JUMP(this);
712         // water jump only in certain situations
713         // this mimics quakeworld code
714         if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc)
715         {
716                 vector yawangles = '0 1 0' * this.v_angle.y;
717                 makevectors(yawangles);
718                 vector forward = v_forward;
719                 vector spot = this.origin + 24 * forward;
720                 spot_z += 8;
721                 traceline(spot, spot, MOVE_NOMONSTERS, this);
722                 if (trace_startsolid)
723                 {
724                         spot_z += 24;
725                         traceline(spot, spot, MOVE_NOMONSTERS, this);
726                         if (!trace_startsolid)
727                         {
728                                 this.velocity = forward * 50;
729                                 this.velocity_z = 310;
730                                 UNSET_ONGROUND(this);
731                                 SET_JUMP_HELD(this);
732                         }
733                 }
734         }
735         makevectors(this.v_angle);
736         float wishdown = this.movement.z;
737         if(PHYS_INPUT_BUTTON_CROUCH(this))
738                 wishdown = -PHYS_MAXSPEED(this);
739         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
740         vector wishvel = v_forward * this.movement.x
741                                         + v_right * this.movement.y
742                                         + '0 0 1' * wishdown;
743         if(this.viewloc)
744                 wishvel.z = -160; // drift anyway
745         else if (wishvel == '0 0 0')
746                 wishvel = '0 0 -60'; // drift towards bottom
747
748         vector wishdir = normalize(wishvel);
749         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod) * 0.7;
750
751 //      if (pmove_waterjumptime <= 0) // TODO: use
752     {
753                 // water friction
754                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this);
755                 f = min(max(0, f), 1);
756                 this.velocity *= f;
757
758                 f = wishspeed - this.velocity * wishdir;
759                 if (f > 0)
760                 {
761                         float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, f);
762                         this.velocity += accelspeed * wishdir;
763                 }
764
765                 // holding jump button swims upward slowly
766                 if (jump && !this.viewloc)
767                 {
768 #if 0
769                         if (this.watertype & CONTENT_LAVA)
770                                 this.velocity_z =  50;
771                         else if (this.watertype & CONTENT_SLIME)
772                                 this.velocity_z =  80;
773                         else
774                         {
775                                 if (IS_NEXUIZ_DERIVED(gamemode))
776 #endif
777                                 if(this.waterlevel >= WATERLEVEL_SUBMERGED)
778                                         this.velocity_z = PHYS_MAXSPEED(this);
779                                 else
780                                         this.velocity_z = 200;
781 #if 0
782                                 else
783                                         this.velocity_z = 100;
784                         }
785 #endif
786                 }
787         }
788         if(this.viewloc)
789         {
790                 const float addspeed = wishspeed - this.velocity * wishdir;
791                 if (addspeed > 0)
792                 {
793                         const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
794                         this.velocity += accelspeed * wishdir;
795                 }
796         }
797         else
798         {
799                 // water acceleration
800                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
801         }
802 }
803
804 .vector oldmovement;
805 void PM_ladder(entity this, float maxspd_mod)
806 {
807         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
808         UNSET_ONGROUND(this);
809
810         float g;
811         g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
812         if (PHYS_ENTGRAVITY(this))
813                 g *= PHYS_ENTGRAVITY(this);
814         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
815         {
816                 g *= 0.5;
817                 this.velocity_z += g;
818         }
819
820         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
821         makevectors(this.v_angle);
822         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
823         vector wishvel = v_forward * this.movement_x
824                                         + v_right * this.movement_y
825                                         + '0 0 1' * this.movement_z;
826         if(this.viewloc)
827                 wishvel.z = this.oldmovement.x;
828         this.velocity_z += g;
829         if (this.ladder_entity.classname == "func_water")
830         {
831                 float f = vlen(wishvel);
832                 if (f > this.ladder_entity.speed)
833                         wishvel *= (this.ladder_entity.speed / f);
834
835                 this.watertype = this.ladder_entity.skin;
836                 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
837                 if ((this.origin_z + this.view_ofs_z) < f)
838                         this.waterlevel = WATERLEVEL_SUBMERGED;
839                 else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f)
840                         this.waterlevel = WATERLEVEL_SWIMMING;
841                 else if ((this.origin_z + this.mins_z + 1) < f)
842                         this.waterlevel = WATERLEVEL_WETFEET;
843                 else
844                 {
845                         this.waterlevel = WATERLEVEL_NONE;
846                         this.watertype = CONTENT_EMPTY;
847                 }
848         }
849         // acceleration
850         vector wishdir = normalize(wishvel);
851         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
852 #ifdef SVQC
853         if(time >= PHYS_TELEPORT_TIME(this))
854 #elif defined(CSQC)
855         if(pmove_waterjumptime <= 0)
856 #endif
857                 // water acceleration
858                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this)*maxspd_mod, 1, 0, 0, 0);
859 }
860
861 void PM_jetpack(entity this, float maxspd_mod)
862 {
863         //makevectors(this.v_angle.y * '0 1 0');
864         makevectors(this.v_angle);
865         vector wishvel = v_forward * this.movement_x
866                                         + v_right * this.movement_y;
867         // add remaining speed as Z component
868         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
869         // fix speedhacks :P
870         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
871         // add the unused velocity as up component
872         wishvel_z = 0;
873
874         // if (PHYS_INPUT_BUTTON_JUMP(this))
875                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
876
877         // it is now normalized, so...
878         float a_side = PHYS_JETPACK_ACCEL_SIDE(this);
879         float a_up = PHYS_JETPACK_ACCEL_UP(this);
880         float a_add = PHYS_JETPACK_ANTIGRAVITY(this) * PHYS_GRAVITY(this);
881
882         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { a_up = PHYS_JETPACK_REVERSE_THRUST(this); }
883
884         wishvel_x *= a_side;
885         wishvel_y *= a_side;
886         wishvel_z *= a_up;
887         wishvel_z += a_add;
888
889         if(PHYS_JETPACK_REVERSE_THRUST(this) && PHYS_INPUT_BUTTON_CROUCH(self)) { wishvel_z *= -1; }
890
891         float best = 0;
892         //////////////////////////////////////////////////////////////////////////////////////
893         // finding the maximum over all vectors of above form
894         // with wishvel having an absolute value of 1
895         //////////////////////////////////////////////////////////////////////////////////////
896         // we're finding the maximum over
897         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
898         // for z in the range from -1 to 1
899         //////////////////////////////////////////////////////////////////////////////////////
900         // maximum is EITHER attained at the single extreme point:
901         float a_diff = a_side * a_side - a_up * a_up;
902         float f;
903         if (a_diff != 0)
904         {
905                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
906                 if (f > -1 && f < 1) // can it be attained?
907                 {
908                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
909                         //print("middle\n");
910                 }
911         }
912         // OR attained at z = 1:
913         f = (a_up + a_add) * (a_up + a_add);
914         if (f > best)
915         {
916                 best = f;
917                 //print("top\n");
918         }
919         // OR attained at z = -1:
920         f = (a_up - a_add) * (a_up - a_add);
921         if (f > best)
922         {
923                 best = f;
924                 //print("bottom\n");
925         }
926         best = sqrt(best);
927         //////////////////////////////////////////////////////////////////////////////////////
928
929         //print("best possible acceleration: ", ftos(best), "\n");
930
931         float fxy, fz;
932         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE(this), 1);
933         if (wishvel_z - PHYS_GRAVITY(this) > 0)
934                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
935         else
936                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP(this), 1);
937
938         float fvel;
939         fvel = vlen(wishvel);
940         wishvel_x *= fxy;
941         wishvel_y *= fxy;
942         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
943
944         fvel = min(1, vlen(wishvel) / best);
945         if (PHYS_JETPACK_FUEL(this) && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
946                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel));
947         else
948                 f = 1;
949
950         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
951
952         if (f > 0 && wishvel != '0 0 0')
953         {
954                 this.velocity = this.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
955                 UNSET_ONGROUND(this);
956
957 #ifdef SVQC
958                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
959                         this.ammo_fuel -= PHYS_JETPACK_FUEL(this) * PHYS_INPUT_TIMELENGTH * fvel * f;
960
961                 ITEMS_STAT(this) |= IT_USING_JETPACK;
962
963                 // jetpack also inhibits health regeneration, but only for 1 second
964                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
965 #endif
966         }
967 }
968
969 void PM_walk(entity this, float maxspd_mod)
970 {
971         if (!WAS_ONGROUND(this))
972         {
973 #ifdef SVQC
974                 if (autocvar_speedmeter)
975                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
976 #endif
977                 if (this.lastground < time - 0.3)
978                         this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
979 #ifdef SVQC
980                 if (this.jumppadcount > 1)
981                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
982                 this.jumppadcount = 0;
983 #endif
984         }
985
986         // walking
987         makevectors(this.v_angle.y * '0 1 0');
988         const vector wishvel = v_forward * this.movement.x
989                                                 + v_right * this.movement.y;
990         // acceleration
991         const vector wishdir = normalize(wishvel);
992         float wishspeed = vlen(wishvel);
993         wishspeed = min(wishspeed, PHYS_MAXSPEED(this) * maxspd_mod);
994         if (IS_DUCKED(this)) wishspeed *= 0.5;
995
996         // apply edge friction
997         const float f2 = vlen2(vec2(this.velocity));
998         if (f2 > 0)
999         {
1000                 trace_dphitq3surfaceflags = 0;
1001                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
1002                 // TODO: apply edge friction
1003                 // apply ground friction
1004                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1005                         ? PHYS_FRICTION_SLICK(this)
1006                         : PHYS_FRICTION(this);
1007
1008                 float f = sqrt(f2);
1009                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
1010                 f = max(0, f);
1011                 this.velocity *= f;
1012                 /*
1013                    Mathematical analysis time!
1014
1015                    Our goal is to invert this mess.
1016
1017                    For the two cases we get:
1018                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
1019                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1020                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1021                    and
1022                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1023                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1024
1025                    These cases would be chosen ONLY if:
1026                         v0 < PHYS_STOPSPEED(this)
1027                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
1028                         v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1029                    and, respectively:
1030                         v0 >= PHYS_STOPSPEED(this)
1031                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
1032                         v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1033                  */
1034         }
1035         const float addspeed = wishspeed - this.velocity * wishdir;
1036         if (addspeed > 0)
1037         {
1038                 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1039                 this.velocity += accelspeed * wishdir;
1040         }
1041 }
1042
1043 void PM_air(entity this, float buttons_prev, float maxspd_mod)
1044 {
1045         makevectors(this.v_angle.y * '0 1 0');
1046         vector wishvel = v_forward * this.movement.x
1047                                         + v_right * this.movement.y;
1048         // acceleration
1049         vector wishdir = normalize(wishvel);
1050         float wishspeed = vlen(wishvel);
1051
1052 #ifdef SVQC
1053         if(time >= PHYS_TELEPORT_TIME(this))
1054 #elif defined(CSQC)
1055         if(pmove_waterjumptime <= 0)
1056 #endif
1057         {
1058                 float maxairspd = PHYS_MAXAIRSPEED(this) * min(maxspd_mod, 1);
1059
1060                 // apply air speed limit
1061                 float airaccelqw = PHYS_AIRACCEL_QW(this);
1062                 float wishspeed0 = wishspeed;
1063                 wishspeed = min(wishspeed, maxairspd);
1064                 if (IS_DUCKED(this))
1065                         wishspeed *= 0.5;
1066                 float airaccel = PHYS_AIRACCELERATE(this) * min(maxspd_mod, 1);
1067
1068                 float accelerating = (this.velocity * wishdir > 0);
1069                 float wishspeed2 = wishspeed;
1070
1071                 // CPM: air control
1072                 if (PHYS_AIRSTOPACCELERATE(this))
1073                 {
1074                         vector curdir = normalize(vec2(this.velocity));
1075                         airaccel += (PHYS_AIRSTOPACCELERATE(this)*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1076                 }
1077                 // note that for straight forward jumping:
1078                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1079                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1080                 // -->
1081                 // dv/dt = accel * maxspeed (when slow)
1082                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1083                 // log dv/dt = logaccel + logmaxspeed (when slow)
1084                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1085                 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
1086                 if (PHYS_MAXAIRSTRAFESPEED(this))
1087                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(this)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED(this)*maxspd_mod));
1088                 if (PHYS_AIRSTRAFEACCELERATE(this))
1089                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(this)*maxspd_mod);
1090                 if (PHYS_AIRSTRAFEACCEL_QW(this))
1091                         airaccelqw =
1092                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
1093                 *
1094                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
1095                 // !CPM
1096
1097                 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0)
1098                         PM_AirAccelerate(this, wishdir, wishspeed2);
1099                 else {
1100                     float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
1101                         PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
1102         }
1103
1104                 if (PHYS_AIRCONTROL(this))
1105                         CPM_PM_Aircontrol(this, wishdir, wishspeed2);
1106         }
1107 }
1108
1109 // used for calculating airshots
1110 bool IsFlying(entity this)
1111 {
1112         if(IS_ONGROUND(this))
1113                 return false;
1114         if(this.waterlevel >= WATERLEVEL_SWIMMING)
1115                 return false;
1116         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
1117         if(trace_fraction < 1)
1118                 return false;
1119         return true;
1120 }
1121
1122 void PM_Main(entity this)
1123 {
1124         int buttons = PHYS_INPUT_BUTTON_MASK(this);
1125 #ifdef CSQC
1126         this.items = STAT(ITEMS);
1127
1128         this.movement = PHYS_INPUT_MOVEVALUES(this);
1129
1130         this.spectatorspeed = STAT(SPECTATORSPEED);
1131
1132         this.team = myteam + 1; // is this correct?
1133         if (!(PHYS_INPUT_BUTTON_JUMP(this))) // !jump
1134                 UNSET_JUMP_HELD(this); // canjump = true
1135         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1136
1137         PM_ClientMovement_UpdateStatus(this);
1138 #endif
1139
1140         this.oldmovement = this.movement;
1141
1142
1143 #ifdef SVQC
1144         WarpZone_PlayerPhysics_FixVAngle(this);
1145 #endif
1146         float maxspeed_mod = 1;
1147         maxspeed_mod *= PHYS_HIGHSPEED(this);
1148
1149 #ifdef SVQC
1150         Physics_UpdateStats(this, maxspeed_mod);
1151
1152         if (this.PlayerPhysplug)
1153                 if (this.PlayerPhysplug(this))
1154                         return;
1155 #elif defined(CSQC)
1156         if(hud != HUD_NORMAL)
1157                 return; // no vehicle prediction (yet)
1158 #endif
1159
1160 #ifdef SVQC
1161         anticheat_physics(this);
1162 #endif
1163
1164         if (PM_check_specialcommand(this, buttons))
1165                 return;
1166 #ifdef SVQC
1167         if (sv_maxidle > 0)
1168         {
1169                 if (buttons != this.buttons_old || this.movement != this.movement_old || this.v_angle != this.v_angle_old)
1170                         this.parm_idlesince = time;
1171         }
1172 #endif
1173         int buttons_prev = this.buttons_old;
1174         this.buttons_old = buttons;
1175         this.movement_old = this.movement;
1176         this.v_angle_old = this.v_angle;
1177
1178         PM_check_nickspam(this);
1179
1180         PM_check_punch(this);
1181 #ifdef SVQC
1182         if (IS_BOT_CLIENT(this))
1183         {
1184                 if (playerdemo_read(this))
1185                         return;
1186                 bot_think(this);
1187         }
1188 #endif
1189
1190 #ifdef SVQC
1191         if (IS_PLAYER(this))
1192         {
1193                 const bool allowed_to_move = (time >= game_starttime);
1194                 if (!allowed_to_move)
1195                 {
1196                         this.velocity = '0 0 0';
1197                         set_movetype(this, MOVETYPE_NONE);
1198                         this.disableclientprediction = 2;
1199                 }
1200                 else if (this.disableclientprediction == 2)
1201                 {
1202                         if (this.move_movetype == MOVETYPE_NONE)
1203                                 set_movetype(this, MOVETYPE_WALK);
1204                         this.disableclientprediction = 0;
1205                 }
1206         }
1207 #endif
1208
1209 #ifdef SVQC
1210         if (this.move_movetype == MOVETYPE_NONE)
1211                 return;
1212
1213         // when we get here, disableclientprediction cannot be 2
1214         this.disableclientprediction = (this.move_qcphysics) ? -1 : 0;
1215 #endif
1216
1217         viewloc_PlayerPhysics(this);
1218
1219         PM_check_frozen(this);
1220
1221         PM_check_blocked(this);
1222
1223         maxspeed_mod = 1;
1224
1225         if (this.in_swamp)
1226                 maxspeed_mod *= this.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1227
1228         // conveyors: first fix velocity
1229         if (this.conveyor.state)
1230                 this.velocity -= this.conveyor.movedir;
1231
1232         MUTATOR_CALLHOOK(PlayerPhysics, this);
1233
1234         if (!IS_PLAYER(this))
1235         {
1236 #ifdef SVQC
1237                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1238                 if (!this.spectatorspeed)
1239                         this.spectatorspeed = maxspeed_mod;
1240                 if (this.impulse && this.impulse <= 19 || (this.impulse >= 200 && this.impulse <= 209) || (this.impulse >= 220 && this.impulse <= 229))
1241                 {
1242                         if (this.lastclassname != STR_PLAYER)
1243                         {
1244                                 if (this.impulse == 10 || this.impulse == 15 || this.impulse == 18 || (this.impulse >= 200 && this.impulse <= 209))
1245                                         this.spectatorspeed = bound(1, this.spectatorspeed + 0.5, 5);
1246                                 else if (this.impulse == 11)
1247                                         this.spectatorspeed = maxspeed_mod;
1248                                 else if (this.impulse == 12 || this.impulse == 16  || this.impulse == 19 || (this.impulse >= 220 && this.impulse <= 229))
1249                                         this.spectatorspeed = bound(1, this.spectatorspeed - 0.5, 5);
1250                                 else if (this.impulse >= 1 && this.impulse <= 9)
1251                                         this.spectatorspeed = 1 + 0.5 * (this.impulse - 1);
1252                         } // otherwise just clear
1253                         this.impulse = 0;
1254                 }
1255 #endif
1256                 maxspeed_mod = this.spectatorspeed;
1257         }
1258 #ifdef SVQC
1259
1260         float spd = max(PHYS_MAXSPEED(this), PHYS_MAXAIRSPEED(this)) * maxspeed_mod;
1261         if(this.speed != spd)
1262         {
1263                 this.speed = spd;
1264                 string temps = ftos(spd);
1265                 stuffcmd(this, strcat("cl_forwardspeed ", temps, "\n"));
1266                 stuffcmd(this, strcat("cl_backspeed ", temps, "\n"));
1267                 stuffcmd(this, strcat("cl_sidespeed ", temps, "\n"));
1268                 stuffcmd(this, strcat("cl_upspeed ", temps, "\n"));
1269         }
1270
1271         if(this.jumpspeedcap_min != autocvar_sv_jumpspeedcap_min)
1272         {
1273                 this.jumpspeedcap_min = autocvar_sv_jumpspeedcap_min;
1274                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_min \"%s\"\n", autocvar_sv_jumpspeedcap_min));
1275         }
1276         if(this.jumpspeedcap_max != autocvar_sv_jumpspeedcap_max)
1277         {
1278                 this.jumpspeedcap_max = autocvar_sv_jumpspeedcap_max;
1279                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_max \"%s\"\n", autocvar_sv_jumpspeedcap_max));
1280         }
1281 #endif
1282
1283         if(IS_DEAD(this))
1284         {
1285                 // handle water here
1286                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
1287                 if(pointcontents(midpoint) == CONTENT_WATER)
1288                 {
1289                         this.velocity = this.velocity * 0.5;
1290
1291                         // do we want this?
1292                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1293                                 //{ this.velocity_z = 70; }
1294                 }
1295                 goto end;
1296         }
1297
1298 #ifdef SVQC
1299         if (!this.fixangle)
1300                 this.angles = '0 1 0' * this.v_angle.y;
1301 #endif
1302
1303         if (IS_PLAYER(this) && IS_ONGROUND(this))
1304         {
1305                 PM_check_hitground(this);
1306                 PM_Footsteps(this);
1307         }
1308
1309 #ifdef SVQC
1310         if(IsFlying(this))
1311                 this.wasFlying = 1;
1312 #endif
1313
1314         if (IS_PLAYER(this))
1315                 CheckPlayerJump(this);
1316
1317         if (this.flags & FL_WATERJUMP)
1318         {
1319                 this.velocity_x = this.movedir.x;
1320                 this.velocity_y = this.movedir.y;
1321                 if (this.waterlevel == WATERLEVEL_NONE
1322                 #ifdef CSQC
1323                         || pmove_waterjumptime <= 0
1324                 #elif defined(SVQC)
1325                         || time > PHYS_TELEPORT_TIME(this)
1326                 #endif
1327                         )
1328                 {
1329                         this.flags &= ~FL_WATERJUMP;
1330                 #ifdef CSQC
1331                         pmove_waterjumptime = 0;
1332                 #elif defined(CSQC)
1333                         PHYS_TELEPORT_TIME(this) = 0;
1334                 #endif
1335                 }
1336         }
1337
1338         else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod))
1339                 { }
1340
1341 #ifdef SVQC
1342         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1343 #elif defined(CSQC)
1344         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1345 #endif
1346                 PM_fly(this, maxspeed_mod);
1347
1348         else if (this.waterlevel >= WATERLEVEL_SWIMMING)
1349                 PM_swim(this, maxspeed_mod);
1350
1351         else if (time < this.ladder_time)
1352                 PM_ladder(this, maxspeed_mod);
1353
1354         else if (ITEMS_STAT(this) & IT_USING_JETPACK)
1355                 PM_jetpack(this, maxspeed_mod);
1356
1357         else if (IS_ONGROUND(this))
1358                 PM_walk(this, maxspeed_mod);
1359
1360         else
1361                 PM_air(this, buttons_prev, maxspeed_mod);
1362
1363 LABEL(end)
1364         if (IS_ONGROUND(this))
1365                 this.lastground = time;
1366
1367         // conveyors: then break velocity again
1368         if(this.conveyor.state)
1369                 this.velocity += this.conveyor.movedir;
1370
1371         this.lastflags = this.flags;
1372
1373         this.lastclassname = this.classname;
1374 }
1375
1376 #if defined(SVQC)
1377 void SV_PlayerPhysics(entity this)
1378 #elif defined(CSQC)
1379 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
1380 #endif
1381 {
1382         PM_Main(this);
1383
1384 #ifdef SVQC
1385         this.pm_frametime = frametime;
1386 #endif
1387 }