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