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