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