Advertisement
Guest User

Untitled

a guest
Jun 12th, 2023
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. // Space Engineers Script: Hydrogen Thruster Fuel Usage with Graph
  2.  
  3. const string cockpitName = "Your Cockpit Name"; // Replace with the name of your cockpit
  4. const double updateInterval = 0.25; // Update interval in seconds
  5. const int graphWidth = 78; // Width of the graph in characters
  6. const int graphHeight = 40; // Height of the graph in characters
  7.  
  8. double timeSinceLastUpdate = 0;
  9. List<double> fuelUsageHistory = new List<double>();
  10.  
  11. void Main(string argument, UpdateType updateType)
  12. {
  13.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  14.     if (updateType == UpdateType.Update1 || updateType == UpdateType.Terminal)
  15.     {
  16.         timeSinceLastUpdate += Runtime.TimeSinceLastRun.TotalSeconds;
  17.  
  18.         if (timeSinceLastUpdate >= updateInterval)
  19.         {
  20.             UpdateFuelUsage();
  21.             timeSinceLastUpdate = 0;
  22.         }
  23.     }
  24. }
  25.  
  26. void UpdateFuelUsage()
  27. {
  28.     var cockpit = GridTerminalSystem.GetBlockWithName(cockpitName) as IMyCockpit;
  29.     if (cockpit == null)
  30.     {
  31.         Echo($"Cockpit '{cockpitName}' not found.");
  32.         return;
  33.     }
  34.  
  35.     double totalFuelUsage = 0;
  36.     var thrusters = new List<IMyThrust>();
  37.     GridTerminalSystem.GetBlocksOfType(thrusters);
  38.  
  39.     var hydrogenThrusters = new List<IMyThrust>();
  40.  
  41.     foreach (var thruster in thrusters)
  42.     {
  43.         if (IsHydrogenThruster(thruster))
  44.         {
  45.             hydrogenThrusters.Add(thruster);
  46.             float currentThrust = thruster.CurrentThrustPercentage * 0.01f;
  47.             float maxConsumption = GetMaxConsumption(thruster);
  48.             double fuelUsage = maxConsumption * currentThrust;
  49.             totalFuelUsage += fuelUsage;
  50.         }
  51.     }
  52.  
  53.     fuelUsageHistory.Add(totalFuelUsage);
  54.     if (fuelUsageHistory.Count > (20 / updateInterval))
  55.     {
  56.         fuelUsageHistory.RemoveAt(0);
  57.     }
  58.  
  59.     string graph = GenerateFuelUsageGraph();
  60.  
  61.     cockpit.GetSurface(1).WriteText(graph);
  62. }
  63.  
  64. bool IsHydrogenThruster(IMyThrust thruster)
  65. {
  66.     var definitionId = thruster.BlockDefinition;
  67.     return definitionId.SubtypeName.Contains("HydrogenThrust");
  68. }
  69.  
  70. float GetMaxConsumption(IMyThrust thruster)
  71. {
  72.     string subtype = thruster.BlockDefinition.SubtypeName;
  73.     float maxConsumption = 0f;
  74.  
  75.     switch (subtype)
  76.     {
  77.         case "SmallBlockSmallHydrogenThrust":
  78.         case "SmallBlockSmallHydrogenThrustIndustrial":
  79.             maxConsumption = 80f;
  80.             break;
  81.         case "SmallBlockLargeHydrogenThrust":
  82.         case "SmallBlockLargeHydrogenThrustIndustrial":
  83.             maxConsumption = 386f;
  84.             break;
  85.         case "LargeBlockSmallHydrogenThrust":
  86.         case "LargeBlockSmallHydrogenThrustIndustrial":
  87.             maxConsumption = 803f;
  88.             break;
  89.         case "LargeBlockLargeHydrogenThrust":
  90.         case "LargeBlockLargeHydrogenThrustIndustrial":
  91.             maxConsumption = 4820f;
  92.             break;
  93.         default:
  94.             break;
  95.     }
  96.  
  97.     return maxConsumption;
  98. }
  99.  
  100. string GenerateFuelUsageGraph()
  101. {
  102.     int maxHistoryCount = (int)(20 / updateInterval);
  103.     int count = fuelUsageHistory.Count;
  104.     double maxValue = GetMaxFuelUsage();
  105.     double valueRange = maxValue;
  106.     double heightRatio = graphHeight / valueRange;
  107.  
  108.     StringBuilder graphBuilder = new StringBuilder();
  109.  
  110.     for (int y = graphHeight - 1; y >= 0; y--)
  111.     {
  112.         for (int x = 0; x < graphWidth; x++)
  113.         {
  114.             int index = count - graphWidth + x;
  115.             if (index >= 0)
  116.             {
  117.                 double value = fuelUsageHistory[index];
  118.                 int height = (int)(value * heightRatio);
  119.                 if (y < height)
  120.                 {
  121.                     graphBuilder.Append("█");
  122.                 }
  123.                 else
  124.                 {
  125.                     graphBuilder.Append(" ");
  126.                 }
  127.             }
  128.             else
  129.             {
  130.                 graphBuilder.Append(" ");
  131.             }
  132.         }
  133.         graphBuilder.AppendLine();
  134.     }
  135.  
  136.     return graphBuilder.ToString();
  137. }
  138.  
  139. double GetMaxFuelUsage()
  140. {
  141.     double maxFuelUsage = 0;
  142.     var thrusters = new List<IMyThrust>();
  143.     GridTerminalSystem.GetBlocksOfType(thrusters);
  144.  
  145.     foreach (var thruster in thrusters)
  146.     {
  147.         if (IsHydrogenThruster(thruster))
  148.         {
  149.             float maxConsumption = GetMaxConsumption(thruster);
  150.             maxFuelUsage += maxConsumption;
  151.         }
  152.     }
  153.  
  154.     return maxFuelUsage;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement