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