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