]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
Merge remote-tracking branch 'origin/master' into terencehill/spectate_prev
[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(g_minstagib && (self.items & IT_INVINCIBLE))
702                 maxspd_mod *= autocvar_g_minstagib_speed_highspeed;
703         if(self.ballcarried)
704                 if(g_nexball)
705                         maxspd_mod *= autocvar_g_nexball_basketball_carrier_highspeed;
706                 else if(g_keepaway)
707                         maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
708
709         maxspd_mod *= autocvar_g_movement_highspeed;
710
711         // fix physics stats for g_movement_highspeed
712         // TODO maybe rather use maxairspeed? needs testing
713         self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
714         if(autocvar_sv_airstrafeaccel_qw)
715                 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
716         else
717                 self.stat_sv_airstrafeaccel_qw = 0;
718         self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
719         self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
720
721     if(self.PlayerPhysplug)
722         if(self.PlayerPhysplug())
723             return;
724
725         self.race_movetime_frac += frametime;
726         f = floor(self.race_movetime_frac);
727         self.race_movetime_frac -= f;
728         self.race_movetime_count += f;
729         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
730
731         anticheat_physics();
732
733         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);
734
735         if(!buttons)
736                 c = "x";
737         else if(buttons == 1)
738                 c = "1";
739         else if(buttons == 2)
740                 c = " ";
741         else if(buttons == 128)
742                 c = "s";
743         else if(buttons == 256)
744                 c = "w";
745         else if(buttons == 512)
746                 c = "a";
747         else if(buttons == 1024)
748                 c = "d";
749         else
750                 c = "?";
751
752         if(c == substring(specialcommand, self.specialcommand_pos, 1))
753         {
754                 self.specialcommand_pos += 1;
755                 if(self.specialcommand_pos >= strlen(specialcommand))
756                 {
757                         self.specialcommand_pos = 0;
758                         SpecialCommand();
759                         return;
760                 }
761         }
762         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
763                 self.specialcommand_pos = 0;
764
765         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
766         {
767                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
768                         self.parm_idlesince = time;
769         }
770         buttons_prev = self.buttons_old;
771         self.buttons_old = buttons;
772         self.movement_old = self.movement;
773         self.v_angle_old = self.v_angle;
774
775         if(time < self.nickspamtime)
776         if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
777         {
778                 // slight annoyance for nick change scripts
779                 self.movement = -1 * self.movement;
780                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
781
782                 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!
783                 {
784                         self.angles_x = random() * 360;
785                         self.angles_y = random() * 360;
786                         // at least I'm not forcing retardedview by also assigning to angles_z
787                         self.fixangle = TRUE;
788                 }
789         }
790
791         if (self.punchangle != '0 0 0')
792         {
793                 f = vlen(self.punchangle) - 10 * frametime;
794                 if (f > 0)
795                         self.punchangle = normalize(self.punchangle) * f;
796                 else
797                         self.punchangle = '0 0 0';
798         }
799
800         if (self.punchvector != '0 0 0')
801         {
802                 f = vlen(self.punchvector) - 30 * frametime;
803                 if (f > 0)
804                         self.punchvector = normalize(self.punchvector) * f;
805                 else
806                         self.punchvector = '0 0 0';
807         }
808
809         if (clienttype(self) == CLIENTTYPE_BOT)
810         {
811                 if(playerdemo_read())
812                         return;
813                 bot_think();
814         }
815         
816         self.items &~= IT_USING_JETPACK;
817
818         if(self.classname == "player")
819         {
820                 if(self.race_penalty)
821                         if(time > self.race_penalty)
822                                 self.race_penalty = 0;
823
824                 not_allowed_to_move = 0;
825                 if(self.race_penalty)
826                         not_allowed_to_move = 1;
827                 if(!autocvar_sv_ready_restart_after_countdown)
828                 if(time < game_starttime)
829                         not_allowed_to_move = 1;
830
831                 if(not_allowed_to_move)
832                 {
833                         self.velocity = '0 0 0';
834                         self.movetype = MOVETYPE_NONE;
835                         self.disableclientprediction = 2;
836                 }
837                 else if(self.disableclientprediction == 2)
838                 {
839                         if(self.movetype == MOVETYPE_NONE)
840                                 self.movetype = MOVETYPE_WALK;
841                         self.disableclientprediction = 0;
842                 }
843         }
844
845         if (self.movetype == MOVETYPE_NONE)
846                 return;
847
848         // when we get here, disableclientprediction cannot be 2
849         self.disableclientprediction = 0;
850         if(time < self.ladder_time)
851                 self.disableclientprediction = 1;
852
853         MUTATOR_CALLHOOK(PlayerPhysics);
854
855         if(self.player_blocked)
856         {
857                 self.movement = '0 0 0';
858                 self.disableclientprediction = 1;
859         }
860
861         maxspd_mod = 1;
862
863         swampspd_mod = 1;
864         if(self.in_swamp) {
865                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
866         }
867
868         // conveyors: first fix velocity
869         if(self.conveyor.state)
870                 self.velocity -= self.conveyor.movedir;
871
872         if(self.classname != "player")
873         {
874                 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
875                 if(!self.spectatorspeed)
876                         self.spectatorspeed = maxspd_mod;
877                 if(self.impulse && self.impulse <= 19 || self.impulse >= 200 && self.impulse <= 209 || self.impulse >= 220 && self.impulse <= 229)
878                 {
879                         if(self.lastclassname != "player")
880                         {
881                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || self.impulse >= 200 && self.impulse <= 209)
882                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
883                                 else if(self.impulse == 11)
884                                         self.spectatorspeed = maxspd_mod;
885                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || self.impulse >= 220 && self.impulse <= 229)
886                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
887                                 else if(self.impulse >= 1 && self.impulse <= 9)
888                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
889                         } // otherwise just clear
890                         self.impulse = 0;
891                 }
892                 maxspd_mod = self.spectatorspeed;
893         }
894
895         spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
896         if(self.speed != spd)
897         {
898                 self.speed = spd;
899                 temps = ftos(spd);
900                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
901                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
902                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
903                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
904         }
905
906         maxspd_mod *= swampspd_mod; // only one common speed modder please!
907         swampspd_mod = 1;
908
909         // if dead, behave differently
910         if (self.deadflag)
911                 goto end;
912
913         if (!self.fixangle && !g_bugrigs)
914         {
915                 self.angles_x = 0;
916                 self.angles_y = self.v_angle_y;
917                 self.angles_z = 0;
918         }
919
920         if(self.flags & FL_ONGROUND)
921         if(self.classname == "player") // no fall sounds for observers thank you very much
922         if(self.wasFlying)
923         {
924                 self.wasFlying = 0;
925
926                 if(self.waterlevel < WATERLEVEL_SWIMMING)
927                 if(time >= self.ladder_time)
928                 if not(self.hook)
929                 {
930                         self.nextstep = time + 0.3 + random() * 0.1;
931                         trace_dphitq3surfaceflags = 0;
932                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
933                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
934                         {
935                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
936                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
937                                 else
938                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
939                         }
940                 }
941         }
942
943         if(IsFlying(self))
944                 self.wasFlying = 1;
945
946         if(self.classname == "player")
947                 CheckPlayerJump();
948
949         if (self.flags & FL_WATERJUMP )
950         {
951                 self.velocity_x = self.movedir_x;
952                 self.velocity_y = self.movedir_y;
953                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
954                 {
955                         self.flags &~= FL_WATERJUMP;
956                         self.teleport_time = 0;
957                 }
958         }
959         else if (g_bugrigs && self.classname == "player")
960         {
961                 RaceCarPhysics();
962         }
963         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
964         {
965                 // noclipping or flying
966                 self.flags &~= FL_ONGROUND;
967
968                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
969                 makevectors(self.v_angle);
970                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
971                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
972                 // acceleration
973                 wishdir = normalize(wishvel);
974                 wishspeed = vlen(wishvel);
975                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
976                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
977                 if (time >= self.teleport_time)
978                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
979         }
980         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
981         {
982                 // swimming
983                 self.flags &~= FL_ONGROUND;
984
985                 makevectors(self.v_angle);
986                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
987                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
988                 if (wishvel == '0 0 0')
989                         wishvel = '0 0 -60'; // drift towards bottom
990
991                 wishdir = normalize(wishvel);
992                 wishspeed = vlen(wishvel);
993                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
994                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
995                 wishspeed = wishspeed * 0.7;
996
997                 // water friction
998                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
999
1000                 // water acceleration
1001                 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1002         }
1003         else if (time < self.ladder_time)
1004         {
1005                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1006                 self.flags &~= FL_ONGROUND;
1007
1008                 float g;
1009                 g = autocvar_sv_gravity * frametime;
1010                 if(self.gravity)
1011                         g *= self.gravity;
1012                 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
1013                 {
1014                         g *= 0.5;
1015                         self.velocity_z += g;
1016                 }
1017
1018                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
1019                 makevectors(self.v_angle);
1020                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1021                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
1022                 self.velocity_z += g;
1023                 if (self.ladder_entity.classname == "func_water")
1024                 {
1025                         f = vlen(wishvel);
1026                         if (f > self.ladder_entity.speed)
1027                                 wishvel = wishvel * (self.ladder_entity.speed / f);
1028
1029                         self.watertype = self.ladder_entity.skin;
1030                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1031                         if ((self.origin_z + self.view_ofs_z) < f)
1032                                 self.waterlevel = WATERLEVEL_SUBMERGED;
1033                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1034                                 self.waterlevel = WATERLEVEL_SWIMMING;
1035                         else if ((self.origin_z + self.mins_z + 1) < f)
1036                                 self.waterlevel = WATERLEVEL_WETFEET;
1037                         else
1038                         {
1039                                 self.waterlevel = WATERLEVEL_NONE;
1040                                 self.watertype = CONTENT_EMPTY;
1041                         }
1042                 }
1043                 // acceleration
1044                 wishdir = normalize(wishvel);
1045                 wishspeed = vlen(wishvel);
1046                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1047                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1048                 if (time >= self.teleport_time)
1049                 {
1050                         // water acceleration
1051                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1052                 }
1053         }
1054         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)
1055         {
1056                 //makevectors(self.v_angle_y * '0 1 0');
1057                 makevectors(self.v_angle);
1058                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1059                 // add remaining speed as Z component
1060                 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1061                 // fix speedhacks :P
1062                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1063                 // add the unused velocity as up component
1064                 wishvel_z = 0;
1065
1066                 // if(self.BUTTON_JUMP)
1067                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1068
1069                 // it is now normalized, so...
1070                 float a_side, a_up, a_add, a_diff;
1071                 a_side = autocvar_g_jetpack_acceleration_side;
1072                 a_up = autocvar_g_jetpack_acceleration_up;
1073                 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1074
1075                 wishvel_x *= a_side;
1076                 wishvel_y *= a_side;
1077                 wishvel_z *= a_up;
1078                 wishvel_z += a_add;
1079
1080                 float best;
1081                 best = 0;
1082                 //////////////////////////////////////////////////////////////////////////////////////
1083                 // finding the maximum over all vectors of above form
1084                 // with wishvel having an absolute value of 1
1085                 //////////////////////////////////////////////////////////////////////////////////////
1086                 // we're finding the maximum over
1087                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1088                 // for z in the range from -1 to 1
1089                 //////////////////////////////////////////////////////////////////////////////////////
1090                 // maximum is EITHER attained at the single extreme point:
1091                 a_diff = a_side * a_side - a_up * a_up;
1092                 if(a_diff != 0)
1093                 {
1094                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1095                         if(f > -1 && f < 1) // can it be attained?
1096                         {
1097                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1098                                 //print("middle\n");
1099                         }
1100                 }
1101                 // OR attained at z = 1:
1102                 f = (a_up + a_add) * (a_up + a_add);
1103                 if(f > best)
1104                 {
1105                         best = f;
1106                         //print("top\n");
1107                 }
1108                 // OR attained at z = -1:
1109                 f = (a_up - a_add) * (a_up - a_add);
1110                 if(f > best)
1111                 {
1112                         best = f;
1113                         //print("bottom\n");
1114                 }
1115                 best = sqrt(best);
1116                 //////////////////////////////////////////////////////////////////////////////////////
1117
1118                 //print("best possible acceleration: ", ftos(best), "\n");
1119
1120                 float fxy, fz;
1121                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1122                 if(wishvel_z - autocvar_sv_gravity > 0)
1123                         fz = bound(0, 1 - self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1124                 else
1125                         fz = bound(0, 1 + self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1126
1127                 float fvel;
1128                 fvel = vlen(wishvel);
1129                 wishvel_x *= fxy;
1130                 wishvel_y *= fxy;
1131                 wishvel_z = (wishvel_z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1132
1133                 fvel = min(1, vlen(wishvel) / best);
1134                 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1135                         f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1136                 else
1137                         f = 1;
1138
1139                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1140
1141                 if (f > 0 && wishvel != '0 0 0')
1142                 {
1143                         self.velocity = self.velocity + wishvel * f * frametime;
1144                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1145                                 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1146                         self.flags &~= FL_ONGROUND;
1147                         self.items |= IT_USING_JETPACK;
1148
1149                         // jetpack also inhibits health regeneration, but only for 1 second
1150                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1151                 }
1152         }
1153         else if (self.flags & FL_ONGROUND)
1154         {
1155                 // we get here if we ran out of ammo
1156                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1157                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1158
1159                 // walking
1160                 makevectors(self.v_angle_y * '0 1 0');
1161                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1162
1163                 if(!(self.lastflags & FL_ONGROUND))
1164                 {
1165                         if(autocvar_speedmeter)
1166                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1167                         if(self.lastground < time - 0.3)
1168                                 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1169                         if(self.jumppadcount > 1)
1170                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1171                         self.jumppadcount = 0;
1172                 }
1173
1174 #ifdef LETS_TEST_FTEQCC
1175                 if(self.velocity_x || self.velocity_y)
1176                 {
1177                         // good
1178                 }
1179                 else
1180                 {
1181                         if(self.velocity_x)
1182                                 checkclient();
1183                         if(self.velocity_y)
1184                                 checkclient();
1185                 }
1186 #endif
1187
1188                 v = self.velocity;
1189                 v_z = 0;
1190                 f = vlen(v);
1191                 if(f > 0)
1192                 {
1193                         if (f < autocvar_sv_stopspeed)
1194                                 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1195                         else
1196                                 f = 1 - frametime * autocvar_sv_friction;
1197                         if (f > 0)
1198                                 self.velocity = self.velocity * f;
1199                         else
1200                                 self.velocity = '0 0 0';
1201                         /*
1202                            Mathematical analysis time!
1203
1204                            Our goal is to invert this mess.
1205
1206                            For the two cases we get:
1207                                 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1208                                   = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1209                                 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1210                            and
1211                                 v = v0 * (1 - frametime * autocvar_sv_friction)
1212                                 v0 = v / (1 - frametime * autocvar_sv_friction)
1213
1214                            These cases would be chosen ONLY if:
1215                                 v0 < autocvar_sv_stopspeed
1216                                 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1217                                 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1218                            and, respectively:
1219                                 v0 >= autocvar_sv_stopspeed
1220                                 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1221                                 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1222                          */
1223                 }
1224
1225                 // acceleration
1226                 wishdir = normalize(wishvel);
1227                 wishspeed = vlen(wishvel);
1228                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1229                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1230                 if (self.crouch)
1231                         wishspeed = wishspeed * 0.5;
1232                 if (time >= self.teleport_time)
1233                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1234         }
1235         else
1236         {
1237                 float wishspeed0;
1238                 // we get here if we ran out of ammo
1239                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1240                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1241
1242                 if(maxspd_mod < 1)
1243                 {
1244                         maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1245                         airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1246                 }
1247                 else
1248                 {
1249                         maxairspd = autocvar_sv_maxairspeed;
1250                         airaccel = autocvar_sv_airaccelerate;
1251                 }
1252                 // airborn
1253                 makevectors(self.v_angle_y * '0 1 0');
1254                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1255                 // acceleration
1256                 wishdir = normalize(wishvel);
1257                 wishspeed = wishspeed0 = vlen(wishvel);
1258                 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1259                         wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1260                 if (wishspeed > maxairspd)
1261                         wishspeed = maxairspd;
1262                 if (self.crouch)
1263                         wishspeed = wishspeed * 0.5;
1264                 if (time >= self.teleport_time)
1265                 {
1266                         float accelerating;
1267                         float wishspeed2;
1268                         float airaccelqw;
1269                         float strafity;
1270
1271                         airaccelqw = self.stat_sv_airaccel_qw;
1272                         accelerating = (self.velocity * wishdir > 0);
1273                         wishspeed2 = wishspeed;
1274
1275                         // CPM
1276                         if(autocvar_sv_airstopaccelerate)
1277                         {
1278                                 vector curdir;
1279                                 curdir = self.velocity;
1280                                 curdir_z = 0;
1281                                 curdir = normalize(curdir);
1282                                 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1283                         }
1284                         // note that for straight forward jumping:
1285                         // step = accel * frametime * wishspeed0;
1286                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1287                         // -->
1288                         // dv/dt = accel * maxspeed (when slow)
1289                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1290                         // log dv/dt = logaccel + logmaxspeed (when slow)
1291                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1292                         strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1293                         if(autocvar_sv_maxairstrafespeed)
1294                                 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1295                         if(autocvar_sv_airstrafeaccelerate)
1296                                 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1297                         if(self.stat_sv_airstrafeaccel_qw)
1298                                 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));
1299                         // !CPM
1300
1301                         if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1302                                 PM_AirAccelerate(wishdir, wishspeed);
1303                         else
1304                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1305
1306                         if(autocvar_sv_aircontrol)
1307                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1308                 }
1309         }
1310
1311         if((g_cts || g_race) && self.classname != "observer") {
1312                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1313                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1314                         speedaward_holder = self.netname;
1315                         speedaward_uid = self.crypto_idfp;
1316                         speedaward_lastupdate = time;
1317                 }
1318                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1319                         string rr;
1320                         if(g_cts)
1321                                 rr = CTS_RECORD;
1322                         else
1323                                 rr = RACE_RECORD;
1324                         race_send_speedaward(MSG_ALL);
1325                         speedaward_lastsent = speedaward_speed;
1326                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "") {
1327                                 speedaward_alltimebest = speedaward_speed;
1328                                 speedaward_alltimebest_holder = speedaward_holder;
1329                                 speedaward_alltimebest_uid = speedaward_uid;
1330                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1331                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1332                                 race_send_speedaward_alltimebest(MSG_ALL);
1333                         }
1334                 }
1335         }
1336
1337         float xyspeed;
1338         xyspeed = vlen('1 0 0' * self.velocity_x + '0 1 0' * self.velocity_y);
1339         if(self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1340         {
1341                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1342                 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1343                 f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1344                 // add the extra charge
1345                 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * frametime);
1346         }
1347 :end
1348         if(self.flags & FL_ONGROUND)
1349                 self.lastground = time;
1350
1351         // conveyors: then break velocity again
1352         if(self.conveyor.state)
1353                 self.velocity += self.conveyor.movedir;
1354
1355         self.lastflags = self.flags;
1356         self.lastclassname = self.classname;
1357 }