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