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