Advertisement
NickNDS

Thrust Calculator

Jul 27th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.91 KB | None | 0 0
  1. public string thrustPanelKeyword = "thrust";
  2.  
  3. public float
  4. largeGridSmallIon = 345600,
  5. largeGridLargeIon = 4320000,
  6. smallGridSmallIon = 14400,
  7. smallGridLargeIon = 172800,
  8. largeGridSmallHydro = 920000,
  9. largeGridLargeHydro = 8200000,
  10. smallGridSmallHydro = 88000,
  11. smallGridLargeHydro = 480000,
  12. largeGridSmallAtmo = 648000,
  13. largeGridLargeAtmo = 6480000,
  14. smallGridSmallAtmo = 96000,
  15. smallGridLargeAtmo = 576000;
  16.  
  17. public IMyShipController controller;
  18.  
  19. public List<string> knownThrusterDefinitions = new List<string>();
  20.  
  21. public List<ThrusterDefinition> thrusterDefinitionList = new List<ThrusterDefinition>();
  22.  
  23. public Program()
  24. {
  25.     List<IMyShipController> blocks = new List<IMyShipController>();
  26.     GridTerminalSystem.GetBlocksOfType<IMyShipController>(blocks, b => b.CubeGrid == Me.CubeGrid);
  27.     if (blocks.Count > 0) controller = blocks[0];
  28.     else throw new Exception("\nNo ship controller found on grid (Remote Controller or Cockpit)");
  29.     CreateDefinitionList();
  30.     FindThrusters();
  31.     Save();
  32. }
  33.  
  34. public void Save() { }
  35.  
  36. public void Main(string argument, UpdateType updateSource)
  37. {
  38.     MyShipMass shipMass = controller.CalculateShipMass();
  39.     float mass = shipMass.PhysicalMass, gravityAcceleration = (float)controller.GetNaturalGravity().Length();
  40.     if (mass == 0f) mass = shipMass.TotalMass;
  41.     if (argument != "") {
  42.         try {
  43.             if (argument.ToLower() == "update") UpdateThrust();
  44.             else if (argument.Contains(" "))
  45.             {
  46.                 mass = float.Parse(argument.Substring(0, argument.IndexOf(" ")));
  47.                 gravityAcceleration = float.Parse(argument.Substring(argument.IndexOf(" ") + 1)) * 9.81f;
  48.             } else mass = float.Parse(argument);
  49.         } catch { }
  50.     }
  51.     float newtonsRequired = (mass * gravityAcceleration);
  52.     StringBuilder builder = new StringBuilder();
  53.     builder.AppendLine("Mass: " + mass);
  54.     builder.AppendLine("Current Gravity: " + gravityAcceleration.ToString() + " : " + (gravityAcceleration / 9.81f));
  55.     Echo("Mass: " + mass);
  56.     Echo("Current Gravity: " + gravityAcceleration.ToString() + " : " + (gravityAcceleration / 9.81f));
  57.     for (int i = 0; i < thrusterDefinitionList.Count; i++)
  58.     {
  59.         string thrusterDefinition = thrusterDefinitionList[i].blockDefinition.Substring(23);
  60.         builder.AppendLine(thrusterDefinition + " : " + (newtonsRequired / thrusterDefinitionList[i].thrustInNewtons));
  61.         Echo(thrusterDefinition + " : " + (newtonsRequired / thrusterDefinitionList[i].thrustInNewtons));
  62.     }
  63.     Output(builder);
  64. }
  65.  
  66. public void Output(StringBuilder builder)
  67. {
  68.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  69.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(blocks, b => b.CustomName.ToLower().Contains(thrustPanelKeyword.ToLower()));
  70.     for (int i = 0; i < blocks.Count; i++)
  71.     {
  72.         ((IMyTextSurface)blocks[i]).ContentType = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE;
  73.         ((IMyTextSurface)blocks[i]).WriteText(builder);
  74.     }
  75. }
  76.  
  77. public void UpdateThrust()
  78. {
  79.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  80.     for (int i = 0; i < thrusterDefinitionList.Count; i++)
  81.     {
  82.         GridTerminalSystem.GetBlocksOfType<IMyThrust>(blocks, b => b.BlockDefinition.ToString() == thrusterDefinitionList[i].blockDefinition);
  83.         if (blocks.Count > 0) thrusterDefinitionList[i].thrustInNewtons = ((IMyThrust)blocks[i]).MaxEffectiveThrust;
  84.     }
  85. }
  86.  
  87. public void SyncRemote()
  88. {
  89.     if (controller == null || !Me.CubeGrid.CubeExists(controller.Position)) {
  90.         controller = null;
  91.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  92.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(blocks, b => b.CubeGrid == Me.CubeGrid);
  93.         if (blocks.Count == 0)
  94.             GridTerminalSystem.GetBlocksOfType<IMyCockpit>(blocks, b => b.CubeGrid == Me.CubeGrid);
  95.         if (blocks.Count > 0)
  96.             controller = (IMyShipController)blocks[0];
  97.     }
  98. }
  99.  
  100. public void FindThrusters()
  101. {
  102.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  103.     GridTerminalSystem.GetBlocksOfType<IMyThrust>(blocks, b => UnknownThrusterDefinition(b.BlockDefinition.ToString()));
  104.     for (int i = 0; i < blocks.Count; i++)
  105.     {
  106.         ThrusterDefinition def = new ThrusterDefinition();
  107.         def.thrustInNewtons = ((IMyThrust)blocks[i]).MaxThrust;
  108.         def.blockDefinition = blocks[i].BlockDefinition.ToString();
  109.         thrusterDefinitionList.Add(def);
  110.     }
  111. }
  112.  
  113. public bool UnknownThrusterDefinition(string def)
  114. {
  115.     if (!knownThrusterDefinitions.Contains(def))
  116.     {
  117.         knownThrusterDefinitions.Add(def);
  118.         return true;
  119.     }
  120.     return false;
  121. }
  122.  
  123. public void CreateDefinitionList()
  124. {
  125.     ThrusterDefinition def = new ThrusterDefinition();
  126.     def.thrustInNewtons = largeGridSmallIon;
  127.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockSmallThrust";
  128.     knownThrusterDefinitions.Add(def.blockDefinition);
  129.     thrusterDefinitionList.Add(def);
  130.  
  131.     def = new ThrusterDefinition();
  132.     def.thrustInNewtons = largeGridLargeIon;
  133.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockLargeThrust";
  134.     knownThrusterDefinitions.Add(def.blockDefinition);
  135.     thrusterDefinitionList.Add(def);
  136.  
  137.     def = new ThrusterDefinition();
  138.     def.thrustInNewtons = smallGridSmallIon;
  139.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockSmallThrust";
  140.     knownThrusterDefinitions.Add(def.blockDefinition);
  141.     thrusterDefinitionList.Add(def);
  142.  
  143.     def = new ThrusterDefinition();
  144.     def.thrustInNewtons = smallGridLargeIon;
  145.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockLargeThrust";
  146.     knownThrusterDefinitions.Add(def.blockDefinition);
  147.     thrusterDefinitionList.Add(def);
  148.  
  149.     def = new ThrusterDefinition();
  150.     def.thrustInNewtons = largeGridSmallHydro;
  151.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockSmallHydrogenThrust";
  152.     knownThrusterDefinitions.Add(def.blockDefinition);
  153.     thrusterDefinitionList.Add(def);
  154.  
  155.     def = new ThrusterDefinition();
  156.     def.thrustInNewtons = largeGridLargeHydro;
  157.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockLargeHydrogenThrust";
  158.     knownThrusterDefinitions.Add(def.blockDefinition);
  159.     thrusterDefinitionList.Add(def);
  160.  
  161.     def = new ThrusterDefinition();
  162.     def.thrustInNewtons = smallGridSmallHydro;
  163.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockSmallHydrogenThrust";
  164.     knownThrusterDefinitions.Add(def.blockDefinition);
  165.     thrusterDefinitionList.Add(def);
  166.  
  167.     def = new ThrusterDefinition();
  168.     def.thrustInNewtons = smallGridLargeHydro;
  169.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockLargeHydrogenThrust";
  170.     knownThrusterDefinitions.Add(def.blockDefinition);
  171.     thrusterDefinitionList.Add(def);
  172.  
  173.     def = new ThrusterDefinition();
  174.     def.thrustInNewtons = largeGridSmallAtmo;
  175.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockSmallAtmosphericThrust";
  176.     knownThrusterDefinitions.Add(def.blockDefinition);
  177.     thrusterDefinitionList.Add(def);
  178.  
  179.     def = new ThrusterDefinition();
  180.     def.thrustInNewtons = largeGridLargeAtmo;
  181.     def.blockDefinition = "MyObjectBuilder_Thrust/LargeBlockLargeAtmosphericThrust";
  182.     knownThrusterDefinitions.Add(def.blockDefinition);
  183.     thrusterDefinitionList.Add(def);
  184.  
  185.     def = new ThrusterDefinition();
  186.     def.thrustInNewtons = smallGridSmallAtmo;
  187.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockSmallAtmosphericThrust";
  188.     knownThrusterDefinitions.Add(def.blockDefinition);
  189.     thrusterDefinitionList.Add(def);
  190.  
  191.     def = new ThrusterDefinition();
  192.     def.thrustInNewtons = smallGridLargeAtmo;
  193.     def.blockDefinition = "MyObjectBuilder_Thrust/SmallBlockLargeAtmosphericThrust";
  194.     knownThrusterDefinitions.Add(def.blockDefinition);
  195.     thrusterDefinitionList.Add(def);
  196. }
  197.  
  198. public class ThrusterDefinition
  199. {
  200.     public float thrustInNewtons = 0f;
  201.     public string blockDefinition = "";
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement