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