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