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