Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************
- ===== Settings that can be Edited =====
- ***************************************************/
- //Custom Name of LCD Panel(s)
- const string lcdName = "LCD Power";
- //Custom Name of Lights
- const string lightsName = "Solar Lights";
- //Bar Graph Starting Character.
- string start = " [";
- //Bar Graph Ending Character.
- string end = "]";
- //Bar Graph Delimiter Character.
- string bar = "I";
- //Bar Graph Empty Space Character.
- string fill = "`";
- // Percent of Battery Storage to Turn on Reactors.
- float BTOn = 10;
- // Percent of Battery Storage to Turn off Reactors.
- float BTOff = 90;
- // Percent of Battery Usage to Turn on Reactors.
- float BPTog = 90;
- readonly string[] runStatus = new[]
- {
- "Program Running [|---]",
- "Program Running [-|--]",
- "Program Running [--|-]",
- "Program Running [---|]",
- "Program Running [--|-]",
- "Program Running [-|--]"
- };
- /***************************************************
- ===== End Editing Settings. =====
- ***************************************************/
- List<IMyTerminalBlock> allSolar = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> allBatteries = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> allReactors = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> allLights = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> allLcds = new List<IMyTerminalBlock>();
- int runAdvancer;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- }
- void Main(string argument)
- {
- GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(allSolar, b => b.CubeGrid.GridSizeEnum == Me.CubeGrid.GridSizeEnum);
- GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(allBatteries, b => b.CubeGrid == Me.CubeGrid);
- GridTerminalSystem.GetBlocksOfType<IMyReactor>(allReactors, b => b.CubeGrid == Me.CubeGrid);
- GridTerminalSystem.SearchBlocksOfName(lightsName, allLights, b => b.CubeGrid.GridSizeEnum == Me.CubeGrid.GridSizeEnum);
- GridTerminalSystem.SearchBlocksOfName(lcdName, allLcds, b => b.CubeGrid == Me.CubeGrid);
- StringBuilder outputSolar = new StringBuilder();
- StringBuilder outputBattery = new StringBuilder();
- StringBuilder outputReactor = new StringBuilder();
- StringBuilder output = new StringBuilder();
- string arg = argument.ToLower();
- float fontSize = 0.99f;
- // Grid Size
- string gridS = Me.CubeGrid.GridSizeEnum.ToString();
- // Solar Stuff
- float solarC = 0; // Solar Current output
- float solarM = 0; // Solar Maximum Output
- float solarIM = 0; // Solar Ideal Maximum Output (based on Grid Size)
- if (gridS == "Large")
- {
- solarIM = allSolar.Count * 120; // Large Grid Solar Maximum
- }
- else if (gridS == "Small")
- {
- solarIM = allSolar.Count * 30; // Small Grid Solar Maximum
- }
- // Battery Stuff
- float battM = 0; // Battery Maximum Stored power
- float battS = 0; // Battery Current Stored power
- float battT = 0; // Battery Storage Percentage
- float battC = 0; // Battery Current Output
- float battI = 0; // Battery Current Input
- float battO = 0; // Battery Maximum Output
- float battP = 0; // Battery Usage Percentage
- float battOT = 0;
- bool mode = false; //Battery Recharge Status
- string bModes = "";
- // Reactor Stuff
- float reactM = 0; // Reactor Maximum Output
- float reactC = 0; // Reactor Current Output
- float reactT = 0; // Reactor Usage Percentage
- float reactMT = 0;
- bool rOnOff = false; // Reactor OnOff Status
- string rOnOffs = "";
- double uranium = 0;
- // Total Stuff
- float totalCap = 0; // Total power the grid can generate.
- float totalM = 0;
- float totalC = 0;
- float totalT = 0;
- /*****************************
- ===== Solar =====
- *****************************/
- if (allSolar.Count > 0)
- {
- foreach (IMySolarPanel solar in allSolar)
- {
- solarM += solar.MaxOutput;
- solarC += solar.CurrentOutput;
- }
- var solarTM = ((solarM * 1000) / solarIM) * 100;
- var solarT = (solarC == 0 || solarM == 0 ? 0 : (solarC / solarM)); // Avoids a NaN if both are at 0
- // Lights based on Solar Maximum.
- if (allLights.Count > 0)
- {
- foreach (IMyLightingBlock lights in allLights)
- {
- if (lights != null)
- {
- if (solarTM <= 10)
- {
- lights.Color = new Color(255, 0, 0);
- lights.BlinkIntervalSeconds = 0;
- }
- else if (solarTM > 10 && solarTM < 90)
- {
- lights.Color = new Color(255, 255, 0);
- lights.BlinkIntervalSeconds = 0;
- }
- else
- {
- lights.Color = new Color(0, 255, 0);
- lights.BlinkIntervalSeconds = 0;
- }
- }
- }
- }
- // Display Solar Information
- outputSolar.Append(
- " Solar Panels: " + allSolar.Count + "\n"
- + " Maximum: " + formatLargeNumber(solarM * 1000000).ToString() + "W of " + formatLargeNumber(solarIM * 1000).ToString() + "W\n"
- + barBuilder((solarM * 1000) / solarIM)
- + " Output: " + formatLargeNumber(solarC * 1000000).ToString() + "W of " + formatLargeNumber(solarM * 1000000).ToString() + "W\n"
- + barBuilder(solarT)
- + "\n"
- );
- }
- /*****************************
- ===== Battery =====
- *****************************/
- if (allBatteries.Count > 0)
- {
- foreach (IMyBatteryBlock batt in allBatteries)
- {
- battM += batt.MaxStoredPower;
- battS += batt.CurrentStoredPower;
- battC += batt.CurrentOutput;
- battI += batt.CurrentInput;
- mode = batt.GetValue<bool>("Recharge");
- //mode = batt.IsCharging;
- }
- battT = battS / battM;
- battO = battM * 4;
- battP = battC / battO;
- foreach (IMyBatteryBlock batt in allBatteries)
- {
- if (allReactors.Count > 0)
- {
- foreach (IMyReactor react in allReactors)
- {
- if (react.Enabled)
- {
- if ((battT * 100 ) < 10 && mode == false)
- {
- batt.ApplyAction("Recharge");
- }
- if ((battT * 100) > 99 && mode == true)
- {
- batt.ApplyAction("Recharge");
- }
- }
- else
- {
- if (mode == true)
- {
- batt.ApplyAction("Recharge");
- }
- }
- }
- }
- if (batt.Enabled)
- {
- battOT = battO;
- }
- else
- {
- battOT = 0;
- }
- }
- if (mode == true)
- {
- bModes = "Charging";
- }
- else if (mode == false)
- {
- bModes = "Discharging";
- }
- // Display Battery Information
- outputBattery.Append(
- " Batteries: " + allBatteries.Count +", " + bModes + ", Input: " + formatLargeNumber(battI * 1000000) + "W\n"
- + " Stored: " + formatLargeNumber(battS * 1000000).ToString() + "Wh of " + formatLargeNumber(battM * 1000000).ToString() + "Wh\n"
- + barBuilder(battT)
- + " Output: " + formatLargeNumber(battC * 1000000).ToString() + "W of " + formatLargeNumber(battO * 1000000).ToString() + "W\n"
- + barBuilder(battP)
- + "\n"
- );
- }
- /*****************************
- ===== Reactor =====
- *****************************/
- if (allReactors.Count > 0)
- {
- foreach (IMyReactor react in allReactors)
- {
- reactM += react.MaxOutput;
- reactC += react.CurrentOutput;
- reactT = reactC / reactM;
- rOnOff = react.GetValue<bool>("OnOff");
- List<MyInventoryItem> reactInventory = new List<MyInventoryItem>();
- react.GetInventory(0).GetItems(reactInventory);
- string reactList = "";
- foreach (MyInventoryItem tempInv in reactInventory)
- {
- reactList = tempInv.Type.ToString();
- if (reactList.Contains("MyObjectBuilder_Ingot/Uranium"))
- {
- //Echo(reactList + tempInv.Amount);
- uranium += (double)tempInv.Amount;
- }
- }
- if (allBatteries.Count != 0)
- {
- foreach (IMyBatteryBlock batt in allBatteries)
- {
- if (batt.Enabled)
- {
- if ((((battT * 100) <= BTOn || (battP * 100) >= BPTog) || mode == true) && uranium > 0)
- {
- react.ApplyAction("OnOff_On");
- }
- else if (((battT * 100) >= BTOff && (battP * 100) < BPTog) || uranium == 0)
- {
- react.ApplyAction("OnOff_Off");
- }
- }
- else
- {
- react.ApplyAction("OnOff_On");
- }
- }
- }
- else
- {
- react.ApplyAction("OnOff_On");
- }
- if (react.Enabled)
- {
- reactMT = reactM;
- }
- else
- {
- reactMT = 0;
- }
- }
- if (rOnOff == true)
- {
- rOnOffs = ", On";
- }
- else if (rOnOff == false)
- {
- rOnOffs = ", Off";
- }
- // Display Reactor Information
- outputReactor.Append(
- " Reactors: " + allReactors.Count + rOnOffs + ", Uranium: " + uranium.ToString("0.00") + "\n"
- + " Output: " + formatLargeNumber(reactC * 1000000).ToString() + "W of " + formatLargeNumber(reactM * 1000000).ToString() + "W\n"
- + barBuilder(reactT)
- + "\n"
- );
- }
- // Total Power
- totalCap = solarM * 1000000 + battOT * 1000000 + reactMT * 1000000;
- totalM = solarM + battOT + reactMT;
- totalC = solarC + battC + reactC;
- totalT = totalC / totalM;
- // Display Total Information
- output.Append(
- " Total Power: " + formatLargeNumber(totalCap).ToString() + "W\n"
- + barBuilder(totalT)
- );
- // Display All Information
- Echo(runStatus[runAdvancer] + "\n"
- + "Solar Panels: " + allSolar.Count + "\n"
- + "Solar Lights: " + allLights.Count + "\n"
- + "Batteries: " + allBatteries.Count + "\n"
- + "Reactors: " + allReactors.Count + "\n\n"
- + gridS + " Grid");
- runAdvancer = (runAdvancer + 1) % runStatus.Length;
- if (allLcds.Count > 0)
- {
- foreach (IMyTextPanel lcd in allLcds)
- {
- if (lcd != null)
- {
- lcd.FontSize = fontSize;
- lcd.WritePublicText(outputSolar.ToString() + outputBattery.ToString() + outputReactor.ToString() + output.ToString());
- lcd.ShowPublicTextOnScreen();
- }
- }
- }
- // Clear Lists
- allSolar.Clear();
- allBatteries.Clear();
- allReactors.Clear();
- allLights.Clear();
- allLcds.Clear();
- }
- public string barBuilder(float num)
- {
- double p = 0.0d;
- int i = 0;
- int l = 0;
- StringBuilder barString = new StringBuilder();
- p = num * 100;
- barString.Append(start);
- for (i = 0; i < (p / 2); i++)
- {
- barString.Append(bar);
- }
- l = 50 - i;
- while (l > 0)
- {
- barString.Append(fill);
- l--;
- }
- barString.Append(end);
- barString.Append(" " + (p / 100).ToString("0.00" + " %") + "\n");
- string barOutput = barString.ToString();
- barString.Clear();
- return barOutput;
- }
- public string formatLargeNumber(double number, bool compress = true, char base_ordinal = ' ')
- {
- if (!compress)
- return number.ToString(
- "#,###,###,###,###,###,###,###,###,###");
- string ordinals = " kMGTPEZY";
- double compressed = number;
- int start = ordinals.IndexOf(base_ordinal);
- var ordinal = (start < 0 ? 0 : start);
- while (compressed >= 1000 && ordinal + 1 < ordinals.Length)
- {
- compressed /= 1000;
- ordinal++;
- }
- string res = Math.Round(compressed, 1, MidpointRounding.AwayFromZero).ToString();
- if (ordinal > 0)
- res += " " + ordinals[ordinal];
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment