]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
multijump: fix velocity_x and _y calculation
[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 = rigvel;
363                 rigvel_xy_z = 0;
364
365                 if(g_bugrigs_planar_movement_car_jumping && !g_touchexplode) // touchexplode is a better way to handle collisions
366                         mt = MOVE_NORMAL;
367                 else
368                         mt = MOVE_NOMONSTERS;
369
370                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
371                 up = trace_endpos - self.origin;
372
373                 // BUG RIGS: align the move to the surface instead of doing collision testing
374                 // can we move?
375                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
376
377                 // align to surface
378                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
379
380                 if(trace_fraction < 0.5)
381                 {
382                         trace_fraction = 1;
383                         neworigin = self.origin;
384                 }
385                 else
386                         neworigin = trace_endpos;
387
388                 if(trace_fraction < 1)
389                 {
390                         // now set angles_x so that the car points parallel to the surface
391                         self.angles = vectoangles(
392                                         '1 0 0' * v_forward_x * trace_plane_normal_z
393                                         +
394                                         '0 1 0' * v_forward_y * trace_plane_normal_z
395                                         +
396                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
397                                         );
398                         self.flags |= FL_ONGROUND;
399                 }
400                 else
401                 {
402                         // now set angles_x so that the car points forward, but is tilted in velocity direction
403                         self.flags &~= FL_ONGROUND;
404                 }
405
406                 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
407                 self.movetype = MOVETYPE_NOCLIP;
408         }
409         else
410         {
411                 rigvel_z -= frametime * sv_gravity; // 4x gravity plays better
412                 self.velocity = rigvel;
413                 self.movetype = MOVETYPE_FLY;
414         }
415
416         trace_fraction = 1;
417         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
418         if(trace_fraction != 1)
419         {
420                 self.angles = vectoangles2(
421                                 '1 0 0' * v_forward_x * trace_plane_normal_z
422                                 +
423                                 '0 1 0' * v_forward_y * trace_plane_normal_z
424                                 +
425                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
426                                 trace_plane_normal
427                                 );
428         }
429         else
430         {
431                 vector vel_local;
432
433                 vel_local_x = v_forward * self.velocity;
434                 vel_local_y = v_right * self.velocity;
435                 vel_local_z = v_up * self.velocity;
436
437                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
438                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
439         }
440
441         // smooth the angles
442         vector vf1, vu1, smoothangles;
443         makevectors(self.angles);
444         f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
445         if(f == 0)
446                 f = 1;
447         vf1 = v_forward * f;
448         vu1 = v_up * f;
449         makevectors(angles_save);
450         vf1 = vf1 + v_forward * (1 - f);
451         vu1 = vu1 + v_up * (1 - f);
452         smoothangles = vectoangles2(vf1, vu1);
453         self.angles_x = -smoothangles_x;
454         self.angles_z =  smoothangles_z;
455 }
456
457 float IsMoveInDirection(vector mv, float angle) // key mix factor
458 {
459         if(mv_x == 0 && mv_y == 0)
460                 return 0; // avoid division by zero
461         angle -= RAD2DEG * atan2(mv_y, mv_x);
462         angle = remainder(angle, 360) / 45;
463         if(angle >  1)
464                 return 0;
465         if(angle < -1)
466                 return 0;
467         return 1 - fabs(angle);
468 }
469
470 float GeomLerp(float a, float lerp, float b)
471 {
472         if(a == 0)
473         {
474                 if(lerp < 1)
475                         return 0;
476                 else
477                         return b;
478         }
479         if(b == 0)
480         {
481                 if(lerp > 0)
482                         return 0;
483                 else
484                         return a;
485         }
486         return a * pow(fabs(b / a), lerp);
487 }
488
489 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
490 {
491         float zspeed, xyspeed, dot, k;
492
493 #if 0
494         // this doesn't play well with analog input
495         if(self.movement_x == 0 || self.movement_y != 0)
496                 return; // can't control movement if not moving forward or backward
497         k = 32;
498 #else
499         k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
500         if(k <= 0)
501                 return;
502 #endif
503
504         k *= bound(0, wishspeed / sv_maxairspeed, 1);
505
506         zspeed = self.velocity_z;
507         self.velocity_z = 0;
508         xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
509
510         dot = self.velocity * wishdir;
511
512         if(dot > 0) // we can't change direction while slowing down
513         {
514                 k *= pow(dot, sv_aircontrol_power)*frametime;
515                 xyspeed = max(0, xyspeed - sv_aircontrol_penalty * sqrt(max(0, 1 - dot*dot)) * k/32);
516                 k *= sv_aircontrol;
517                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
518         }
519
520         self.velocity = self.velocity * xyspeed;
521         self.velocity_z = zspeed;
522 }
523
524 float AdjustAirAccelQW(float accelqw, float factor)
525 {
526         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
527 }
528
529 // example config for alternate speed clamping:
530 //   sv_airaccel_qw 0.8
531 //   sv_airaccel_sideways_friction 0
532 //   prvm_globalset server speedclamp_mode 1
533 //     (or 2)
534 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float sidefric, float speedlimit)
535 {
536         float vel_straight;
537         float vel_z;
538         vector vel_perpend;
539         float step;
540
541         vector vel_xy;
542         float vel_xy_current;
543         float vel_xy_backward, vel_xy_forward;
544         float speedclamp;
545
546         speedclamp = (accelqw < 0);
547         if(speedclamp)
548                 accelqw = -accelqw;
549
550         if(cvar("sv_gameplayfix_q2airaccelerate"))
551                 wishspeed0 = wishspeed;
552
553         vel_straight = self.velocity * wishdir;
554         vel_z = self.velocity_z;
555         vel_xy = self.velocity - vel_z * '0 0 1';
556         vel_perpend = vel_xy - vel_straight * wishdir;
557
558         step = accel * frametime * wishspeed0;
559
560         vel_xy_current  = vlen(vel_xy);
561         if(speedlimit)
562                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
563         vel_xy_forward  = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
564         vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
565         if(vel_xy_backward < 0)
566                 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
567
568         vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
569
570         if(sidefric < 0 && (vel_perpend*vel_perpend))
571                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
572         {
573                 float f, fminimum;
574                 f = max(0, 1 + frametime * wishspeed * sidefric);
575                 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
576                 // this cannot be > 1
577                 if(fminimum <= 0)
578                         vel_perpend = vel_perpend * max(0, f);
579                 else
580                 {
581                         fminimum = sqrt(fminimum);
582                         vel_perpend = vel_perpend * max(fminimum, f);
583                 }
584         }
585         else
586                 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
587         
588         vel_xy = vel_straight * wishdir + vel_perpend;
589         
590         if(speedclamp)
591         {
592                 // ensure we don't get too fast or decelerate faster than we should
593                 vel_xy_current = min(vlen(vel_xy), vel_xy_forward);
594                 if(vel_xy_current > 0) // prevent division by zero
595                         vel_xy = normalize(vel_xy) * vel_xy_current;
596         }
597
598         self.velocity = vel_xy + vel_z * '0 0 1';
599 }
600
601 void PM_AirAccelerate(vector wishdir, float wishspeed)
602 {
603         vector curvel, wishvel, acceldir, curdir;
604         float addspeed, accelspeed, curspeed, f;
605         float dot;
606
607         if(wishspeed == 0)
608                 return;
609
610         curvel = self.velocity;
611         curvel_z = 0;
612         curspeed = vlen(curvel);
613
614         if(wishspeed > curspeed * 1.01)
615         {
616                 wishspeed = min(wishspeed, curspeed + sv_warsowbunny_airforwardaccel * sv_maxspeed * frametime);
617         }
618         else
619         {
620                 f = max(0, (sv_warsowbunny_topspeed - curspeed) / (sv_warsowbunny_topspeed - sv_maxspeed));
621                 wishspeed = max(curspeed, sv_maxspeed) + sv_warsowbunny_accel * f * sv_maxspeed * frametime;
622         }
623         wishvel = wishdir * wishspeed;
624         acceldir = wishvel - curvel;
625         addspeed = vlen(acceldir);
626         acceldir = normalize(acceldir);
627
628         accelspeed = min(addspeed, sv_warsowbunny_turnaccel * sv_maxspeed * frametime);
629
630         if(sv_warsowbunny_backtosideratio < 1)
631         {
632                 curdir = normalize(curvel);
633                 dot = acceldir * curdir;
634                 if(dot < 0)
635                         acceldir = acceldir - (1 - sv_warsowbunny_backtosideratio) * dot * curdir;
636         }
637
638         self.velocity += accelspeed * acceldir;
639 }
640
641 .vector movement_old;
642 .float buttons_old;
643 .vector v_angle_old;
644 .string lastclassname;
645
646 .float() PlayerPhysplug;
647
648 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
649 .float specialcommand_pos;
650 void SpecialCommand()
651 {
652 #ifdef TETRIS
653         TetrisImpulse();
654 #else
655         if(!CheatImpulse(99))
656                 print("A hollow voice says \"Plugh\".\n");
657 #endif
658 }
659
660 float speedaward_speed;
661 string speedaward_holder;
662 void race_send_speedaward(float msg)
663 {
664         // send the best speed of the round
665         WriteByte(msg, SVC_TEMPENTITY);
666         WriteByte(msg, TE_CSQC_RACE);
667         WriteByte(msg, RACE_NET_SPEED_AWARD);
668         WriteInt24_t(msg, floor(speedaward_speed+0.5));
669         WriteString(msg, speedaward_holder);
670 }
671
672 float speedaward_alltimebest;
673 string speedaward_alltimebest_holder;
674 void race_send_speedaward_alltimebest(float msg)
675 {
676         // send the best speed
677         WriteByte(msg, SVC_TEMPENTITY);
678         WriteByte(msg, TE_CSQC_RACE);
679         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
680         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
681         WriteString(msg, speedaward_alltimebest_holder);
682 }
683
684 string GetMapname(void);
685 float speedaward_lastupdate;
686 float speedaward_lastsent;
687 void SV_PlayerPhysics()
688 {
689         local vector wishvel, wishdir, v;
690         local float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
691         string temps;
692         float buttons_prev;
693         float not_allowed_to_move;
694         string c;
695         
696         // fix physics stats for g_movement_highspeed
697         self.stat_sv_airaccel_qw = AdjustAirAccelQW(sv_airaccel_qw, autocvar_g_movement_highspeed);
698         if(sv_airstrafeaccel_qw)
699                 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(sv_airstrafeaccel_qw, autocvar_g_movement_highspeed);
700         else
701                 self.stat_sv_airstrafeaccel_qw = 0;
702         self.stat_sv_airspeedlimit_nonqw = sv_airspeedlimit_nonqw * autocvar_g_movement_highspeed;
703
704     if(self.PlayerPhysplug)
705         if(self.PlayerPhysplug())
706             return;
707
708         self.race_movetime_frac += frametime;
709         f = floor(self.race_movetime_frac);
710         self.race_movetime_frac -= f;
711         self.race_movetime_count += f;
712         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
713
714         anticheat_physics();
715
716         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);
717
718         if(!buttons)
719                 c = "x";
720         else if(buttons == 1)
721                 c = "1";
722         else if(buttons == 2)
723                 c = " ";
724         else if(buttons == 128)
725                 c = "s";
726         else if(buttons == 256)
727                 c = "w";
728         else if(buttons == 512)
729                 c = "a";
730         else if(buttons == 1024)
731                 c = "d";
732         else
733                 c = "?";
734
735         if(c == substring(specialcommand, self.specialcommand_pos, 1))
736         {
737                 self.specialcommand_pos += 1;
738                 if(self.specialcommand_pos >= strlen(specialcommand))
739                 {
740                         self.specialcommand_pos = 0;
741                         SpecialCommand();
742                         return;
743                 }
744         }
745         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
746                 self.specialcommand_pos = 0;
747
748         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
749         {
750                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
751                         self.parm_idlesince = time;
752         }
753         buttons_prev = self.buttons_old;
754         self.buttons_old = buttons;
755         self.movement_old = self.movement;
756         self.v_angle_old = self.v_angle;
757
758         if(time < self.nickspamtime)
759         if(self.nickspamcount >= cvar("g_nick_flood_penalty_yellow"))
760         {
761                 // slight annoyance for nick change scripts
762                 self.movement = -1 * self.movement;
763                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
764
765                 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!
766                 {
767                         self.angles_x = random() * 360;
768                         self.angles_y = random() * 360;
769                         // at least I'm not forcing retardedview by also assigning to angles_z
770                         self.fixangle = 1;
771                 }
772         }
773
774         if (self.punchangle != '0 0 0')
775         {
776                 f = vlen(self.punchangle) - 10 * frametime;
777                 if (f > 0)
778                         self.punchangle = normalize(self.punchangle) * f;
779                 else
780                         self.punchangle = '0 0 0';
781         }
782
783         if (self.punchvector != '0 0 0')
784         {
785                 f = vlen(self.punchvector) - 30 * frametime;
786                 if (f > 0)
787                         self.punchvector = normalize(self.punchvector) * f;
788                 else
789                         self.punchvector = '0 0 0';
790         }
791
792         if (clienttype(self) == CLIENTTYPE_BOT)
793         {
794                 if(playerdemo_read())
795                         return;
796                 bot_think();
797         }
798         
799         MUTATOR_CALLHOOK(PlayerPhysics);
800
801         self.items &~= IT_USING_JETPACK;
802
803         if(self.classname == "player")
804         {
805                 if(self.race_penalty)
806                         if(time > self.race_penalty)
807                                 self.race_penalty = 0;
808
809                 not_allowed_to_move = 0;
810                 if(self.race_penalty)
811                         not_allowed_to_move = 1;
812                 if(!cvar("sv_ready_restart_after_countdown"))
813                 if(time < game_starttime)
814                         not_allowed_to_move = 1;
815
816                 if(not_allowed_to_move)
817                 {
818                         self.velocity = '0 0 0';
819                         self.movetype = MOVETYPE_NONE;
820                         self.disableclientprediction = 2;
821                 }
822                 else if(self.disableclientprediction == 2)
823                 {
824                         if(self.movetype == MOVETYPE_NONE)
825                                 self.movetype = MOVETYPE_WALK;
826                         self.disableclientprediction = 0;
827                 }
828         }
829
830         if (self.movetype == MOVETYPE_NONE)
831                 return;
832
833         maxspd_mod = 1;
834
835         if(g_runematch)
836         {
837                 if(self.runes & RUNE_SPEED)
838                 {
839                         if(self.runes & CURSE_SLOW)
840                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_combo_moverate");
841                         else
842                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_moverate");
843                 }
844                 else if(self.runes & CURSE_SLOW)
845                 {
846                         maxspd_mod = maxspd_mod * cvar("g_balance_curse_slow_moverate");
847                 }
848         }
849
850         if(g_minstagib && (self.items & IT_INVINCIBLE))
851         {
852                 maxspd_mod = cvar("g_minstagib_speed_moverate");
853         }
854
855         if(g_nexball && self.ballcarried)
856         {
857                 maxspd_mod = cvar("g_nexball_basketball_carrier_speed");
858         }
859
860         swampspd_mod = 1;
861         if(self.in_swamp) {
862                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
863         }
864
865         if(self.classname != "player")
866         {
867                 maxspd_mod = cvar("sv_spectator_speed_multiplier");
868                 if(!self.spectatorspeed)
869                         self.spectatorspeed = maxspd_mod;
870                 if(self.impulse && self.impulse <= 19)
871                 {
872                         if(self.lastclassname != "player")
873                         {
874                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
875                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
876                                 else if(self.impulse == 11)
877                                         self.spectatorspeed = maxspd_mod;
878                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
879                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
880                                 else if(self.impulse >= 1 && self.impulse <= 9)
881                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
882                         } // otherwise just clear
883                         self.impulse = 0;
884                 }
885                 maxspd_mod = self.spectatorspeed;
886         }
887
888         spd = max(sv_maxspeed, sv_maxairspeed) * maxspd_mod * swampspd_mod;
889         if(self.speed != spd)
890         {
891                 self.speed = spd;
892                 temps = ftos(spd);
893                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
894                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
895                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
896                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
897         }
898
899         maxspd_mod *= swampspd_mod; // only one common speed modder please!
900         swampspd_mod = 1;
901
902         // if dead, behave differently
903         if (self.deadflag)
904                 goto end;
905
906         if (!self.fixangle && !g_bugrigs)
907         {
908                 self.angles_x = 0;
909                 self.angles_y = self.v_angle_y;
910                 self.angles_z = 0;
911         }
912
913         if(self.flags & FL_ONGROUND)
914         if(self.wasFlying)
915         {
916                 self.wasFlying = 0;
917
918                 if(self.waterlevel < WATERLEVEL_SWIMMING)
919                 if(time >= self.ladder_time)
920                 if not(self.hook)
921                 {
922                         self.nextstep = time + 0.3 + random() * 0.1;
923                         trace_dphitq3surfaceflags = 0;
924                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
925                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
926                         {
927                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
928                                         GlobalSound(globalsound_metalfall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
929                                 else
930                                         GlobalSound(globalsound_fall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
931                         }
932                 }
933         }
934
935         if(IsFlying(self))
936                 self.wasFlying = 1;
937
938         if(self.classname == "player")
939         {
940                 if(self.flags & FL_ONGROUND)
941                 {
942                         if (cvar("g_multijump") > 0)
943                                 self.multijump_count = 0;
944                         else
945                                 self.multijump_count = -2; // the cvar value for infinite jumps is -1, so this needs to be smaller
946                 }
947
948                 if(vlen(self.velocity) >= self.prevtopspeed || time - self.prevtopspeed_time > 0.25)
949                 {
950                         self.prevtopspeed_time = time;
951                         self.prevtopspeed = vlen(self.velocity - '0 0 1' * self.velocity_z);
952                 }
953
954                 if (self.BUTTON_JUMP)
955                         PlayerJump ();
956                 else
957                         self.flags |= FL_JUMPRELEASED;
958
959                 if (self.waterlevel == WATERLEVEL_SWIMMING)
960                         CheckWaterJump ();
961                 self.prevjumpbutton = self.BUTTON_JUMP;
962         }
963
964         if (self.flags & FL_WATERJUMP )
965         {
966                 self.velocity_x = self.movedir_x;
967                 self.velocity_y = self.movedir_y;
968                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
969                 {
970                         self.flags &~= FL_WATERJUMP;
971                         self.teleport_time = 0;
972                 }
973         }
974         else if (g_bugrigs && self.classname == "player")
975         {
976                 RaceCarPhysics();
977         }
978         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
979         {
980                 // noclipping or flying
981                 self.flags &~= FL_ONGROUND;
982
983                 self.velocity = self.velocity * (1 - frametime * sv_friction);
984                 makevectors(self.v_angle);
985                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
986                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
987                 // acceleration
988                 wishdir = normalize(wishvel);
989                 wishspeed = vlen(wishvel);
990                 if (wishspeed > sv_maxspeed*maxspd_mod)
991                         wishspeed = sv_maxspeed*maxspd_mod;
992                 if (time >= self.teleport_time)
993                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0, 0);
994         }
995         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
996         {
997                 // swimming
998                 self.flags &~= FL_ONGROUND;
999
1000                 makevectors(self.v_angle);
1001                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1002                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
1003                 if (wishvel == '0 0 0')
1004                         wishvel = '0 0 -60'; // drift towards bottom
1005
1006                 wishdir = normalize(wishvel);
1007                 wishspeed = vlen(wishvel);
1008                 if (wishspeed > sv_maxspeed*maxspd_mod)
1009                         wishspeed = sv_maxspeed*maxspd_mod;
1010                 wishspeed = wishspeed * 0.7;
1011
1012                 // water friction
1013                 self.velocity = self.velocity * (1 - frametime * sv_friction);
1014
1015                 // water acceleration
1016                 PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0, 0);
1017         }
1018         else if (time < self.ladder_time)
1019         {
1020                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1021                 self.flags &~= FL_ONGROUND;
1022
1023                 self.velocity = self.velocity * (1 - frametime * sv_friction);
1024                 makevectors(self.v_angle);
1025                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1026                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
1027                 if (self.gravity)
1028                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
1029                 else
1030                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
1031                 if (self.ladder_entity.classname == "func_water")
1032                 {
1033                         f = vlen(wishvel);
1034                         if (f > self.ladder_entity.speed)
1035                                 wishvel = wishvel * (self.ladder_entity.speed / f);
1036
1037                         self.watertype = self.ladder_entity.skin;
1038                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1039                         if ((self.origin_z + self.view_ofs_z) < f)
1040                                 self.waterlevel = WATERLEVEL_SUBMERGED;
1041                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1042                                 self.waterlevel = WATERLEVEL_SWIMMING;
1043                         else if ((self.origin_z + self.mins_z + 1) < f)
1044                                 self.waterlevel = WATERLEVEL_WETFEET;
1045                         else
1046                         {
1047                                 self.waterlevel = WATERLEVEL_NONE;
1048                                 self.watertype = CONTENT_EMPTY;
1049                         }
1050                 }
1051                 // acceleration
1052                 wishdir = normalize(wishvel);
1053                 wishspeed = vlen(wishvel);
1054                 if (wishspeed > sv_maxspeed*maxspd_mod)
1055                         wishspeed = sv_maxspeed*maxspd_mod;
1056                 if (time >= self.teleport_time)
1057                 {
1058                         // water acceleration
1059                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0, 0);
1060                 }
1061         }
1062         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!cvar("g_jetpack_fuel") || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
1063         {
1064                 //makevectors(self.v_angle_y * '0 1 0');
1065                 makevectors(self.v_angle);
1066                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1067                 // add remaining speed as Z component
1068                 maxairspd = sv_maxairspeed*max(1, maxspd_mod);
1069                 // fix speedhacks :P
1070                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1071                 // add the unused velocity as up component
1072                 wishvel_z = 0;
1073
1074                 // if(self.BUTTON_JUMP)
1075                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1076
1077                 // it is now normalized, so...
1078                 float a_side, a_up, a_add, a_diff;
1079                 a_side = cvar("g_jetpack_acceleration_side");
1080                 a_up = cvar("g_jetpack_acceleration_up");
1081                 a_add = cvar("g_jetpack_antigravity") * sv_gravity;
1082
1083                 wishvel_x *= a_side;
1084                 wishvel_y *= a_side;
1085                 wishvel_z *= a_up;
1086                 wishvel_z += a_add;
1087
1088                 float best;
1089                 best = 0;
1090                 //////////////////////////////////////////////////////////////////////////////////////
1091                 // finding the maximum over all vectors of above form
1092                 // with wishvel having an absolute value of 1
1093                 //////////////////////////////////////////////////////////////////////////////////////
1094                 // we're finding the maximum over
1095                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1096                 // for z in the range from -1 to 1
1097                 //////////////////////////////////////////////////////////////////////////////////////
1098                 // maximum is EITHER attained at the single extreme point:
1099                 a_diff = a_side * a_side - a_up * a_up;
1100                 if(a_diff != 0)
1101                 {
1102                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1103                         if(f > -1 && f < 1) // can it be attained?
1104                         {
1105                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1106                                 //print("middle\n");
1107                         }
1108                 }
1109                 // OR attained at z = 1:
1110                 f = (a_up + a_add) * (a_up + a_add);
1111                 if(f > best)
1112                 {
1113                         best = f;
1114                         //print("top\n");
1115                 }
1116                 // OR attained at z = -1:
1117                 f = (a_up - a_add) * (a_up - a_add);
1118                 if(f > best)
1119                 {
1120                         best = f;
1121                         //print("bottom\n");
1122                 }
1123                 best = sqrt(best);
1124                 //////////////////////////////////////////////////////////////////////////////////////
1125
1126                 //print("best possible acceleration: ", ftos(best), "\n");
1127
1128                 float fxy, fz;
1129                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / cvar("g_jetpack_maxspeed_side"), 1);
1130                 if(wishvel_z - sv_gravity > 0)
1131                         fz = bound(0, 1 - self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1132                 else
1133                         fz = bound(0, 1 + self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1134
1135                 float fvel;
1136                 fvel = vlen(wishvel);
1137                 wishvel_x *= fxy;
1138                 wishvel_y *= fxy;
1139                 wishvel_z = (wishvel_z - sv_gravity) * fz + sv_gravity;
1140
1141                 fvel = min(1, vlen(wishvel) / best);
1142                 if(cvar("g_jetpack_fuel") && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1143                         f = min(1, self.ammo_fuel / (cvar("g_jetpack_fuel") * frametime * fvel));
1144                 else
1145                         f = 1;
1146
1147                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1148
1149                 if (f > 0 && wishvel != '0 0 0')
1150                 {
1151                         self.velocity = self.velocity + wishvel * f * frametime;
1152                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1153                                 self.ammo_fuel -= cvar("g_jetpack_fuel") * frametime * fvel * f;
1154                         self.flags &~= FL_ONGROUND;
1155                         self.items |= IT_USING_JETPACK;
1156
1157                         // jetpack also inhibits health regeneration, but only for 1 second
1158                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
1159                 }
1160         }
1161         else if (self.flags & FL_ONGROUND)
1162         {
1163                 // we get here if we ran out of ammo
1164                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1165                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1166
1167                 // walking
1168                 makevectors(self.v_angle_y * '0 1 0');
1169                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1170
1171                 if(!(self.lastflags & FL_ONGROUND))
1172                 {
1173                         if(cvar("speedmeter"))
1174                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1175                         if(self.lastground < time - 0.3)
1176                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
1177                         if(self.jumppadcount > 1)
1178                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1179                         self.jumppadcount = 0;
1180                 }
1181
1182 #ifdef LETS_TEST_FTEQCC
1183                 if(self.velocity_x || self.velocity_y)
1184                 {
1185                         // good
1186                 }
1187                 else
1188                 {
1189                         if(self.velocity_x)
1190                                 checkclient();
1191                         if(self.velocity_y)
1192                                 checkclient();
1193                 }
1194 #endif
1195
1196                 v = self.velocity;
1197                 v_z = 0;
1198                 f = vlen(v);
1199                 if(f > 0)
1200                 {
1201                         if (f < sv_stopspeed)
1202                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
1203                         else
1204                                 f = 1 - frametime * sv_friction;
1205                         if (f > 0)
1206                                 self.velocity = self.velocity * f;
1207                         else
1208                                 self.velocity = '0 0 0';
1209                 }
1210
1211                 // acceleration
1212                 wishdir = normalize(wishvel);
1213                 wishspeed = vlen(wishvel);
1214                 if (wishspeed > sv_maxspeed*maxspd_mod)
1215                         wishspeed = sv_maxspeed*maxspd_mod;
1216                 if (self.crouch)
1217                         wishspeed = wishspeed * 0.5;
1218                 if (time >= self.teleport_time)
1219                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0, 0);
1220         }
1221         else
1222         {
1223                 float wishspeed0;
1224                 // we get here if we ran out of ammo
1225                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1226                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1227
1228                 if(maxspd_mod < 1)
1229                 {
1230                         maxairspd = sv_maxairspeed*maxspd_mod;
1231                         airaccel = sv_airaccelerate*maxspd_mod;
1232                 }
1233                 else
1234                 {
1235                         maxairspd = sv_maxairspeed;
1236                         airaccel = sv_airaccelerate;
1237                 }
1238                 // airborn
1239                 makevectors(self.v_angle_y * '0 1 0');
1240                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1241                 // acceleration
1242                 wishdir = normalize(wishvel);
1243                 wishspeed = wishspeed0 = vlen(wishvel);
1244                 if (wishspeed0 > sv_maxspeed*maxspd_mod)
1245                         wishspeed0 = sv_maxspeed*maxspd_mod;
1246                 if (wishspeed > maxairspd)
1247                         wishspeed = maxairspd;
1248                 if (self.crouch)
1249                         wishspeed = wishspeed * 0.5;
1250                 if (time >= self.teleport_time)
1251                 {
1252                         float accelerating;
1253                         float wishspeed2;
1254                         float airaccelqw;
1255                         float strafity;
1256
1257                         airaccelqw = self.stat_sv_airaccel_qw;
1258                         accelerating = (self.velocity * wishdir > 0);
1259                         wishspeed2 = wishspeed;
1260
1261                         // CPM
1262                         if(sv_airstopaccelerate)
1263                         {
1264                                 vector curdir;
1265                                 curdir = self.velocity;
1266                                 curdir_z = 0;
1267                                 curdir = normalize(curdir);
1268                                 airaccel = airaccel + (sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1269                         }
1270                         // note that for straight forward jumping:
1271                         // step = accel * frametime * wishspeed0;
1272                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1273                         // -->
1274                         // dv/dt = accel * maxspeed (when slow)
1275                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1276                         // log dv/dt = logaccel + logmaxspeed (when slow)
1277                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1278                         strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1279                         if(sv_maxairstrafespeed)
1280                                 wishspeed = min(wishspeed, GeomLerp(sv_maxairspeed*maxspd_mod, strafity, sv_maxairstrafespeed*maxspd_mod));
1281                         if(sv_airstrafeaccelerate)
1282                                 airaccel = GeomLerp(airaccel, strafity, sv_airstrafeaccelerate*maxspd_mod);
1283                         if(self.stat_sv_airstrafeaccel_qw)
1284                                 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));
1285                         // !CPM
1286
1287                         if(sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1288                                 PM_AirAccelerate(wishdir, wishspeed);
1289                         else
1290                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1291
1292                         if(sv_aircontrol)
1293                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1294                 }
1295         }
1296
1297         if((g_cts || g_race) && self.classname != "observer") {
1298                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1299                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1300                         speedaward_holder = self.netname;
1301                         speedaward_lastupdate = time;
1302                 }
1303                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1304                         string rr;
1305                         if(g_cts)
1306                                 rr = CTS_RECORD;
1307                         else
1308                                 rr = RACE_RECORD;
1309                         race_send_speedaward(MSG_ALL);
1310                         speedaward_lastsent = speedaward_speed;
1311                         if (speedaward_speed > speedaward_alltimebest) {
1312                                 speedaward_alltimebest = speedaward_speed;
1313                                 speedaward_alltimebest_holder = speedaward_holder;
1314                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1315                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/netname"), speedaward_alltimebest_holder);
1316                                 race_send_speedaward_alltimebest(MSG_ALL);
1317                         }
1318                 }
1319         }
1320 :end
1321         if(self.flags & FL_ONGROUND)
1322                 self.lastground = time;
1323
1324         self.lastflags = self.flags;
1325         self.lastclassname = self.classname;
1326 };