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