]> 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 returns TRUE if handled
434 =============
435 */
436 float PlayerJump (void)
437 {
438         if (PHYS_FROZEN(self))
439                 return TRUE; // no jumping in freezetag when frozen
440
441 #ifdef SVQC
442         if (self.player_blocked)
443                 return TRUE; // no jumping while blocked
444 #endif
445
446         float doublejump = FALSE;
447         float mjumpheight = PHYS_JUMPVELOCITY;
448
449         player_multijump = doublejump;
450         player_jumpheight = mjumpheight;
451 #ifdef SVQC
452         if (MUTATOR_CALLHOOK(PlayerJump))
453 #elif defined(CSQC)
454         if(PM_multijump_checkjump())
455 #endif
456                 return TRUE;
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 TRUE;
480         }
481
482         if (!doublejump)
483                 if (!IS_ONGROUND(self))
484                         return IS_JUMP_HELD(self);
485
486         if (PHYS_TRACK_CANJUMP(self))
487                 if (IS_JUMP_HELD(self))
488                         return TRUE;
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         return TRUE;
547 }
548
549 void CheckWaterJump()
550 {
551 // check for a jump-out-of-water
552         makevectors(PHYS_INPUT_ANGLES(self));
553         vector start = self.origin;
554         start_z += 8;
555         v_forward_z = 0;
556         normalize(v_forward);
557         vector end = start + v_forward*24;
558         traceline (start, end, TRUE, self);
559         if (trace_fraction < 1)
560         {       // solid at waist
561                 start_z = start_z + self.maxs_z - 8;
562                 end = start + v_forward*24;
563                 self.movedir = trace_plane_normal * -50;
564                 traceline(start, end, TRUE, self);
565                 if (trace_fraction == 1)
566                 {       // open at eye level
567                         self.velocity_z = 225;
568 #ifdef SVQC
569                         self.flags |= FL_WATERJUMP;
570                         SET_JUMP_HELD(self);
571                         self.teleport_time = time + 2;  // safety net
572 #endif
573                 }
574         }
575 }
576
577
578 #ifdef SVQC
579         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
580 #elif defined(CSQC)
581         float autocvar_cl_jetpack_jump;
582         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
583 #endif
584 .float jetpack_stopped;
585 // Hack: shouldn't need to know about this
586 .float multijump_count;
587 void CheckPlayerJump()
588 {
589 #ifdef SVQC
590         float was_flying = ITEMS(self) & IT_USING_JETPACK;
591 #endif
592         if (JETPACK_JUMP(self) < 2)
593 #ifdef SVQC
594                 ITEMS(self) &= ~IT_USING_JETPACK;
595 #endif
596
597         if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
598         {
599 #ifdef SVQC
600                 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
601                 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
602                 float has_fuel = !autocvar_g_jetpack_fuel || self.ammo_fuel || ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO;
603 #else
604                 PlayerJump(); // Client only
605                 float has_fuel = TRUE; // TODO
606 #endif
607                 if (!(ITEMS(self) & IT_JETPACK)) { }
608                 else if (self.jetpack_stopped) { }
609                 else if (!has_fuel)
610                 {
611 #ifdef SVQC
612                         if (was_flying) // TODO: ran out of fuel message
613                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
614                         else if (activate)
615                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
616 #endif
617                         self.jetpack_stopped = TRUE;
618 #ifdef SVQC
619                         ITEMS(self) &= ~IT_USING_JETPACK;
620 #endif
621                 }
622 #ifdef SVQC
623                 else if (activate && !PHYS_FROZEN(self))
624                         ITEMS(self) |= IT_USING_JETPACK;
625 #endif
626         }
627         else
628         {
629                 self.jetpack_stopped = FALSE;
630 #ifdef SVQC
631                 ITEMS(self) &= ~IT_USING_JETPACK;
632 #endif
633         }
634         if (!PHYS_INPUT_BUTTON_JUMP(self))
635                 UNSET_JUMP_HELD(self);
636
637         if (self.waterlevel == WATERLEVEL_SWIMMING)
638                 CheckWaterJump();
639 }
640
641 float racecar_angle(float forward, float down)
642 {
643         if (forward < 0)
644         {
645                 forward = -forward;
646                 down = -down;
647         }
648
649         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
650
651         float angle_mult = forward / (800 + forward);
652
653         if (ret > 180)
654                 return ret * angle_mult + 360 * (1 - angle_mult);
655         else
656                 return ret * angle_mult;
657 }
658
659 void RaceCarPhysics()
660 {
661 #ifdef SVQC
662         // using this move type for "big rigs"
663         // the engine does not push the entity!
664
665         vector rigvel;
666
667         vector angles_save = self.angles;
668         float accel = bound(-1, PHYS_INPUT_MOVEVALUES(self).x / PHYS_MAXSPEED(self), 1);
669         float steer = bound(-1, PHYS_INPUT_MOVEVALUES(self).y / PHYS_MAXSPEED(self), 1);
670
671         if (g_bugrigs_reverse_speeding)
672         {
673                 if (accel < 0)
674                 {
675                         // back accel is DIGITAL
676                         // to prevent speedhack
677                         if (accel < -0.5)
678                                 accel = -1;
679                         else
680                                 accel = 0;
681                 }
682         }
683
684         self.angles_x = 0;
685         self.angles_z = 0;
686         makevectors(self.angles); // new forward direction!
687
688         if (IS_ONGROUND(self) || g_bugrigs_air_steering)
689         {
690                 float myspeed = self.velocity * v_forward;
691                 float upspeed = self.velocity * v_up;
692
693                 // responsiveness factor for steering and acceleration
694                 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
695                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
696
697                 float steerfactor;
698                 if (myspeed < 0 && g_bugrigs_reverse_spinning)
699                         steerfactor = -myspeed * g_bugrigs_steer;
700                 else
701                         steerfactor = -myspeed * f * g_bugrigs_steer;
702
703                 float accelfactor;
704                 if (myspeed < 0 && g_bugrigs_reverse_speeding)
705                         accelfactor = g_bugrigs_accel;
706                 else
707                         accelfactor = f * g_bugrigs_accel;
708                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
709
710                 if (accel < 0)
711                 {
712                         if (myspeed > 0)
713                         {
714                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
715                         }
716                         else
717                         {
718                                 if (!g_bugrigs_reverse_speeding)
719                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
720                         }
721                 }
722                 else
723                 {
724                         if (myspeed >= 0)
725                         {
726                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
727                         }
728                         else
729                         {
730                                 if (g_bugrigs_reverse_stopping)
731                                         myspeed = 0;
732                                 else
733                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
734                         }
735                 }
736                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
737                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
738
739                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
740                 makevectors(self.angles); // new forward direction!
741
742                 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
743
744                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
745         }
746         else
747         {
748                 float myspeed = vlen(self.velocity);
749
750                 // responsiveness factor for steering and acceleration
751                 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
752                 float steerfactor = -myspeed * f;
753                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
754
755                 rigvel = self.velocity;
756                 makevectors(self.angles); // new forward direction!
757         }
758
759         rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
760         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
761         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
762         //MAXIMA: solve(total_acceleration(v) = 0, v);
763
764         if (g_bugrigs_planar_movement)
765         {
766                 vector rigvel_xy, neworigin, up;
767                 float mt;
768
769                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
770                 rigvel_xy = vec2(rigvel);
771
772                 if (g_bugrigs_planar_movement_car_jumping)
773                         mt = MOVE_NORMAL;
774                 else
775                         mt = MOVE_NOMONSTERS;
776
777                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
778                 up = trace_endpos - self.origin;
779
780                 // BUG RIGS: align the move to the surface instead of doing collision testing
781                 // can we move?
782                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
783
784                 // align to surface
785                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
786
787                 if (trace_fraction < 0.5)
788                 {
789                         trace_fraction = 1;
790                         neworigin = self.origin;
791                 }
792                 else
793                         neworigin = trace_endpos;
794
795                 if (trace_fraction < 1)
796                 {
797                         // now set angles_x so that the car points parallel to the surface
798                         self.angles = vectoangles(
799                                         '1 0 0' * v_forward_x * trace_plane_normal_z
800                                         +
801                                         '0 1 0' * v_forward_y * trace_plane_normal_z
802                                         +
803                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
804                                         );
805                         SET_ONGROUND(self);
806                 }
807                 else
808                 {
809                         // now set angles_x so that the car points forward, but is tilted in velocity direction
810                         UNSET_ONGROUND(self);
811                 }
812
813                 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
814                 self.movetype = MOVETYPE_NOCLIP;
815         }
816         else
817         {
818                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
819                 self.velocity = rigvel;
820                 self.movetype = MOVETYPE_FLY;
821         }
822
823         trace_fraction = 1;
824         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
825         if (trace_fraction != 1)
826         {
827                 self.angles = vectoangles2(
828                                 '1 0 0' * v_forward_x * trace_plane_normal_z
829                                 +
830                                 '0 1 0' * v_forward_y * trace_plane_normal_z
831                                 +
832                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
833                                 trace_plane_normal
834                                 );
835         }
836         else
837         {
838                 vector vel_local;
839
840                 vel_local_x = v_forward * self.velocity;
841                 vel_local_y = v_right * self.velocity;
842                 vel_local_z = v_up * self.velocity;
843
844                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
845                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
846         }
847
848         // smooth the angles
849         vector vf1, vu1, smoothangles;
850         makevectors(self.angles);
851         float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
852         if (f == 0)
853                 f = 1;
854         vf1 = v_forward * f;
855         vu1 = v_up * f;
856         makevectors(angles_save);
857         vf1 = vf1 + v_forward * (1 - f);
858         vu1 = vu1 + v_up * (1 - f);
859         smoothangles = vectoangles2(vf1, vu1);
860         self.angles_x = -smoothangles_x;
861         self.angles_z =  smoothangles_z;
862 #endif
863 }
864
865 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
866 .float specialcommand_pos;
867 void SpecialCommand()
868 {
869 #ifdef SVQC
870 #ifdef TETRIS
871         TetrisImpulse();
872 #else
873         if (!CheatImpulse(99))
874                 print("A hollow voice says \"Plugh\".\n");
875 #endif
876 #endif
877 }
878
879 #ifdef SVQC
880 float speedaward_speed;
881 string speedaward_holder;
882 string speedaward_uid;
883 #endif
884 void race_send_speedaward(float msg)
885 {
886 #ifdef SVQC
887         // send the best speed of the round
888         WriteByte(msg, SVC_TEMPENTITY);
889         WriteByte(msg, TE_CSQC_RACE);
890         WriteByte(msg, RACE_NET_SPEED_AWARD);
891         WriteInt24_t(msg, floor(speedaward_speed+0.5));
892         WriteString(msg, speedaward_holder);
893 #endif
894 }
895
896 #ifdef SVQC
897 float speedaward_alltimebest;
898 string speedaward_alltimebest_holder;
899 string speedaward_alltimebest_uid;
900 #endif
901 void race_send_speedaward_alltimebest(float msg)
902 {
903 #ifdef SVQC
904         // send the best speed
905         WriteByte(msg, SVC_TEMPENTITY);
906         WriteByte(msg, TE_CSQC_RACE);
907         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
908         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
909         WriteString(msg, speedaward_alltimebest_holder);
910 #endif
911 }
912
913 float PM_check_keepaway(void)
914 {
915 #ifdef SVQC
916         return (self.ballcarried && g_keepaway) ? autocvar_g_keepaway_ballcarrier_highspeed : 1;
917 #else
918         return 1;
919 #endif
920 }
921
922 void PM_check_race_movetime(void)
923 {
924 #ifdef SVQC
925         self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
926         float f = floor(self.race_movetime_frac);
927         self.race_movetime_frac -= f;
928         self.race_movetime_count += f;
929         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
930 #endif
931 }
932
933 float PM_check_specialcommand(float buttons)
934 {
935 #ifdef SVQC
936         string c;
937         if (!buttons)
938                 c = "x";
939         else if (buttons == 1)
940                 c = "1";
941         else if (buttons == 2)
942                 c = " ";
943         else if (buttons == 128)
944                 c = "s";
945         else if (buttons == 256)
946                 c = "w";
947         else if (buttons == 512)
948                 c = "a";
949         else if (buttons == 1024)
950                 c = "d";
951         else
952                 c = "?";
953
954         if (c == substring(specialcommand, self.specialcommand_pos, 1))
955         {
956                 self.specialcommand_pos += 1;
957                 if (self.specialcommand_pos >= strlen(specialcommand))
958                 {
959                         self.specialcommand_pos = 0;
960                         SpecialCommand();
961                         return TRUE;
962                 }
963         }
964         else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
965                 self.specialcommand_pos = 0;
966 #endif
967         return FALSE;
968 }
969
970 void PM_check_nickspam(void)
971 {
972 #ifdef SVQC
973         if (time >= self.nickspamtime)
974                 return;
975         if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
976         {
977                 // slight annoyance for nick change scripts
978                 PHYS_INPUT_MOVEVALUES(self) = -1 * PHYS_INPUT_MOVEVALUES(self);
979                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
980
981                 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!
982                 {
983                         PHYS_INPUT_ANGLES(self)_x = random() * 360;
984                         PHYS_INPUT_ANGLES(self)_y = random() * 360;
985                         // at least I'm not forcing retardedview by also assigning to angles_z
986                         self.fixangle = TRUE;
987                 }
988         }
989 #endif
990 }
991
992 void PM_check_punch()
993 {
994 #ifdef SVQC
995         if (self.punchangle != '0 0 0')
996         {
997                 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
998                 if (f > 0)
999                         self.punchangle = normalize(self.punchangle) * f;
1000                 else
1001                         self.punchangle = '0 0 0';
1002         }
1003
1004         if (self.punchvector != '0 0 0')
1005         {
1006                 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1007                 if (f > 0)
1008                         self.punchvector = normalize(self.punchvector) * f;
1009                 else
1010                         self.punchvector = '0 0 0';
1011         }
1012 #endif
1013 }
1014
1015 void PM_check_spider(void)
1016 {
1017 #ifdef SVQC
1018         if (time >= self.spider_slowness)
1019                 return;
1020         PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1021         self.stat_sv_airspeedlimit_nonqw *= 0.5;
1022 #endif
1023 }
1024
1025 // predict frozen movement, as frozen players CAN move in some cases
1026 void PM_check_frozen(void)
1027 {
1028         if (!PHYS_FROZEN(self))
1029                 return;
1030         if (PHYS_DODGING_FROZEN
1031 #ifdef SVQC
1032         && IS_REAL_CLIENT(self)
1033 #endif
1034         )
1035         {
1036                 PHYS_INPUT_MOVEVALUES(self)_x = bound(-5, PHYS_INPUT_MOVEVALUES(self).x, 5);
1037                 PHYS_INPUT_MOVEVALUES(self)_y = bound(-5, PHYS_INPUT_MOVEVALUES(self).y, 5);
1038                 PHYS_INPUT_MOVEVALUES(self)_z = bound(-5, PHYS_INPUT_MOVEVALUES(self).z, 5);
1039         }
1040         else
1041                 PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
1042
1043         vector midpoint = ((self.absmin + self.absmax) * 0.5);
1044         if (pointcontents(midpoint) == CONTENT_WATER)
1045         {
1046                 self.velocity = self.velocity * 0.5;
1047
1048                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1049                         self.velocity_z = 200;
1050         }
1051 }
1052
1053 void PM_check_blocked(void)
1054 {
1055 #ifdef SVQC
1056         if (!self.player_blocked)
1057                 return;
1058         PHYS_INPUT_MOVEVALUES(self) = '0 0 0';
1059         self.disableclientprediction = 1;
1060 #endif
1061 }
1062
1063 #ifdef SVQC
1064 float speedaward_lastsent;
1065 float speedaward_lastupdate;
1066 string GetMapname(void);
1067 #endif
1068 void PM_check_race(void)
1069 {
1070 #ifdef SVQC
1071         if not(g_cts || g_race)
1072                 return;
1073         if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1074         {
1075                 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1076                 speedaward_holder = self.netname;
1077                 speedaward_uid = self.crypto_idfp;
1078                 speedaward_lastupdate = time;
1079         }
1080         if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1081         {
1082                 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1083                 race_send_speedaward(MSG_ALL);
1084                 speedaward_lastsent = speedaward_speed;
1085                 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1086                 {
1087                         speedaward_alltimebest = speedaward_speed;
1088                         speedaward_alltimebest_holder = speedaward_holder;
1089                         speedaward_alltimebest_uid = speedaward_uid;
1090                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1091                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1092                         race_send_speedaward_alltimebest(MSG_ALL);
1093                 }
1094         }
1095 #endif
1096 }
1097
1098 void PM_check_vortex(void)
1099 {
1100 #ifdef SVQC
1101         // WEAPONTODO
1102         float xyspeed = vlen(vec2(self.velocity));
1103         if (self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1104         {
1105                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1106                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1107                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1108                 // add the extra charge
1109                 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1110         }
1111 #endif
1112 }
1113
1114 void PM_fly(float maxspd_mod)
1115 {
1116         // noclipping or flying
1117         UNSET_ONGROUND(self);
1118
1119         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1120         makevectors(PHYS_INPUT_ANGLES(self));
1121         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1122         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1123                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y
1124                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1125         // acceleration
1126         vector wishdir = normalize(wishvel);
1127         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1128         if (time >= self.teleport_time)
1129                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1130 }
1131
1132 void PM_swim(float maxspd_mod)
1133 {
1134         // swimming
1135         UNSET_ONGROUND(self);
1136
1137         float jump = PHYS_INPUT_BUTTON_JUMP(self);
1138         // water jump only in certain situations
1139         // this mimics quakeworld code
1140         if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1141         {
1142                 vector yawangles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1143                 makevectors(yawangles);
1144                 vector forward = v_forward;
1145                 vector spot = self.origin + 24 * forward;
1146                 spot_z += 8;
1147                 traceline(spot, spot, MOVE_NOMONSTERS, self);
1148                 if (trace_startsolid)
1149                 {
1150                         spot_z += 24;
1151                         traceline(spot, spot, MOVE_NOMONSTERS, self);
1152                         if (!trace_startsolid)
1153                         {
1154                                 self.velocity = forward * 50;
1155                                 self.velocity_z = 310;
1156                                 pmove_waterjumptime = 2;
1157                                 UNSET_ONGROUND(self);
1158                                 SET_JUMP_HELD(self);
1159                         }
1160                 }
1161         }
1162         makevectors(PHYS_INPUT_ANGLES(self));
1163         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1164         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1165                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y
1166                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self).z;
1167         if (wishvel == '0 0 0')
1168                 wishvel = '0 0 -60'; // drift towards bottom
1169
1170         vector wishdir = normalize(wishvel);
1171         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1172
1173         if (IS_DUCKED(self))
1174         wishspeed *= 0.5;
1175
1176 //      if (pmove_waterjumptime <= 0) // TODO: use
1177     {
1178                 // water friction
1179                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1180                 f = min(max(0, f), 1);
1181                 self.velocity *= f;
1182
1183                 f = wishspeed - self.velocity * wishdir;
1184                 if (f > 0)
1185                 {
1186                         float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1187                         self.velocity += accelspeed * wishdir;
1188                 }
1189
1190                 // holding jump button swims upward slowly
1191                 if (jump)
1192                 {
1193 #if 0
1194                         if (self.watertype & CONTENT_LAVA)
1195                                 self.velocity_z =  50;
1196                         else if (self.watertype & CONTENT_SLIME)
1197                                 self.velocity_z =  80;
1198                         else
1199                         {
1200                                 if (IS_NEXUIZ_DERIVED(gamemode))
1201 #endif
1202                                         self.velocity_z = 200;
1203 #if 0
1204                                 else
1205                                         self.velocity_z = 100;
1206                         }
1207 #endif
1208                 }
1209         }
1210         PM_ClientMovement_Move();
1211         // water acceleration
1212         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1213 }
1214
1215 void PM_ladder(float maxspd_mod)
1216 {
1217         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1218         UNSET_ONGROUND(self);
1219
1220         float g;
1221         g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1222         if (PHYS_ENTGRAVITY(self))
1223                 g *= PHYS_ENTGRAVITY(self);
1224         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1225         {
1226                 g *= 0.5;
1227                 self.velocity_z += g;
1228         }
1229
1230         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1231         makevectors(PHYS_INPUT_ANGLES(self));
1232         //wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x + v_right * PHYS_INPUT_MOVEVALUES(self).y + v_up * PHYS_INPUT_MOVEVALUES(self).z;
1233         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1234                                         + v_right * PHYS_INPUT_MOVEVALUES(self)_y
1235                                         + '0 0 1' * PHYS_INPUT_MOVEVALUES(self)_z;
1236         self.velocity_z += g;
1237         if (self.ladder_entity.classname == "func_water")
1238         {
1239                 float f = vlen(wishvel);
1240                 if (f > self.ladder_entity.speed)
1241                         wishvel *= (self.ladder_entity.speed / f);
1242
1243                 self.watertype = self.ladder_entity.skin;
1244                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1245                 if ((self.origin_z + self.view_ofs_z) < f)
1246                         self.waterlevel = WATERLEVEL_SUBMERGED;
1247                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1248                         self.waterlevel = WATERLEVEL_SWIMMING;
1249                 else if ((self.origin_z + self.mins_z + 1) < f)
1250                         self.waterlevel = WATERLEVEL_WETFEET;
1251                 else
1252                 {
1253                         self.waterlevel = WATERLEVEL_NONE;
1254                         self.watertype = CONTENT_EMPTY;
1255                 }
1256         }
1257         // acceleration
1258         vector wishdir = normalize(wishvel);
1259         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1260         PM_ClientMovement_Move();
1261 #ifdef SVQC
1262         if (time >= self.teleport_time)
1263 #endif
1264                 // water acceleration
1265                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1266 }
1267
1268 void PM_check_jumppad()
1269 {
1270 #ifdef CSQC
1271         entity oldself = self;
1272
1273         for(self = world; (self = find(self, classname, "jumppad")); )
1274                 trigger_push_draw();
1275
1276         self = oldself;
1277 #endif
1278 }
1279
1280 void PM_jetpack(float maxspd_mod)
1281 {
1282         //makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1283         makevectors(PHYS_INPUT_ANGLES(self));
1284         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self)_x
1285                                         + v_right * PHYS_INPUT_MOVEVALUES(self)_y;
1286         // add remaining speed as Z component
1287         float maxairspd = PHYS_MAXAIRSPEED * max(1, maxspd_mod);
1288         // fix speedhacks :P
1289         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1290         // add the unused velocity as up component
1291         wishvel_z = 0;
1292
1293         // if (self.BUTTON_JUMP)
1294                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1295
1296         // it is now normalized, so...
1297         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1298         float a_up = PHYS_JETPACK_ACCEL_UP;
1299         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1300
1301         wishvel_x *= a_side;
1302         wishvel_y *= a_side;
1303         wishvel_z *= a_up;
1304         wishvel_z += a_add;
1305
1306         float best = 0;
1307         //////////////////////////////////////////////////////////////////////////////////////
1308         // finding the maximum over all vectors of above form
1309         // with wishvel having an absolute value of 1
1310         //////////////////////////////////////////////////////////////////////////////////////
1311         // we're finding the maximum over
1312         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1313         // for z in the range from -1 to 1
1314         //////////////////////////////////////////////////////////////////////////////////////
1315         // maximum is EITHER attained at the single extreme point:
1316         float a_diff = a_side * a_side - a_up * a_up;
1317         float f;
1318         if (a_diff != 0)
1319         {
1320                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1321                 if (f > -1 && f < 1) // can it be attained?
1322                 {
1323                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1324                         //print("middle\n");
1325                 }
1326         }
1327         // OR attained at z = 1:
1328         f = (a_up + a_add) * (a_up + a_add);
1329         if (f > best)
1330         {
1331                 best = f;
1332                 //print("top\n");
1333         }
1334         // OR attained at z = -1:
1335         f = (a_up - a_add) * (a_up - a_add);
1336         if (f > best)
1337         {
1338                 best = f;
1339                 //print("bottom\n");
1340         }
1341         best = sqrt(best);
1342         //////////////////////////////////////////////////////////////////////////////////////
1343
1344         //print("best possible acceleration: ", ftos(best), "\n");
1345
1346         float fxy, fz;
1347         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1348         if (wishvel_z - PHYS_GRAVITY > 0)
1349                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1350         else
1351                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1352
1353         float fvel;
1354         fvel = vlen(wishvel);
1355         wishvel_x *= fxy;
1356         wishvel_y *= fxy;
1357         wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1358
1359         fvel = min(1, vlen(wishvel) / best);
1360         if (PHYS_JETPACK_FUEL && !(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1361                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1362         else
1363                 f = 1;
1364
1365         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1366
1367         if (f > 0 && wishvel != '0 0 0')
1368         {
1369                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1370                 UNSET_ONGROUND(self);
1371
1372 #ifdef SVQC
1373                 if (!(ITEMS(self) & IT_UNLIMITED_WEAPON_AMMO))
1374                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1375
1376                 ITEMS(self) |= IT_USING_JETPACK;
1377
1378                 // jetpack also inhibits health regeneration, but only for 1 second
1379                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1380 #endif
1381         }
1382
1383 #ifdef CSQC
1384         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1385         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1386                 self.velocity_z -= g * 0.5;
1387         else
1388                 self.velocity_z -= g;
1389         PM_ClientMovement_Move();
1390         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1391                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1392                         self.velocity_z -= g * 0.5;
1393 #endif
1394 }
1395 #ifdef SVQC
1396 void SV_WalkMove ()
1397 {
1398         // if PHYS_INPUT_TIMELENGTH is 0 (due to client sending the same timestamp twice),
1399         // don't move
1400         if (PHYS_INPUT_TIMELENGTH <= 0)
1401                 return;
1402
1403 //      if (autocvar_sv_gameplayfix_unstickplayers)
1404 //              SV_CheckStuck (self);
1405
1406 //      applygravity = !SV_CheckWater(self) && self.movetype == MOVETYPE_WALK && !(self.flags & FL_WATERJUMP);
1407
1408 //      hitsupercontentsmask = SV_GenericHitSuperContentsMask(self);
1409
1410 //      SV_CheckVelocity(self);
1411
1412         // do a regular slide move unless it looks like you ran into a step
1413 //      float oldonground = self.flags & FL_ONGROUND;
1414
1415         vector start_origin = self.origin;
1416         vector start_velocity = self.velocity;
1417
1418         float clip = 0;
1419 //      clip = SV_FlyMove (self, PHYS_INPUT_TIMELENGTH, applygravity, NULL, hitsupercontentsmask, sv_gameplayfix_stepmultipletimes.integer ? sv_stepheight.value : 0);
1420
1421 //      if(sv_gameplayfix_downtracesupportsongroundflag.integer)
1422 //      if(!(clip & 1))
1423         {
1424                 // only try this if there was no floor in the way in the trace (no,
1425                 // this check seems to be not REALLY necessary, because if clip & 1,
1426                 // our trace will hit that thing too)
1427                 vector upmove = self.origin;
1428                 upmove_z++;
1429                 vector downmove = self.origin;
1430                 upmove_z--;
1431                 float type;
1432                 if (self.movetype == MOVETYPE_FLYMISSILE)
1433                         type = MOVE_MISSILE;
1434                 else if (self.movetype == MOVETYPE_FLY_WORLDONLY)
1435                         type = MOVE_WORLDONLY;
1436                 else if (self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
1437                         type = MOVE_NOMONSTERS; // only clip against bmodels
1438                 else
1439                         type = MOVE_NORMAL;
1440                 vector entmins = self.mins;
1441                 vector entmaxs = self.maxs;
1442                 tracebox(upmove, entmins, entmaxs, downmove, type, self);
1443                 if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
1444                         clip |= 1; // but we HAVE found a floor
1445         }
1446
1447         // if the move did not hit the ground at any point, we're not on ground
1448 //      if(!(clip & 1))
1449 //              self.flags = self.flags & ~FL_ONGROUND;
1450
1451 //      SV_CheckVelocity(self);
1452 //      SV_LinkEdict(self);
1453 //      SV_LinkEdict_TouchAreaGrid(self);
1454
1455         if(clip & 8) // teleport
1456                 return;
1457
1458         if (self.flags & FL_WATERJUMP)
1459                 return;
1460
1461 //      if (autocvar_sv_nostep)
1462 //              return;
1463
1464         vector originalmove_origin = self.origin;
1465         vector originalmove_velocity = self.velocity;
1466         float originalmove_flags = self.flags;
1467         entity originalmove_groundentity = self.groundentity;
1468
1469         // if move didn't block on a step, return
1470         if (clip & 2)
1471         {
1472                 // if move was not trying to move into the step, return
1473                 if (fabs(start_velocity_x) < 0.03125 && fabs(start_velocity_y) < 0.03125)
1474                         return;
1475
1476                 if (self.movetype != MOVETYPE_FLY)
1477                 {
1478                         // return if gibbed by a trigger
1479                         if (self.movetype != MOVETYPE_WALK)
1480                                 return;
1481
1482                         // return if attempting to jump while airborn (unless sv_jumpstep)
1483 //                      if (!autocvar_sv_jumpstep)
1484 //                              if (!oldonground && PRVM_serveredictfloat(self, waterlevel) == 0)
1485 //                                      return;
1486                 }
1487
1488                 // try moving up and forward to go up a step
1489                 // back to start pos
1490                 self.origin = start_origin;
1491                 self.velocity = start_velocity;
1492
1493                 // move up
1494                 vector upmove = '0 0 0';
1495                 upmove_z = autocvar_sv_stepheight;
1496 //              if(!SV_PushEntity(&trace, self, upmove, true))
1497 //              {
1498 //                      // we got teleported when upstepping... must abort the move
1499 //                      return;
1500 //              }
1501
1502                 // move forward
1503                 self.velocity_z = 0;
1504 //              clip = SV_FlyMove (self, PHYS_INPUT_TIMELENGTH, applygravity, stepnormal, hitsupercontentsmask, 0);
1505                 self.velocity_z += start_velocity_z;
1506 //              if(clip & 8)
1507 //              {
1508 //                      // we got teleported when upstepping... must abort the move
1509 //                      // note that z velocity handling may not be what QC expects here, but we cannot help it
1510 //                      return;
1511 //              }
1512
1513 //              SV_CheckVelocity(self);
1514 //              SV_LinkEdict(self);
1515 //              SV_LinkEdict_TouchAreaGrid(self);
1516
1517                 // check for stuckness, possibly due to the limited precision of floats
1518                 // in the clipping hulls
1519                 if (clip
1520                  && fabs(originalmove_origin_y - self.origin_y < 0.03125)
1521                  && fabs(originalmove_origin_x - self.origin_x < 0.03125))
1522                 {
1523                         //Con_Printf("wall\n");
1524                         // stepping up didn't make any progress, revert to original move
1525                         self.origin = originalmove_origin;
1526                         self.velocity = originalmove_velocity;
1527                         self.flags = originalmove_flags;
1528                         self.groundentity = originalmove_groundentity;
1529                         return;
1530                 }
1531
1532                 //Con_Printf("step - ");
1533
1534                 // extra friction based on view angle
1535 //              if (clip & 2 && sv_wallfriction.integer)
1536 //                      SV_WallFriction (self, stepnormal);
1537         }
1538         // don't do the down move if stepdown is disabled, moving upward, not in water, or the move started offground or ended onground
1539 //      else if (!autocvar_sv_gameplayfix_stepdown || self.waterlevel >= 3 || start_velocity_z >= (1.0 / 32.0) || !oldonground || (self.flags & FL_ONGROUND))
1540 //              return;
1541
1542         // move down
1543         vector downmove = '0 0 0';
1544         downmove_z = -autocvar_sv_stepheight + start_velocity_z*PHYS_INPUT_TIMELENGTH;
1545 //      if(!SV_PushEntity (&downtrace, self, downmove, true))
1546 //      {
1547 //              // we got teleported when downstepping... must abort the move
1548 //              return;
1549 //      }
1550
1551         if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
1552         {
1553                 // this has been disabled so that you can't jump when you are stepping
1554                 // up while already jumping (also known as the Quake2 double jump bug)
1555         }
1556         else
1557         {
1558                 //Con_Printf("slope\n");
1559                 // if the push down didn't end up on good ground, use the move without
1560                 // the step up.  This happens near wall / slope combinations, and can
1561                 // cause the player to hop up higher on a slope too steep to climb
1562                 self.origin = originalmove_origin;
1563                 self.velocity = originalmove_velocity;
1564                 self.flags = originalmove_flags;
1565                 self.groundentity = originalmove_groundentity;
1566         }
1567
1568 //      SV_CheckVelocity(self);
1569 //      SV_LinkEdict(self);
1570 //      SV_LinkEdict_TouchAreaGrid(self);
1571 }
1572 #endif
1573 void PM_walk(float buttons_prev, float maxspd_mod)
1574 {
1575 #ifdef SVQC
1576         if (!(self.lastflags & FL_ONGROUND))
1577         {
1578                 if (autocvar_speedmeter)
1579                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1580                 if (self.lastground < time - 0.3)
1581                         self.velocity *= (1 - autocvar_sv_friction_on_land);
1582                 if (self.jumppadcount > 1)
1583                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1584                 self.jumppadcount = 0;
1585         }
1586 #endif
1587         // walking
1588         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1589         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1590                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1591         // acceleration
1592         vector wishdir = normalize(wishvel);
1593         float wishspeed = vlen(wishvel);
1594
1595         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1596         if (IS_DUCKED(self))
1597                 wishspeed *= 0.5;
1598
1599         // apply edge friction
1600         float f = vlen(vec2(self.velocity));
1601         if (f > 0)
1602         {
1603                 // TODO: apply edge friction
1604                 // apply ground friction
1605                 f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1606                 f = max(0, f);
1607                 self.velocity *= f;
1608                 /*
1609                    Mathematical analysis time!
1610
1611                    Our goal is to invert this mess.
1612
1613                    For the two cases we get:
1614                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1615                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1616                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1617                    and
1618                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1619                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1620
1621                    These cases would be chosen ONLY if:
1622                         v0 < PHYS_STOPSPEED
1623                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1624                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1625                    and, respectively:
1626                         v0 >= PHYS_STOPSPEED
1627                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1628                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1629                  */
1630         }
1631         float addspeed = wishspeed - self.velocity * wishdir;
1632         if (addspeed > 0)
1633         {
1634                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1635                 self.velocity += accelspeed * wishdir;
1636         }
1637         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1638         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1639                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1640         if (self.velocity * self.velocity)
1641                 PM_ClientMovement_Move();
1642         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1643                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1644                         self.velocity_z -= g * 0.5;
1645 }
1646
1647 void PM_air(float buttons_prev, float maxspd_mod)
1648 {
1649         makevectors(PHYS_INPUT_ANGLES(self).y * '0 1 0');
1650         vector wishvel = v_forward * PHYS_INPUT_MOVEVALUES(self).x
1651                                         + v_right * PHYS_INPUT_MOVEVALUES(self).y;
1652         // acceleration
1653         vector wishdir = normalize(wishvel);
1654         float wishspeed = vlen(wishvel);
1655
1656 #ifdef SVQC
1657         if (time >= self.teleport_time)
1658 #else
1659         if (pmove_waterjumptime <= 0)
1660 #endif
1661         {
1662                 float maxairspd = PHYS_MAXAIRSPEED * min(maxspd_mod, 1);
1663
1664                 // apply air speed limit
1665                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1666                 float wishspeed0 = wishspeed;
1667                 wishspeed = min(wishspeed, maxairspd);
1668                 if (IS_DUCKED(self))
1669                         wishspeed *= 0.5;
1670                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1671
1672                 float accelerating = (self.velocity * wishdir > 0);
1673                 float wishspeed2 = wishspeed;
1674
1675                 // CPM: air control
1676                 if (PHYS_AIRSTOPACCELERATE)
1677                 {
1678                         vector curdir = normalize(vec2(self.velocity));
1679                         airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1680                 }
1681                 // note that for straight forward jumping:
1682                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1683                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1684                 // -->
1685                 // dv/dt = accel * maxspeed (when slow)
1686                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1687                 // log dv/dt = logaccel + logmaxspeed (when slow)
1688                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1689                 float strafity = IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), -90) + IsMoveInDirection(PHYS_INPUT_MOVEVALUES(self), +90); // if one is nonzero, other is always zero
1690                 if (PHYS_MAXAIRSTRAFESPEED)
1691                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1692                 if (PHYS_AIRSTRAFEACCELERATE)
1693                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE*maxspd_mod);
1694                 if (PHYS_AIRSTRAFEACCEL_QW(self))
1695                         airaccelqw =
1696                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1697                 *
1698                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1699                 // !CPM
1700
1701                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && PHYS_INPUT_MOVEVALUES(self).y == 0 && PHYS_INPUT_MOVEVALUES(self).x != 0)
1702                         PM_AirAccelerate(wishdir, wishspeed2);
1703                 else
1704                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1705
1706                 if (PHYS_AIRCONTROL)
1707                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1708         }
1709         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1710         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1711                 self.velocity_z -= g * 0.5;
1712         else
1713                 self.velocity_z -= g;
1714         PM_ClientMovement_Move();
1715         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1716                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1717                         self.velocity_z -= g * 0.5;
1718 }
1719
1720 // used for calculating airshots
1721 float PM_is_flying()
1722 {
1723         if (IS_ONGROUND(self))
1724                 return 0;
1725         if (self.waterlevel >= WATERLEVEL_SWIMMING)
1726                 return 0;
1727         traceline(self.origin, self.origin - '0 0 48', MOVE_NORMAL, self);
1728         return trace_fraction >= 1;
1729 }
1730
1731 void PM_Main()
1732 {
1733         float buttons = PHYS_INPUT_BUTTON_MASK(self);
1734 #ifdef CSQC
1735         self.team = myteam + 1; // is this correct?
1736         if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1737                 UNSET_JUMP_HELD(self); // canjump = true
1738         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1739 #endif
1740         PM_ClientMovement_UpdateStatus();
1741
1742 #ifdef SVQC
1743         WarpZone_PlayerPhysics_FixVAngle();
1744 #endif
1745         float maxspeed_mod = 1;
1746         maxspeed_mod *= PM_check_keepaway();
1747         maxspeed_mod *= PHYS_HIGHSPEED;
1748
1749 #ifdef SVQC
1750         Physics_UpdateStats(maxspeed_mod);
1751
1752         if (self.PlayerPhysplug)
1753                 if (self.PlayerPhysplug())
1754                         return;
1755 #endif
1756
1757         PM_check_race_movetime();
1758 #ifdef SVQC
1759         anticheat_physics();
1760 #endif
1761
1762         if (PM_check_specialcommand(buttons))
1763                 return;
1764 #ifdef SVQC
1765         if (sv_maxidle > 0)
1766         {
1767                 if (buttons != self.buttons_old || PHYS_INPUT_MOVEVALUES(self) != self.movement_old || PHYS_INPUT_ANGLES(self) != self.v_angle_old)
1768                         self.parm_idlesince = time;
1769         }
1770 #endif
1771         float buttons_prev = self.buttons_old;
1772         self.buttons_old = buttons;
1773         self.movement_old = PHYS_INPUT_MOVEVALUES(self);
1774         self.v_angle_old = PHYS_INPUT_ANGLES(self);
1775
1776         PM_check_nickspam();
1777
1778         PM_check_punch();
1779 #ifdef SVQC
1780         if (IS_BOT_CLIENT(self))
1781         {
1782                 if (playerdemo_read())
1783                         return;
1784                 bot_think();
1785         }
1786
1787         if (IS_PLAYER(self))
1788 #endif
1789         {
1790 #ifdef SVQC
1791                 if (self.race_penalty)
1792                         if (time > self.race_penalty)
1793                                 self.race_penalty = 0;
1794 #endif
1795
1796                 float not_allowed_to_move = 0;
1797 #ifdef SVQC
1798                 if (self.race_penalty)
1799                         not_allowed_to_move = 1;
1800 #endif
1801 #ifdef SVQC
1802                 if (!autocvar_sv_ready_restart_after_countdown)
1803                         if (time < game_starttime)
1804                                 not_allowed_to_move = 1;
1805 #endif
1806
1807                 if (not_allowed_to_move)
1808                 {
1809                         self.velocity = '0 0 0';
1810                         self.movetype = MOVETYPE_NONE;
1811 #ifdef SVQC
1812                         self.disableclientprediction = 2;
1813 #endif
1814                 }
1815 #ifdef SVQC
1816                 else if (self.disableclientprediction == 2)
1817                 {
1818                         if (self.movetype == MOVETYPE_NONE)
1819                                 self.movetype = MOVETYPE_WALK;
1820                         self.disableclientprediction = 0;
1821                 }
1822 #endif
1823         }
1824
1825 #ifdef SVQC
1826         if (self.movetype == MOVETYPE_NONE)
1827                 return;
1828
1829         // when we get here, disableclientprediction cannot be 2
1830         self.disableclientprediction = 0;
1831 #endif
1832
1833         PM_check_spider();
1834
1835         PM_check_frozen();
1836
1837         PM_check_blocked();
1838
1839         maxspeed_mod = 1;
1840
1841 #ifdef SVQC
1842         if (self.in_swamp) {
1843                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1844         }
1845 #endif
1846
1847 #ifdef SVQC
1848         // conveyors: first fix velocity
1849         if (self.conveyor.state)
1850                 self.velocity -= self.conveyor.movedir;
1851 #endif
1852
1853 #ifdef SVQC
1854         MUTATOR_CALLHOOK(PlayerPhysics);
1855 #endif
1856 #ifdef CSQC
1857         PM_multijump();
1858 #endif
1859
1860 //      float forcedodge = 1;
1861 //      if(forcedodge) {
1862 //#ifdef CSQC
1863 //              PM_dodging_checkpressedkeys();
1864 //#endif
1865 //              PM_dodging();
1866 //              PM_ClientMovement_Move();
1867 //              return;
1868 //      }
1869
1870 #ifdef SVQC
1871         if (!IS_PLAYER(self))
1872         {
1873                 maxspeed_mod *= autocvar_sv_spectator_speed_multiplier;
1874                 if (!self.spectatorspeed)
1875                         self.spectatorspeed = maxspeed_mod;
1876                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1877                 {
1878                         if (self.lastclassname != "player")
1879                         {
1880                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1881                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1882                                 else if (self.impulse == 11)
1883                                         self.spectatorspeed = maxspeed_mod;
1884                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1885                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1886                                 else if (self.impulse >= 1 && self.impulse <= 9)
1887                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1888                         } // otherwise just clear
1889                         self.impulse = 0;
1890                 }
1891                 maxspeed_mod *= self.spectatorspeed;
1892         }
1893 #endif
1894
1895 #ifdef SVQC
1896         // if dead, behave differently
1897         // in CSQC, physics don't handle dead player
1898         if (self.deadflag)
1899                 goto end;
1900 #endif
1901
1902 #ifdef SVQC
1903         if (!self.fixangle && !g_bugrigs)
1904                 self.angles = '0 1 0' * PHYS_INPUT_ANGLES(self).y;
1905 #endif
1906
1907 #ifdef SVQC
1908         if (IS_ONGROUND(self))
1909         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1910         if (self.wasFlying)
1911         {
1912                 self.wasFlying = 0;
1913                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1914                 if (time >= self.ladder_time)
1915                 if (!self.hook)
1916                 {
1917                         self.nextstep = time + 0.3 + random() * 0.1;
1918                         trace_dphitq3surfaceflags = 0;
1919                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1920                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1921                         {
1922                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1923                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1924                                 else
1925                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1926                         }
1927                 }
1928         }
1929 #endif
1930
1931         if (PM_is_flying())
1932                 self.wasFlying = 1;
1933
1934 #ifdef SVQC
1935         if (IS_PLAYER(self))
1936 #endif
1937                 CheckPlayerJump();
1938
1939         PM_check_jumppad();
1940
1941         if (self.flags & /* FL_WATERJUMP */ 2048)
1942         {
1943                 self.velocity_x = self.movedir_x;
1944                 self.velocity_y = self.movedir_y;
1945                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1946                 {
1947                         self.flags &= ~/* FL_WATERJUMP */ 2048;
1948                         self.teleport_time = 0;
1949                 }
1950         }
1951
1952 #ifdef SVQC
1953         else if (g_bugrigs && IS_PLAYER(self))
1954                 RaceCarPhysics();
1955 #endif
1956
1957         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
1958                 PM_fly(maxspeed_mod);
1959
1960         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1961                 PM_swim(maxspeed_mod);
1962
1963         else if (time < self.ladder_time)
1964                 PM_ladder(maxspeed_mod);
1965
1966         else if (ITEMS(self) & IT_USING_JETPACK)
1967                 PM_jetpack(maxspeed_mod);
1968
1969         else
1970         {
1971                 if (IS_ONGROUND(self))
1972                         PM_walk(buttons_prev, maxspeed_mod);
1973                 else
1974                         PM_air(buttons_prev, maxspeed_mod);
1975         }
1976
1977 #ifdef SVQC
1978         if (!IS_OBSERVER(self))
1979                 PM_check_race();
1980 #endif
1981         PM_check_vortex();
1982
1983 :end
1984         if (IS_ONGROUND(self))
1985                 self.lastground = time;
1986
1987 #ifdef SVQC
1988         // conveyors: then break velocity again
1989         if (self.conveyor.state)
1990                 self.velocity += self.conveyor.movedir;
1991 #endif
1992         self.lastflags = self.flags;
1993         self.lastclassname = self.classname;
1994 }
1995
1996 #ifdef SVQC
1997 void SV_PlayerPhysics(void)
1998 #elif defined(CSQC)
1999 void CSQC_ClientMovement_PlayerMove_Frame(void)
2000 #endif
2001 {
2002         PM_Main();
2003 }