Morphik08

Space Engineers - Power Display

Mar 2nd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.46 KB | None | 0 0
  1. /***************************************************                      
  2. =====    Settings that can be Edited    =====                      
  3. ***************************************************/  
  4.      
  5. //Custom Name of LCD Panel(s)                      
  6. const string lcdName = "LCD Power";      
  7.      
  8. //Custom Name of Lights                      
  9. const string lightsName = "Solar Lights";      
  10.      
  11. //Bar Graph Starting Character.                      
  12. string start = "    [";      
  13.      
  14. //Bar Graph Ending Character.                      
  15. string end = "]";      
  16.      
  17. //Bar Graph Delimiter Character.                      
  18. string bar = "I";      
  19.      
  20. //Bar Graph Empty Space Character.                      
  21. string fill = "`";      
  22.      
  23. // Percent of Battery Storage to Turn on Reactors.          
  24. float BTOn = 10;      
  25.      
  26. // Percent of Battery Storage to Turn off Reactors.          
  27. float BTOff = 90;      
  28.      
  29. // Percent of Battery Usage to Turn on Reactors.          
  30. float BPTog = 90;      
  31.  
  32. readonly string[] runStatus = new[]
  33. {
  34.     "Program Running [|---]",
  35.     "Program Running [-|--]",
  36.     "Program Running [--|-]",
  37.     "Program Running [---|]",
  38.     "Program Running [--|-]",
  39.     "Program Running [-|--]"
  40. };
  41.      
  42. /***************************************************                      
  43. =====         End Editing Settings.         =====                      
  44. ***************************************************/      
  45.      
  46. List<IMyTerminalBlock> allSolar = new List<IMyTerminalBlock>();      
  47. List<IMyTerminalBlock> allBatteries = new List<IMyTerminalBlock>();      
  48. List<IMyTerminalBlock> allReactors = new List<IMyTerminalBlock>();      
  49. List<IMyTerminalBlock> allLights = new List<IMyTerminalBlock>();      
  50. List<IMyTerminalBlock> allLcds = new List<IMyTerminalBlock>();
  51.  
  52. int runAdvancer;
  53.  
  54. public Program()  
  55. {  
  56.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  57. }    
  58.      
  59. void Main(string argument)      
  60. {      
  61.  
  62.     GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(allSolar, b => b.CubeGrid.GridSizeEnum == Me.CubeGrid.GridSizeEnum);      
  63.     GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(allBatteries, b => b.CubeGrid == Me.CubeGrid);      
  64.     GridTerminalSystem.GetBlocksOfType<IMyReactor>(allReactors, b => b.CubeGrid == Me.CubeGrid);      
  65.     GridTerminalSystem.SearchBlocksOfName(lightsName, allLights, b => b.CubeGrid.GridSizeEnum == Me.CubeGrid.GridSizeEnum);      
  66.     GridTerminalSystem.SearchBlocksOfName(lcdName, allLcds, b => b.CubeGrid == Me.CubeGrid);      
  67.      
  68.     StringBuilder outputSolar = new StringBuilder();      
  69.     StringBuilder outputBattery = new StringBuilder();      
  70.     StringBuilder outputReactor = new StringBuilder();      
  71.     StringBuilder output = new StringBuilder();      
  72.      
  73.     string arg = argument.ToLower();      
  74.      
  75.     float fontSize = 0.99f;  
  76.  
  77.     // Grid Size                      
  78.     string gridS = Me.CubeGrid.GridSizeEnum.ToString();      
  79.      
  80.     // Solar Stuff                              
  81.     float solarC = 0; // Solar Current output                                      
  82.     float solarM = 0; // Solar Maximum Output                      
  83.     float solarIM = 0; // Solar Ideal Maximum Output (based on Grid Size)                          
  84.      
  85.     if (gridS == "Large")      
  86.     {      
  87.         solarIM = allSolar.Count * 120; // Large Grid Solar Maximum                        
  88.     }      
  89.     else if (gridS == "Small")      
  90.     {      
  91.         solarIM = allSolar.Count * 30; // Small Grid Solar Maximum                      
  92.     }      
  93.      
  94.     // Battery Stuff                          
  95.     float battM = 0; // Battery Maximum Stored power                          
  96.     float battS = 0; // Battery Current Stored power                                        
  97.     float battT = 0; // Battery Storage Percentage                            
  98.     float battC = 0; // Battery Current Output            
  99.     float battI = 0; // Battery Current Input    
  100.     float battO = 0; // Battery Maximum Output                            
  101.     float battP = 0; // Battery Usage Percentage      
  102.     float battOT = 0;      
  103.     bool mode = false; //Battery Recharge Status        
  104.     string bModes = "";      
  105.      
  106.     // Reactor Stuff                                      
  107.     float reactM = 0; // Reactor Maximum Output                            
  108.     float reactC = 0; // Reactor Current Output                            
  109.     float reactT = 0; // Reactor Usage Percentage      
  110.     float reactMT = 0;      
  111.     bool rOnOff = false; // Reactor OnOff Status              
  112.     string rOnOffs = "";      
  113.     double uranium = 0;    
  114.      
  115.     // Total Stuff                          
  116.     float totalCap = 0; // Total power the grid can generate.        
  117.     float totalM = 0;      
  118.     float totalC = 0;      
  119.     float totalT = 0;      
  120.      
  121.     /*****************************                
  122.     =====      Solar      =====                
  123.     *****************************/      
  124.     if (allSolar.Count > 0)      
  125.     {      
  126.         foreach (IMySolarPanel solar in allSolar)      
  127.         {      
  128.             solarM += solar.MaxOutput;      
  129.             solarC += solar.CurrentOutput;      
  130.         }      
  131.      
  132.         var solarTM = ((solarM * 1000) / solarIM) * 100;      
  133.         var solarT = (solarC == 0 || solarM == 0 ? 0 : (solarC / solarM)); // Avoids a NaN if both are at 0                
  134.      
  135.         // Lights based on Solar Maximum.          
  136.         if (allLights.Count > 0)      
  137.         {      
  138.             foreach (IMyLightingBlock lights in allLights)      
  139.             {      
  140.                 if (lights != null)      
  141.                 {      
  142.                     if (solarTM <= 10)      
  143.                     {      
  144.                         lights.Color = new Color(255, 0, 0);
  145.                         lights.BlinkIntervalSeconds = 0;  
  146.                     }      
  147.                     else if (solarTM > 10 && solarTM < 90)      
  148.                     {      
  149.                         lights.Color = new Color(255, 255, 0);
  150.                         lights.BlinkIntervalSeconds = 0;    
  151.                     }      
  152.                     else      
  153.                     {      
  154.                         lights.Color = new Color(0, 255, 0);
  155.                         lights.BlinkIntervalSeconds = 0;  
  156.                     }      
  157.                 }      
  158.             }      
  159.         }      
  160.      
  161.         // Display Solar Information                
  162.         outputSolar.Append(      
  163.         "    Solar Panels: " + allSolar.Count + "\n"      
  164.         + "    Maximum:  " + formatLargeNumber(solarM * 1000000).ToString() + "W of " + formatLargeNumber(solarIM * 1000).ToString() + "W\n"      
  165.         + barBuilder((solarM * 1000) / solarIM)      
  166.         + "    Output: " + formatLargeNumber(solarC * 1000000).ToString() + "W of " + formatLargeNumber(solarM * 1000000).ToString() + "W\n"      
  167.         + barBuilder(solarT)      
  168.         + "\n"      
  169.         );      
  170.     }      
  171.      
  172.     /*****************************                
  173.     =====     Battery     =====                
  174.     *****************************/      
  175.     if (allBatteries.Count > 0)      
  176.     {      
  177.         foreach (IMyBatteryBlock batt in allBatteries)      
  178.         {      
  179.             battM += batt.MaxStoredPower;      
  180.             battS += batt.CurrentStoredPower;      
  181.             battC += batt.CurrentOutput;      
  182.             battI += batt.CurrentInput;    
  183.             mode = batt.GetValue<bool>("Recharge");    
  184.             //mode = batt.IsCharging;      
  185.         }      
  186.         battT = battS / battM;      
  187.         battO = battM * 4;      
  188.         battP = battC / battO;      
  189.         foreach (IMyBatteryBlock batt in allBatteries)      
  190.         {      
  191.             if (allReactors.Count > 0)
  192.             {
  193.                 foreach (IMyReactor react in allReactors)      
  194.                 {
  195.                     if (react.Enabled)
  196.                     {
  197.                         if ((battT * 100 ) < 10 && mode == false)      
  198.                         {      
  199.                             batt.ApplyAction("Recharge");      
  200.                         }      
  201.                         if ((battT * 100) > 99 && mode == true)      
  202.                         {      
  203.                             batt.ApplyAction("Recharge");      
  204.                         }
  205.                     }
  206.                     else
  207.                     {
  208.                         if (mode == true)
  209.                         {
  210.                             batt.ApplyAction("Recharge");
  211.                         }
  212.                     }
  213.                 }
  214.             }
  215.                  
  216.             if (batt.Enabled)      
  217.             {      
  218.                 battOT = battO;      
  219.             }      
  220.             else      
  221.             {      
  222.                 battOT = 0;      
  223.             }      
  224.         }      
  225.         if (mode ==  true)      
  226.         {      
  227.             bModes = "Charging";      
  228.         }      
  229.         else if (mode == false)      
  230.         {      
  231.             bModes = "Discharging";      
  232.         }      
  233.                  
  234.         // Display Battery Information                
  235.         outputBattery.Append(      
  236.         "    Batteries: " + allBatteries.Count +", " + bModes + ", Input: " + formatLargeNumber(battI * 1000000) + "W\n"      
  237.         + "    Stored: " + formatLargeNumber(battS * 1000000).ToString() + "Wh of " + formatLargeNumber(battM * 1000000).ToString() + "Wh\n"      
  238.         + barBuilder(battT)      
  239.         + "    Output: " + formatLargeNumber(battC * 1000000).ToString() + "W of " + formatLargeNumber(battO * 1000000).ToString() + "W\n"      
  240.         + barBuilder(battP)      
  241.         + "\n"      
  242.         );      
  243.     }      
  244.      
  245.     /*****************************                
  246.     =====    Reactor    =====                
  247.     *****************************/      
  248.     if (allReactors.Count > 0)      
  249.     {      
  250.         foreach (IMyReactor react in allReactors)      
  251.         {      
  252.             reactM += react.MaxOutput;      
  253.             reactC += react.CurrentOutput;      
  254.             reactT = reactC / reactM;      
  255.             rOnOff = react.GetValue<bool>("OnOff");      
  256.                
  257.             List<MyInventoryItem> reactInventory = new List<MyInventoryItem>();
  258.             react.GetInventory(0).GetItems(reactInventory);  
  259.             string reactList = "";
  260.            
  261.             foreach (MyInventoryItem tempInv in reactInventory)
  262.             {
  263.                 reactList = tempInv.Type.ToString();
  264.  
  265.                 if (reactList.Contains("MyObjectBuilder_Ingot/Uranium"))          
  266.                 {      
  267.                     //Echo(reactList + tempInv.Amount);
  268.                     uranium += (double)tempInv.Amount;
  269.                 }
  270.             }    
  271.      
  272.             if (allBatteries.Count != 0)      
  273.             {      
  274.                 foreach (IMyBatteryBlock batt in allBatteries)      
  275.                 {      
  276.                     if (batt.Enabled)      
  277.                     {      
  278.                         if ((((battT * 100) <= BTOn || (battP * 100) >= BPTog) || mode == true) && uranium > 0)      
  279.                         {      
  280.                             react.ApplyAction("OnOff_On");      
  281.                         }      
  282.                         else if (((battT * 100) >= BTOff && (battP * 100) < BPTog) || uranium == 0)      
  283.                         {      
  284.                             react.ApplyAction("OnOff_Off");      
  285.                         }      
  286.                     }      
  287.                     else      
  288.                     {      
  289.                         react.ApplyAction("OnOff_On");      
  290.                     }      
  291.                 }      
  292.             }      
  293.             else      
  294.             {      
  295.                 react.ApplyAction("OnOff_On");      
  296.             }      
  297.      
  298.             if (react.Enabled)      
  299.             {      
  300.                 reactMT = reactM;      
  301.             }      
  302.             else      
  303.             {      
  304.                 reactMT = 0;      
  305.             }      
  306.         }      
  307.      
  308.         if (rOnOff == true)      
  309.         {      
  310.             rOnOffs = ", On";      
  311.         }      
  312.         else if (rOnOff == false)      
  313.         {      
  314.             rOnOffs = ", Off";      
  315.         }      
  316.      
  317.         // Display Reactor Information                
  318.         outputReactor.Append(      
  319.         "    Reactors: " + allReactors.Count + rOnOffs + ", Uranium: " + uranium.ToString("0.00") + "\n"      
  320.         + "    Output: " + formatLargeNumber(reactC * 1000000).ToString() + "W of " + formatLargeNumber(reactM * 1000000).ToString() + "W\n"      
  321.         + barBuilder(reactT)      
  322.         + "\n"      
  323.         );      
  324.     }      
  325.      
  326.     // Total Power                
  327.     totalCap = solarM * 1000000 + battOT * 1000000 + reactMT * 1000000;      
  328.     totalM = solarM + battOT + reactMT;      
  329.     totalC = solarC + battC + reactC;      
  330.     totalT = totalC / totalM;      
  331.      
  332.     // Display Total Information                
  333.     output.Append(      
  334.     "    Total Power: " + formatLargeNumber(totalCap).ToString() + "W\n"      
  335.     + barBuilder(totalT)      
  336.     );      
  337.      
  338.     // Display All Information  
  339.  
  340.     Echo(runStatus[runAdvancer] + "\n"    
  341.     + "Solar Panels: " + allSolar.Count + "\n"      
  342.     + "Solar Lights: " + allLights.Count + "\n"      
  343.     + "Batteries: " + allBatteries.Count + "\n"      
  344.     + "Reactors: " + allReactors.Count + "\n\n"      
  345.     + gridS + " Grid");
  346.     runAdvancer = (runAdvancer + 1) % runStatus.Length;
  347.     if (allLcds.Count > 0)      
  348.     {      
  349.         foreach (IMyTextPanel lcd in allLcds)      
  350.         {      
  351.             if (lcd != null)      
  352.             {      
  353.                 lcd.FontSize = fontSize;      
  354.                 lcd.WritePublicText(outputSolar.ToString() + outputBattery.ToString() + outputReactor.ToString() + output.ToString());      
  355.                 lcd.ShowPublicTextOnScreen();      
  356.             }      
  357.         }      
  358.     }      
  359.      
  360.     // Clear Lists                          
  361.     allSolar.Clear();      
  362.     allBatteries.Clear();      
  363.     allReactors.Clear();      
  364.     allLights.Clear();      
  365.     allLcds.Clear();      
  366. }      
  367.      
  368. public string barBuilder(float num)      
  369. {      
  370.     double p = 0.0d;      
  371.     int i = 0;      
  372.     int l = 0;      
  373.     StringBuilder barString = new StringBuilder();      
  374.      
  375.     p = num * 100;      
  376.     barString.Append(start);      
  377.     for (i = 0; i < (p / 2); i++)      
  378.     {      
  379.         barString.Append(bar);      
  380.     }      
  381.     l = 50 - i;      
  382.     while (l > 0)      
  383.     {      
  384.         barString.Append(fill);      
  385.         l--;      
  386.     }      
  387.     barString.Append(end);      
  388.     barString.Append(" " + (p / 100).ToString("0.00" + " %") + "\n");      
  389.     string barOutput = barString.ToString();      
  390.     barString.Clear();      
  391.     return barOutput;      
  392. }      
  393.      
  394. public string formatLargeNumber(double number, bool compress = true, char base_ordinal = ' ')      
  395. {      
  396.     if (!compress)      
  397.         return number.ToString(      
  398.         "#,###,###,###,###,###,###,###,###,###");      
  399.      
  400.     string ordinals = " kMGTPEZY";      
  401.     double compressed = number;      
  402.      
  403.     int start = ordinals.IndexOf(base_ordinal);      
  404.     var ordinal = (start < 0 ? 0 : start);      
  405.      
  406.     while (compressed >= 1000 && ordinal + 1 < ordinals.Length)      
  407.     {      
  408.         compressed /= 1000;      
  409.         ordinal++;      
  410.     }      
  411.      
  412.     string res = Math.Round(compressed, 1, MidpointRounding.AwayFromZero).ToString();      
  413.      
  414.     if (ordinal > 0)      
  415.         res += " " + ordinals[ordinal];      
  416.      
  417.     return res;      
  418. }
Advertisement
Add Comment
Please, Sign In to add comment