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