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