]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
Add special water physics for side scroller
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics.qc
1 #include "physics.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)
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 cvar(strcat("sv_", option));
31 }
32
33 void Physics_UpdateStats(entity this, float maxspd_mod)
34 {
35         STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw"), maxspd_mod);
36         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw"))
37                 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw"), maxspd_mod)
38                 : 0;
39         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw") * maxspd_mod;
40         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "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");
45         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed");
46         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed");
47         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate");
48         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel");
49         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction");
50         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol");
51         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power");
52         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty");
53         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel");
54         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed");
55         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel");
56         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio");
57         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction");
58         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate");
59         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed");
60         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate");
61         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate");
62         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity");
63         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "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, PL_CROUCH_MIN, PL_CROUCH_MAX, 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, bool ground)
122 {
123 #ifdef CSQC
124         // make sure player is not stuck
125         if(autocvar_cl_movement != 3)
126                 PM_ClientMovement_Unstick(this);
127
128         // set crouched
129         if (PHYS_INPUT_BUTTON_CROUCH(this))
130         {
131                 // wants to crouch, this always works
132                 if (!IS_DUCKED(this)) SET_DUCKED(this);
133         }
134         else
135         {
136                 // wants to stand, if currently crouching we need to check for a low ceiling first
137                 if (IS_DUCKED(this))
138                 {
139                         tracebox(this.origin, PL_MIN, PL_MAX, this.origin, MOVE_NORMAL, this);
140                         if (!trace_startsolid) UNSET_DUCKED(this);
141                 }
142         }
143
144         // set onground
145         vector origin1 = this.origin + '0 0 1';
146         vector origin2 = this.origin - '0 0 1';
147
148         if (ground && autocvar_cl_movement != 3)
149         {
150                 tracebox(origin1, this.mins, this.maxs, origin2, MOVE_NORMAL, this);
151                 if (trace_fraction < 1.0 && trace_plane_normal.z > 0.7)
152                 {
153                         SET_ONGROUND(this);
154
155                         // this code actually "predicts" an impact; so let's clip velocity first
156                         this.velocity -= this.velocity * trace_plane_normal * trace_plane_normal;
157                 }
158                 else
159                         UNSET_ONGROUND(this);
160         }
161
162         if(autocvar_cl_movement != 3)
163         {
164                 // set watertype/waterlevel
165                 origin1 = this.origin;
166                 origin1.z += this.mins_z + 1;
167                 this.waterlevel = WATERLEVEL_NONE;
168
169                 int thepoint = pointcontents(origin1);
170
171                 this.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
172
173                 if (this.watertype)
174                 {
175                         this.waterlevel = WATERLEVEL_WETFEET;
176                         origin1.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
177                         thepoint = pointcontents(origin1);
178                         if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
179                         {
180                                 this.waterlevel = WATERLEVEL_SWIMMING;
181                                 origin1.z = this.origin.z + 22;
182                                 thepoint = pointcontents(origin1);
183                                 if (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
184                                         this.waterlevel = WATERLEVEL_SUBMERGED;
185                         }
186                 }
187         }
188
189         if (IS_ONGROUND(this) || this.velocity.z <= 0 || pmove_waterjumptime <= 0)
190                 pmove_waterjumptime = 0;
191 #endif
192 }
193
194 void PM_ClientMovement_Move(entity this)
195 {
196 #ifdef CSQC
197
198         PM_ClientMovement_UpdateStatus(this, false);
199         if(autocvar_cl_movement == 3)
200                 return;
201
202         int bump;
203         float t;
204         float f;
205         vector neworigin;
206         vector currentorigin2;
207         vector neworigin2;
208         vector primalvelocity;
209
210         vector trace1_endpos = '0 0 0';
211         vector trace2_endpos = '0 0 0';
212         vector trace3_endpos = '0 0 0';
213         float trace1_fraction = 0;
214         float trace2_fraction = 0;
215         float trace3_fraction = 0;
216         vector trace1_plane_normal = '0 0 0';
217         vector trace2_plane_normal = '0 0 0';
218         vector trace3_plane_normal = '0 0 0';
219
220         primalvelocity = this.velocity;
221         for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (this.velocity * this.velocity) > 0; bump++)
222         {
223                 neworigin = this.origin + t * this.velocity;
224                 tracebox(this.origin, this.mins, this.maxs, neworigin, MOVE_NORMAL, this);
225                 trace1_endpos = trace_endpos;
226                 trace1_fraction = trace_fraction;
227                 trace1_plane_normal = trace_plane_normal;
228                 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
229                 {
230                         // may be a step or wall, try stepping up
231                         // first move forward at a higher level
232                         currentorigin2 = this.origin;
233                         currentorigin2_z += PHYS_STEPHEIGHT;
234                         neworigin2 = neworigin;
235                         neworigin2_z += PHYS_STEPHEIGHT;
236                         tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
237                         trace2_endpos = trace_endpos;
238                         trace2_fraction = trace_fraction;
239                         trace2_plane_normal = trace_plane_normal;
240                         if(!trace_startsolid)
241                         {
242                                 // then move down from there
243                                 currentorigin2 = trace2_endpos;
244                                 neworigin2 = trace2_endpos;
245                                 neworigin2_z = this.origin_z;
246                                 tracebox(currentorigin2, this.mins, this.maxs, neworigin2, MOVE_NORMAL, this);
247                                 trace3_endpos = trace_endpos;
248                                 trace3_fraction = trace_fraction;
249                                 trace3_plane_normal = trace_plane_normal;
250                                 // accept the new trace if it made some progress
251                                 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
252                                 {
253                                         trace1_endpos = trace2_endpos;
254                                         trace1_fraction = trace2_fraction;
255                                         trace1_plane_normal = trace2_plane_normal;
256                                         trace1_endpos = trace3_endpos;
257                                 }
258                         }
259                 }
260
261                 // check if it moved at all
262                 if(trace1_fraction >= 0.001)
263                         setorigin(this, trace1_endpos);
264
265                 // check if it moved all the way
266                 if(trace1_fraction == 1)
267                         break;
268
269                 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
270                 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
271                 // this got commented out in a change that supposedly makes the code match QW better
272                 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
273                 if(trace1_plane_normal_z > 0.7)
274                         SET_ONGROUND(this);
275
276                 t -= t * trace1_fraction;
277
278                 f = (this.velocity * trace1_plane_normal);
279                 this.velocity = this.velocity + -f * trace1_plane_normal;
280         }
281         if(PHYS_TELEPORT_TIME(this) > 0)
282                 this.velocity = primalvelocity;
283 #endif
284 }
285
286 void CPM_PM_Aircontrol(entity this, vector wishdir, float wishspeed)
287 {
288         float k = 32 * (2 * IsMoveInDirection(this.movement, 0) - 1);
289         if (k <= 0)
290                 return;
291
292         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(this), 1);
293
294         float zspeed = this.velocity_z;
295         this.velocity_z = 0;
296         float xyspeed = vlen(this.velocity);
297         this.velocity = normalize(this.velocity);
298
299         float dot = this.velocity * wishdir;
300
301         if (dot > 0) // we can't change direction while slowing down
302         {
303                 k *= pow(dot, PHYS_AIRCONTROL_POWER(this)) * PHYS_INPUT_TIMELENGTH;
304                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY(this) * sqrt(max(0, 1 - dot*dot)) * k/32);
305                 k *= PHYS_AIRCONTROL(this);
306                 this.velocity = normalize(this.velocity * xyspeed + wishdir * k);
307         }
308
309         this.velocity = this.velocity * xyspeed;
310         this.velocity_z = zspeed;
311 }
312
313 float AdjustAirAccelQW(float accelqw, float factor)
314 {
315         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
316 }
317
318 // example config for alternate speed clamping:
319 //   sv_airaccel_qw 0.8
320 //   sv_airaccel_sideways_friction 0
321 //   prvm_globalset server speedclamp_mode 1
322 //     (or 2)
323 void PM_Accelerate(entity this, vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
324 {
325         float speedclamp = stretchfactor > 0 ? stretchfactor
326         : accelqw < 0 ? 1 // full clamping, no stretch
327         : -1; // no clamping
328
329         accelqw = fabs(accelqw);
330
331         if (GAMEPLAYFIX_Q2AIRACCELERATE)
332                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
333
334         float vel_straight = this.velocity * wishdir;
335         float vel_z = this.velocity_z;
336         vector vel_xy = vec2(this.velocity);
337         vector vel_perpend = vel_xy - vel_straight * wishdir;
338
339         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
340
341         float vel_xy_current  = vlen(vel_xy);
342         if (speedlimit)
343                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
344         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
345         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
346         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
347         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
348
349         if (sidefric < 0 && (vel_perpend*vel_perpend))
350                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
351         {
352                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
353                 float themin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
354                 // assume: themin > 1
355                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
356                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
357                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
358                 // obviously, this cannot be
359                 if (themin <= 0)
360                         vel_perpend *= f;
361                 else
362                 {
363                         themin = sqrt(themin);
364                         vel_perpend *= max(themin, f);
365                 }
366         }
367         else
368                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
369
370         vel_xy = vel_straight * wishdir + vel_perpend;
371
372         if (speedclamp >= 0)
373         {
374                 float vel_xy_preclamp;
375                 vel_xy_preclamp = vlen(vel_xy);
376                 if (vel_xy_preclamp > 0) // prevent division by zero
377                 {
378                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
379                         if (vel_xy_current < vel_xy_preclamp)
380                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
381                 }
382         }
383
384         this.velocity = vel_xy + vel_z * '0 0 1';
385 }
386
387 void PM_AirAccelerate(entity this, vector wishdir, float wishspeed)
388 {
389         if (wishspeed == 0)
390                 return;
391
392         vector curvel = this.velocity;
393         curvel_z = 0;
394         float curspeed = vlen(curvel);
395
396         if (wishspeed > curspeed * 1.01)
397                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
398         else
399         {
400                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED(this) - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED(this) - PHYS_MAXSPEED(this)));
401                 wishspeed = max(curspeed, PHYS_MAXSPEED(this)) + PHYS_WARSOWBUNNY_ACCEL(this) * f * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH;
402         }
403         vector wishvel = wishdir * wishspeed;
404         vector acceldir = wishvel - curvel;
405         float addspeed = vlen(acceldir);
406         acceldir = normalize(acceldir);
407
408         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL(this) * PHYS_MAXSPEED(this) * PHYS_INPUT_TIMELENGTH);
409
410         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this) < 1)
411         {
412                 vector curdir = normalize(curvel);
413                 float dot = acceldir * curdir;
414                 if (dot < 0)
415                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO(this)) * dot * curdir;
416         }
417
418         this.velocity += accelspeed * acceldir;
419 }
420
421
422 /*
423 =============
424 PlayerJump
425
426 When you press the jump key
427 returns true if handled
428 =============
429 */
430 bool PlayerJump(entity this)
431 {
432         if (PHYS_FROZEN(this))
433                 return true; // no jumping in freezetag when frozen
434
435 #ifdef SVQC
436         if (this.player_blocked)
437                 return true; // no jumping while blocked
438 #endif
439
440         bool doublejump = false;
441         float mjumpheight = PHYS_JUMPVELOCITY(this);
442
443         if (MUTATOR_CALLHOOK(PlayerJump, this, doublejump, mjumpheight))
444                 return true;
445
446         doublejump = player_multijump;
447         mjumpheight = player_jumpheight;
448
449         if (this.waterlevel >= WATERLEVEL_SWIMMING)
450         {
451                 if(this.viewloc)
452                 {
453                         doublejump = true;
454                         mjumpheight *= 0.7;
455                 }
456                 else
457                 {
458                         this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
459                         return true;
460                 }
461         }
462
463         if (!doublejump)
464                 if (!IS_ONGROUND(this))
465                         return IS_JUMP_HELD(this);
466
467         bool track_jump = PHYS_CL_TRACK_CANJUMP(this);
468         if(PHYS_TRACK_CANJUMP(this))
469                 track_jump = true;
470
471         if (track_jump)
472                 if (IS_JUMP_HELD(this))
473                         return true;
474
475         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
476         // velocity bounds.  Final velocity is bound between (jumpheight *
477         // min + jumpheight) and (jumpheight * max + jumpheight);
478
479         if(PHYS_JUMPSPEEDCAP_MIN != "")
480         {
481                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
482
483                 if (this.velocity_z < minjumpspeed)
484                         mjumpheight += minjumpspeed - this.velocity_z;
485         }
486
487         if(PHYS_JUMPSPEEDCAP_MAX != "")
488         {
489                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
490                 tracebox(this.origin + '0 0 0.01', this.mins, this.maxs, this.origin - '0 0 0.01', MOVE_NORMAL, this);
491
492                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
493                 {
494                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
495
496                         if (this.velocity_z > maxjumpspeed)
497                                 mjumpheight -= this.velocity_z - maxjumpspeed;
498                 }
499         }
500
501         if (!WAS_ONGROUND(this))
502         {
503 #ifdef SVQC
504                 if(autocvar_speedmeter)
505                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
506 #endif
507                 if(this.lastground < time - 0.3)
508                 {
509                         this.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
510                         this.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
511                 }
512 #ifdef SVQC
513                 if(this.jumppadcount > 1)
514                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
515                 this.jumppadcount = 0;
516 #endif
517         }
518
519         this.velocity_z += mjumpheight;
520
521         UNSET_ONGROUND(this);
522         SET_JUMP_HELD(this);
523
524 #ifdef SVQC
525
526         this.oldvelocity_z = this.velocity_z;
527
528         animdecide_setaction(this, ANIMACTION_JUMP, true);
529
530         if (autocvar_g_jump_grunt)
531                 PlayerSound(this, playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
532 #endif
533         return true;
534 }
535
536 void CheckWaterJump(entity this)
537 {
538 // check for a jump-out-of-water
539         makevectors(this.v_angle);
540         vector start = this.origin;
541         start_z += 8;
542         v_forward_z = 0;
543         normalize(v_forward);
544         vector end = start + v_forward*24;
545         traceline (start, end, true, this);
546         if (trace_fraction < 1)
547         {       // solid at waist
548                 start_z = start_z + this.maxs_z - 8;
549                 end = start + v_forward*24;
550                 this.movedir = trace_plane_normal * -50;
551                 traceline(start, end, true, this);
552                 if (trace_fraction == 1)
553                 {       // open at eye level
554                         this.velocity_z = 225;
555                         this.flags |= FL_WATERJUMP;
556                         SET_JUMP_HELD(this);
557                 #ifdef SVQC
558                         PHYS_TELEPORT_TIME(this) = time + 2;    // safety net
559                 #elif defined(CSQC)
560                         pmove_waterjumptime = time + 2;
561                 #endif
562                 }
563         }
564 }
565
566
567 #ifdef SVQC
568         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
569 #elif defined(CSQC)
570         float autocvar_cl_jetpack_jump;
571         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
572 #endif
573 .float jetpack_stopped;
574 void CheckPlayerJump(entity this)
575 {
576 #ifdef SVQC
577         float was_flying = ITEMS_STAT(this) & IT_USING_JETPACK;
578 #endif
579         if (JETPACK_JUMP(this) < 2)
580                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
581
582         if(PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this))
583         {
584                 float air_jump = !PlayerJump(this) || player_multijump; // PlayerJump() has important side effects
585                 float activate = JETPACK_JUMP(this) && air_jump && PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_JETPACK(this);
586                 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(this) || ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO;
587
588                 if (!(ITEMS_STAT(this) & ITEM_Jetpack.m_itemid)) { }
589                 else if (this.jetpack_stopped) { }
590                 else if (!has_fuel)
591                 {
592 #ifdef SVQC
593                         if (was_flying) // TODO: ran out of fuel message
594                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
595                         else if (activate)
596                                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_JETPACK_NOFUEL);
597 #endif
598                         this.jetpack_stopped = true;
599                         ITEMS_STAT(this) &= ~IT_USING_JETPACK;
600                 }
601                 else if (activate && !PHYS_FROZEN(this))
602                         ITEMS_STAT(this) |= IT_USING_JETPACK;
603         }
604         else
605         {
606                 this.jetpack_stopped = false;
607                 ITEMS_STAT(this) &= ~IT_USING_JETPACK;
608         }
609         if (!PHYS_INPUT_BUTTON_JUMP(this))
610                 UNSET_JUMP_HELD(this);
611
612         if (this.waterlevel == WATERLEVEL_SWIMMING)
613                 CheckWaterJump(this);
614 }
615
616 float racecar_angle(float forward, float down)
617 {
618         if (forward < 0)
619         {
620                 forward = -forward;
621                 down = -down;
622         }
623
624         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
625
626         float angle_mult = forward / (800 + forward);
627
628         if (ret > 180)
629                 return ret * angle_mult + 360 * (1 - angle_mult);
630         else
631                 return ret * angle_mult;
632 }
633
634 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
635 .float specialcommand_pos;
636 void SpecialCommand()
637 {
638 #ifdef SVQC
639         if (!CheatImpulse(CHIMPULSE_GIVE_ALL.impulse))
640                 LOG_INFO("A hollow voice says \"Plugh\".\n");
641 #endif
642 }
643
644 float PM_check_specialcommand(entity this, float buttons)
645 {
646 #ifdef SVQC
647         string c;
648         if (!buttons)
649                 c = "x";
650         else if (buttons == 1)
651                 c = "1";
652         else if (buttons == 2)
653                 c = " ";
654         else if (buttons == 128)
655                 c = "s";
656         else if (buttons == 256)
657                 c = "w";
658         else if (buttons == 512)
659                 c = "a";
660         else if (buttons == 1024)
661                 c = "d";
662         else
663                 c = "?";
664
665         if (c == substring(specialcommand, this.specialcommand_pos, 1))
666         {
667                 this.specialcommand_pos += 1;
668                 if (this.specialcommand_pos >= strlen(specialcommand))
669                 {
670                         this.specialcommand_pos = 0;
671                         SpecialCommand();
672                         return true;
673                 }
674         }
675         else if (this.specialcommand_pos && (c != substring(specialcommand, this.specialcommand_pos - 1, 1)))
676                 this.specialcommand_pos = 0;
677 #endif
678         return false;
679 }
680
681 void PM_check_nickspam(entity this)
682 {
683 #ifdef SVQC
684         if (time >= this.nickspamtime)
685                 return;
686         if (this.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
687         {
688                 // slight annoyance for nick change scripts
689                 this.movement = -1 * this.movement;
690                 this.BUTTON_ATCK = this.BUTTON_JUMP = this.BUTTON_ATCK2 = this.BUTTON_ZOOM = this.BUTTON_CROUCH = this.BUTTON_HOOK = this.BUTTON_USE = 0;
691
692                 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!
693                 {
694                         this.v_angle_x = random() * 360;
695                         this.v_angle_y = random() * 360;
696                         // at least I'm not forcing retardedview by also assigning to angles_z
697                         this.fixangle = true;
698                 }
699         }
700 #endif
701 }
702
703 void PM_check_punch(entity this)
704 {
705 #ifdef SVQC
706         if (this.punchangle != '0 0 0')
707         {
708                 float f = vlen(this.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
709                 if (f > 0)
710                         this.punchangle = normalize(this.punchangle) * f;
711                 else
712                         this.punchangle = '0 0 0';
713         }
714
715         if (this.punchvector != '0 0 0')
716         {
717                 float f = vlen(this.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
718                 if (f > 0)
719                         this.punchvector = normalize(this.punchvector) * f;
720                 else
721                         this.punchvector = '0 0 0';
722         }
723 #endif
724 }
725
726 // predict frozen movement, as frozen players CAN move in some cases
727 void PM_check_frozen(entity this)
728 {
729         if (!PHYS_FROZEN(this))
730                 return;
731         if (PHYS_DODGING_FROZEN
732 #ifdef SVQC
733         && IS_REAL_CLIENT(this)
734 #endif
735         )
736         {
737                 this.movement_x = bound(-5, this.movement.x, 5);
738                 this.movement_y = bound(-5, this.movement.y, 5);
739                 this.movement_z = bound(-5, this.movement.z, 5);
740         }
741         else
742                 this.movement = '0 0 0';
743
744         vector midpoint = ((this.absmin + this.absmax) * 0.5);
745         if (pointcontents(midpoint) == CONTENT_WATER)
746         {
747                 this.velocity = this.velocity * 0.5;
748
749                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
750                         this.velocity_z = 200;
751         }
752 }
753
754 void PM_check_hitground(entity this)
755 {
756 #ifdef SVQC
757         if (!this.wasFlying) return;
758     this.wasFlying = false;
759     if (this.waterlevel >= WATERLEVEL_SWIMMING) return;
760     if (time < this.ladder_time) return;
761     if (this.hook) return;
762     this.nextstep = time + 0.3 + random() * 0.1;
763     trace_dphitq3surfaceflags = 0;
764     tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
765     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
766     entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
767         ? GS_FALL_METAL
768         : GS_FALL;
769     GlobalSound(this, gs, CH_PLAYER, VOICETYPE_PLAYERSOUND);
770 #endif
771 }
772
773 void PM_Footsteps(entity this)
774 {
775 #ifdef SVQC
776         if (!g_footsteps) return;
777         if (IS_DUCKED(this)) return;
778         if (time >= this.lastground + 0.2) return;
779         if (vdist(this.velocity, <=, autocvar_sv_maxspeed * 0.6)) return;
780         if ((time > this.nextstep) || (time < (this.nextstep - 10.0)))
781         {
782                 this.nextstep = time + 0.3 + random() * 0.1;
783                 trace_dphitq3surfaceflags = 0;
784                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
785                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS) return;
786                 entity gs = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
787                         ? GS_STEP_METAL
788                         : GS_STEP;
789                 GlobalSound(this, gs, CH_PLAYER, VOICETYPE_PLAYERSOUND);
790         }
791 #endif
792 }
793
794 void PM_check_blocked(entity this)
795 {
796 #ifdef SVQC
797         if (!this.player_blocked)
798                 return;
799         this.movement = '0 0 0';
800         this.disableclientprediction = 1;
801 #endif
802 }
803
804 void PM_fly(entity this, float maxspd_mod)
805 {
806         // noclipping or flying
807         UNSET_ONGROUND(this);
808
809         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
810         makevectors(this.v_angle);
811         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
812         vector wishvel = v_forward * this.movement.x
813                                         + v_right * this.movement.y
814                                         + '0 0 1' * this.movement.z;
815         // acceleration
816         vector wishdir = normalize(wishvel);
817         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
818 #ifdef SVQC
819         if(time >= PHYS_TELEPORT_TIME(this))
820 #endif
821                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
822         PM_ClientMovement_Move(this);
823 }
824
825 void PM_swim(entity this, float maxspd_mod)
826 {
827         // swimming
828         UNSET_ONGROUND(this);
829
830         float jump = PHYS_INPUT_BUTTON_JUMP(this);
831         // water jump only in certain situations
832         // this mimics quakeworld code
833         if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc)
834         {
835                 vector yawangles = '0 1 0' * this.v_angle.y;
836                 makevectors(yawangles);
837                 vector forward = v_forward;
838                 vector spot = this.origin + 24 * forward;
839                 spot_z += 8;
840                 traceline(spot, spot, MOVE_NOMONSTERS, this);
841                 if (trace_startsolid)
842                 {
843                         spot_z += 24;
844                         traceline(spot, spot, MOVE_NOMONSTERS, this);
845                         if (!trace_startsolid)
846                         {
847                                 this.velocity = forward * 50;
848                                 this.velocity_z = 310;
849                         #ifdef CSQC
850                                 pmove_waterjumptime = 2;
851                         #endif
852                                 UNSET_ONGROUND(this);
853                                 SET_JUMP_HELD(this);
854                         }
855                 }
856         }
857         makevectors(this.v_angle);
858         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
859         vector wishvel = v_forward * this.movement.x
860                                         + v_right * this.movement.y
861                                         + '0 0 1' * this.movement.z;
862         if(this.viewloc)
863                 wishvel.z = -160; // drift anyway
864         else if (wishvel == '0 0 0')
865                 wishvel = '0 0 -60'; // drift towards bottom
866
867
868         vector wishdir = normalize(wishvel);
869         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod) * 0.7;
870
871         if (IS_DUCKED(this))
872         wishspeed *= 0.5;
873
874 //      if (pmove_waterjumptime <= 0) // TODO: use
875     {
876                 // water friction
877                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this);
878                 f = min(max(0, f), 1);
879                 this.velocity *= f;
880
881                 f = wishspeed - this.velocity * wishdir;
882                 if (f > 0)
883                 {
884                         float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, f);
885                         this.velocity += accelspeed * wishdir;
886                 }
887
888                 // holding jump button swims upward slowly
889                 if (jump && !this.viewloc)
890                 {
891 #if 0
892                         if (this.watertype & CONTENT_LAVA)
893                                 this.velocity_z =  50;
894                         else if (this.watertype & CONTENT_SLIME)
895                                 this.velocity_z =  80;
896                         else
897                         {
898                                 if (IS_NEXUIZ_DERIVED(gamemode))
899 #endif
900                                         this.velocity_z = 200;
901 #if 0
902                                 else
903                                         this.velocity_z = 100;
904                         }
905 #endif
906                 }
907         }
908         if(this.viewloc)
909         {
910                 const float addspeed = wishspeed - this.velocity * wishdir;
911                 if (addspeed > 0)
912                 {
913                         const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
914                         this.velocity += accelspeed * wishdir;
915                 }
916         }
917         else
918         {
919                 // water acceleration
920                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this) * maxspd_mod, 1, 0, 0, 0);
921                 PM_ClientMovement_Move(this);
922         }
923 }
924
925 void PM_ladder(entity this, float maxspd_mod)
926 {
927         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
928         UNSET_ONGROUND(this);
929
930         float g;
931         g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
932         if (PHYS_ENTGRAVITY(this))
933                 g *= PHYS_ENTGRAVITY(this);
934         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
935         {
936                 g *= 0.5;
937                 this.velocity_z += g;
938         }
939
940         this.velocity = this.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this));
941         makevectors(this.v_angle);
942         //wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
943         vector wishvel = v_forward * this.movement_x
944                                         + v_right * this.movement_y
945                                         + '0 0 1' * this.movement_z;
946         this.velocity_z += g;
947         if (this.ladder_entity.classname == "func_water")
948         {
949                 float f = vlen(wishvel);
950                 if (f > this.ladder_entity.speed)
951                         wishvel *= (this.ladder_entity.speed / f);
952
953                 this.watertype = this.ladder_entity.skin;
954                 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
955                 if ((this.origin_z + this.view_ofs_z) < f)
956                         this.waterlevel = WATERLEVEL_SUBMERGED;
957                 else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f)
958                         this.waterlevel = WATERLEVEL_SWIMMING;
959                 else if ((this.origin_z + this.mins_z + 1) < f)
960                         this.waterlevel = WATERLEVEL_WETFEET;
961                 else
962                 {
963                         this.waterlevel = WATERLEVEL_NONE;
964                         this.watertype = CONTENT_EMPTY;
965                 }
966         }
967         // acceleration
968         vector wishdir = normalize(wishvel);
969         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(this) * maxspd_mod);
970         if(time >= PHYS_TELEPORT_TIME(this))
971                 // water acceleration
972                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, PHYS_ACCELERATE(this)*maxspd_mod, 1, 0, 0, 0);
973         PM_ClientMovement_Move(this);
974 }
975
976 void PM_jetpack(entity this, float maxspd_mod)
977 {
978         //makevectors(this.v_angle.y * '0 1 0');
979         makevectors(this.v_angle);
980         vector wishvel = v_forward * this.movement_x
981                                         + v_right * this.movement_y;
982         // add remaining speed as Z component
983         float maxairspd = PHYS_MAXAIRSPEED(this) * max(1, maxspd_mod);
984         // fix speedhacks :P
985         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
986         // add the unused velocity as up component
987         wishvel_z = 0;
988
989         // if (this.BUTTON_JUMP)
990                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
991
992         // it is now normalized, so...
993         float a_side = PHYS_JETPACK_ACCEL_SIDE;
994         float a_up = PHYS_JETPACK_ACCEL_UP;
995         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY(this);
996
997         wishvel_x *= a_side;
998         wishvel_y *= a_side;
999         wishvel_z *= a_up;
1000         wishvel_z += a_add;
1001
1002         float best = 0;
1003         //////////////////////////////////////////////////////////////////////////////////////
1004         // finding the maximum over all vectors of above form
1005         // with wishvel having an absolute value of 1
1006         //////////////////////////////////////////////////////////////////////////////////////
1007         // we're finding the maximum over
1008         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1009         // for z in the range from -1 to 1
1010         //////////////////////////////////////////////////////////////////////////////////////
1011         // maximum is EITHER attained at the single extreme point:
1012         float a_diff = a_side * a_side - a_up * a_up;
1013         float f;
1014         if (a_diff != 0)
1015         {
1016                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1017                 if (f > -1 && f < 1) // can it be attained?
1018                 {
1019                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1020                         //print("middle\n");
1021                 }
1022         }
1023         // OR attained at z = 1:
1024         f = (a_up + a_add) * (a_up + a_add);
1025         if (f > best)
1026         {
1027                 best = f;
1028                 //print("top\n");
1029         }
1030         // OR attained at z = -1:
1031         f = (a_up - a_add) * (a_up - a_add);
1032         if (f > best)
1033         {
1034                 best = f;
1035                 //print("bottom\n");
1036         }
1037         best = sqrt(best);
1038         //////////////////////////////////////////////////////////////////////////////////////
1039
1040         //print("best possible acceleration: ", ftos(best), "\n");
1041
1042         float fxy, fz;
1043         fxy = bound(0, 1 - (this.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1044         if (wishvel_z - PHYS_GRAVITY(this) > 0)
1045                 fz = bound(0, 1 - this.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1046         else
1047                 fz = bound(0, 1 + this.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1048
1049         float fvel;
1050         fvel = vlen(wishvel);
1051         wishvel_x *= fxy;
1052         wishvel_y *= fxy;
1053         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
1054
1055         fvel = min(1, vlen(wishvel) / best);
1056         if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1057                 f = min(1, PHYS_AMMO_FUEL(this) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1058         else
1059                 f = 1;
1060
1061         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1062
1063         if (f > 0 && wishvel != '0 0 0')
1064         {
1065                 this.velocity = this.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1066                 UNSET_ONGROUND(this);
1067
1068 #ifdef SVQC
1069                 if (!(ITEMS_STAT(this) & IT_UNLIMITED_WEAPON_AMMO))
1070                         this.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1071
1072                 ITEMS_STAT(this) |= IT_USING_JETPACK;
1073
1074                 // jetpack also inhibits health regeneration, but only for 1 second
1075                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1076 #endif
1077         }
1078
1079 #ifdef CSQC
1080         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1081         if(autocvar_cl_movement != 3)
1082         {
1083                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1084                         this.velocity_z -= g * 0.5;
1085                 else
1086                         this.velocity_z -= g;
1087         }
1088         PM_ClientMovement_Move(this);
1089         if(autocvar_cl_movement != 3)
1090         {
1091                 if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1092                         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1093                                 this.velocity_z -= g * 0.5;
1094         }
1095 #endif
1096 }
1097
1098 void PM_walk(entity this, float maxspd_mod)
1099 {
1100         if (!WAS_ONGROUND(this))
1101         {
1102 #ifdef SVQC
1103                 if (autocvar_speedmeter)
1104                         LOG_TRACE(strcat("landing velocity: ", vtos(this.velocity), " (abs: ", ftos(vlen(this.velocity)), ")\n"));
1105 #endif
1106                 if (this.lastground < time - 0.3)
1107                         this.velocity *= (1 - PHYS_FRICTION_ONLAND);
1108 #ifdef SVQC
1109                 if (this.jumppadcount > 1)
1110                         LOG_TRACE(strcat(ftos(this.jumppadcount), "x jumppad combo\n"));
1111                 this.jumppadcount = 0;
1112 #endif
1113         }
1114
1115         // walking
1116         makevectors(this.v_angle.y * '0 1 0');
1117         const vector wishvel = v_forward * this.movement.x
1118                                                 + v_right * this.movement.y;
1119         // acceleration
1120         const vector wishdir = normalize(wishvel);
1121         float wishspeed = vlen(wishvel);
1122         wishspeed = min(wishspeed, PHYS_MAXSPEED(this) * maxspd_mod);
1123         if (IS_DUCKED(this)) wishspeed *= 0.5;
1124
1125         // apply edge friction
1126         const float f2 = vlen2(vec2(this.velocity));
1127         if (f2 > 0)
1128         {
1129                 trace_dphitq3surfaceflags = 0;
1130                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
1131                 // TODO: apply edge friction
1132                 // apply ground friction
1133                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1134                         ? PHYS_FRICTION_SLICK
1135                         : PHYS_FRICTION(this);
1136
1137                 float f = sqrt(f2);
1138                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
1139                 f = max(0, f);
1140                 this.velocity *= f;
1141                 /*
1142                    Mathematical analysis time!
1143
1144                    Our goal is to invert this mess.
1145
1146                    For the two cases we get:
1147                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
1148                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1149                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
1150                    and
1151                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1152                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1153
1154                    These cases would be chosen ONLY if:
1155                         v0 < PHYS_STOPSPEED(this)
1156                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
1157                         v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1158                    and, respectively:
1159                         v0 >= PHYS_STOPSPEED(this)
1160                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
1161                         v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
1162                  */
1163         }
1164         const float addspeed = wishspeed - this.velocity * wishdir;
1165         if (addspeed > 0)
1166         {
1167                 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1168                 this.velocity += accelspeed * wishdir;
1169         }
1170 #ifdef CSQC
1171         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1172         if(autocvar_cl_movement != 3)
1173         {
1174                 if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1175                         this.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1176         }
1177         if (vdist(this.velocity, >, 0))
1178                 PM_ClientMovement_Move(this);
1179         if(autocvar_cl_movement != 3)
1180         {
1181                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1182                         if (!IS_ONGROUND(this) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1183                                 this.velocity_z -= g * 0.5;
1184         }
1185 #endif
1186 }
1187
1188 void PM_air(entity this, float buttons_prev, float maxspd_mod)
1189 {
1190         makevectors(this.v_angle.y * '0 1 0');
1191         vector wishvel = v_forward * this.movement.x
1192                                         + v_right * this.movement.y;
1193         // acceleration
1194         vector wishdir = normalize(wishvel);
1195         float wishspeed = vlen(wishvel);
1196
1197 #ifdef SVQC
1198         if(time >= PHYS_TELEPORT_TIME(this))
1199 #elif defined(CSQC)
1200         if(pmove_waterjumptime <= 0)
1201 #endif
1202         {
1203                 float maxairspd = PHYS_MAXAIRSPEED(this) * min(maxspd_mod, 1);
1204
1205                 // apply air speed limit
1206                 float airaccelqw = PHYS_AIRACCEL_QW(this);
1207                 float wishspeed0 = wishspeed;
1208                 wishspeed = min(wishspeed, maxairspd);
1209                 if (IS_DUCKED(this))
1210                         wishspeed *= 0.5;
1211                 float airaccel = PHYS_AIRACCELERATE(this) * min(maxspd_mod, 1);
1212
1213                 float accelerating = (this.velocity * wishdir > 0);
1214                 float wishspeed2 = wishspeed;
1215
1216                 // CPM: air control
1217                 if (PHYS_AIRSTOPACCELERATE(this))
1218                 {
1219                         vector curdir = normalize(vec2(this.velocity));
1220                         airaccel += (PHYS_AIRSTOPACCELERATE(this)*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1221                 }
1222                 // note that for straight forward jumping:
1223                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1224                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1225                 // -->
1226                 // dv/dt = accel * maxspeed (when slow)
1227                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1228                 // log dv/dt = logaccel + logmaxspeed (when slow)
1229                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1230                 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
1231                 if (PHYS_MAXAIRSTRAFESPEED(this))
1232                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(this)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED(this)*maxspd_mod));
1233                 if (PHYS_AIRSTRAFEACCELERATE(this))
1234                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(this)*maxspd_mod);
1235                 if (PHYS_AIRSTRAFEACCEL_QW(this))
1236                         airaccelqw =
1237                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
1238                 *
1239                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
1240                 // !CPM
1241
1242                 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0)
1243                         PM_AirAccelerate(this, wishdir, wishspeed2);
1244                 else
1245                         PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(this), PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(this));
1246
1247                 if (PHYS_AIRCONTROL(this))
1248                         CPM_PM_Aircontrol(this, wishdir, wishspeed2);
1249         }
1250 #ifdef CSQC
1251         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1252         if(autocvar_cl_movement != 3)
1253         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1254                 this.velocity_z -= g * 0.5;
1255         else
1256                 this.velocity_z -= g;
1257 #endif
1258         PM_ClientMovement_Move(this);
1259 #ifdef CSQC
1260         if(autocvar_cl_movement != 3)
1261         if (!IS_ONGROUND(this) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1262                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1263                         this.velocity_z -= g * 0.5;
1264 #endif
1265 }
1266
1267 // used for calculating airshots
1268 bool IsFlying(entity this)
1269 {
1270         if(IS_ONGROUND(this))
1271                 return false;
1272         if(this.waterlevel >= WATERLEVEL_SWIMMING)
1273                 return false;
1274         traceline(this.origin, this.origin - '0 0 48', MOVE_NORMAL, this);
1275         if(trace_fraction < 1)
1276                 return false;
1277         return true;
1278 }
1279
1280 void PM_Main(entity this)
1281 {
1282         int buttons = PHYS_INPUT_BUTTON_MASK(this);
1283 #ifdef CSQC
1284         this.items = STAT(ITEMS);
1285
1286         this.movement = PHYS_INPUT_MOVEVALUES(this);
1287
1288         vector oldv_angle = this.v_angle;
1289         vector oldangles = this.angles; // we need to save these, as they're abused by other code
1290         this.v_angle = PHYS_INPUT_ANGLES(this);
1291         this.angles = PHYS_WORLD_ANGLES(this);
1292
1293         this.team = myteam + 1; // is this correct?
1294         if (!(PHYS_INPUT_BUTTON_JUMP(this))) // !jump
1295                 UNSET_JUMP_HELD(this); // canjump = true
1296         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1297
1298         PM_ClientMovement_UpdateStatus(this, true);
1299 #endif
1300
1301
1302 #ifdef SVQC
1303         WarpZone_PlayerPhysics_FixVAngle();
1304 #endif
1305         float maxspeed_mod = 1;
1306         maxspeed_mod *= PHYS_HIGHSPEED;
1307
1308 #ifdef SVQC
1309         Physics_UpdateStats(this, maxspeed_mod);
1310
1311         if (this.PlayerPhysplug)
1312                 if (this.PlayerPhysplug())
1313                         return;
1314 #endif
1315
1316 #ifdef SVQC
1317         anticheat_physics(this);
1318 #endif
1319
1320         if (PM_check_specialcommand(this, buttons))
1321                 return;
1322 #ifdef SVQC
1323         if (sv_maxidle > 0)
1324         {
1325                 if (buttons != this.buttons_old || this.movement != this.movement_old || this.v_angle != this.v_angle_old)
1326                         this.parm_idlesince = time;
1327         }
1328 #endif
1329         int buttons_prev = this.buttons_old;
1330         this.buttons_old = buttons;
1331         this.movement_old = this.movement;
1332         this.v_angle_old = this.v_angle;
1333
1334         PM_check_nickspam(this);
1335
1336         PM_check_punch(this);
1337 #ifdef SVQC
1338         if (IS_BOT_CLIENT(this))
1339         {
1340                 if (playerdemo_read(this))
1341                         return;
1342                 WITH(entity, self, this, bot_think());
1343         }
1344 #endif
1345
1346 #ifdef SVQC
1347         if (IS_PLAYER(this))
1348         {
1349                 const bool allowed_to_move = (time >= game_starttime);
1350                 if (!allowed_to_move)
1351                 {
1352                         this.velocity = '0 0 0';
1353                         this.movetype = MOVETYPE_NONE;
1354                         this.disableclientprediction = 2;
1355                 }
1356                 else if (this.disableclientprediction == 2)
1357                 {
1358                         if (this.movetype == MOVETYPE_NONE)
1359                                 this.movetype = MOVETYPE_WALK;
1360                         this.disableclientprediction = 0;
1361                 }
1362         }
1363 #endif
1364
1365 #ifdef SVQC
1366         if (this.movetype == MOVETYPE_NONE)
1367                 return;
1368
1369         // when we get here, disableclientprediction cannot be 2
1370         this.disableclientprediction = 0;
1371 #endif
1372
1373         viewloc_PlayerPhysics(this);
1374
1375         PM_check_frozen(this);
1376
1377         PM_check_blocked(this);
1378
1379         maxspeed_mod = 1;
1380
1381         if (this.in_swamp)
1382                 maxspeed_mod *= this.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1383
1384         // conveyors: first fix velocity
1385         if (this.conveyor.state)
1386                 this.velocity -= this.conveyor.movedir;
1387
1388         MUTATOR_CALLHOOK(PlayerPhysics, this);
1389
1390 #ifdef SVQC
1391         if (!IS_PLAYER(this))
1392         {
1393                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1394                 if (!this.spectatorspeed)
1395                         this.spectatorspeed = maxspeed_mod;
1396                 if (this.impulse && this.impulse <= 19 || (this.impulse >= 200 && this.impulse <= 209) || (this.impulse >= 220 && this.impulse <= 229))
1397                 {
1398                         if (this.lastclassname != "player")
1399                         {
1400                                 if (this.impulse == 10 || this.impulse == 15 || this.impulse == 18 || (this.impulse >= 200 && this.impulse <= 209))
1401                                         this.spectatorspeed = bound(1, this.spectatorspeed + 0.5, 5);
1402                                 else if (this.impulse == 11)
1403                                         this.spectatorspeed = maxspeed_mod;
1404                                 else if (this.impulse == 12 || this.impulse == 16  || this.impulse == 19 || (this.impulse >= 220 && this.impulse <= 229))
1405                                         this.spectatorspeed = bound(1, this.spectatorspeed - 0.5, 5);
1406                                 else if (this.impulse >= 1 && this.impulse <= 9)
1407                                         this.spectatorspeed = 1 + 0.5 * (this.impulse - 1);
1408                         } // otherwise just clear
1409                         this.impulse = 0;
1410                 }
1411                 maxspeed_mod = this.spectatorspeed;
1412         }
1413
1414         float spd = max(PHYS_MAXSPEED(this), PHYS_MAXAIRSPEED(this)) * maxspeed_mod;
1415         if(this.speed != spd)
1416         {
1417                 this.speed = spd;
1418                 string temps = ftos(spd);
1419                 stuffcmd(this, strcat("cl_forwardspeed ", temps, "\n"));
1420                 stuffcmd(this, strcat("cl_backspeed ", temps, "\n"));
1421                 stuffcmd(this, strcat("cl_sidespeed ", temps, "\n"));
1422                 stuffcmd(this, strcat("cl_upspeed ", temps, "\n"));
1423         }
1424
1425         if(this.jumpspeedcap_min != autocvar_sv_jumpspeedcap_min)
1426         {
1427                 this.jumpspeedcap_min = autocvar_sv_jumpspeedcap_min;
1428                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_min \"%s\"\n", autocvar_sv_jumpspeedcap_min));
1429         }
1430         if(this.jumpspeedcap_max != autocvar_sv_jumpspeedcap_max)
1431         {
1432                 this.jumpspeedcap_max = autocvar_sv_jumpspeedcap_max;
1433                 stuffcmd(this, sprintf("\ncl_jumpspeedcap_max \"%s\"\n", autocvar_sv_jumpspeedcap_max));
1434         }
1435 #endif
1436
1437         if(PHYS_DEAD(this))
1438         {
1439                 // handle water here
1440                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
1441                 if(pointcontents(midpoint) == CONTENT_WATER)
1442                 {
1443                         this.velocity = this.velocity * 0.5;
1444
1445                         // do we want this?
1446                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1447                                 //{ this.velocity_z = 70; }
1448                 }
1449                 goto end;
1450         }
1451
1452 #ifdef SVQC
1453         if (!this.fixangle)
1454                 this.angles = '0 1 0' * this.v_angle.y;
1455 #endif
1456
1457         if (IS_PLAYER(this) && IS_ONGROUND(this))
1458         {
1459                 PM_check_hitground(this);
1460                 PM_Footsteps(this);
1461         }
1462
1463         if(IsFlying(this))
1464                 this.wasFlying = 1;
1465
1466         if (IS_PLAYER(this))
1467                 CheckPlayerJump(this);
1468
1469         if (this.flags & FL_WATERJUMP)
1470         {
1471                 this.velocity_x = this.movedir.x;
1472                 this.velocity_y = this.movedir.y;
1473                 if (time > PHYS_TELEPORT_TIME(this) || this.waterlevel == WATERLEVEL_NONE)
1474                 {
1475                         this.flags &= ~FL_WATERJUMP;
1476                         PHYS_TELEPORT_TIME(this) = 0;
1477                 }
1478         }
1479
1480         else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod))
1481                 { }
1482
1483 #ifdef SVQC
1484         else if (this.movetype == MOVETYPE_NOCLIP || this.movetype == MOVETYPE_FLY || this.movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1485 #elif defined(CSQC)
1486         else if (this.move_movetype == MOVETYPE_NOCLIP || this.move_movetype == MOVETYPE_FLY || this.move_movetype == MOVETYPE_FLY_WORLDONLY || MUTATOR_CALLHOOK(IsFlying, this))
1487 #endif
1488                 PM_fly(this, maxspeed_mod);
1489
1490         else if (this.waterlevel >= WATERLEVEL_SWIMMING)
1491                 PM_swim(this, maxspeed_mod);
1492
1493         else if (time < this.ladder_time)
1494                 PM_ladder(this, maxspeed_mod);
1495
1496         else if (ITEMS_STAT(this) & IT_USING_JETPACK)
1497                 PM_jetpack(this, maxspeed_mod);
1498
1499         else if (IS_ONGROUND(this))
1500                 PM_walk(this, maxspeed_mod);
1501
1502         else
1503                 PM_air(this, buttons_prev, maxspeed_mod);
1504
1505 :end
1506         if (IS_ONGROUND(this))
1507                 this.lastground = time;
1508
1509         // conveyors: then break velocity again
1510         if(this.conveyor.state)
1511                 this.velocity += this.conveyor.movedir;
1512
1513         this.lastflags = this.flags;
1514
1515         this.lastclassname = this.classname;
1516
1517 #ifdef CSQC
1518         this.v_angle = oldv_angle;
1519         this.angles = oldangles;
1520 #endif
1521 }
1522
1523 #if defined(SVQC)
1524 void SV_PlayerPhysics()
1525 #elif defined(CSQC)
1526 void CSQC_ClientMovement_PlayerMove_Frame(entity this)
1527 #endif
1528 {
1529 #ifdef SVQC
1530         SELFPARAM();
1531 #endif
1532         PM_Main(this);
1533 }