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