]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
Buffs mutator, loosely based on the old relics mutator
[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         float mjumpheight = autocvar_sv_jumpvelocity;
24
25         player_multijump = doublejump;
26         player_jumpheight = mjumpheight;
27         if(MUTATOR_CALLHOOK(PlayerJump))
28                 return;
29
30         doublejump = player_multijump;
31         mjumpheight = player_jumpheight;
32
33         if (autocvar_sv_doublejump)
34         {
35                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
36                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
37                 {
38                         doublejump = TRUE;
39
40                         // we MUST clip velocity here!
41                         float f;
42                         f = self.velocity * trace_plane_normal;
43                         if(f < 0)
44                                 self.velocity -= f * trace_plane_normal;
45                 }
46         }
47
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_keepaway)
640                         maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
641
642         maxspd_mod *= autocvar_g_movement_highspeed;
643
644         // fix physics stats for g_movement_highspeed
645         // TODO maybe rather use maxairspeed? needs testing
646         self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
647         if(autocvar_sv_airstrafeaccel_qw)
648                 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
649         else
650                 self.stat_sv_airstrafeaccel_qw = 0;
651         self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
652         self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
653
654     if(self.PlayerPhysplug)
655         if(self.PlayerPhysplug())
656             return;
657
658         self.race_movetime_frac += frametime;
659         f = floor(self.race_movetime_frac);
660         self.race_movetime_frac -= f;
661         self.race_movetime_count += f;
662         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
663
664         anticheat_physics();
665
666         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);
667
668         if(!buttons)
669                 c = "x";
670         else if(buttons == 1)
671                 c = "1";
672         else if(buttons == 2)
673                 c = " ";
674         else if(buttons == 128)
675                 c = "s";
676         else if(buttons == 256)
677                 c = "w";
678         else if(buttons == 512)
679                 c = "a";
680         else if(buttons == 1024)
681                 c = "d";
682         else
683                 c = "?";
684
685         if(c == substring(specialcommand, self.specialcommand_pos, 1))
686         {
687                 self.specialcommand_pos += 1;
688                 if(self.specialcommand_pos >= strlen(specialcommand))
689                 {
690                         self.specialcommand_pos = 0;
691                         SpecialCommand();
692                         return;
693                 }
694         }
695         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
696                 self.specialcommand_pos = 0;
697
698         if(sv_maxidle > 0)
699         {
700                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
701                         self.parm_idlesince = time;
702         }
703         buttons_prev = self.buttons_old;
704         self.buttons_old = buttons;
705         self.movement_old = self.movement;
706         self.v_angle_old = self.v_angle;
707
708         if(time < self.nickspamtime)
709         if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
710         {
711                 // slight annoyance for nick change scripts
712                 self.movement = -1 * self.movement;
713                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
714
715                 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!
716                 {
717                         self.angles_x = random() * 360;
718                         self.angles_y = random() * 360;
719                         // at least I'm not forcing retardedview by also assigning to angles_z
720                         self.fixangle = TRUE;
721                 }
722         }
723
724         if (self.punchangle != '0 0 0')
725         {
726                 f = vlen(self.punchangle) - 10 * frametime;
727                 if (f > 0)
728                         self.punchangle = normalize(self.punchangle) * f;
729                 else
730                         self.punchangle = '0 0 0';
731         }
732
733         if (self.punchvector != '0 0 0')
734         {
735                 f = vlen(self.punchvector) - 30 * frametime;
736                 if (f > 0)
737                         self.punchvector = normalize(self.punchvector) * f;
738                 else
739                         self.punchvector = '0 0 0';
740         }
741
742         if (IS_BOT_CLIENT(self))
743         {
744                 if(playerdemo_read())
745                         return;
746                 bot_think();
747         }
748
749         self.items &= ~IT_USING_JETPACK;
750
751         if(IS_PLAYER(self))
752         {
753                 if(self.race_penalty)
754                         if(time > self.race_penalty)
755                                 self.race_penalty = 0;
756
757                 not_allowed_to_move = 0;
758                 if(self.race_penalty)
759                         not_allowed_to_move = 1;
760                 if(!autocvar_sv_ready_restart_after_countdown)
761                 if(time < game_starttime)
762                         not_allowed_to_move = 1;
763
764                 if(not_allowed_to_move)
765                 {
766                         self.velocity = '0 0 0';
767                         self.movetype = MOVETYPE_NONE;
768                         self.disableclientprediction = 2;
769                 }
770                 else if(self.disableclientprediction == 2)
771                 {
772                         if(self.movetype == MOVETYPE_NONE)
773                                 self.movetype = MOVETYPE_WALK;
774                         self.disableclientprediction = 0;
775                 }
776         }
777
778         if (self.movetype == MOVETYPE_NONE)
779                 return;
780
781         // when we get here, disableclientprediction cannot be 2
782         self.disableclientprediction = 0;
783         if(time < self.ladder_time)
784                 self.disableclientprediction = 1;
785
786         if(time < self.spider_slowness)
787         {
788                 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
789                 self.stat_sv_airspeedlimit_nonqw *= 0.5;
790         }
791
792         MUTATOR_CALLHOOK(PlayerPhysics);
793
794         if(self.player_blocked)
795         {
796                 self.movement = '0 0 0';
797                 self.disableclientprediction = 1;
798         }
799
800         maxspd_mod = 1;
801
802         swampspd_mod = 1;
803         if(self.in_swamp) {
804                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
805         }
806
807         // conveyors: first fix velocity
808         if(self.conveyor.state)
809                 self.velocity -= self.conveyor.movedir;
810
811         if (!IS_PLAYER(self))
812         {
813                 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
814                 if(!self.spectatorspeed)
815                         self.spectatorspeed = maxspd_mod;
816                 if(self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
817                 {
818                         if(self.lastclassname != "player")
819                         {
820                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
821                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
822                                 else if(self.impulse == 11)
823                                         self.spectatorspeed = maxspd_mod;
824                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
825                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
826                                 else if(self.impulse >= 1 && self.impulse <= 9)
827                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
828                         } // otherwise just clear
829                         self.impulse = 0;
830                 }
831                 maxspd_mod = self.spectatorspeed;
832         }
833
834         spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
835         if(self.speed != spd)
836         {
837                 self.speed = spd;
838                 temps = ftos(spd);
839                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
840                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
841                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
842                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
843         }
844
845         maxspd_mod *= swampspd_mod; // only one common speed modder please!
846         swampspd_mod = 1;
847
848         // if dead, behave differently
849         if (self.deadflag)
850                 goto end;
851
852         if (!self.fixangle && !g_bugrigs)
853         {
854                 self.angles_x = 0;
855                 self.angles_y = self.v_angle_y;
856                 self.angles_z = 0;
857         }
858
859         if(self.flags & FL_ONGROUND)
860         if(IS_PLAYER(self)) // no fall sounds for observers thank you very much
861         if(self.wasFlying)
862         {
863                 self.wasFlying = 0;
864
865                 if(self.waterlevel < WATERLEVEL_SWIMMING)
866                 if(time >= self.ladder_time)
867                 if (!self.hook)
868                 {
869                         self.nextstep = time + 0.3 + random() * 0.1;
870                         trace_dphitq3surfaceflags = 0;
871                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
872                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
873                         {
874                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
875                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
876                                 else
877                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
878                         }
879                 }
880         }
881
882         if(IsFlying(self))
883                 self.wasFlying = 1;
884
885         if(IS_PLAYER(self))
886                 CheckPlayerJump();
887
888         if (self.flags & FL_WATERJUMP )
889         {
890                 self.velocity_x = self.movedir_x;
891                 self.velocity_y = self.movedir_y;
892                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
893                 {
894                         self.flags &= ~FL_WATERJUMP;
895                         self.teleport_time = 0;
896                 }
897         }
898         else if (g_bugrigs && IS_PLAYER(self))
899         {
900                 RaceCarPhysics();
901         }
902         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
903         {
904                 // noclipping or flying
905                 self.flags &= ~FL_ONGROUND;
906
907                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
908                 makevectors(self.v_angle);
909                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
910                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
911                 // acceleration
912                 wishdir = normalize(wishvel);
913                 wishspeed = vlen(wishvel);
914                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
915                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
916                 if (time >= self.teleport_time)
917                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
918         }
919         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
920         {
921                 // swimming
922                 self.flags &= ~FL_ONGROUND;
923
924                 makevectors(self.v_angle);
925                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
926                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
927                 if (wishvel == '0 0 0')
928                         wishvel = '0 0 -60'; // drift towards bottom
929
930                 wishdir = normalize(wishvel);
931                 wishspeed = vlen(wishvel);
932                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
933                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
934                 wishspeed = wishspeed * 0.7;
935
936                 // water friction
937                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
938
939                 // water acceleration
940                 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
941         }
942         else if (time < self.ladder_time)
943         {
944                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
945                 self.flags &= ~FL_ONGROUND;
946
947                 float g;
948                 g = autocvar_sv_gravity * frametime;
949                 if(self.gravity)
950                         g *= self.gravity;
951                 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
952                 {
953                         g *= 0.5;
954                         self.velocity_z += g;
955                 }
956
957                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
958                 makevectors(self.v_angle);
959                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
960                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
961                 self.velocity_z += g;
962                 if (self.ladder_entity.classname == "func_water")
963                 {
964                         f = vlen(wishvel);
965                         if (f > self.ladder_entity.speed)
966                                 wishvel = wishvel * (self.ladder_entity.speed / f);
967
968                         self.watertype = self.ladder_entity.skin;
969                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
970                         if ((self.origin_z + self.view_ofs_z) < f)
971                                 self.waterlevel = WATERLEVEL_SUBMERGED;
972                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
973                                 self.waterlevel = WATERLEVEL_SWIMMING;
974                         else if ((self.origin_z + self.mins_z + 1) < f)
975                                 self.waterlevel = WATERLEVEL_WETFEET;
976                         else
977                         {
978                                 self.waterlevel = WATERLEVEL_NONE;
979                                 self.watertype = CONTENT_EMPTY;
980                         }
981                 }
982                 // acceleration
983                 wishdir = normalize(wishvel);
984                 wishspeed = vlen(wishvel);
985                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
986                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
987                 if (time >= self.teleport_time)
988                 {
989                         // water acceleration
990                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
991                 }
992         }
993         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)
994         {
995                 //makevectors(self.v_angle_y * '0 1 0');
996                 makevectors(self.v_angle);
997                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
998                 // add remaining speed as Z component
999                 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1000                 // fix speedhacks :P
1001                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1002                 // add the unused velocity as up component
1003                 wishvel_z = 0;
1004
1005                 // if(self.BUTTON_JUMP)
1006                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1007
1008                 // it is now normalized, so...
1009                 float a_side, a_up, a_add, a_diff;
1010                 a_side = autocvar_g_jetpack_acceleration_side;
1011                 a_up = autocvar_g_jetpack_acceleration_up;
1012                 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1013
1014                 wishvel_x *= a_side;
1015                 wishvel_y *= a_side;
1016                 wishvel_z *= a_up;
1017                 wishvel_z += a_add;
1018
1019                 float best;
1020                 best = 0;
1021                 //////////////////////////////////////////////////////////////////////////////////////
1022                 // finding the maximum over all vectors of above form
1023                 // with wishvel having an absolute value of 1
1024                 //////////////////////////////////////////////////////////////////////////////////////
1025                 // we're finding the maximum over
1026                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1027                 // for z in the range from -1 to 1
1028                 //////////////////////////////////////////////////////////////////////////////////////
1029                 // maximum is EITHER attained at the single extreme point:
1030                 a_diff = a_side * a_side - a_up * a_up;
1031                 if(a_diff != 0)
1032                 {
1033                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1034                         if(f > -1 && f < 1) // can it be attained?
1035                         {
1036                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1037                                 //print("middle\n");
1038                         }
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("top\n");
1046                 }
1047                 // OR attained at z = -1:
1048                 f = (a_up - a_add) * (a_up - a_add);
1049                 if(f > best)
1050                 {
1051                         best = f;
1052                         //print("bottom\n");
1053                 }
1054                 best = sqrt(best);
1055                 //////////////////////////////////////////////////////////////////////////////////////
1056
1057                 //print("best possible acceleration: ", ftos(best), "\n");
1058
1059                 float fxy, fz;
1060                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1061                 if(wishvel_z - autocvar_sv_gravity > 0)
1062                         fz = bound(0, 1 - self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1063                 else
1064                         fz = bound(0, 1 + self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1065
1066                 float fvel;
1067                 fvel = vlen(wishvel);
1068                 wishvel_x *= fxy;
1069                 wishvel_y *= fxy;
1070                 wishvel_z = (wishvel_z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1071
1072                 fvel = min(1, vlen(wishvel) / best);
1073                 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1074                         f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1075                 else
1076                         f = 1;
1077
1078                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1079
1080                 if (f > 0 && wishvel != '0 0 0')
1081                 {
1082                         self.velocity = self.velocity + wishvel * f * frametime;
1083                         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
1084                                 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1085                         self.flags &= ~FL_ONGROUND;
1086                         self.items |= IT_USING_JETPACK;
1087
1088                         // jetpack also inhibits health regeneration, but only for 1 second
1089                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1090                 }
1091         }
1092         else if (self.flags & FL_ONGROUND)
1093         {
1094                 // we get here if we ran out of ammo
1095                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1096                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1097
1098                 // walking
1099                 makevectors(self.v_angle_y * '0 1 0');
1100                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1101
1102                 if(!(self.lastflags & FL_ONGROUND))
1103                 {
1104                         if(autocvar_speedmeter)
1105                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1106                         if(self.lastground < time - 0.3)
1107                                 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1108                         if(self.jumppadcount > 1)
1109                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1110                         self.jumppadcount = 0;
1111                 }
1112
1113 #ifdef LETS_TEST_FTEQCC
1114                 if(self.velocity_x || self.velocity_y)
1115                 {
1116                         // good
1117                 }
1118                 else
1119                 {
1120                         if(self.velocity_x)
1121                                 checkclient();
1122                         if(self.velocity_y)
1123                                 checkclient();
1124                 }
1125 #endif
1126
1127                 v = self.velocity;
1128                 v_z = 0;
1129                 f = vlen(v);
1130                 if(f > 0)
1131                 {
1132                         if (f < autocvar_sv_stopspeed)
1133                                 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1134                         else
1135                                 f = 1 - frametime * autocvar_sv_friction;
1136                         if (f > 0)
1137                                 self.velocity = self.velocity * f;
1138                         else
1139                                 self.velocity = '0 0 0';
1140                         /*
1141                            Mathematical analysis time!
1142
1143                            Our goal is to invert this mess.
1144
1145                            For the two cases we get:
1146                                 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1147                                   = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1148                                 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1149                            and
1150                                 v = v0 * (1 - frametime * autocvar_sv_friction)
1151                                 v0 = v / (1 - frametime * autocvar_sv_friction)
1152
1153                            These cases would be chosen ONLY if:
1154                                 v0 < autocvar_sv_stopspeed
1155                                 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1156                                 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1157                            and, respectively:
1158                                 v0 >= autocvar_sv_stopspeed
1159                                 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1160                                 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1161                          */
1162                 }
1163
1164                 // acceleration
1165                 wishdir = normalize(wishvel);
1166                 wishspeed = vlen(wishvel);
1167                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1168                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1169                 if (self.crouch)
1170                         wishspeed = wishspeed * 0.5;
1171                 if (time >= self.teleport_time)
1172                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1173         }
1174         else
1175         {
1176                 float wishspeed0;
1177                 // we get here if we ran out of ammo
1178                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1179                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1180
1181                 if(maxspd_mod < 1)
1182                 {
1183                         maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1184                         airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1185                 }
1186                 else
1187                 {
1188                         maxairspd = autocvar_sv_maxairspeed;
1189                         airaccel = autocvar_sv_airaccelerate;
1190                 }
1191                 // airborn
1192                 makevectors(self.v_angle_y * '0 1 0');
1193                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1194                 // acceleration
1195                 wishdir = normalize(wishvel);
1196                 wishspeed = wishspeed0 = vlen(wishvel);
1197                 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1198                         wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1199                 if (wishspeed > maxairspd)
1200                         wishspeed = maxairspd;
1201                 if (self.crouch)
1202                         wishspeed = wishspeed * 0.5;
1203                 if (time >= self.teleport_time)
1204                 {
1205                         float accelerating;
1206                         float wishspeed2;
1207                         float airaccelqw;
1208                         float strafity;
1209
1210                         airaccelqw = self.stat_sv_airaccel_qw;
1211                         accelerating = (self.velocity * wishdir > 0);
1212                         wishspeed2 = wishspeed;
1213
1214                         // CPM
1215                         if(autocvar_sv_airstopaccelerate)
1216                         {
1217                                 vector curdir;
1218                                 curdir = self.velocity;
1219                                 curdir_z = 0;
1220                                 curdir = normalize(curdir);
1221                                 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1222                         }
1223                         // note that for straight forward jumping:
1224                         // step = accel * frametime * wishspeed0;
1225                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1226                         // -->
1227                         // dv/dt = accel * maxspeed (when slow)
1228                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1229                         // log dv/dt = logaccel + logmaxspeed (when slow)
1230                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1231                         strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1232                         if(autocvar_sv_maxairstrafespeed)
1233                                 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1234                         if(autocvar_sv_airstrafeaccelerate)
1235                                 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1236                         if(self.stat_sv_airstrafeaccel_qw)
1237                                 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));
1238                         // !CPM
1239
1240                         if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1241                                 PM_AirAccelerate(wishdir, wishspeed);
1242                         else
1243                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1244
1245                         if(autocvar_sv_aircontrol)
1246                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1247                 }
1248         }
1249
1250         if((g_cts || g_race) && !IS_OBSERVER(self)) {
1251                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1252                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1253                         speedaward_holder = self.netname;
1254                         speedaward_uid = self.crypto_idfp;
1255                         speedaward_lastupdate = time;
1256                 }
1257                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1258                         string rr;
1259                         if(g_cts)
1260                                 rr = CTS_RECORD;
1261                         else
1262                                 rr = RACE_RECORD;
1263                         race_send_speedaward(MSG_ALL);
1264                         speedaward_lastsent = speedaward_speed;
1265                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "") {
1266                                 speedaward_alltimebest = speedaward_speed;
1267                                 speedaward_alltimebest_holder = speedaward_holder;
1268                                 speedaward_alltimebest_uid = speedaward_uid;
1269                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1270                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1271                                 race_send_speedaward_alltimebest(MSG_ALL);
1272                         }
1273                 }
1274         }
1275
1276         float xyspeed;
1277         xyspeed = vlen('1 0 0' * self.velocity_x + '0 1 0' * self.velocity_y);
1278         if(self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1279         {
1280                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1281                 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1282                 f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1283                 // add the extra charge
1284                 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * frametime);
1285         }
1286 :end
1287         if(self.flags & FL_ONGROUND)
1288                 self.lastground = time;
1289
1290         // conveyors: then break velocity again
1291         if(self.conveyor.state)
1292                 self.velocity += self.conveyor.movedir;
1293
1294         self.lastflags = self.flags;
1295         self.lastclassname = self.classname;
1296 }