]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
f55bf3351cf6edeb34610bcc9aea6fe33b190bc7
[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 pl, string option)
17 {
18         if(Physics_Valid(pl.cvar_cl_physics))
19         {
20                 string s = sprintf("g_physics_%s_%s", pl.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_AddStats()
34 {
35         // static view offset and hitbox vectors
36         // networked for all you bandwidth pigs out there
37         addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
38         addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
39         addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
40         addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
41         addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
42         addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
43
44         addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
45         addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
46         addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
47         addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
48         addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
49         addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
50         addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
51         addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
52         addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
53         addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
54         addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
55         addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
56 }
57
58 void Physics_UpdateStats(entity this, float maxspd_mod)
59 {
60         // blah
61         this.stat_pl_view_ofs = PL_VIEW_OFS;
62         this.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
63
64         this.stat_pl_min = PL_MIN;
65         this.stat_pl_max = PL_MAX;
66         this.stat_pl_crouch_min = PL_CROUCH_MIN;
67         this.stat_pl_crouch_max = PL_CROUCH_MAX;
68
69         STAT(MOVEVARS_AIRACCEL_QW, this) = AdjustAirAccelQW(Physics_ClientOption(this, "airaccel_qw"), maxspd_mod);
70         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(this, "airstrafeaccel_qw"))
71                 ? AdjustAirAccelQW(Physics_ClientOption(this, "airstrafeaccel_qw"), maxspd_mod)
72                 : 0;
73         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(this, "airspeedlimit_nonqw") * maxspd_mod;
74         STAT(MOVEVARS_MAXSPEED, this) = Physics_ClientOption(this, "maxspeed") * maxspd_mod; // also slow walking
75
76         // old stats
77         // fix some new settings
78         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(this, "airaccel_qw_stretchfactor");
79         STAT(MOVEVARS_MAXAIRSTRAFESPEED, this) = Physics_ClientOption(this, "maxairstrafespeed");
80         STAT(MOVEVARS_MAXAIRSPEED, this) = Physics_ClientOption(this, "maxairspeed");
81         STAT(MOVEVARS_AIRSTRAFEACCELERATE, this) = Physics_ClientOption(this, "airstrafeaccelerate");
82         STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, this) = Physics_ClientOption(this, "warsowbunny_turnaccel");
83         STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, this) = Physics_ClientOption(this, "airaccel_sideways_friction");
84         STAT(MOVEVARS_AIRCONTROL, this) = Physics_ClientOption(this, "aircontrol");
85         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(this, "aircontrol_power");
86         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(this, "aircontrol_penalty");
87         STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, this) = Physics_ClientOption(this, "warsowbunny_airforwardaccel");
88         STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, this) = Physics_ClientOption(this, "warsowbunny_topspeed");
89         STAT(MOVEVARS_WARSOWBUNNY_ACCEL, this) = Physics_ClientOption(this, "warsowbunny_accel");
90         STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, this) = Physics_ClientOption(this, "warsowbunny_backtosideratio");
91         STAT(MOVEVARS_FRICTION, this) = Physics_ClientOption(this, "friction");
92         STAT(MOVEVARS_ACCELERATE, this) = Physics_ClientOption(this, "accelerate");
93         STAT(MOVEVARS_STOPSPEED, this) = Physics_ClientOption(this, "stopspeed");
94         STAT(MOVEVARS_AIRACCELERATE, this) = Physics_ClientOption(this, "airaccelerate");
95         STAT(MOVEVARS_AIRSTOPACCELERATE, this) = Physics_ClientOption(this, "airstopaccelerate");
96         STAT(MOVEVARS_JUMPVELOCITY, this) = Physics_ClientOption(this, "jumpvelocity");
97         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(this, "track_canjump");
98 }
99 #endif
100
101 float IsMoveInDirection(vector mv, float ang) // key mix factor
102 {
103         if (mv_x == 0 && mv_y == 0)
104                 return 0; // avoid division by zero
105         ang -= RAD2DEG * atan2(mv_y, mv_x);
106         ang = remainder(ang, 360) / 45;
107         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
108 }
109
110 float GeomLerp(float a, float lerp, float b)
111 {
112         return a == 0 ? (lerp < 1 ? 0 : b)
113                 : b == 0 ? (lerp > 0 ? 0 : a)
114                 : a * pow(fabs(b / a), lerp);
115 }
116
117 noref float pmove_waterjumptime;
118
119 const float unstick_count = 27;
120 vector unstick_offsets[unstick_count] =
121 {
122 // 1 no nudge (just return the original if this test passes)
123         '0.000   0.000  0.000',
124 // 6 simple nudges
125         ' 0.000  0.000  0.125', '0.000  0.000 -0.125',
126         '-0.125  0.000  0.000', '0.125  0.000  0.000',
127         ' 0.000 -0.125  0.000', '0.000  0.125  0.000',
128 // 4 diagonal flat nudges
129         '-0.125 -0.125  0.000', '0.125 -0.125  0.000',
130         '-0.125  0.125  0.000', '0.125  0.125  0.000',
131 // 8 diagonal upward nudges
132         '-0.125  0.000  0.125', '0.125  0.000  0.125',
133         ' 0.000 -0.125  0.125', '0.000  0.125  0.125',
134         '-0.125 -0.125  0.125', '0.125 -0.125  0.125',
135         '-0.125  0.125  0.125', '0.125  0.125  0.125',
136 // 8 diagonal downward nudges
137         '-0.125  0.000 -0.125', '0.125  0.000 -0.125',
138         ' 0.000 -0.125 -0.125', '0.000  0.125 -0.125',
139         '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
140         '-0.125  0.125 -0.125', '0.125  0.125 -0.125',
141 };
142
143 void PM_ClientMovement_Unstick()
144 {SELFPARAM();
145         float i;
146         for (i = 0; i < unstick_count; i++)
147         {
148                 vector neworigin = unstick_offsets[i] + self.origin;
149                 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
150                 if (!trace_startsolid)
151                 {
152                         setorigin(self, neworigin);
153                         return;// true;
154                 }
155         }
156 }
157
158 void PM_ClientMovement_UpdateStatus(bool ground)
159 {SELFPARAM();
160         // make sure player is not stuck
161         PM_ClientMovement_Unstick();
162
163         // set crouched
164         if (PHYS_INPUT_BUTTON_CROUCH(self))
165         {
166                 // wants to crouch, this always works..
167                 if (!IS_DUCKED(self))
168                         SET_DUCKED(self);
169         }
170         else
171         {
172                 // wants to stand, if currently crouching we need to check for a
173                 // low ceiling first
174                 if (IS_DUCKED(self))
175                 {
176                         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
177                         if (!trace_startsolid)
178                                 UNSET_DUCKED(self);
179                 }
180         }
181
182         // set onground
183         vector origin1 = self.origin + '0 0 1';
184         vector origin2 = self.origin - '0 0 1';
185
186         if(ground)
187         {
188                 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
189                 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
190                 {
191                         SET_ONGROUND(self);
192
193                         // this code actually "predicts" an impact; so let's clip velocity first
194                         float f = self.velocity * trace_plane_normal;
195                         self.velocity -= f * trace_plane_normal;
196                 }
197                 else
198                         UNSET_ONGROUND(self);
199         }
200
201         // set watertype/waterlevel
202         origin1 = self.origin;
203         origin1_z += self.mins_z + 1;
204         self.waterlevel = WATERLEVEL_NONE;
205
206         int thepoint = pointcontents(origin1);
207
208         self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
209
210         if(self.watertype)
211         {
212                 self.waterlevel = WATERLEVEL_WETFEET;
213                 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
214                 thepoint = pointcontents(origin1);
215                 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
216                 {
217                         self.waterlevel = WATERLEVEL_SWIMMING;
218                         origin1_z = self.origin_z + 22;
219                         thepoint = pointcontents(origin1);
220                         if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
221                                 self.waterlevel = WATERLEVEL_SUBMERGED;
222                 }
223         }
224
225         if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
226                 pmove_waterjumptime = 0;
227 }
228
229 void PM_ClientMovement_Move()
230 {SELFPARAM();
231 #ifdef CSQC
232         int bump;
233         float t;
234         float f;
235         vector neworigin;
236         vector currentorigin2;
237         vector neworigin2;
238         vector primalvelocity;
239
240         vector trace1_endpos = '0 0 0';
241         vector trace2_endpos = '0 0 0';
242         vector trace3_endpos = '0 0 0';
243         float trace1_fraction = 0;
244         float trace2_fraction = 0;
245         float trace3_fraction = 0;
246         vector trace1_plane_normal = '0 0 0';
247         vector trace2_plane_normal = '0 0 0';
248         vector trace3_plane_normal = '0 0 0';
249
250
251         PM_ClientMovement_UpdateStatus(false);
252         primalvelocity = self.velocity;
253         for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
254         {
255                 neworigin = self.origin + t * self.velocity;
256                 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
257                 trace1_endpos = trace_endpos;
258                 trace1_fraction = trace_fraction;
259                 trace1_plane_normal = trace_plane_normal;
260                 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
261                 {
262                         // may be a step or wall, try stepping up
263                         // first move forward at a higher level
264                         currentorigin2 = self.origin;
265                         currentorigin2_z += PHYS_STEPHEIGHT;
266                         neworigin2 = neworigin;
267                         neworigin2_z += PHYS_STEPHEIGHT;
268                         tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
269                         trace2_endpos = trace_endpos;
270                         trace2_fraction = trace_fraction;
271                         trace2_plane_normal = trace_plane_normal;
272                         if(!trace_startsolid)
273                         {
274                                 // then move down from there
275                                 currentorigin2 = trace2_endpos;
276                                 neworigin2 = trace2_endpos;
277                                 neworigin2_z = self.origin_z;
278                                 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
279                                 trace3_endpos = trace_endpos;
280                                 trace3_fraction = trace_fraction;
281                                 trace3_plane_normal = trace_plane_normal;
282                                 // accept the new trace if it made some progress
283                                 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
284                                 {
285                                         trace1_endpos = trace2_endpos;
286                                         trace1_fraction = trace2_fraction;
287                                         trace1_plane_normal = trace2_plane_normal;
288                                         trace1_endpos = trace3_endpos;
289                                 }
290                         }
291                 }
292
293                 // check if it moved at all
294                 if(trace1_fraction >= 0.001)
295                         setorigin(self, trace1_endpos);
296
297                 // check if it moved all the way
298                 if(trace1_fraction == 1)
299                         break;
300
301                 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
302                 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
303                 // this got commented out in a change that supposedly makes the code match QW better
304                 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
305                 if(trace1_plane_normal_z > 0.7)
306                         SET_ONGROUND(self);
307
308                 t -= t * trace1_fraction;
309
310                 f = (self.velocity * trace1_plane_normal);
311                 self.velocity = self.velocity + -f * trace1_plane_normal;
312         }
313         if(pmove_waterjumptime > 0)
314                 self.velocity = primalvelocity;
315 #endif
316 }
317
318 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
319 {SELFPARAM();
320         float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
321         if (k <= 0)
322                 return;
323
324         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(self), 1);
325
326         float zspeed = self.velocity_z;
327         self.velocity_z = 0;
328         float xyspeed = vlen(self.velocity);
329         self.velocity = normalize(self.velocity);
330
331         float dot = self.velocity * wishdir;
332
333         if (dot > 0) // we can't change direction while slowing down
334         {
335                 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
336                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
337                 k *= PHYS_AIRCONTROL;
338                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
339         }
340
341         self.velocity = self.velocity * xyspeed;
342         self.velocity_z = zspeed;
343 }
344
345 float AdjustAirAccelQW(float accelqw, float factor)
346 {
347         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
348 }
349
350 // example config for alternate speed clamping:
351 //   sv_airaccel_qw 0.8
352 //   sv_airaccel_sideways_friction 0
353 //   prvm_globalset server speedclamp_mode 1
354 //     (or 2)
355 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
356 {SELFPARAM();
357         float speedclamp = stretchfactor > 0 ? stretchfactor
358         : accelqw < 0 ? 1 // full clamping, no stretch
359         : -1; // no clamping
360
361         accelqw = fabs(accelqw);
362
363         if (GAMEPLAYFIX_Q2AIRACCELERATE)
364                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
365
366         float vel_straight = self.velocity * wishdir;
367         float vel_z = self.velocity_z;
368         vector vel_xy = vec2(self.velocity);
369         vector vel_perpend = vel_xy - vel_straight * wishdir;
370
371         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
372
373         float vel_xy_current  = vlen(vel_xy);
374         if (speedlimit)
375                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
376         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
377         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
378         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
379         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
380
381         if (sidefric < 0 && (vel_perpend*vel_perpend))
382                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
383         {
384                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
385                 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
386                 // assume: fmin > 1
387                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
388                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
389                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
390                 // obviously, this cannot be
391                 if (fmin <= 0)
392                         vel_perpend *= f;
393                 else
394                 {
395                         fmin = sqrt(fmin);
396                         vel_perpend *= max(fmin, f);
397                 }
398         }
399         else
400                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
401
402         vel_xy = vel_straight * wishdir + vel_perpend;
403
404         if (speedclamp >= 0)
405         {
406                 float vel_xy_preclamp;
407                 vel_xy_preclamp = vlen(vel_xy);
408                 if (vel_xy_preclamp > 0) // prevent division by zero
409                 {
410                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
411                         if (vel_xy_current < vel_xy_preclamp)
412                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
413                 }
414         }
415
416         self.velocity = vel_xy + vel_z * '0 0 1';
417 }
418
419 void PM_AirAccelerate(vector wishdir, float wishspeed)
420 {SELFPARAM();
421         if (wishspeed == 0)
422                 return;
423
424         vector curvel = self.velocity;
425         curvel_z = 0;
426         float curspeed = vlen(curvel);
427
428         if (wishspeed > curspeed * 1.01)
429                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
430         else
431         {
432                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
433                 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
434         }
435         vector wishvel = wishdir * wishspeed;
436         vector acceldir = wishvel - curvel;
437         float addspeed = vlen(acceldir);
438         acceldir = normalize(acceldir);
439
440         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
441
442         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
443         {
444                 vector curdir = normalize(curvel);
445                 float dot = acceldir * curdir;
446                 if (dot < 0)
447                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
448         }
449
450         self.velocity += accelspeed * acceldir;
451 }
452
453
454 /*
455 =============
456 PlayerJump
457
458 When you press the jump key
459 returns true if handled
460 =============
461 */
462 bool PlayerJump ()
463 {SELFPARAM();
464         if (PHYS_FROZEN(self))
465                 return true; // no jumping in freezetag when frozen
466
467 #ifdef SVQC
468         if (self.player_blocked)
469                 return true; // no jumping while blocked
470 #endif
471
472         bool doublejump = false;
473         float mjumpheight = PHYS_JUMPVELOCITY;
474 #ifdef CSQC
475         player_multijump = doublejump;
476         player_jumpheight = mjumpheight;
477 #endif
478
479         if (MUTATOR_CALLHOOK(PlayerJump, doublejump, mjumpheight)
480 #ifdef CSQC
481                 || PM_multijump_checkjump()
482 #endif
483                 ) { return true; }
484
485         doublejump = player_multijump;
486         mjumpheight = player_jumpheight;
487
488         if (PHYS_DOUBLEJUMP)
489         {
490                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
491                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
492                 {
493                         doublejump = true;
494
495                         // we MUST clip velocity here!
496                         float f;
497                         f = self.velocity * trace_plane_normal;
498                         if (f < 0)
499                                 self.velocity -= f * trace_plane_normal;
500                 }
501         }
502
503         if (self.waterlevel >= WATERLEVEL_SWIMMING)
504         {
505                 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
506                 return true;
507         }
508
509         if (!doublejump)
510                 if (!IS_ONGROUND(self))
511                         return IS_JUMP_HELD(self);
512
513         bool track_jump = PHYS_CL_TRACK_CANJUMP(self);
514         if(PHYS_TRACK_CANJUMP(self))
515                 track_jump = true;
516
517         if (track_jump)
518                 if (IS_JUMP_HELD(self))
519                         return true;
520
521         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
522         // velocity bounds.  Final velocity is bound between (jumpheight *
523         // min + jumpheight) and (jumpheight * max + jumpheight);
524
525         if(PHYS_JUMPSPEEDCAP_MIN != "")
526         {
527                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
528
529                 if (self.velocity_z < minjumpspeed)
530                         mjumpheight += minjumpspeed - self.velocity_z;
531         }
532
533         if(PHYS_JUMPSPEEDCAP_MAX != "")
534         {
535                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
536                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
537
538                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
539                 {
540                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
541
542                         if (self.velocity_z > maxjumpspeed)
543                                 mjumpheight -= self.velocity_z - maxjumpspeed;
544                 }
545         }
546
547         if (!WAS_ONGROUND(self))
548         {
549 #ifdef SVQC
550                 if(autocvar_speedmeter)
551                         LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
552 #endif
553                 if(self.lastground < time - 0.3)
554                 {
555                         self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
556                         self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
557                 }
558 #ifdef SVQC
559                 if(self.jumppadcount > 1)
560                         LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
561                 self.jumppadcount = 0;
562 #endif
563         }
564
565         self.velocity_z += mjumpheight;
566
567         UNSET_ONGROUND(self);
568         SET_JUMP_HELD(self);
569
570 #ifdef SVQC
571
572         self.oldvelocity_z = self.velocity_z;
573
574         animdecide_setaction(self, ANIMACTION_JUMP, true);
575
576         if (autocvar_g_jump_grunt)
577                 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
578 #endif
579         return true;
580 }
581
582 void CheckWaterJump()
583 {SELFPARAM();
584 // check for a jump-out-of-water
585         makevectors(self.v_angle);
586         vector start = self.origin;
587         start_z += 8;
588         v_forward_z = 0;
589         normalize(v_forward);
590         vector end = start + v_forward*24;
591         traceline (start, end, true, self);
592         if (trace_fraction < 1)
593         {       // solid at waist
594                 start_z = start_z + self.maxs_z - 8;
595                 end = start + v_forward*24;
596                 self.movedir = trace_plane_normal * -50;
597                 traceline(start, end, true, self);
598                 if (trace_fraction == 1)
599                 {       // open at eye level
600                         self.velocity_z = 225;
601                         self.flags |= FL_WATERJUMP;
602                         SET_JUMP_HELD(self);
603 #ifdef SVQC
604                         self.teleport_time = time + 2;  // safety net
605 #elif defined(CSQC)
606                         pmove_waterjumptime = time + 2;
607 #endif
608                 }
609         }
610 }
611
612
613 #ifdef SVQC
614         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
615 #elif defined(CSQC)
616         float autocvar_cl_jetpack_jump;
617         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
618 #endif
619 .float jetpack_stopped;
620 // Hack: shouldn't need to know about this
621 .float multijump_count;
622 void CheckPlayerJump()
623 {SELFPARAM();
624 #ifdef SVQC
625         float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
626 #endif
627         if (JETPACK_JUMP(self) < 2)
628                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
629
630         if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
631         {
632                 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
633                 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
634                 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
635
636                 if (!(ITEMS_STAT(self) & ITEM_Jetpack.m_itemid)) { }
637                 else if (self.jetpack_stopped) { }
638                 else if (!has_fuel)
639                 {
640 #ifdef SVQC
641                         if (was_flying) // TODO: ran out of fuel message
642                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
643                         else if (activate)
644                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
645 #endif
646                         self.jetpack_stopped = true;
647                         ITEMS_STAT(self) &= ~IT_USING_JETPACK;
648                 }
649                 else if (activate && !PHYS_FROZEN(self))
650                         ITEMS_STAT(self) |= IT_USING_JETPACK;
651         }
652         else
653         {
654                 self.jetpack_stopped = false;
655                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
656         }
657         if (!PHYS_INPUT_BUTTON_JUMP(self))
658                 UNSET_JUMP_HELD(self);
659
660         if (self.waterlevel == WATERLEVEL_SWIMMING)
661                 CheckWaterJump();
662 }
663
664 float racecar_angle(float forward, float down)
665 {
666         if (forward < 0)
667         {
668                 forward = -forward;
669                 down = -down;
670         }
671
672         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
673
674         float angle_mult = forward / (800 + forward);
675
676         if (ret > 180)
677                 return ret * angle_mult + 360 * (1 - angle_mult);
678         else
679                 return ret * angle_mult;
680 }
681
682 void RaceCarPhysics()
683 {SELFPARAM();
684 #ifdef SVQC
685         // using this move type for "big rigs"
686         // the engine does not push the entity!
687
688         vector rigvel;
689
690         vector angles_save = self.angles;
691         float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
692         float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
693
694         if (g_bugrigs_reverse_speeding)
695         {
696                 if (accel < 0)
697                 {
698                         // back accel is DIGITAL
699                         // to prevent speedhack
700                         if (accel < -0.5)
701                                 accel = -1;
702                         else
703                                 accel = 0;
704                 }
705         }
706
707         self.angles_x = 0;
708         self.angles_z = 0;
709         makevectors(self.angles); // new forward direction!
710
711         if (IS_ONGROUND(self) || g_bugrigs_air_steering)
712         {
713                 float myspeed = self.velocity * v_forward;
714                 float upspeed = self.velocity * v_up;
715
716                 // responsiveness factor for steering and acceleration
717                 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
718                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
719
720                 float steerfactor;
721                 if (myspeed < 0 && g_bugrigs_reverse_spinning)
722                         steerfactor = -myspeed * g_bugrigs_steer;
723                 else
724                         steerfactor = -myspeed * f * g_bugrigs_steer;
725
726                 float accelfactor;
727                 if (myspeed < 0 && g_bugrigs_reverse_speeding)
728                         accelfactor = g_bugrigs_accel;
729                 else
730                         accelfactor = f * g_bugrigs_accel;
731                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
732
733                 if (accel < 0)
734                 {
735                         if (myspeed > 0)
736                         {
737                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
738                         }
739                         else
740                         {
741                                 if (!g_bugrigs_reverse_speeding)
742                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
743                         }
744                 }
745                 else
746                 {
747                         if (myspeed >= 0)
748                         {
749                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
750                         }
751                         else
752                         {
753                                 if (g_bugrigs_reverse_stopping)
754                                         myspeed = 0;
755                                 else
756                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
757                         }
758                 }
759                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
760                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
761
762                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
763                 makevectors(self.angles); // new forward direction!
764
765                 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
766
767                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
768         }
769         else
770         {
771                 float myspeed = vlen(self.velocity);
772
773                 // responsiveness factor for steering and acceleration
774                 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
775                 float steerfactor = -myspeed * f;
776                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
777
778                 rigvel = self.velocity;
779                 makevectors(self.angles); // new forward direction!
780         }
781
782         rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
783         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
784         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
785         //MAXIMA: solve(total_acceleration(v) = 0, v);
786
787         if (g_bugrigs_planar_movement)
788         {
789                 vector rigvel_xy, neworigin, up;
790                 float mt;
791
792                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY(this); // 4x gravity plays better
793                 rigvel_xy = vec2(rigvel);
794
795                 if (g_bugrigs_planar_movement_car_jumping)
796                         mt = MOVE_NORMAL;
797                 else
798                         mt = MOVE_NOMONSTERS;
799
800                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
801                 up = trace_endpos - self.origin;
802
803                 // BUG RIGS: align the move to the surface instead of doing collision testing
804                 // can we move?
805                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
806
807                 // align to surface
808                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
809
810                 if (trace_fraction < 0.5)
811                 {
812                         trace_fraction = 1;
813                         neworigin = self.origin;
814                 }
815                 else
816                         neworigin = trace_endpos;
817
818                 if (trace_fraction < 1)
819                 {
820                         // now set angles_x so that the car points parallel to the surface
821                         self.angles = vectoangles(
822                                         '1 0 0' * v_forward_x * trace_plane_normal_z
823                                         +
824                                         '0 1 0' * v_forward_y * trace_plane_normal_z
825                                         +
826                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
827                                         );
828                         SET_ONGROUND(self);
829                 }
830                 else
831                 {
832                         // now set angles_x so that the car points forward, but is tilted in velocity direction
833                         UNSET_ONGROUND(self);
834                 }
835
836                 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
837                 self.movetype = MOVETYPE_NOCLIP;
838         }
839         else
840         {
841                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY(this); // 4x gravity plays better
842                 self.velocity = rigvel;
843                 self.movetype = MOVETYPE_FLY;
844         }
845
846         trace_fraction = 1;
847         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
848         if (trace_fraction != 1)
849         {
850                 self.angles = vectoangles2(
851                                 '1 0 0' * v_forward_x * trace_plane_normal_z
852                                 +
853                                 '0 1 0' * v_forward_y * trace_plane_normal_z
854                                 +
855                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
856                                 trace_plane_normal
857                                 );
858         }
859         else
860         {
861                 vector vel_local;
862
863                 vel_local_x = v_forward * self.velocity;
864                 vel_local_y = v_right * self.velocity;
865                 vel_local_z = v_up * self.velocity;
866
867                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
868                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
869         }
870
871         // smooth the angles
872         vector vf1, vu1, smoothangles;
873         makevectors(self.angles);
874         float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
875         if (f == 0)
876                 f = 1;
877         vf1 = v_forward * f;
878         vu1 = v_up * f;
879         makevectors(angles_save);
880         vf1 = vf1 + v_forward * (1 - f);
881         vu1 = vu1 + v_up * (1 - f);
882         smoothangles = vectoangles2(vf1, vu1);
883         self.angles_x = -smoothangles_x;
884         self.angles_z =  smoothangles_z;
885 #endif
886 }
887
888 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
889 .float specialcommand_pos;
890 void SpecialCommand()
891 {
892 #ifdef SVQC
893 #ifdef TETRIS
894         TetrisImpulse();
895 #else
896         if (!CheatImpulse(99))
897                 LOG_INFO("A hollow voice says \"Plugh\".\n");
898 #endif
899 #endif
900 }
901
902 float PM_check_specialcommand(float buttons)
903 {SELFPARAM();
904 #ifdef SVQC
905         string c;
906         if (!buttons)
907                 c = "x";
908         else if (buttons == 1)
909                 c = "1";
910         else if (buttons == 2)
911                 c = " ";
912         else if (buttons == 128)
913                 c = "s";
914         else if (buttons == 256)
915                 c = "w";
916         else if (buttons == 512)
917                 c = "a";
918         else if (buttons == 1024)
919                 c = "d";
920         else
921                 c = "?";
922
923         if (c == substring(specialcommand, self.specialcommand_pos, 1))
924         {
925                 self.specialcommand_pos += 1;
926                 if (self.specialcommand_pos >= strlen(specialcommand))
927                 {
928                         self.specialcommand_pos = 0;
929                         SpecialCommand();
930                         return true;
931                 }
932         }
933         else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
934                 self.specialcommand_pos = 0;
935 #endif
936         return false;
937 }
938
939 void PM_check_nickspam()
940 {SELFPARAM();
941 #ifdef SVQC
942         if (time >= self.nickspamtime)
943                 return;
944         if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
945         {
946                 // slight annoyance for nick change scripts
947                 self.movement = -1 * self.movement;
948                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
949
950                 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
951                 {
952                         self.v_angle_x = random() * 360;
953                         self.v_angle_y = random() * 360;
954                         // at least I'm not forcing retardedview by also assigning to angles_z
955                         self.fixangle = true;
956                 }
957         }
958 #endif
959 }
960
961 void PM_check_punch()
962 {SELFPARAM();
963 #ifdef SVQC
964         if (self.punchangle != '0 0 0')
965         {
966                 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
967                 if (f > 0)
968                         self.punchangle = normalize(self.punchangle) * f;
969                 else
970                         self.punchangle = '0 0 0';
971         }
972
973         if (self.punchvector != '0 0 0')
974         {
975                 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
976                 if (f > 0)
977                         self.punchvector = normalize(self.punchvector) * f;
978                 else
979                         self.punchvector = '0 0 0';
980         }
981 #endif
982 }
983
984 void PM_check_spider()
985 {SELFPARAM();
986 #ifdef SVQC
987         if (time >= self.spider_slowness)
988                 return;
989         PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
990         PHYS_MAXAIRSPEED(self) *= 0.5;
991         PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
992         PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
993 #endif
994 }
995
996 // predict frozen movement, as frozen players CAN move in some cases
997 void PM_check_frozen()
998 {SELFPARAM();
999         if (!PHYS_FROZEN(self))
1000                 return;
1001         if (PHYS_DODGING_FROZEN
1002 #ifdef SVQC
1003         && IS_REAL_CLIENT(self)
1004 #endif
1005         )
1006         {
1007                 self.movement_x = bound(-5, self.movement.x, 5);
1008                 self.movement_y = bound(-5, self.movement.y, 5);
1009                 self.movement_z = bound(-5, self.movement.z, 5);
1010         }
1011         else
1012                 self.movement = '0 0 0';
1013
1014         vector midpoint = ((self.absmin + self.absmax) * 0.5);
1015         if (pointcontents(midpoint) == CONTENT_WATER)
1016         {
1017                 self.velocity = self.velocity * 0.5;
1018
1019                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1020                         self.velocity_z = 200;
1021         }
1022 }
1023
1024 void PM_check_hitground()
1025 {SELFPARAM();
1026 #ifdef SVQC
1027         if (IS_ONGROUND(self))
1028         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1029         if (self.wasFlying)
1030         {
1031                 self.wasFlying = 0;
1032                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1033                 if (time >= self.ladder_time)
1034                 if (!self.hook)
1035                 {
1036                         self.nextstep = time + 0.3 + random() * 0.1;
1037                         trace_dphitq3surfaceflags = 0;
1038                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1039                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1040                         {
1041                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1042                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1043                                 else
1044                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1045                         }
1046                 }
1047         }
1048 #endif
1049 }
1050
1051 void PM_check_blocked()
1052 {SELFPARAM();
1053 #ifdef SVQC
1054         if (!self.player_blocked)
1055                 return;
1056         self.movement = '0 0 0';
1057         self.disableclientprediction = 1;
1058 #endif
1059 }
1060
1061 void PM_check_vortex()
1062 {SELFPARAM();
1063 #ifdef SVQC
1064         // WEAPONTODO
1065         float xyspeed = vlen(vec2(self.velocity));
1066         if (self.weapon == WEP_VORTEX.m_id && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1067         {
1068                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1069                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1070                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1071                 // add the extra charge
1072                 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1073         }
1074 #endif
1075 }
1076
1077 void PM_fly(float maxspd_mod)
1078 {SELFPARAM();
1079         // noclipping or flying
1080         UNSET_ONGROUND(self);
1081
1082         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1083         makevectors(self.v_angle);
1084         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1085         vector wishvel = v_forward * self.movement.x
1086                                         + v_right * self.movement.y
1087                                         + '0 0 1' * self.movement.z;
1088         // acceleration
1089         vector wishdir = normalize(wishvel);
1090         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1091 #ifdef SVQC
1092         if (time >= self.teleport_time)
1093 #endif
1094                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1095         PM_ClientMovement_Move();
1096 }
1097
1098 void PM_swim(float maxspd_mod)
1099 {SELFPARAM();
1100         // swimming
1101         UNSET_ONGROUND(self);
1102
1103         float jump = PHYS_INPUT_BUTTON_JUMP(self);
1104         // water jump only in certain situations
1105         // this mimics quakeworld code
1106         if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1107         {
1108                 vector yawangles = '0 1 0' * self.v_angle.y;
1109                 makevectors(yawangles);
1110                 vector forward = v_forward;
1111                 vector spot = self.origin + 24 * forward;
1112                 spot_z += 8;
1113                 traceline(spot, spot, MOVE_NOMONSTERS, self);
1114                 if (trace_startsolid)
1115                 {
1116                         spot_z += 24;
1117                         traceline(spot, spot, MOVE_NOMONSTERS, self);
1118                         if (!trace_startsolid)
1119                         {
1120                                 self.velocity = forward * 50;
1121                                 self.velocity_z = 310;
1122                                 pmove_waterjumptime = 2;
1123                                 UNSET_ONGROUND(self);
1124                                 SET_JUMP_HELD(self);
1125                         }
1126                 }
1127         }
1128         makevectors(self.v_angle);
1129         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1130         vector wishvel = v_forward * self.movement.x
1131                                         + v_right * self.movement.y
1132                                         + '0 0 1' * self.movement.z;
1133         if (wishvel == '0 0 0')
1134                 wishvel = '0 0 -60'; // drift towards bottom
1135
1136         vector wishdir = normalize(wishvel);
1137         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1138
1139         if (IS_DUCKED(self))
1140         wishspeed *= 0.5;
1141
1142 //      if (pmove_waterjumptime <= 0) // TODO: use
1143     {
1144                 // water friction
1145                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1146                 f = min(max(0, f), 1);
1147                 self.velocity *= f;
1148
1149                 f = wishspeed - self.velocity * wishdir;
1150                 if (f > 0)
1151                 {
1152                         float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1153                         self.velocity += accelspeed * wishdir;
1154                 }
1155
1156                 // holding jump button swims upward slowly
1157                 if (jump)
1158                 {
1159 #if 0
1160                         if (self.watertype & CONTENT_LAVA)
1161                                 self.velocity_z =  50;
1162                         else if (self.watertype & CONTENT_SLIME)
1163                                 self.velocity_z =  80;
1164                         else
1165                         {
1166                                 if (IS_NEXUIZ_DERIVED(gamemode))
1167 #endif
1168                                         self.velocity_z = 200;
1169 #if 0
1170                                 else
1171                                         self.velocity_z = 100;
1172                         }
1173 #endif
1174                 }
1175         }
1176         // water acceleration
1177         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1178         PM_ClientMovement_Move();
1179 }
1180
1181 void PM_ladder(float maxspd_mod)
1182 {SELFPARAM();
1183         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1184         UNSET_ONGROUND(self);
1185
1186         float g;
1187         g = PHYS_GRAVITY(this) * PHYS_INPUT_TIMELENGTH;
1188         if (PHYS_ENTGRAVITY(self))
1189                 g *= PHYS_ENTGRAVITY(self);
1190         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1191         {
1192                 g *= 0.5;
1193                 self.velocity_z += g;
1194         }
1195
1196         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1197         makevectors(self.v_angle);
1198         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1199         vector wishvel = v_forward * self.movement_x
1200                                         + v_right * self.movement_y
1201                                         + '0 0 1' * self.movement_z;
1202         self.velocity_z += g;
1203         if (self.ladder_entity.classname == "func_water")
1204         {
1205                 float f = vlen(wishvel);
1206                 if (f > self.ladder_entity.speed)
1207                         wishvel *= (self.ladder_entity.speed / f);
1208
1209                 self.watertype = self.ladder_entity.skin;
1210                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1211                 if ((self.origin_z + self.view_ofs_z) < f)
1212                         self.waterlevel = WATERLEVEL_SUBMERGED;
1213                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1214                         self.waterlevel = WATERLEVEL_SWIMMING;
1215                 else if ((self.origin_z + self.mins_z + 1) < f)
1216                         self.waterlevel = WATERLEVEL_WETFEET;
1217                 else
1218                 {
1219                         self.waterlevel = WATERLEVEL_NONE;
1220                         self.watertype = CONTENT_EMPTY;
1221                 }
1222         }
1223         // acceleration
1224         vector wishdir = normalize(wishvel);
1225         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1226 #ifdef SVQC
1227         if (time >= self.teleport_time)
1228 #endif
1229                 // water acceleration
1230                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1231         PM_ClientMovement_Move();
1232 }
1233
1234 void PM_jetpack(float maxspd_mod)
1235 {SELFPARAM();
1236         //makevectors(self.v_angle.y * '0 1 0');
1237         makevectors(self.v_angle);
1238         vector wishvel = v_forward * self.movement_x
1239                                         + v_right * self.movement_y;
1240         // add remaining speed as Z component
1241         float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1242         // fix speedhacks :P
1243         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1244         // add the unused velocity as up component
1245         wishvel_z = 0;
1246
1247         // if (self.BUTTON_JUMP)
1248                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1249
1250         // it is now normalized, so...
1251         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1252         float a_up = PHYS_JETPACK_ACCEL_UP;
1253         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY(this);
1254
1255         wishvel_x *= a_side;
1256         wishvel_y *= a_side;
1257         wishvel_z *= a_up;
1258         wishvel_z += a_add;
1259
1260         float best = 0;
1261         //////////////////////////////////////////////////////////////////////////////////////
1262         // finding the maximum over all vectors of above form
1263         // with wishvel having an absolute value of 1
1264         //////////////////////////////////////////////////////////////////////////////////////
1265         // we're finding the maximum over
1266         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1267         // for z in the range from -1 to 1
1268         //////////////////////////////////////////////////////////////////////////////////////
1269         // maximum is EITHER attained at the single extreme point:
1270         float a_diff = a_side * a_side - a_up * a_up;
1271         float f;
1272         if (a_diff != 0)
1273         {
1274                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1275                 if (f > -1 && f < 1) // can it be attained?
1276                 {
1277                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1278                         //print("middle\n");
1279                 }
1280         }
1281         // OR attained at z = 1:
1282         f = (a_up + a_add) * (a_up + a_add);
1283         if (f > best)
1284         {
1285                 best = f;
1286                 //print("top\n");
1287         }
1288         // OR attained at z = -1:
1289         f = (a_up - a_add) * (a_up - a_add);
1290         if (f > best)
1291         {
1292                 best = f;
1293                 //print("bottom\n");
1294         }
1295         best = sqrt(best);
1296         //////////////////////////////////////////////////////////////////////////////////////
1297
1298         //print("best possible acceleration: ", ftos(best), "\n");
1299
1300         float fxy, fz;
1301         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1302         if (wishvel_z - PHYS_GRAVITY(this) > 0)
1303                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1304         else
1305                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1306
1307         float fvel;
1308         fvel = vlen(wishvel);
1309         wishvel_x *= fxy;
1310         wishvel_y *= fxy;
1311         wishvel_z = (wishvel_z - PHYS_GRAVITY(this)) * fz + PHYS_GRAVITY(this);
1312
1313         fvel = min(1, vlen(wishvel) / best);
1314         if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1315                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1316         else
1317                 f = 1;
1318
1319         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1320
1321         if (f > 0 && wishvel != '0 0 0')
1322         {
1323                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1324                 UNSET_ONGROUND(self);
1325
1326 #ifdef SVQC
1327                 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1328                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1329
1330                 ITEMS_STAT(self) |= IT_USING_JETPACK;
1331
1332                 // jetpack also inhibits health regeneration, but only for 1 second
1333                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1334 #endif
1335         }
1336
1337 #ifdef CSQC
1338         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1339         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1340                 self.velocity_z -= g * 0.5;
1341         else
1342                 self.velocity_z -= g;
1343         PM_ClientMovement_Move();
1344         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1345                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1346                         self.velocity_z -= g * 0.5;
1347 #endif
1348 }
1349
1350 void PM_walk(float buttons_prev, float maxspd_mod)
1351 {SELFPARAM();
1352         if (!WAS_ONGROUND(self))
1353         {
1354 #ifdef SVQC
1355                 if (autocvar_speedmeter)
1356                         LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1357 #endif
1358                 if (self.lastground < time - 0.3)
1359                         self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1360 #ifdef SVQC
1361                 if (self.jumppadcount > 1)
1362                         LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1363                 self.jumppadcount = 0;
1364 #endif
1365         }
1366
1367         // walking
1368         makevectors(self.v_angle.y * '0 1 0');
1369         vector wishvel = v_forward * self.movement.x
1370                                         + v_right * self.movement.y;
1371         // acceleration
1372         vector wishdir = normalize(wishvel);
1373         float wishspeed = vlen(wishvel);
1374
1375         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1376         if (IS_DUCKED(self))
1377                 wishspeed *= 0.5;
1378
1379         // apply edge friction
1380         float f = vlen(vec2(self.velocity));
1381         if (f > 0)
1382         {
1383                 float realfriction;
1384                 trace_dphitq3surfaceflags = 0;
1385                 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1386                 // TODO: apply edge friction
1387                 // apply ground friction
1388                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1389                         realfriction = PHYS_FRICTION_SLICK;
1390                 else
1391                         realfriction = PHYS_FRICTION;
1392
1393                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1394                 f = max(0, f);
1395                 self.velocity *= f;
1396                 /*
1397                    Mathematical analysis time!
1398
1399                    Our goal is to invert this mess.
1400
1401                    For the two cases we get:
1402                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1403                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1404                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1405                    and
1406                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1407                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1408
1409                    These cases would be chosen ONLY if:
1410                         v0 < PHYS_STOPSPEED
1411                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1412                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1413                    and, respectively:
1414                         v0 >= PHYS_STOPSPEED
1415                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1416                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1417                  */
1418         }
1419         float addspeed = wishspeed - self.velocity * wishdir;
1420         if (addspeed > 0)
1421         {
1422                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1423                 self.velocity += accelspeed * wishdir;
1424         }
1425         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1426         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1427                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1428         if (self.velocity * self.velocity)
1429                 PM_ClientMovement_Move();
1430         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1431                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1432                         self.velocity_z -= g * 0.5;
1433 }
1434
1435 void PM_air(float buttons_prev, float maxspd_mod)
1436 {SELFPARAM();
1437         makevectors(self.v_angle.y * '0 1 0');
1438         vector wishvel = v_forward * self.movement.x
1439                                         + v_right * self.movement.y;
1440         // acceleration
1441         vector wishdir = normalize(wishvel);
1442         float wishspeed = vlen(wishvel);
1443
1444 #ifdef SVQC
1445         if (time >= self.teleport_time)
1446 #else
1447         if (pmove_waterjumptime <= 0)
1448 #endif
1449         {
1450                 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1451
1452                 // apply air speed limit
1453                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1454                 float wishspeed0 = wishspeed;
1455                 wishspeed = min(wishspeed, maxairspd);
1456                 if (IS_DUCKED(self))
1457                         wishspeed *= 0.5;
1458                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1459
1460                 float accelerating = (self.velocity * wishdir > 0);
1461                 float wishspeed2 = wishspeed;
1462
1463                 // CPM: air control
1464                 if (PHYS_AIRSTOPACCELERATE)
1465                 {
1466                         vector curdir = normalize(vec2(self.velocity));
1467                         airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1468                 }
1469                 // note that for straight forward jumping:
1470                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1471                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1472                 // -->
1473                 // dv/dt = accel * maxspeed (when slow)
1474                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1475                 // log dv/dt = logaccel + logmaxspeed (when slow)
1476                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1477                 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1478                 if (PHYS_MAXAIRSTRAFESPEED)
1479                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1480                 if (PHYS_AIRSTRAFEACCELERATE(self))
1481                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1482                 if (PHYS_AIRSTRAFEACCEL_QW(self))
1483                         airaccelqw =
1484                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1485                 *
1486                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1487                 // !CPM
1488
1489                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1490                         PM_AirAccelerate(wishdir, wishspeed2);
1491                 else
1492                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1493
1494                 if (PHYS_AIRCONTROL)
1495                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1496         }
1497         float g = PHYS_GRAVITY(this) * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1498         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1499                 self.velocity_z -= g * 0.5;
1500         else
1501                 self.velocity_z -= g;
1502         PM_ClientMovement_Move();
1503         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1504                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1505                         self.velocity_z -= g * 0.5;
1506 }
1507
1508 // used for calculating airshots
1509 bool IsFlying(entity a)
1510 {
1511         if(IS_ONGROUND(a))
1512                 return false;
1513         if(a.waterlevel >= WATERLEVEL_SWIMMING)
1514                 return false;
1515         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1516         if(trace_fraction < 1)
1517                 return false;
1518         return true;
1519 }
1520
1521 void PM_Main()
1522 {SELFPARAM();
1523         int buttons = PHYS_INPUT_BUTTON_MASK(self);
1524 #ifdef CSQC
1525         self.items = getstati(STAT_ITEMS, 0, 24);
1526
1527         self.movement = PHYS_INPUT_MOVEVALUES(self);
1528
1529         vector oldv_angle = self.v_angle;
1530         vector oldangles = self.angles; // we need to save these, as they're abused by other code
1531         self.v_angle = PHYS_INPUT_ANGLES(self);
1532         self.angles = PHYS_WORLD_ANGLES(self);
1533
1534         self.team = myteam + 1; // is this correct?
1535         if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1536                 UNSET_JUMP_HELD(self); // canjump = true
1537         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1538
1539         PM_ClientMovement_UpdateStatus(true);
1540 #endif
1541
1542
1543 #ifdef SVQC
1544         WarpZone_PlayerPhysics_FixVAngle();
1545 #endif
1546         float maxspeed_mod = 1;
1547         maxspeed_mod *= PHYS_HIGHSPEED;
1548
1549 #ifdef SVQC
1550         Physics_UpdateStats(this, maxspeed_mod);
1551
1552         if (self.PlayerPhysplug)
1553                 if (self.PlayerPhysplug())
1554                         return;
1555 #endif
1556
1557 #ifdef SVQC
1558         anticheat_physics();
1559 #endif
1560
1561         if (PM_check_specialcommand(buttons))
1562                 return;
1563 #ifdef SVQC
1564         if (sv_maxidle > 0)
1565         {
1566                 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1567                         self.parm_idlesince = time;
1568         }
1569 #endif
1570         int buttons_prev = self.buttons_old;
1571         self.buttons_old = buttons;
1572         self.movement_old = self.movement;
1573         self.v_angle_old = self.v_angle;
1574
1575         PM_check_nickspam();
1576
1577         PM_check_punch();
1578 #ifdef SVQC
1579         if (IS_BOT_CLIENT(self))
1580         {
1581                 if (playerdemo_read())
1582                         return;
1583                 bot_think();
1584         }
1585
1586         if (IS_PLAYER(self))
1587 #endif
1588         {
1589                 bool not_allowed_to_move = false;
1590 #ifdef SVQC
1591                 if (time < game_starttime)
1592                         not_allowed_to_move = true;
1593 #endif
1594
1595                 if (not_allowed_to_move)
1596                 {
1597                         self.velocity = '0 0 0';
1598                         self.movetype = MOVETYPE_NONE;
1599 #ifdef SVQC
1600                         self.disableclientprediction = 2;
1601 #endif
1602                 }
1603 #ifdef SVQC
1604                 else if (self.disableclientprediction == 2)
1605                 {
1606                         if (self.movetype == MOVETYPE_NONE)
1607                                 self.movetype = MOVETYPE_WALK;
1608                         self.disableclientprediction = 0;
1609                 }
1610 #endif
1611         }
1612
1613 #ifdef SVQC
1614         if (self.movetype == MOVETYPE_NONE)
1615                 return;
1616
1617         // when we get here, disableclientprediction cannot be 2
1618         self.disableclientprediction = 0;
1619 #endif
1620
1621         viewloc_PlayerPhysics();
1622
1623         PM_check_spider();
1624
1625         PM_check_frozen();
1626
1627         PM_check_blocked();
1628
1629         maxspeed_mod = 1;
1630
1631         if (self.in_swamp)
1632                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1633
1634         // conveyors: first fix velocity
1635         if (self.conveyor.state)
1636                 self.velocity -= self.conveyor.movedir;
1637
1638 #ifdef SVQC
1639         MUTATOR_CALLHOOK(PlayerPhysics);
1640 #endif
1641 #ifdef CSQC
1642         PM_multijump();
1643 #endif
1644
1645 //      float forcedodge = 1;
1646 //      if(forcedodge) {
1647 //#ifdef CSQC
1648 //              PM_dodging_checkpressedkeys();
1649 //#endif
1650 //              PM_dodging();
1651 //              PM_ClientMovement_Move();
1652 //              return;
1653 //      }
1654
1655 #ifdef SVQC
1656         if (!IS_PLAYER(self))
1657         {
1658                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1659                 if (!self.spectatorspeed)
1660                         self.spectatorspeed = maxspeed_mod;
1661                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1662                 {
1663                         if (self.lastclassname != "player")
1664                         {
1665                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1666                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1667                                 else if (self.impulse == 11)
1668                                         self.spectatorspeed = maxspeed_mod;
1669                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1670                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1671                                 else if (self.impulse >= 1 && self.impulse <= 9)
1672                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1673                         } // otherwise just clear
1674                         self.impulse = 0;
1675                 }
1676                 maxspeed_mod = self.spectatorspeed;
1677         }
1678
1679         float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1680         if(self.speed != spd)
1681         {
1682                 self.speed = spd;
1683                 string temps = ftos(spd);
1684                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1685                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1686                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1687                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1688         }
1689
1690         if(self.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1691         {
1692                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1693                 stuffcmd(self, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1694         }
1695         if(self.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1696         {
1697                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1698                 stuffcmd(self, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1699         }
1700 #endif
1701
1702         if(PHYS_DEAD(self))
1703         {
1704                 // handle water here
1705                 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1706                 if(pointcontents(midpoint) == CONTENT_WATER)
1707                 {
1708                         self.velocity = self.velocity * 0.5;
1709
1710                         // do we want this?
1711                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1712                                 //{ self.velocity_z = 70; }
1713                 }
1714                 goto end;
1715         }
1716
1717 #ifdef SVQC
1718         if (!self.fixangle && !g_bugrigs)
1719                 self.angles = '0 1 0' * self.v_angle.y;
1720 #endif
1721
1722         PM_check_hitground();
1723
1724         if(IsFlying(self))
1725                 self.wasFlying = 1;
1726
1727         if (IS_PLAYER(self))
1728                 CheckPlayerJump();
1729
1730         if (self.flags & FL_WATERJUMP)
1731         {
1732                 self.velocity_x = self.movedir_x;
1733                 self.velocity_y = self.movedir_y;
1734                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1735                 {
1736                         self.flags &= ~FL_WATERJUMP;
1737                         self.teleport_time = 0;
1738                 }
1739         }
1740
1741 #ifdef SVQC
1742         else if (g_bugrigs && IS_PLAYER(self))
1743                 RaceCarPhysics();
1744 #endif
1745
1746         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS_STAT(self) & BUFF_FLIGHT.m_itemid))
1747                 PM_fly(maxspeed_mod);
1748
1749         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1750                 PM_swim(maxspeed_mod);
1751
1752         else if (time < self.ladder_time)
1753                 PM_ladder(maxspeed_mod);
1754
1755         else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1756                 PM_jetpack(maxspeed_mod);
1757
1758         else if (IS_ONGROUND(self))
1759                 PM_walk(buttons_prev, maxspeed_mod);
1760
1761         else
1762                 PM_air(buttons_prev, maxspeed_mod);
1763
1764         PM_check_vortex();
1765
1766 :end
1767         if (IS_ONGROUND(self))
1768                 self.lastground = time;
1769
1770         // conveyors: then break velocity again
1771         if(self.conveyor.state)
1772                 self.velocity += self.conveyor.movedir;
1773
1774         self.lastflags = self.flags;
1775
1776         self.lastclassname = self.classname;
1777
1778 #ifdef CSQC
1779         self.v_angle = oldv_angle;
1780         self.angles = oldangles;
1781 #endif
1782 }
1783
1784 #ifdef SVQC
1785 void SV_PlayerPhysics()
1786 #elif defined(CSQC)
1787 void CSQC_ClientMovement_PlayerMove_Frame()
1788 #endif
1789 {SELFPARAM();
1790         PM_Main();
1791
1792 #ifdef CSQC
1793         self.pmove_flags =
1794                         ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1795                         (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1796                         ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);
1797 #endif
1798 }