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