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