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