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