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