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