Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Deceleration Time and Distance Script v2 - 11/18/16 ///
- */
- const string textPanelName = "Deceleration";
- const double updatesPerSecond = 10;
- //-------------------------------------------------------------------------
- //============ NO TOUCH BELOW HERE!!! =====================================
- //-------------------------------------------------------------------------
- const double timeMaxCycle = 1 / updatesPerSecond;
- double timeCurrentCycle = 0;
- List<List<IMyThrust>> masterThrustList = new List<List<IMyThrust>>();
- List<IMyThrust> forwardThrust = new List<IMyThrust>();
- List<IMyThrust> backThrust = new List<IMyThrust>();
- List<IMyThrust> upThrust = new List<IMyThrust>();
- List<IMyThrust> downThrust = new List<IMyThrust>();
- List<IMyThrust> leftThrust = new List<IMyThrust>();
- List<IMyThrust> rightThrust = new List<IMyThrust>();
- Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- }
- void Main(string argument, UpdateType updateType)
- {
- if ((updateType & UpdateType.Update10) != 0)
- {
- GetBrakingDistanceAndTime();
- timeCurrentCycle = 0;
- }
- }
- void GetBrakingDistanceAndTime()
- {
- List<IMyTerminalBlock> shipControllers = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
- if (shipControllers.Count == 0)
- {
- Echo("Critical Error: no ship controllers found");
- return;
- }
- IMyShipController thisController = shipControllers[0] as IMyShipController; ///w.h-i*p
- Vector3D velocityVec = thisController.GetShipVelocities().LinearVelocity; //get ship velocity vector
- double shipSpeed = thisController.GetShipSpeed();
- double shipMass = thisController.CalculateShipMass().TotalMass; //get ship mass
- GetThrusterDirections(thisController); //get all thruster direcions
- double thrusterForceSum = 0;
- //Get the effective force sum from all normally mounted thrusters
- foreach (List<IMyThrust> thrusterList in masterThrustList)
- {
- if (thrusterList.Count == 0)
- {
- continue;
- }
- IMyThrust thisThrust = thrusterList[0];
- Vector3D thrustVec = thisThrust.WorldMatrix.Forward; //since we are attempting to brake
- if (thrustVec.Dot(velocityVec) > 0)
- {
- double effectiveThrustRatio = VectorProjection(thrustVec, velocityVec).Length(); //this ranges from 0 to 1
- thrusterForceSum += GetThrusterForceSum(thrusterList) * effectiveThrustRatio;
- }
- }
- //Calculate our max deceleration magnitude
- double deceleration = thrusterForceSum / shipMass;
- double time2Stop = shipSpeed / deceleration; //derived from: vf = vi + a*t
- double distance2Stop = shipSpeed * shipSpeed / (2 * deceleration); //derived from: vf^2 = vi^2 + 2*a*d
- if (shipSpeed == 0)
- {
- time2Stop = 0; distance2Stop = 0;
- }
- string outputText = $"\n Speed: {Math.Round(shipSpeed, 1).ToString()} m/s\n Deceleration: {Math.Round(deceleration, 1).ToString()} m/s/2\n Time to stop: {Math.Round(time2Stop, 1).ToString()} s \n Distance to stop: {Math.Round(distance2Stop, 1).ToString()} m";
- WriteToTextPanel(textPanelName, outputText, null, null);
- }
- void GetThrusterDirections(IMyTerminalBlock reference)
- {
- masterThrustList.Clear();
- forwardThrust.Clear();
- backThrust.Clear();
- upThrust.Clear();
- downThrust.Clear();
- leftThrust.Clear();
- rightThrust.Clear();
- List<IMyTerminalBlock> allThrusters = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyThrust>(allThrusters);
- foreach (IMyThrust thrust in allThrusters)
- {
- if (thrust.CubeGrid != reference.CubeGrid)
- {
- continue;
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Forward)
- {
- forwardThrust.Add(thrust);
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Backward)
- {
- backThrust.Add(thrust);
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Up)
- {
- upThrust.Add(thrust);
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Down)
- {
- downThrust.Add(thrust);
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Left)
- {
- leftThrust.Add(thrust);
- }
- else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Right)
- {
- rightThrust.Add(thrust);
- }
- }
- masterThrustList.Add(forwardThrust);
- masterThrustList.Add(backThrust);
- masterThrustList.Add(upThrust);
- masterThrustList.Add(downThrust);
- masterThrustList.Add(leftThrust);
- masterThrustList.Add(rightThrust);
- }
- double GetThrusterForceSum(List<IMyThrust> thrustList)
- {
- double sum = 0;
- foreach (IMyThrust thisThrust in thrustList)
- {
- if (thisThrust.Enabled)
- sum += thisThrust.MaxEffectiveThrust;
- }
- return sum;
- }
- Vector3D VectorProjection(Vector3D a, Vector3D b)
- {
- Vector3D projection = a.Dot(b) / b.LengthSquared() * b;
- return projection;
- }
- //Whip's Write to Text Panel Method v2
- void WriteToTextPanel(string textPanelName, string textToWrite, float? fontSize, Color? fontColor)
- {
- var listScreens = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName(textPanelName, listScreens, block => block is IMyTextPanel);
- if (listScreens.Count == 0)
- {
- Echo("Error: No text panel with name tag '" + textPanelName + "' was found");
- return;
- }
- else
- {
- foreach (IMyTextPanel thisScreen in listScreens)
- {
- thisScreen.WritePublicText(textToWrite);
- thisScreen.ShowPublicTextOnScreen();
- if (fontSize.HasValue)
- thisScreen.SetValue("FontSize", fontSize);
- if (fontColor.HasValue)
- thisScreen.SetValue("FontColor", fontColor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement