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