# Untitled

May 14th, 2021
866
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2.         //Returns the speed (m/s) you should be going at, given a distance (m) to the target,
3.         //the target speed (m/s) at distance Zero and a theoretical max deceleration (m/s^2)
4.         public static float MapIdealSpeedForDistanceG(Racer r, float distance, float speedThere)
5.         {
6.             //Absolute minimum
7.             if (distance <= 0f) return speedThere;
8.
9.             //Math variables
10.             float targetDistance = distance;
11.             if (targetDistance < 0f) targetDistance = 0f;
12.             float velCurrent = r.Car.Velocity.Length();
13.             float velTarget = speedThere;
14.             float timeToReachTarget = targetDistance / velCurrent;
15.
16.             //Braking ability has to account for at least 25% of the wheel grip to count as fully taking advantage of the grip. Else, braking Gs are less than grip Gs
17.             float brakingAbility = (float)Math.Round(ARS.map(ARS.GetPercent(r.handlingData.BrakingAbility, r.vehData.WheelsGrip), 0f, 25f, 0f, 1f, true), 2);
18.
19.             //Gravity equals to 1G when fully pointing down;
20.             float gravityAssist = ARS.map(r.Car.ForwardVector.Normalized.Z, -1, 1, -1, 1, true);
21.
22.             //We want to decelerate this many Gs
23.             float targetDeceleration = ((velTarget - velCurrent) / (timeToReachTarget - 0)) / 9.8f;
24.             if (targetDistance == 0f) targetDeceleration = (velTarget - velCurrent);
25.
26.             //We can decelerate this many Gs
27.             float availableDeceleration = -(((r.vehData.WheelsGrip * brakingAbility) + gravityAssist));
28.
29.             //How many Gs over the car's braking ability we are allowed to go
30.             float Risk = DevSettingsFile.GetValue<float>("RACERS", "BaseBrakeRisk", 0.0f) + (r.mem.intention.Aggression * DevSettingsFile.GetValue<float>("RACERS", "AggroBrakeRisk", 0.0f));
31.
32.             //Brake risk. There's a 0.5Gs deceleration leeway between no braking and full brake.
33.             float NoBrake = -0.25f + Risk;
34.             float FullBrake = 0.25f + Risk;
35.
36.             float SpeedMod = ARS.map(targetDeceleration, availableDeceleration + NoBrake, availableDeceleration - FullBrake, -ARS.AIData.SpeedToInput, ARS.AIData.SpeedToInput, true);
37.             float spd = velCurrent + SpeedMod;
38.             return spd;
39.         }