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