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