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