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