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