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