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