]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
Merge remote-tracking branch 'origin/master' into samual/splashing
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_physics.qc
1 .float race_penalty;
2 .float restart_jump;
3
4 .float ladder_time;
5 .entity ladder_entity;
6 .float gravity;
7 .float swamp_slowdown;
8 .float lastflags;
9 .float lastground;
10 .float wasFlying;
11 .float spectatorspeed;
12
13 /*
14 =============
15 PlayerJump
16
17 When you press the jump key
18 =============
19 */
20 void PlayerJump (void)
21 {
22         float doublejump = FALSE;
23
24         player_multijump = doublejump;
25         if(MUTATOR_CALLHOOK(PlayerJump))
26                 return;
27
28         doublejump = player_multijump;
29
30         float mjumpheight;
31
32         if (autocvar_sv_doublejump)
33         {
34                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
35                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
36                 {
37                         doublejump = TRUE;
38
39                         // we MUST clip velocity here!
40                         float f;
41                         f = self.velocity * trace_plane_normal;
42                         if(f < 0)
43                                 self.velocity -= f * trace_plane_normal;
44                 }
45         }
46
47         mjumpheight = autocvar_sv_jumpvelocity;
48         if (self.waterlevel >= WATERLEVEL_SWIMMING)
49         {
50                 self.velocity_z = self.stat_sv_maxspeed * 0.7;
51                 return;
52         }
53
54         if (!doublejump)
55                 if (!(self.flags & FL_ONGROUND))
56                         return;
57
58         if(self.cvar_cl_movement_track_canjump)
59                 if (!(self.flags & FL_JUMPRELEASED))
60                         return;
61
62         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
63         // velocity bounds.  Final velocity is bound between (jumpheight *
64         // min + jumpheight) and (jumpheight * max + jumpheight);
65
66         if(autocvar_sv_jumpspeedcap_min != "")
67         {
68                 float minjumpspeed;
69
70                 minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
71
72                 if (self.velocity_z < minjumpspeed)
73                         mjumpheight += minjumpspeed - self.velocity_z;
74         }
75
76         if(autocvar_sv_jumpspeedcap_max != "")
77         {
78                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
79                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
80
81                 if(!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
82                 {
83                         float maxjumpspeed;
84
85                         maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
86
87                         if (self.velocity_z > maxjumpspeed)
88                                 mjumpheight -= self.velocity_z - maxjumpspeed;
89                 }
90         }
91
92         if(!(self.lastflags & FL_ONGROUND))
93         {
94                 if(autocvar_speedmeter)
95                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
96                 if(self.lastground < time - 0.3)
97                 {
98                         self.velocity_x *= (1 - autocvar_sv_friction_on_land);
99                         self.velocity_y *= (1 - autocvar_sv_friction_on_land);
100                 }
101                 if(self.jumppadcount > 1)
102                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
103                 self.jumppadcount = 0;
104         }
105
106         self.velocity_z = self.velocity_z + mjumpheight;
107         self.oldvelocity_z = self.velocity_z;
108
109         self.flags &= ~FL_ONGROUND;
110         self.flags &= ~FL_JUMPRELEASED;
111
112         animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
113
114         if(autocvar_g_jump_grunt)
115                 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
116
117         self.restart_jump = -1; // restart jump anim next time
118         // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
119 }
120 void CheckWaterJump()
121 {
122         vector start, end;
123
124 // check for a jump-out-of-water
125         makevectors (self.angles);
126         start = self.origin;
127         start_z = start_z + 8;
128         v_forward_z = 0;
129         normalize(v_forward);
130         end = start + v_forward*24;
131         traceline (start, end, TRUE, self);
132         if (trace_fraction < 1)
133         {       // solid at waist
134                 start_z = start_z + self.maxs_z - 8;
135                 end = start + v_forward*24;
136                 self.movedir = trace_plane_normal * -50;
137                 traceline (start, end, TRUE, self);
138                 if (trace_fraction == 1)
139                 {       // open at eye level
140                         self.flags |= FL_WATERJUMP;
141                         self.velocity_z = 225;
142                         self.flags &= ~FL_JUMPRELEASED;
143                         self.teleport_time = time + 2;  // safety net
144                         return;
145                 }
146         }
147 }
148 void CheckPlayerJump()
149 {
150         if (self.BUTTON_JUMP)
151                 PlayerJump ();
152         else
153                 self.flags |= FL_JUMPRELEASED;
154
155         if (self.waterlevel == WATERLEVEL_SWIMMING)
156                 CheckWaterJump ();
157 }
158
159 float racecar_angle(float forward, float down)
160 {
161         float ret, angle_mult;
162
163         if(forward < 0)
164         {
165                 forward = -forward;
166                 down = -down;
167         }
168
169         ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
170
171         angle_mult = forward / (800 + forward);
172
173         if(ret > 180)
174                 return ret * angle_mult + 360 * (1 - angle_mult);
175         else
176                 return ret * angle_mult;
177 }
178
179 void RaceCarPhysics()
180 {
181         // using this move type for "big rigs"
182         // the engine does not push the entity!
183
184         float accel, steer, f, myspeed, steerfactor;
185         vector angles_save, rigvel;
186
187         angles_save = self.angles;
188         accel = bound(-1, self.movement_x / self.stat_sv_maxspeed, 1);
189         steer = bound(-1, self.movement_y / self.stat_sv_maxspeed, 1);
190
191         if(g_bugrigs_reverse_speeding)
192         {
193                 if(accel < 0)
194                 {
195                         // back accel is DIGITAL
196                         // to prevent speedhack
197                         if(accel < -0.5)
198                                 accel = -1;
199                         else
200                                 accel = 0;
201                 }
202         }
203
204         self.angles_x = 0;
205         self.angles_z = 0;
206         makevectors(self.angles); // new forward direction!
207
208         if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
209         {
210                 float upspeed, accelfactor;
211
212                 myspeed = self.velocity * v_forward;
213                 upspeed = self.velocity * v_up;
214
215                 // responsiveness factor for steering and acceleration
216                 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
217                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
218
219                 if(myspeed < 0 && g_bugrigs_reverse_spinning)
220                         steerfactor = -myspeed * g_bugrigs_steer;
221                 else
222                         steerfactor = -myspeed * f * g_bugrigs_steer;
223
224                 if(myspeed < 0 && g_bugrigs_reverse_speeding)
225                         accelfactor = g_bugrigs_accel;
226                 else
227                         accelfactor = f * g_bugrigs_accel;
228                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
229
230                 if(accel < 0)
231                 {
232                         if(myspeed > 0)
233                         {
234                                 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
235                         }
236                         else
237                         {
238                                 if(!g_bugrigs_reverse_speeding)
239                                         myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
240                         }
241                 }
242                 else
243                 {
244                         if(myspeed >= 0)
245                         {
246                                 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
247                         }
248                         else
249                         {
250                                 if(g_bugrigs_reverse_stopping)
251                                         myspeed = 0;
252                                 else
253                                         myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
254                         }
255                 }
256                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
257                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
258
259                 self.angles_y += steer * frametime * steerfactor; // apply steering
260                 makevectors(self.angles); // new forward direction!
261
262                 myspeed += accel * accelfactor * frametime;
263
264                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
265         }
266         else
267         {
268                 myspeed = vlen(self.velocity);
269
270                 // responsiveness factor for steering and acceleration
271                 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
272                 steerfactor = -myspeed * f;
273                 self.angles_y += steer * frametime * steerfactor; // apply steering
274
275                 rigvel = self.velocity;
276                 makevectors(self.angles); // new forward direction!
277         }
278
279         rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
280         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
281         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
282         //MAXIMA: solve(total_acceleration(v) = 0, v);
283
284         if(g_bugrigs_planar_movement)
285         {
286                 vector rigvel_xy, neworigin, up;
287                 float mt;
288
289                 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
290                 rigvel_xy = vec2(rigvel);
291
292                 if(g_bugrigs_planar_movement_car_jumping)
293                         mt = MOVE_NORMAL;
294                 else
295                         mt = MOVE_NOMONSTERS;
296
297                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
298                 up = trace_endpos - self.origin;
299
300                 // BUG RIGS: align the move to the surface instead of doing collision testing
301                 // can we move?
302                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
303
304                 // align to surface
305                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
306
307                 if(trace_fraction < 0.5)
308                 {
309                         trace_fraction = 1;
310                         neworigin = self.origin;
311                 }
312                 else
313                         neworigin = trace_endpos;
314
315                 if(trace_fraction < 1)
316                 {
317                         // now set angles_x so that the car points parallel to the surface
318                         self.angles = vectoangles(
319                                         '1 0 0' * v_forward_x * trace_plane_normal_z
320                                         +
321                                         '0 1 0' * v_forward_y * trace_plane_normal_z
322                                         +
323                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
324                                         );
325                         self.flags |= FL_ONGROUND;
326                 }
327                 else
328                 {
329                         // now set angles_x so that the car points forward, but is tilted in velocity direction
330                         self.flags &= ~FL_ONGROUND;
331                 }
332
333                 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
334                 self.movetype = MOVETYPE_NOCLIP;
335         }
336         else
337         {
338                 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
339                 self.velocity = rigvel;
340                 self.movetype = MOVETYPE_FLY;
341         }
342
343         trace_fraction = 1;
344         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
345         if(trace_fraction != 1)
346         {
347                 self.angles = vectoangles2(
348                                 '1 0 0' * v_forward_x * trace_plane_normal_z
349                                 +
350                                 '0 1 0' * v_forward_y * trace_plane_normal_z
351                                 +
352                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
353                                 trace_plane_normal
354                                 );
355         }
356         else
357         {
358                 vector vel_local;
359
360                 vel_local_x = v_forward * self.velocity;
361                 vel_local_y = v_right * self.velocity;
362                 vel_local_z = v_up * self.velocity;
363
364                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
365                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
366         }
367
368         // smooth the angles
369         vector vf1, vu1, smoothangles;
370         makevectors(self.angles);
371         f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
372         if(f == 0)
373                 f = 1;
374         vf1 = v_forward * f;
375         vu1 = v_up * f;
376         makevectors(angles_save);
377         vf1 = vf1 + v_forward * (1 - f);
378         vu1 = vu1 + v_up * (1 - f);
379         smoothangles = vectoangles2(vf1, vu1);
380         self.angles_x = -smoothangles_x;
381         self.angles_z =  smoothangles_z;
382 }
383
384 float IsMoveInDirection(vector mv, float angle) // key mix factor
385 {
386         if(mv_x == 0 && mv_y == 0)
387                 return 0; // avoid division by zero
388         angle -= RAD2DEG * atan2(mv_y, mv_x);
389         angle = remainder(angle, 360) / 45;
390         if(angle >  1)
391                 return 0;
392         if(angle < -1)
393                 return 0;
394         return 1 - fabs(angle);
395 }
396
397 float GeomLerp(float a, float lerp, float b)
398 {
399         if(a == 0)
400         {
401                 if(lerp < 1)
402                         return 0;
403                 else
404                         return b;
405         }
406         if(b == 0)
407         {
408                 if(lerp > 0)
409                         return 0;
410                 else
411                         return a;
412         }
413         return a * pow(fabs(b / a), lerp);
414 }
415
416 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
417 {
418         float zspeed, xyspeed, dot, k;
419
420 #if 0
421         // this doesn't play well with analog input
422         if(self.movement_x == 0 || self.movement_y != 0)
423                 return; // can't control movement if not moving forward or backward
424         k = 32;
425 #else
426         k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
427         if(k <= 0)
428                 return;
429 #endif
430
431         k *= bound(0, wishspeed / autocvar_sv_maxairspeed, 1);
432
433         zspeed = self.velocity_z;
434         self.velocity_z = 0;
435         xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
436
437         dot = self.velocity * wishdir;
438
439         if(dot > 0) // we can't change direction while slowing down
440         {
441                 k *= pow(dot, autocvar_sv_aircontrol_power)*frametime;
442                 xyspeed = max(0, xyspeed - autocvar_sv_aircontrol_penalty * sqrt(max(0, 1 - dot*dot)) * k/32);
443                 k *= autocvar_sv_aircontrol;
444                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
445         }
446
447         self.velocity = self.velocity * xyspeed;
448         self.velocity_z = zspeed;
449 }
450
451 float AdjustAirAccelQW(float accelqw, float factor)
452 {
453         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
454 }
455
456 // example config for alternate speed clamping:
457 //   sv_airaccel_qw 0.8
458 //   sv_airaccel_sideways_friction 0
459 //   prvm_globalset server speedclamp_mode 1
460 //     (or 2)
461 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
462 {
463         float vel_straight;
464         float vel_z;
465         vector vel_perpend;
466         float step;
467
468         vector vel_xy;
469         float vel_xy_current;
470         float vel_xy_backward, vel_xy_forward;
471         float speedclamp;
472
473         if(stretchfactor > 0)
474                 speedclamp = stretchfactor;
475         else if(accelqw < 0)
476                 speedclamp = 1; // full clamping, no stretch
477         else
478                 speedclamp = -1; // no clamping
479
480         if(accelqw < 0)
481                 accelqw = -accelqw;
482
483         if(autocvar_sv_gameplayfix_q2airaccelerate)
484                 wishspeed0 = wishspeed;
485
486         vel_straight = self.velocity * wishdir;
487         vel_z = self.velocity_z;
488         vel_xy = vec2(self.velocity);
489         vel_perpend = vel_xy - vel_straight * wishdir;
490
491         step = accel * frametime * wishspeed0;
492
493         vel_xy_current  = vlen(vel_xy);
494         if(speedlimit)
495                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
496         vel_xy_forward  = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
497         vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
498         if(vel_xy_backward < 0)
499                 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
500
501         vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
502
503         if(sidefric < 0 && (vel_perpend*vel_perpend))
504                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
505         {
506                 float f, fminimum;
507                 f = max(0, 1 + frametime * wishspeed * sidefric);
508                 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
509                 // this cannot be > 1
510                 if(fminimum <= 0)
511                         vel_perpend = vel_perpend * max(0, f);
512                 else
513                 {
514                         fminimum = sqrt(fminimum);
515                         vel_perpend = vel_perpend * max(fminimum, f);
516                 }
517         }
518         else
519                 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
520
521         vel_xy = vel_straight * wishdir + vel_perpend;
522
523         if(speedclamp >= 0)
524         {
525                 float vel_xy_preclamp;
526                 vel_xy_preclamp = vlen(vel_xy);
527                 if(vel_xy_preclamp > 0) // prevent division by zero
528                 {
529                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
530                         if(vel_xy_current < vel_xy_preclamp)
531                                 vel_xy = vel_xy * (vel_xy_current / vel_xy_preclamp);
532                 }
533         }
534
535         self.velocity = vel_xy + vel_z * '0 0 1';
536 }
537
538 void PM_AirAccelerate(vector wishdir, float wishspeed)
539 {
540         vector curvel, wishvel, acceldir, curdir;
541         float addspeed, accelspeed, curspeed, f;
542         float dot;
543
544         if(wishspeed == 0)
545                 return;
546
547         curvel = self.velocity;
548         curvel_z = 0;
549         curspeed = vlen(curvel);
550
551         if(wishspeed > curspeed * 1.01)
552         {
553                 wishspeed = min(wishspeed, curspeed + autocvar_sv_warsowbunny_airforwardaccel * self.stat_sv_maxspeed * frametime);
554         }
555         else
556         {
557                 f = max(0, (autocvar_sv_warsowbunny_topspeed - curspeed) / (autocvar_sv_warsowbunny_topspeed - self.stat_sv_maxspeed));
558                 wishspeed = max(curspeed, self.stat_sv_maxspeed) + autocvar_sv_warsowbunny_accel * f * self.stat_sv_maxspeed * frametime;
559         }
560         wishvel = wishdir * wishspeed;
561         acceldir = wishvel - curvel;
562         addspeed = vlen(acceldir);
563         acceldir = normalize(acceldir);
564
565         accelspeed = min(addspeed, autocvar_sv_warsowbunny_turnaccel * self.stat_sv_maxspeed * frametime);
566
567         if(autocvar_sv_warsowbunny_backtosideratio < 1)
568         {
569                 curdir = normalize(curvel);
570                 dot = acceldir * curdir;
571                 if(dot < 0)
572                         acceldir = acceldir - (1 - autocvar_sv_warsowbunny_backtosideratio) * dot * curdir;
573         }
574
575         self.velocity += accelspeed * acceldir;
576 }
577
578 .vector movement_old;
579 .float buttons_old;
580 .vector v_angle_old;
581 .string lastclassname;
582
583 .float() PlayerPhysplug;
584
585 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
586 .float specialcommand_pos;
587 void SpecialCommand()
588 {
589 #ifdef TETRIS
590         TetrisImpulse();
591 #else
592         if(!CheatImpulse(99))
593                 print("A hollow voice says \"Plugh\".\n");
594 #endif
595 }
596
597 float speedaward_speed;
598 string speedaward_holder;
599 string speedaward_uid;
600 void race_send_speedaward(float msg)
601 {
602         // send the best speed of the round
603         WriteByte(msg, SVC_TEMPENTITY);
604         WriteByte(msg, TE_CSQC_RACE);
605         WriteByte(msg, RACE_NET_SPEED_AWARD);
606         WriteInt24_t(msg, floor(speedaward_speed+0.5));
607         WriteString(msg, speedaward_holder);
608 }
609
610 float speedaward_alltimebest;
611 string speedaward_alltimebest_holder;
612 string speedaward_alltimebest_uid;
613 void race_send_speedaward_alltimebest(float msg)
614 {
615         // send the best speed
616         WriteByte(msg, SVC_TEMPENTITY);
617         WriteByte(msg, TE_CSQC_RACE);
618         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
619         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
620         WriteString(msg, speedaward_alltimebest_holder);
621 }
622
623 string GetMapname(void);
624 float speedaward_lastupdate;
625 float speedaward_lastsent;
626 void SV_PlayerPhysics()
627 {
628         vector wishvel, wishdir, v;
629         float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
630         string temps;
631         float buttons_prev;
632         float not_allowed_to_move;
633         string c;
634
635         WarpZone_PlayerPhysics_FixVAngle();
636
637         maxspd_mod = 1;
638         if(self.ballcarried)
639                 if(g_nexball)
640                         maxspd_mod *= autocvar_g_nexball_basketball_carrier_highspeed;
641                 else if(g_keepaway)
642                         maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
643
644         maxspd_mod *= autocvar_g_movement_highspeed;
645
646         // fix physics stats for g_movement_highspeed
647         // TODO maybe rather use maxairspeed? needs testing
648         self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
649         if(autocvar_sv_airstrafeaccel_qw)
650                 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
651         else
652                 self.stat_sv_airstrafeaccel_qw = 0;
653         self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
654         self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
655
656     if(self.PlayerPhysplug)
657         if(self.PlayerPhysplug())
658             return;
659
660         self.race_movetime_frac += frametime;
661         f = floor(self.race_movetime_frac);
662         self.race_movetime_frac -= f;
663         self.race_movetime_count += f;
664         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
665
666         anticheat_physics();
667
668         buttons = self.BUTTON_ATCK + 2 * self.BUTTON_JUMP + 4 * self.BUTTON_ATCK2 + 8 * self.BUTTON_ZOOM + 16 * self.BUTTON_CROUCH + 32 * self.BUTTON_HOOK + 64 * self.BUTTON_USE + 128 * (self.movement_x < 0) + 256 * (self.movement_x > 0) + 512 * (self.movement_y < 0) + 1024 * (self.movement_y > 0);
669
670         if(!buttons)
671                 c = "x";
672         else if(buttons == 1)
673                 c = "1";
674         else if(buttons == 2)
675                 c = " ";
676         else if(buttons == 128)
677                 c = "s";
678         else if(buttons == 256)
679                 c = "w";
680         else if(buttons == 512)
681                 c = "a";
682         else if(buttons == 1024)
683                 c = "d";
684         else
685                 c = "?";
686
687         if(c == substring(specialcommand, self.specialcommand_pos, 1))
688         {
689                 self.specialcommand_pos += 1;
690                 if(self.specialcommand_pos >= strlen(specialcommand))
691                 {
692                         self.specialcommand_pos = 0;
693                         SpecialCommand();
694                         return;
695                 }
696         }
697         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
698                 self.specialcommand_pos = 0;
699
700         if(sv_maxidle > 0)
701         {
702                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
703                         self.parm_idlesince = time;
704         }
705         buttons_prev = self.buttons_old;
706         self.buttons_old = buttons;
707         self.movement_old = self.movement;
708         self.v_angle_old = self.v_angle;
709
710         if(time < self.nickspamtime)
711         if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
712         {
713                 // slight annoyance for nick change scripts
714                 self.movement = -1 * self.movement;
715                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
716
717                 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!
718                 {
719                         self.angles_x = random() * 360;
720                         self.angles_y = random() * 360;
721                         // at least I'm not forcing retardedview by also assigning to angles_z
722                         self.fixangle = TRUE;
723                 }
724         }
725
726         if (self.punchangle != '0 0 0')
727         {
728                 f = vlen(self.punchangle) - 10 * frametime;
729                 if (f > 0)
730                         self.punchangle = normalize(self.punchangle) * f;
731                 else
732                         self.punchangle = '0 0 0';
733         }
734
735         if (self.punchvector != '0 0 0')
736         {
737                 f = vlen(self.punchvector) - 30 * frametime;
738                 if (f > 0)
739                         self.punchvector = normalize(self.punchvector) * f;
740                 else
741                         self.punchvector = '0 0 0';
742         }
743
744         if (IS_BOT_CLIENT(self))
745         {
746                 if(playerdemo_read())
747                         return;
748                 bot_think();
749         }
750
751         self.items &= ~IT_USING_JETPACK;
752
753         if(IS_PLAYER(self))
754         {
755                 if(self.race_penalty)
756                         if(time > self.race_penalty)
757                                 self.race_penalty = 0;
758
759                 not_allowed_to_move = 0;
760                 if(self.race_penalty)
761                         not_allowed_to_move = 1;
762                 if(!autocvar_sv_ready_restart_after_countdown)
763                 if(time < game_starttime)
764                         not_allowed_to_move = 1;
765
766                 if(not_allowed_to_move)
767                 {
768                         self.velocity = '0 0 0';
769                         self.movetype = MOVETYPE_NONE;
770                         self.disableclientprediction = 2;
771                 }
772                 else if(self.disableclientprediction == 2)
773                 {
774                         if(self.movetype == MOVETYPE_NONE)
775                                 self.movetype = MOVETYPE_WALK;
776                         self.disableclientprediction = 0;
777                 }
778         }
779
780         if (self.movetype == MOVETYPE_NONE)
781                 return;
782
783         // when we get here, disableclientprediction cannot be 2
784         self.disableclientprediction = 0;
785         if(time < self.ladder_time)
786                 self.disableclientprediction = 1;
787
788         if(time < self.spider_slowness)
789         {
790                 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
791                 self.stat_sv_airspeedlimit_nonqw *= 0.5;
792         }
793
794         MUTATOR_CALLHOOK(PlayerPhysics);
795
796         if(self.player_blocked)
797         {
798                 self.movement = '0 0 0';
799                 self.disableclientprediction = 1;
800         }
801
802         maxspd_mod = 1;
803
804         swampspd_mod = 1;
805         if(self.in_swamp) {
806                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
807         }
808
809         // conveyors: first fix velocity
810         if(self.conveyor.state)
811                 self.velocity -= self.conveyor.movedir;
812
813         if (!IS_PLAYER(self))
814         {
815                 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
816                 if(!self.spectatorspeed)
817                         self.spectatorspeed = maxspd_mod;
818                 if(self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
819                 {
820                         if(self.lastclassname != "player")
821                         {
822                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
823                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
824                                 else if(self.impulse == 11)
825                                         self.spectatorspeed = maxspd_mod;
826                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
827                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
828                                 else if(self.impulse >= 1 && self.impulse <= 9)
829                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
830                         } // otherwise just clear
831                         self.impulse = 0;
832                 }
833                 maxspd_mod = self.spectatorspeed;
834         }
835
836         spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
837         if(self.speed != spd)
838         {
839                 self.speed = spd;
840                 temps = ftos(spd);
841                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
842                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
843                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
844                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
845         }
846
847         maxspd_mod *= swampspd_mod; // only one common speed modder please!
848         swampspd_mod = 1;
849
850         // if dead, behave differently
851         if (self.deadflag)
852                 goto end;
853
854         if (!self.fixangle && !g_bugrigs)
855         {
856                 self.angles_x = 0;
857                 self.angles_y = self.v_angle_y;
858                 self.angles_z = 0;
859         }
860
861         if(self.flags & FL_ONGROUND)
862         if(IS_PLAYER(self)) // no fall sounds for observers thank you very much
863         if(self.wasFlying)
864         {
865                 self.wasFlying = 0;
866
867                 if(self.waterlevel < WATERLEVEL_SWIMMING)
868                 if(time >= self.ladder_time)
869                 if (!self.hook)
870                 {
871                         self.nextstep = time + 0.3 + random() * 0.1;
872                         trace_dphitq3surfaceflags = 0;
873                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
874                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
875                         {
876                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
877                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
878                                 else
879                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
880                         }
881                 }
882         }
883
884         if(IsFlying(self))
885                 self.wasFlying = 1;
886
887         if(IS_PLAYER(self))
888                 CheckPlayerJump();
889
890         if (self.flags & FL_WATERJUMP )
891         {
892                 self.velocity_x = self.movedir_x;
893                 self.velocity_y = self.movedir_y;
894                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
895                 {
896                         self.flags &= ~FL_WATERJUMP;
897                         self.teleport_time = 0;
898                 }
899         }
900         else if (g_bugrigs && IS_PLAYER(self))
901         {
902                 RaceCarPhysics();
903         }
904         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
905         {
906                 // noclipping or flying
907                 self.flags &= ~FL_ONGROUND;
908
909                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
910                 makevectors(self.v_angle);
911                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
912                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
913                 // acceleration
914                 wishdir = normalize(wishvel);
915                 wishspeed = vlen(wishvel);
916                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
917                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
918                 if (time >= self.teleport_time)
919                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
920         }
921         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
922         {
923                 // swimming
924                 self.flags &= ~FL_ONGROUND;
925
926                 makevectors(self.v_angle);
927                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
928                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
929                 if (wishvel == '0 0 0')
930                         wishvel = '0 0 -60'; // drift towards bottom
931
932                 wishdir = normalize(wishvel);
933                 wishspeed = vlen(wishvel);
934                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
935                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
936                 wishspeed = wishspeed * 0.7;
937
938                 // water friction
939                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
940
941                 // water acceleration
942                 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
943         }
944         else if (time < self.ladder_time)
945         {
946                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
947                 self.flags &= ~FL_ONGROUND;
948
949                 float g;
950                 g = autocvar_sv_gravity * frametime;
951                 if(self.gravity)
952                         g *= self.gravity;
953                 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
954                 {
955                         g *= 0.5;
956                         self.velocity_z += g;
957                 }
958
959                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
960                 makevectors(self.v_angle);
961                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
962                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
963                 self.velocity_z += g;
964                 if (self.ladder_entity.classname == "func_water")
965                 {
966                         f = vlen(wishvel);
967                         if (f > self.ladder_entity.speed)
968                                 wishvel = wishvel * (self.ladder_entity.speed / f);
969
970                         self.watertype = self.ladder_entity.skin;
971                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
972                         if ((self.origin_z + self.view_ofs_z) < f)
973                                 self.waterlevel = WATERLEVEL_SUBMERGED;
974                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
975                                 self.waterlevel = WATERLEVEL_SWIMMING;
976                         else if ((self.origin_z + self.mins_z + 1) < f)
977                                 self.waterlevel = WATERLEVEL_WETFEET;
978                         else
979                         {
980                                 self.waterlevel = WATERLEVEL_NONE;
981                                 self.watertype = CONTENT_EMPTY;
982                         }
983                 }
984                 // acceleration
985                 wishdir = normalize(wishvel);
986                 wishspeed = vlen(wishvel);
987                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
988                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
989                 if (time >= self.teleport_time)
990                 {
991                         // water acceleration
992                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
993                 }
994         }
995         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!autocvar_g_jetpack_fuel || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO) && !self.freezetag_frozen)
996         {
997                 //makevectors(self.v_angle_y * '0 1 0');
998                 makevectors(self.v_angle);
999                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1000                 // add remaining speed as Z component
1001                 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1002                 // fix speedhacks :P
1003                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1004                 // add the unused velocity as up component
1005                 wishvel_z = 0;
1006
1007                 // if(self.BUTTON_JUMP)
1008                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1009
1010                 // it is now normalized, so...
1011                 float a_side, a_up, a_add, a_diff;
1012                 a_side = autocvar_g_jetpack_acceleration_side;
1013                 a_up = autocvar_g_jetpack_acceleration_up;
1014                 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1015
1016                 wishvel_x *= a_side;
1017                 wishvel_y *= a_side;
1018                 wishvel_z *= a_up;
1019                 wishvel_z += a_add;
1020
1021                 float best;
1022                 best = 0;
1023                 //////////////////////////////////////////////////////////////////////////////////////
1024                 // finding the maximum over all vectors of above form
1025                 // with wishvel having an absolute value of 1
1026                 //////////////////////////////////////////////////////////////////////////////////////
1027                 // we're finding the maximum over
1028                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1029                 // for z in the range from -1 to 1
1030                 //////////////////////////////////////////////////////////////////////////////////////
1031                 // maximum is EITHER attained at the single extreme point:
1032                 a_diff = a_side * a_side - a_up * a_up;
1033                 if(a_diff != 0)
1034                 {
1035                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1036                         if(f > -1 && f < 1) // can it be attained?
1037                         {
1038                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1039                                 //print("middle\n");
1040                         }
1041                 }
1042                 // OR attained at z = 1:
1043                 f = (a_up + a_add) * (a_up + a_add);
1044                 if(f > best)
1045                 {
1046                         best = f;
1047                         //print("top\n");
1048                 }
1049                 // OR attained at z = -1:
1050                 f = (a_up - a_add) * (a_up - a_add);
1051                 if(f > best)
1052                 {
1053                         best = f;
1054                         //print("bottom\n");
1055                 }
1056                 best = sqrt(best);
1057                 //////////////////////////////////////////////////////////////////////////////////////
1058
1059                 //print("best possible acceleration: ", ftos(best), "\n");
1060
1061                 float fxy, fz;
1062                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1063                 if(wishvel_z - autocvar_sv_gravity > 0)
1064                         fz = bound(0, 1 - self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1065                 else
1066                         fz = bound(0, 1 + self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1067
1068                 float fvel;
1069                 fvel = vlen(wishvel);
1070                 wishvel_x *= fxy;
1071                 wishvel_y *= fxy;
1072                 wishvel_z = (wishvel_z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1073
1074                 fvel = min(1, vlen(wishvel) / best);
1075                 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1076                         f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1077                 else
1078                         f = 1;
1079
1080                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1081
1082                 if (f > 0 && wishvel != '0 0 0')
1083                 {
1084                         self.velocity = self.velocity + wishvel * f * frametime;
1085                         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
1086                                 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1087                         self.flags &= ~FL_ONGROUND;
1088                         self.items |= IT_USING_JETPACK;
1089
1090                         // jetpack also inhibits health regeneration, but only for 1 second
1091                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1092                 }
1093         }
1094         else if (self.flags & FL_ONGROUND)
1095         {
1096                 // we get here if we ran out of ammo
1097                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1098                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1099
1100                 // walking
1101                 makevectors(self.v_angle_y * '0 1 0');
1102                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1103
1104                 if(!(self.lastflags & FL_ONGROUND))
1105                 {
1106                         if(autocvar_speedmeter)
1107                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1108                         if(self.lastground < time - 0.3)
1109                                 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1110                         if(self.jumppadcount > 1)
1111                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1112                         self.jumppadcount = 0;
1113                 }
1114
1115 #ifdef LETS_TEST_FTEQCC
1116                 if(self.velocity_x || self.velocity_y)
1117                 {
1118                         // good
1119                 }
1120                 else
1121                 {
1122                         if(self.velocity_x)
1123                                 checkclient();
1124                         if(self.velocity_y)
1125                                 checkclient();
1126                 }
1127 #endif
1128
1129                 v = self.velocity;
1130                 v_z = 0;
1131                 f = vlen(v);
1132                 if(f > 0)
1133                 {
1134                         if (f < autocvar_sv_stopspeed)
1135                                 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1136                         else
1137                                 f = 1 - frametime * autocvar_sv_friction;
1138                         if (f > 0)
1139                                 self.velocity = self.velocity * f;
1140                         else
1141                                 self.velocity = '0 0 0';
1142                         /*
1143                            Mathematical analysis time!
1144
1145                            Our goal is to invert this mess.
1146
1147                            For the two cases we get:
1148                                 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1149                                   = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1150                                 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1151                            and
1152                                 v = v0 * (1 - frametime * autocvar_sv_friction)
1153                                 v0 = v / (1 - frametime * autocvar_sv_friction)
1154
1155                            These cases would be chosen ONLY if:
1156                                 v0 < autocvar_sv_stopspeed
1157                                 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1158                                 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1159                            and, respectively:
1160                                 v0 >= autocvar_sv_stopspeed
1161                                 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1162                                 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1163                          */
1164                 }
1165
1166                 // acceleration
1167                 wishdir = normalize(wishvel);
1168                 wishspeed = vlen(wishvel);
1169                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1170                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1171                 if (self.crouch)
1172                         wishspeed = wishspeed * 0.5;
1173                 if (time >= self.teleport_time)
1174                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1175         }
1176         else
1177         {
1178                 float wishspeed0;
1179                 // we get here if we ran out of ammo
1180                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1181                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1182
1183                 if(maxspd_mod < 1)
1184                 {
1185                         maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1186                         airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1187                 }
1188                 else
1189                 {
1190                         maxairspd = autocvar_sv_maxairspeed;
1191                         airaccel = autocvar_sv_airaccelerate;
1192                 }
1193                 // airborn
1194                 makevectors(self.v_angle_y * '0 1 0');
1195                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1196                 // acceleration
1197                 wishdir = normalize(wishvel);
1198                 wishspeed = wishspeed0 = vlen(wishvel);
1199                 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1200                         wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1201                 if (wishspeed > maxairspd)
1202                         wishspeed = maxairspd;
1203                 if (self.crouch)
1204                         wishspeed = wishspeed * 0.5;
1205                 if (time >= self.teleport_time)
1206                 {
1207                         float accelerating;
1208                         float wishspeed2;
1209                         float airaccelqw;
1210                         float strafity;
1211
1212                         airaccelqw = self.stat_sv_airaccel_qw;
1213                         accelerating = (self.velocity * wishdir > 0);
1214                         wishspeed2 = wishspeed;
1215
1216                         // CPM
1217                         if(autocvar_sv_airstopaccelerate)
1218                         {
1219                                 vector curdir;
1220                                 curdir = self.velocity;
1221                                 curdir_z = 0;
1222                                 curdir = normalize(curdir);
1223                                 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1224                         }
1225                         // note that for straight forward jumping:
1226                         // step = accel * frametime * wishspeed0;
1227                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1228                         // -->
1229                         // dv/dt = accel * maxspeed (when slow)
1230                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1231                         // log dv/dt = logaccel + logmaxspeed (when slow)
1232                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1233                         strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1234                         if(autocvar_sv_maxairstrafespeed)
1235                                 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1236                         if(autocvar_sv_airstrafeaccelerate)
1237                                 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1238                         if(self.stat_sv_airstrafeaccel_qw)
1239                                 airaccelqw = copysign(1-GeomLerp(1-fabs(self.stat_sv_airaccel_qw), strafity, 1-fabs(self.stat_sv_airstrafeaccel_qw)), ((strafity > 0.5) ? self.stat_sv_airstrafeaccel_qw : self.stat_sv_airaccel_qw));
1240                         // !CPM
1241
1242                         if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1243                                 PM_AirAccelerate(wishdir, wishspeed);
1244                         else
1245                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1246
1247                         if(autocvar_sv_aircontrol)
1248                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1249                 }
1250         }
1251
1252         if((g_cts || g_race) && !IS_OBSERVER(self)) {
1253                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1254                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1255                         speedaward_holder = self.netname;
1256                         speedaward_uid = self.crypto_idfp;
1257                         speedaward_lastupdate = time;
1258                 }
1259                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1260                         string rr;
1261                         if(g_cts)
1262                                 rr = CTS_RECORD;
1263                         else
1264                                 rr = RACE_RECORD;
1265                         race_send_speedaward(MSG_ALL);
1266                         speedaward_lastsent = speedaward_speed;
1267                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "") {
1268                                 speedaward_alltimebest = speedaward_speed;
1269                                 speedaward_alltimebest_holder = speedaward_holder;
1270                                 speedaward_alltimebest_uid = speedaward_uid;
1271                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1272                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1273                                 race_send_speedaward_alltimebest(MSG_ALL);
1274                         }
1275                 }
1276         }
1277
1278         float xyspeed;
1279         xyspeed = vlen('1 0 0' * self.velocity_x + '0 1 0' * self.velocity_y);
1280         if(self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1281         {
1282                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1283                 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1284                 f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1285                 // add the extra charge
1286                 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * frametime);
1287         }
1288 :end
1289         if(self.flags & FL_ONGROUND)
1290                 self.lastground = time;
1291
1292         // conveyors: then break velocity again
1293         if(self.conveyor.state)
1294                 self.velocity += self.conveyor.movedir;
1295
1296         self.lastflags = self.flags;
1297         self.lastclassname = self.classname;
1298 }