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