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