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