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