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