Advertisement
Whiplash141

Whip's Deceleration Computer v2

Sep 26th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.22 KB | None | 0 0
  1. /*
  2. /// Whip's Deceleration Time and Distance Script v2 - 11/18/16 ///
  3. */
  4.  
  5.     const string textPanelName = "Deceleration";
  6.     const double updatesPerSecond = 10;
  7.  
  8. //-------------------------------------------------------------------------
  9. //============ NO TOUCH BELOW HERE!!! =====================================
  10. //-------------------------------------------------------------------------
  11.  
  12.     const double timeMaxCycle = 1 / updatesPerSecond;
  13.     double timeCurrentCycle = 0;
  14.  
  15.     List<List<IMyThrust>> masterThrustList = new List<List<IMyThrust>>();
  16.     List<IMyThrust> forwardThrust = new List<IMyThrust>();
  17.     List<IMyThrust> backThrust = new List<IMyThrust>();
  18.     List<IMyThrust> upThrust = new List<IMyThrust>();
  19.     List<IMyThrust> downThrust = new List<IMyThrust>();
  20.     List<IMyThrust> leftThrust = new List<IMyThrust>();
  21.     List<IMyThrust> rightThrust = new List<IMyThrust>();
  22.    
  23. Program()
  24. {
  25.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  26. }
  27.  
  28. void Main(string argument, UpdateType updateType)
  29. {
  30.     if ((updateType & UpdateType.Update10) != 0)
  31.     {
  32.         GetBrakingDistanceAndTime();
  33.         timeCurrentCycle = 0;
  34.     }
  35. }
  36.  
  37. void GetBrakingDistanceAndTime()
  38. {
  39.     List<IMyTerminalBlock> shipControllers = new List<IMyTerminalBlock>();
  40.     GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
  41.  
  42.     if (shipControllers.Count == 0)
  43.     {
  44.         Echo("Critical Error: no ship controllers found");
  45.         return;
  46.     }
  47.  
  48.     IMyShipController thisController = shipControllers[0] as IMyShipController;                                                                                                                                                                          ///w.h-i*p
  49.     Vector3D velocityVec = thisController.GetShipVelocities().LinearVelocity; //get ship velocity vector
  50.     double shipSpeed = thisController.GetShipSpeed();
  51.     double shipMass = thisController.CalculateShipMass().TotalMass; //get ship mass
  52.  
  53.     GetThrusterDirections(thisController); //get all thruster direcions
  54.  
  55.     double thrusterForceSum = 0;
  56.  
  57.     //Get the effective force sum from all normally mounted thrusters
  58.     foreach (List<IMyThrust> thrusterList in masterThrustList)
  59.     {
  60.         if (thrusterList.Count == 0)
  61.         {
  62.             continue;
  63.         }
  64.  
  65.         IMyThrust thisThrust = thrusterList[0];
  66.         Vector3D thrustVec = thisThrust.WorldMatrix.Forward; //since we are attempting to brake
  67.         if (thrustVec.Dot(velocityVec) > 0)
  68.         {
  69.             double effectiveThrustRatio = VectorProjection(thrustVec, velocityVec).Length(); //this ranges from 0 to 1
  70.             thrusterForceSum += GetThrusterForceSum(thrusterList) * effectiveThrustRatio;
  71.         }
  72.     }
  73.  
  74.     //Calculate our max deceleration magnitude
  75.     double deceleration = thrusterForceSum / shipMass;
  76.  
  77.     double time2Stop = shipSpeed / deceleration; //derived from: vf = vi + a*t
  78.     double distance2Stop = shipSpeed * shipSpeed / (2 * deceleration); //derived from: vf^2 = vi^2 + 2*a*d
  79.    
  80.     if (shipSpeed == 0)
  81.     {
  82.         time2Stop = 0; distance2Stop = 0;
  83.     }
  84.  
  85.     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";
  86.     WriteToTextPanel(textPanelName, outputText, null, null);
  87. }
  88.  
  89. void GetThrusterDirections(IMyTerminalBlock reference)
  90. {
  91.     masterThrustList.Clear();
  92.     forwardThrust.Clear();
  93.     backThrust.Clear();
  94.     upThrust.Clear();
  95.     downThrust.Clear();
  96.     leftThrust.Clear();
  97.     rightThrust.Clear();
  98.  
  99.     List<IMyTerminalBlock> allThrusters = new List<IMyTerminalBlock>();
  100.     GridTerminalSystem.GetBlocksOfType<IMyThrust>(allThrusters);
  101.  
  102.     foreach (IMyThrust thrust in allThrusters)
  103.     {
  104.         if (thrust.CubeGrid != reference.CubeGrid)
  105.         {
  106.             continue;
  107.         }
  108.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Forward)
  109.         {
  110.             forwardThrust.Add(thrust);
  111.         }
  112.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Backward)
  113.         {
  114.             backThrust.Add(thrust);
  115.         }
  116.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Up)
  117.         {
  118.             upThrust.Add(thrust);
  119.         }
  120.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Down)
  121.         {
  122.             downThrust.Add(thrust);
  123.         }
  124.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Left)
  125.         {
  126.             leftThrust.Add(thrust);
  127.         }
  128.         else if (thrust.WorldMatrix.Backward == reference.WorldMatrix.Right)
  129.         {
  130.             rightThrust.Add(thrust);
  131.         }
  132.     }
  133.  
  134.     masterThrustList.Add(forwardThrust);
  135.     masterThrustList.Add(backThrust);
  136.     masterThrustList.Add(upThrust);
  137.     masterThrustList.Add(downThrust);
  138.     masterThrustList.Add(leftThrust);
  139.     masterThrustList.Add(rightThrust);
  140. }
  141.  
  142. double GetThrusterForceSum(List<IMyThrust> thrustList)
  143. {
  144.     double sum = 0;
  145.     foreach (IMyThrust thisThrust in thrustList)
  146.     {
  147.         if (thisThrust.Enabled)
  148.             sum += thisThrust.MaxEffectiveThrust;
  149.     }
  150.     return sum;
  151. }
  152.  
  153. Vector3D VectorProjection(Vector3D a, Vector3D b)
  154. {
  155.     Vector3D projection = a.Dot(b) / b.LengthSquared() * b;
  156.     return projection;
  157. }
  158.  
  159. //Whip's Write to Text Panel Method v2
  160. void WriteToTextPanel(string textPanelName, string textToWrite, float? fontSize, Color? fontColor)
  161. {
  162.     var listScreens = new List<IMyTerminalBlock>();
  163.     GridTerminalSystem.SearchBlocksOfName(textPanelName, listScreens, block => block is IMyTextPanel);
  164.     if (listScreens.Count == 0)
  165.     {
  166.         Echo("Error: No text panel with name tag '" + textPanelName + "' was found");
  167.         return;
  168.     }
  169.     else
  170.     {
  171.         foreach (IMyTextPanel thisScreen in listScreens)
  172.         {
  173.             thisScreen.WritePublicText(textToWrite);
  174.             thisScreen.ShowPublicTextOnScreen();
  175.  
  176.             if (fontSize.HasValue)
  177.                 thisScreen.SetValue("FontSize", fontSize);
  178.  
  179.             if (fontColor.HasValue)
  180.                 thisScreen.SetValue("FontColor", fontColor);
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement