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