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