Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region CodeEditor
- /*
- * Full ship/station AI system by Anthropy in February 2015
- * Parts of battery management code originally written by Stiggan and Malakeh in January 2015.
- * There are lots of comments throughout the code to help you along.
- * 100K is a lot of bytes so I'm not afraid to run out of them any time soon.
- * Note however that it's still in heavy development and may radically change with updates.
- */
- //You can change some basic behavior here with these variables.
- //If you want to toggle modules, see the Main() method at the bottom of the file.
- //Due to execution limits, we can not enumerate more than about 120 devices per catagory, or the script will simply be halted.
- const int executionCap = 120;
- const string shipName = "ShipAI Core v1.3-WIP";
- const bool ignoreDevicesThatAreTooMuch = true; //Enforces execution cap
- const bool maintenanceMode = false; //Set this to disable alarms on broken blocks
- const string healingProjectorName = "Projector_healing";
- #region Variables
- //Cached variables, attempting to only fetch everything a single time throughout program life.
- List<IMyAssembler> assemblers = new List<IMyAssembler>();
- List<IMyRefinery> refineries = new List<IMyRefinery>();
- List<IMyTerminalBlock> batteries = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> solarpanels = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> reactors = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> antennas = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> beacons = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> turrets = new List<IMyTerminalBlock>();
- List<IMyTextPanel> statusPanels = new List<IMyTextPanel>();
- List<IMyTextPanel> iconPanels = new List<IMyTextPanel>();
- IMyTextPanel debugPanel;
- IMyTextPanel storagePanel;
- IMyProjector healingProjector;
- Dictionary<string, int> missingBlocks = new Dictionary<string, int>();
- List<IMySoundBlock> speakersBlue = new List<IMySoundBlock>();
- List<IMySoundBlock> speakersOrange = new List<IMySoundBlock>();
- List<IMySoundBlock> speakersRed = new List<IMySoundBlock>();
- List<IMySoundBlock> speakersPowerlow = new List<IMySoundBlock>();
- public double wattHourInBatteries = 0;
- public double maxWattHourInBatteries = 0;
- public double solarpanelWatts = 0;
- public double reactorWatts = 0;
- public double batteryWatts = 0;
- public double batteryInputWatts = 0;
- public double maxPower = 0;
- public double powerUsage = 0;
- public int dischargingBatteries = 0;
- public int brokenBlockCount = 0;
- public int offlineBlockCount = 0;
- public int missingBlockCount = 0;
- int activeAssemblers = 0;
- int activeRefineries = 0;
- int currentInternalIteration; //This is used to trigger things like purging old modules and such, and distribute load across iterations. Should never go higher than 10.
- public bool tooManyBatteries = false;
- public bool tooManyReactors = false;
- public bool tooManySolarpanels = false;
- public bool tooManyAntennas = false;
- public bool tooManyBeacons = false;
- public bool isScreenModulePresent = false;
- public bool isProjectorPresent = false;
- public string alertState = "";
- public string powerState = ""; //This is NOT the message, this is the internal powerstate of the AI!
- public string antennaString = "";
- public string beaconString = "";
- public string StatusString = "";
- public string shortStatus = "";
- public string damageReport = "";
- public string offlineReport = "";
- //public string missingReport; //I implemented everything, only to realize I don't even have the data :v..
- public string powerReport = ""; //This is the message, NOT the internal powerstate!
- public string shortCommsStatus = "";
- //Below here are technically variables, but they mask a var from ai_storage. don't use them more than needed.
- public bool codeBlue
- {
- get { return alertState.Equals("blue"); }
- set
- {
- if (value)
- {
- alertState = "blue";
- }
- else
- {
- alertState = "none";
- }
- }
- }
- public bool codeOrange
- {
- get { return alertState.Equals("orange"); }
- set
- {
- if (value)
- {
- alertState = "orange";
- }
- else
- {
- alertState = "none";
- }
- }
- }
- public bool codeRed
- {
- //the easteregg here has been removed due to potential issues with the screen module template engine.
- get { return getVariable("alertState").Equals("red"); }
- set
- {
- if (value)
- {
- storeVariable("alertState", "red");
- }
- else
- {
- storeVariable("alertState", "none");
- }
- }
- }
- #endregion
- #region variableUpdaters
- void updateSpeakers()
- {
- debugOutput("Updating speakers..");
- List<IMyTerminalBlock> templist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMySoundBlock>(templist);
- speakersRed.Clear();
- speakersOrange.Clear();
- speakersBlue.Clear();
- for (int i = 0; i < templist.Count; i++)
- {
- if (!templist[i].DisplayNameText.EndsWith("_unmanaged"))
- {
- if (templist[i].DisplayNameText.EndsWith("_blue"))
- {
- speakersBlue.Add((IMySoundBlock)templist[i]);
- }
- else if (templist[i].DisplayNameText.EndsWith("_orange"))
- {
- speakersOrange.Add((IMySoundBlock)templist[i]);
- }
- else if (templist[i].DisplayNameText.EndsWith("_red"))
- {
- speakersRed.Add((IMySoundBlock)templist[i]);
- }
- }
- }
- debugOutput(string.Format("Found {0} blue, {1} orange, {2} red speakers.", speakersBlue.Count, speakersOrange.Count, speakersRed.Count));
- }
- /// <summary>
- /// All external modules must check in every 10 iterations to make sure they're still OK
- /// </summary>
- void updateModules()
- {
- var screenModuleMessage = getVariable("screenModuleStatus") ;
- if (screenModuleMessage == "Red one standing by.")
- {
- isScreenModulePresent = screenModuleMessage == "Red one standing by.";
- }
- else
- {
- debugOutput("No screen module found");
- }
- //storeVariable("", "screenModuleStatus"); //Reset the var so the Screen module has to check in again.
- }
- void updateLCDPanels()
- {
- debugPanel = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("debugpanel");
- storagePanel = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("ai_storage");
- if (storagePanel == null)
- {
- debugOutput("ERROR: NO STORAGE FOUND. Throwing exception..");
- debugOutput("Please create an LCD panel named 'ai_storage'.");
- debugOutput("Storage is required for proper functioning of the AI.");
- throw new Exception("No storage found!\nPlease create an LCD panel named 'ai_storage', edit the script and Remember and Exit again.\nCreate a panel named 'debugpanel' for more info.");
- }
- if (debugPanel != null) { debugPanel.WritePublicText(""); }
- debugOutput("Updating LCD Panels..");
- List<IMyTerminalBlock> templist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(templist);
- statusPanels.Clear();
- iconPanels.Clear();
- for (int i = 0; i < templist.Count; i++)
- {
- if (!templist[i].DisplayNameText.EndsWith("_unmanaged"))
- {
- if (templist[i].DisplayNameText.EndsWith("_status"))
- {
- statusPanels.Add((IMyTextPanel)templist[i]);
- }
- else if (templist[i].DisplayNameText.EndsWith("_icons") || templist[i].DisplayNameText.EndsWith("_icon"))
- {
- iconPanels.Add((IMyTextPanel)templist[i]);
- }
- }
- }
- }
- void updateRefineries()
- {
- activeRefineries = 0;
- if (debugPanel != null)
- {
- debugPanel.WritePublicText("Updating refineries..", true);
- }
- refineries = getRefineries();
- for (int i = 0; i < refineries.Count; i++)
- {
- if (!refineries[i].DisplayNameText.EndsWith("_unmanaged"))
- {
- if (refineries[i].IsProducing) { activeRefineries++; }
- }
- }
- if (debugPanel != null)
- {
- debugPanel.WritePublicText(refineries.Count + " found.\n", true);
- }
- }
- void updateAssemblers()
- {
- activeAssemblers = 0;
- if (debugPanel != null)
- {
- debugPanel.WritePublicText("Updating assemblers..", true);
- }
- assemblers = getAssemblers();
- for (int i = 0; i < assemblers.Count; i++)
- {
- if (!assemblers[i].DisplayNameText.EndsWith("_unmanaged"))
- {
- if (assemblers[i].IsProducing) { activeAssemblers++; }
- }
- }
- if (debugPanel != null)
- {
- debugPanel.WritePublicText(assemblers.Count + " found.\n", true);
- }
- }
- void updateBrokenBlocks()
- {
- if (healingProjector == null)
- {
- debugOutput("No projector found, cannot update broken blocks!");
- return;
- }
- missingBlocks = new Dictionary<string, int>();
- missingBlockCount = 0;
- string[] tempLines = ((IMyTerminalBlock)healingProjector).DetailedInfo.Split('\n');
- bool a = false;
- for (int i = 0; i < tempLines.Length; i++)
- {
- if (tempLines[i] == "Blocks remaining:") { a = true; continue; }
- if (a)
- {
- string[] tempParts = tempLines[i].Split(':');
- int parsedCount = 0;
- if (tempParts[1].Length > 0)
- {
- int.TryParse(tempParts[1].Trim(), out parsedCount);
- }
- missingBlocks.Add(tempParts[0], parsedCount);
- missingBlockCount += parsedCount;
- }
- }
- }
- void updateMiscWithBlacklist()
- {
- debugOutput("Updating Misc items..");
- if (alertState == null)
- {
- storeVariable("none", "alertState");
- debugOutput("alertstate persistent variable created");
- }
- if (powerState == null)
- {
- storeVariable("none", "powerState");
- debugOutput("powerstate persistent variable created");
- }
- healingProjector = (IMyProjector)GridTerminalSystem.GetBlockWithName(healingProjectorName);
- if (healingProjector == null)
- {
- debugOutput("No projector found! " + healingProjectorName);
- }
- else
- {
- debugOutput("Found projector: " + ((IMyTerminalBlock)healingProjector).DisplayNameText);
- isProjectorPresent = true;
- }
- batteries.Clear();
- solarpanels.Clear();
- reactors.Clear();
- beacons.Clear();
- antennas.Clear();
- List<IMyTerminalBlock> newlist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);
- for (int i = 0; i < batteries.Count; i++)
- { if (!batteries[i].DisplayNameText.EndsWith("_unmanaged"))
- { newlist.Add(batteries[i]); } if (i > executionCap) { tooManyBatteries = true; if (ignoreDevicesThatAreTooMuch) { debugOutput("Ignoring batteries out of processing range."); break; } }
- } debugOutput(newlist.Count + " batteries found.");
- batteries.Clear(); batteries = newlist; newlist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(solarpanels);
- for (int i = 0; i < solarpanels.Count; i++)
- { if (!solarpanels[i].DisplayNameText.EndsWith("_unmanaged"))
- { newlist.Add(solarpanels[i]); if (i > executionCap) { tooManySolarpanels = true; if (ignoreDevicesThatAreTooMuch) { debugOutput("Ignoring solarpanels out of processing range."); break; } } }
- }
- solarpanels.Clear(); solarpanels = newlist; newlist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyReactor>(reactors);
- for (int i = 0; i < reactors.Count; i++)
- { if (!reactors[i].DisplayNameText.EndsWith("_unmanaged"))
- { newlist.Add(reactors[i]); if (i > executionCap) { tooManyReactors = true; if (ignoreDevicesThatAreTooMuch) { debugOutput("Ignoring reactors out of processing range."); break; } } }
- }
- reactors.Clear(); reactors = newlist; newlist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyBeacon>(beacons);
- for (int i = 0; i < beacons.Count; i++)
- { if (!beacons[i].DisplayNameText.EndsWith("_unmanaged"))
- { newlist.Add(beacons[i]); if (i > executionCap) { tooManyBeacons = true; if (ignoreDevicesThatAreTooMuch) { debugOutput("Ignoring beacons out of processing range."); break; } } }
- }
- beacons.Clear(); beacons = newlist; newlist = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(antennas);
- for (int i = 0; i < antennas.Count; i++)
- { if (!antennas[i].DisplayNameText.EndsWith("_unmanaged"))
- { newlist.Add(antennas[i]); if (i > executionCap) { tooManyAntennas = true; if (ignoreDevicesThatAreTooMuch) { debugOutput("Ignoring beacons out of processing range."); break; } } }
- }
- antennas.Clear(); antennas = newlist; newlist = new List<IMyTerminalBlock>();
- wattHourInBatteries = getWattHourInBatteries();
- maxWattHourInBatteries = getMaxWattHourInBatteries();
- solarpanelWatts = solarPower(true);
- reactorWatts = reactorPower(true);
- batteryWatts = batteryPower();
- batteryInputWatts = batteryPowerInput();
- maxPower = reactorWatts + solarpanelWatts;
- powerUsage = reactorPower(false) + solarPower(false) + batteryWatts;
- dischargingBatteries = getDischargingBatteryCount();
- debugOutput("Done.");
- }
- void updateScreenModuleValues()
- {
- //Layout below should be EXACTLY that of the above Variables region!
- //This is to make sure discrependancies are easily spotted.
- debugOutput("Writing missing blockcounts");
- storeVariable(serializeDictionary(missingBlocks), "missingBlocks");
- debugOutput("Writing power values");
- storeVariable(wattHourInBatteries.ToString(), "wattHourInBatteries");
- storeVariable(maxWattHourInBatteries.ToString(), "maxWattHourInBatteries");
- storeVariable(solarpanelWatts.ToString(), "solarpanelWatts");
- storeVariable(solarPower(true).ToString(), "solarpanelMaxWatts");
- storeVariable(reactorWatts.ToString(), "reactorWatts");
- storeVariable(reactorPower(true).ToString(), "reactorMaxWatts");
- storeVariable(batteryWatts.ToString(), "batteryWatts");
- storeVariable(batteryPower(true).ToString(), "batteryMaxWatts");
- storeVariable(batteryInputWatts.ToString(), "batteryInputWatts");
- storeVariable(batteryPower(true).ToString(), "batteryInputMaxWatts");
- storeVariable(maxPower.ToString(), "maxPower");
- storeVariable(powerUsage.ToString(), "powerUsage");
- debugOutput("Writing block counts");
- storeVariable(reactors.Count.ToString(), "reactorCount");
- storeVariable(solarpanels.Count.ToString(), "solarpanelCount");
- storeVariable(batteries.Count.ToString(), "batteryCount");
- storeVariable(refineries.Count.ToString(), "refineryCount");
- storeVariable(assemblers.Count.ToString(), "assemblerCount");
- storeVariable(antennas.Count.ToString(), "antennaCount");
- storeVariable(beacons.Count.ToString(), "beaconCount");
- debugOutput("Writing meta blockcounts");
- storeVariable(dischargingBatteries.ToString(), "dischargingBatteries");
- storeVariable(wattHourInBatteries.ToString(), "brokenBlockCount");
- storeVariable(offlineBlockCount.ToString(), "offlineBlockCount");
- storeVariable(missingBlockCount.ToString(), "missingBlockCount");
- storeVariable(activeAssemblers.ToString(), "activeAssemblers");
- storeVariable(activeRefineries.ToString(), "activeRefineries");
- storeVariable(currentInternalIteration.ToString(), "currentInternalIteration");
- debugOutput("Writing booleans");
- storeVariable(tooManyBatteries.ToString(), "tooManyBatteries");
- storeVariable(tooManyReactors.ToString(), "tooManyReactors");
- storeVariable(tooManySolarpanels.ToString(), "tooManySolarpanels");
- storeVariable(tooManyAntennas.ToString(), "tooManyAntennas");
- storeVariable(tooManyBeacons.ToString(), "tooManyBeacons");
- debugOutput("Writing strings");
- storeVariable(shipName, "shipName");
- storeVariable(alertState, "alertState");
- storeVariable(powerState, "powerState");
- storeVariable(antennaString, "antennaString");
- storeVariable(beaconString, "beaconString");
- storeVariable(StatusString, "StatusString");
- storeVariable(shortStatus, "shortStatus");
- storeVariable(damageReport, "damageReport");
- storeVariable(offlineReport, "offlineReport");
- //storeVariable(missingReport, "missingReport");
- storeVariable(powerReport, "powerReport");
- storeVariable(shortCommsStatus, "shortCommsStatus");
- debugOutput("Done writing values to disk");
- }
- #endregion
- #region internalFunctions
- /// <summary>
- /// Original function written by Stiggan/Malekeh, took from the Battery management script.
- /// Splits down the DetailedInfo field of a block and retrieves the value corresponding to the given name of a field.
- /// </summary>
- /// <param name="block"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public string getDetailedInfoValue(IMyTerminalBlock block, string name)
- {
- string value = "";
- string[] lines = block.DetailedInfo.Split('\n');
- for (int i = 0; i < lines.Length; i++)
- {
- string[] line = lines[i].Split(':');
- if (line[0].Equals(name))
- {
- value = line[1].Trim();
- break;
- }
- }
- return value;
- }
- public string generateReportFromDictionary(Dictionary<string, int> dict)
- {
- string toReturn = "";
- string[] keys = new string[dict.Keys.Count];
- int[] values = new int[dict.Values.Count];
- dict.Keys.CopyTo(keys, 0);
- dict.Values.CopyTo(values, 0);
- for (int i = 0; i < dict.Count; i++)
- {
- toReturn += string.Format("\n >Missing {0}: {1}", keys[i], values[i]);
- }
- return toReturn;
- }
- public bool detailExist(IMyTerminalBlock block, string name)
- {
- return !String.IsNullOrEmpty(getDetailedInfoValue(block, name));
- }
- /// <summary>
- /// Internal function to convert a string like "2.00 MWh" or "536 W" to a normalized double value in watts.
- /// </summary>
- /// <param name="text"></param>
- /// <param name="hour">Whether or not you're retrieving a battery value, e.g 203.12 KWh. </param>
- /// <returns>A normalized double value in watt(hour)s</returns>
- public double getPowerAsDouble(string text, bool hour = false)
- {
- string h = "";
- if (hour) { h = "h"; }
- double totalPowerWatts = 0;
- if (String.IsNullOrWhiteSpace(text))
- {
- return 0;
- }
- var splittedStoredPower = text.ToLower().Split(' ');
- if (splittedStoredPower[1] == "mw" + h)
- {
- totalPowerWatts += double.Parse(splittedStoredPower[0]) * 1000000;
- }
- else if (splittedStoredPower[1] == "gw" + h)
- {
- totalPowerWatts += double.Parse(splittedStoredPower[0]) * 1000000000;
- }
- else if (splittedStoredPower[1] == "kw" + h)
- {
- totalPowerWatts += double.Parse(splittedStoredPower[0]) * 1000;
- }
- else if (splittedStoredPower[1] == "w" + h)
- {
- totalPowerWatts += double.Parse(splittedStoredPower[0]);
- }
- return totalPowerWatts;
- }
- public bool isHealthy(IMyTerminalBlock block)
- {
- return (block.IsFunctional && block.IsWorking);
- }
- /// <summary>
- /// Gets the current power drawn from the batteries.
- /// </summary>
- /// <returns></returns>
- public double batteryPower(bool max = false)
- {
- double output = 0;
- for (int i = 0; i < batteries.Count; i++)
- {
- if (isHealthy(batteries[i]) && ((IMyFunctionalBlock)batteries[i]).Enabled && !max)
- {
- output += getPowerAsDouble(getDetailedInfoValue(batteries[i], "Current Output"));
- output -= getPowerAsDouble(getDetailedInfoValue(batteries[i], "Current Input"));
- }
- else if (max){
- output += getPowerAsDouble(getDetailedInfoValue(batteries[i], "Max Output"));
- }
- }
- return output;
- }
- public double batteryPowerInput(bool max = false)
- {
- double output = 0;
- if (!max)
- {
- for (int i = 0; i < batteries.Count; i++)
- {
- output += getPowerAsDouble(getDetailedInfoValue(batteries[i], "Current Input"));
- }
- }
- else
- {
- for (int i = 0; i < batteries.Count; i++)
- {
- output += getPowerAsDouble(getDetailedInfoValue(batteries[i], "Max Required Input"));
- }
- }
- return output;
- }
- public bool healthyReactor(IMyTerminalBlock b)
- {
- // Abusing unfinished reactors not displaying Current Output field.
- return isHealthy(b) && detailExist(b, "Current Output");
- }
- /// <summary>
- /// Prints a line to the debugpanel, if there is one (doesn't throw exceptions)
- /// </summary>
- /// <param name="output"></param>
- void debugOutput(string output)
- {
- if (debugPanel != null)
- {
- debugPanel.WritePublicText(output + "\n", true);
- if (debugPanel.GetPublicText().Length > 1000)
- {
- debugPanel.WritePublicText("WARNING: Debug log truncated due to size!\n");
- }
- }
- }
- public double getPower(IMyTerminalBlock block, bool max)
- {
- if (max)
- {
- if (!block.IsBeingHacked)
- {
- return getPowerAsDouble(getDetailedInfoValue(block, "Max Output"));
- }
- }
- else
- {
- return getPowerAsDouble(getDetailedInfoValue(block, "Current Output"));
- }
- return 0;
- }
- public int getDischargingBatteryCount()
- {
- int dischargingBats = 0;
- for (int i = 0; i < batteries.Count; i++)
- {
- var bat = (IMyBatteryBlock)batteries[i];
- if (!isRecharging(bat))
- {
- dischargingBats++;
- }
- }
- return dischargingBats;
- }
- public double reactorPower(bool max)
- {
- double power = 0;
- for (int i = 0; i < reactors.Count; i++)
- {
- if (healthyReactor(reactors[i]) && ((IMyFunctionalBlock)reactors[i]).Enabled)
- {
- power += getPower(reactors[i], max);
- }
- }
- return power;
- }
- public double solarPower(bool max)
- {
- double power = 0;
- for (int i = 0; i < solarpanels.Count; i++)
- {
- if (isHealthy(solarpanels[i]))
- {
- power += getPower(solarpanels[i], max);
- }
- }
- return power;
- }
- public bool isRecharging(IMyTerminalBlock block)
- {
- return detailExist(block, "Fully recharged in");
- }
- public void chargeAllBatteries()
- {
- debugOutput("Setting all but one battery to charge.");
- dischargingBatteries = 0;
- for (int i = 0; i < batteries.Count; i++)
- {
- if (dischargingBatteries > 0) //Make sure there's at least one battery always on to prevent blackouts.
- {
- batteries[i].GetActionWithName("Recharge").Apply(batteries[i]);
- }
- else { dischargingBatteries++; }
- }
- }
- public void toggleRandomBattery()
- {
- Random rnd = new Random();
- int i = rnd.Next(0, batteries.Count);
- if (i < batteries.Count)
- {
- if (!isRecharging(batteries[i]) && dischargingBatteries < 2) //If we're requested to toggle the only currently online battery
- {
- if (batteries.Count < 2) //And there's no other batteries available
- {
- debugOutput("There are no secondary batteries available. This means there's no protection against blackouts. Toggling anyway.");
- batteries[i].GetActionWithName("Recharge").Apply(batteries[i]);
- }
- else
- {
- //First enable a different battery to make sure there are no blackouts.
- int tryingBat = 0;
- bool success = false;
- int retries = 0;
- while (!success)
- {
- tryingBat = rnd.Next(0, batteries.Count);
- if (isRecharging(batteries[tryingBat]) && tryingBat != i)
- {
- debugOutput("Turning on secondary battery to prevent blackouts");
- batteries[tryingBat].GetActionWithName("Recharge").Apply(batteries[tryingBat]);
- success = true;
- //We can't check if this has worked within the same cycle, the status updates too slow.
- //So we can't do anything else here.
- }
- retries++;
- if (retries > 10) { debugOutput("FIXME: Loop detected"); break; } //Against infinity and beyond..
- }
- }
- }
- else
- {
- debugOutput(string.Format("Toggling random {0}", batteries[i].DisplayNameText));
- batteries[i].GetActionWithName("Recharge").Apply(batteries[i]);
- }
- }
- }
- /// <summary>
- /// Get normalized power string, in watts, kilowatts, megawatts, or gigawatts.
- /// </summary>
- /// <param name="watts">Raw value in watts</param>
- /// <param name="hour">Whether or not to add an h to the end of the string</param>
- /// <returns></returns>
- public string getNormalizedPowerString(double watts, bool hour)
- {
- string h = "";
- if(hour){ h = "h";}
- if (watts > 1000000000)
- {
- return string.Format("{0}Gw{1}", watts / 1000000000, h);
- }
- else if (watts > 1000000)
- {
- return string.Format("{0}Mw{1}", watts / 1000000, h);
- }
- else if (watts > 1000)
- {
- return string.Format("{0}Kw{1}", watts / 1000, h);
- }
- else
- {
- return string.Format("{0}w{1}", watts, h);
- }
- }
- string getDamageReport(bool listOfflineBlocks)
- {
- StringBuilder report = new StringBuilder();
- brokenBlockCount = 0;
- offlineBlockCount = 0;
- for (int i = 0; i < GridTerminalSystem.Blocks.Count; i++)
- {
- if (!GridTerminalSystem.Blocks[i].IsFunctional)
- {
- report.AppendFormat("\n >{0} is not functional.", GridTerminalSystem.Blocks[i].DisplayNameText);
- brokenBlockCount++;
- codeBlue = true; //Alert for damaged blocks
- debugOutput("set codeOrange to " + codeOrange.ToString());
- }
- if (!GridTerminalSystem.Blocks[i].IsWorking && GridTerminalSystem.Blocks[i].IsFunctional)
- {
- if (listOfflineBlocks)
- {
- report.AppendFormat("\n -{0} is offline.", GridTerminalSystem.Blocks[i].DisplayNameText);
- }
- offlineBlockCount++;
- }
- }
- if (brokenBlockCount < 1 && codeBlue)
- {
- codeBlue = false; //Turn off alarm again
- }
- return report.ToString();
- }
- /// <summary>
- /// As proud as I am of this function I really hope it becomes legacy fast, KeenSWH. :L
- /// </summary>
- /// <returns>Amount of KWh in all the batteries</returns>
- public double getWattHourInBatteries()
- {
- var screen = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("debugpanel");
- if (batteries.Count < 1) { return 0; }
- //Note: Function does not support ranges in terawatts. Add support if necessary.
- double totalPowerWatts = 0;
- for (int y = 0; y < batteries.Count; y++)
- {
- var storedPower = getDetailedInfoValue(batteries[y], "Stored power");
- //screen.WritePublicText(storedPower.Replace("kWh", "").Trim(), false); //Debugging line.
- totalPowerWatts += getPowerAsDouble(storedPower, true);
- }
- return totalPowerWatts;
- }
- /// <summary>
- /// Gets the maximum amount of energy stored in all batteries (i.e full capacity)
- /// </summary>
- /// <returns></returns>
- public double getMaxWattHourInBatteries()
- {
- var screen = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("debugpanel");
- if (batteries.Count < 1) { return 0; }
- double totalPowerWatts = 0;
- for (int y = 0; y < batteries.Count; y++)
- {
- var storedPower = getDetailedInfoValue(batteries[y], "Max Stored Power");
- //screen.WritePublicText(storedPower.Replace("kWh", "").Trim(), false); //Debugging line.
- totalPowerWatts += getPowerAsDouble(storedPower, true);
- }
- return totalPowerWatts;
- }
- public List<IMyRefinery> getRefineries()
- {
- List<IMyTerminalBlock> a = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyRefinery>(a);
- List<IMyRefinery> refineries = new List<IMyRefinery>();
- for (int i = 0; i < a.Count; i++)
- {
- refineries.Add((IMyRefinery)a[i]);
- }
- return refineries;
- }
- public List<IMyAssembler> getAssemblers()
- {
- List<IMyTerminalBlock> a = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyAssembler>(a);
- List<IMyAssembler> assemblers = new List<IMyAssembler>();
- for (int i = 0; i < a.Count; i++)
- {
- assemblers.Add((IMyAssembler)a[i]);
- }
- return assemblers;
- }
- public string serializeDictionary(Dictionary<string, int> dict)
- {
- string output = "";
- string[] keys = new string[dict.Keys.Count];
- int[] values = new int[dict.Values.Count];
- dict.Keys.CopyTo(keys, 0);
- dict.Values.CopyTo(values, 0);
- for (int i = 0; i < dict.Count; i++)
- {
- output += "\n" + keys[i] + ":" + values[i].ToString();
- }
- return output;
- }
- public Dictionary<string, int> deserializeDictionary(string input)
- {
- Dictionary<string, int> returndict = new Dictionary<string, int>();
- string[] inputLines = input.Split('\n');
- for (int i = 0; i < inputLines.Length - 1; i++)
- {
- if (inputLines[i].Length < 1) { continue; }
- string[] inputLineParts = inputLines[i].Split(':');
- if (inputLineParts.Length > 0)
- {
- returndict.Add(inputLineParts[0], int.Parse(inputLineParts[1]));
- }
- }
- return returndict;
- }
- //Stores a variable using a LCD panel named 'ai_storage'.
- //Colons and newlines can now be used, as I'm substituting them internally.
- void storeVariable(string variable, string name)
- {
- variable = variable.Replace(":", "[COLON]").Replace("\n", "[NEWLINE]");
- List<string> c = new List<string>();
- string b = storagePanel.GetPublicText();
- string oldvar;
- if (b.Contains(name + ":"))
- {
- oldvar = getVariable(name).Replace(":", "[COLON]").Replace("\n", "[NEWLINE]");
- b = b.Replace(name + ":" + oldvar, name + ":" + variable);
- }
- else
- {
- b += "\n"+ name + ":" + variable;
- }
- storagePanel.WritePublicText(b);
- //storeVariable is called too much to always log on AI core.
- //debugOutput(string.Format("storeVariable(): {0} '{1}'", name, variable));
- }
- //Retrieves a variables from the ai_storage LCD panel.
- string getVariable(string name)
- {
- List<string> b = new List<string>();
- b.AddRange(storagePanel.GetPublicText().Split('\n'));
- string toReturn = null;
- for (int i = 0; i < b.Count; i++)
- {
- if (b[i].StartsWith(name))
- {
- toReturn = b[i].Split(':')[1].Replace("[NEWLINE]", "\n").Replace("[COLON]", ":");
- break;
- }
- }
- if (toReturn == null)
- {
- debugOutput(string.Format("No variable '{0}' found.", name));
- }
- else
- {
- //This is too heavily used to always debug. Turn on if necessary.
- //debugOutput(string.Format("getVariable(): {0} '{1}'", name, toReturn));
- }
- return toReturn;
- }
- #endregion
- #region AI_Modules
- //Below here are the modules of the script you can enable in the Main() void.
- void soundAlerts()
- {
- if (codeBlue) {
- for (int i = 0; i < speakersBlue.Count; i++)
- {
- debugOutput("Playing sound on " + speakersBlue[i].DefinitionDisplayNameText);
- speakersBlue[i].ApplyAction("PlaySound");
- if (!speakersBlue[i].IsSoundSelected)
- {
- debugOutput("No sound selected on " + speakersBlue[i].DefinitionDisplayNameText);
- }
- }
- }
- else if (codeOrange) {
- for (int i = 0; i < speakersOrange.Count; i++)
- {
- debugOutput("Playing sound on " + speakersOrange[i].DefinitionDisplayNameText);
- speakersOrange[i].ApplyAction("PlaySound");
- if (!speakersOrange[i].IsSoundSelected)
- {
- debugOutput("No sound selected on " + speakersOrange[i].DefinitionDisplayNameText);
- }
- }
- }
- else if (codeRed) {
- for (int i = 0; i < speakersRed.Count; i++)
- {
- debugOutput("Playing sound on " + speakersRed[i].DefinitionDisplayNameText);
- speakersRed[i].ApplyAction("PlaySound");
- if (!speakersRed[i].IsSoundSelected)
- {
- debugOutput("No sound selected on " + speakersRed[i].DefinitionDisplayNameText);
- }
- }
- }
- }
- //This is needed both for the core as well as the screen module.
- void updateStatusString(bool doDamageReport = true)
- {
- debugOutput("Updating information messages..");
- StatusString = ""; //used by status panels
- shortStatus = "All systems functional."; //Override if necessary
- damageReport = "";
- shortCommsStatus = " OFFLINE"; //Overridden if there's antennas found.
- if (doDamageReport)
- {
- damageReport = getDamageReport(false);
- }
- if (offlineBlockCount > 0)
- {
- shortStatus = string.Format("All systems functional. {0} systems offline.", offlineBlockCount);
- }
- if (brokenBlockCount > 0)
- {
- shortStatus = string.Format("Warning: {0} systems unavailable.", brokenBlockCount);
- }
- if (isProjectorPresent)
- {
- if (missingBlockCount > 0)
- {
- shortStatus = string.Format("Warning: {0} missing blocks detected.", missingBlockCount);
- }
- if (missingBlockCount > 0 && brokenBlockCount > 0)
- {
- shortStatus = "Warning: Ship has sustained damage to various systems.";
- }
- if (missingBlockCount > 10 && brokenBlockCount > 1)
- {
- shortStatus = "Error: Ship has sustained extensive damage to various systems. Exit strategy advised.";
- }
- debugOutput("Adding missing block report");
- StatusString += generateReportFromDictionary(missingBlocks);
- }
- else
- {
- StatusString += "\n -No projector detected. Missing block detection offline.";
- }
- //Update stuff for the Screens module
- if (isScreenModulePresent)
- {
- offlineReport = getDamageReport(true);
- }
- // Generate Status messages
- if (refineries.Count > 0)
- {
- for (int i = 0; i < refineries.Count; i++)
- {
- if (refineries[i].IsProducing)
- { StatusString += string.Format("\n -{0} is processing ore.", refineries[i].DisplayNameText); }
- }
- }
- if (assemblers.Count > 0)
- {
- for (int i = 0; i < assemblers.Count; i++)
- {
- if (assemblers[i].IsProducing)
- { StatusString += string.Format("\n -{0} is assembling parts.", assemblers[i].DisplayNameText); }
- }
- }
- if (maxPower < powerUsage) { StatusString += "\n -Warning: Draining batteries."; }
- if ((maxPower > powerUsage) && (wattHourInBatteries < maxWattHourInBatteries)) { StatusString += "\n -Batteries charging."; }
- if (wattHourInBatteries == maxWattHourInBatteries) { StatusString += "\n -Batteries fully charged."; }
- else if (wattHourInBatteries > (maxWattHourInBatteries) / 4) { StatusString += "\n -Batteries OK."; }
- else if (wattHourInBatteries < (maxWattHourInBatteries / 4)) { StatusString += "\n -Batteries low."; shortStatus = "Low power."; }
- bool tooManyMessage = false;
- if (tooManyAntennas) { StatusString += "\n >Too many antennas to handle."; tooManyMessage = true; }
- if (tooManyBatteries) { StatusString += "\n >Too many batteries to handle."; tooManyMessage = true; }
- if (tooManyBeacons) { StatusString += "\n >Too many beacons to handle."; tooManyMessage = true; }
- if (tooManyReactors) { StatusString += "\n >Too many reactors to handle."; tooManyMessage = true; }
- if (tooManySolarpanels) { StatusString += "\n >Too many solar panels to handle."; tooManyMessage = true; }
- if (tooManyMessage) { StatusString += "\n >Remove devices or alter execution cap at own risk. (see top of script)"; }
- if (!ignoreDevicesThatAreTooMuch) { StatusString += "\n >Execution cap disabled."; }
- if (batteryInputWatts > powerUsage)
- {
- StatusString += "\n >Unknown power source detected."; //What the hell are we charging the batteries with?
- }
- // Generate Power Overview
- powerReport = string.Format(
- " {0}/{1} currently used.",
- new string[] {
- getNormalizedPowerString(powerUsage, false) ,
- getNormalizedPowerString(maxPower, false)
- }); ;
- if (batteries.Count > 0)
- {
- powerReport += string.Format(
- "\n *Batteries: {0}/{1} in {2} batteries ({3} in use)",
- new string[] {
- getNormalizedPowerString(wattHourInBatteries,true),
- getNormalizedPowerString(maxWattHourInBatteries,true),
- batteries.Count.ToString(),
- getDischargingBatteryCount().ToString()
- });
- }
- if (solarpanels.Count > 0)
- {
- powerReport += string.Format(
- "\n *Solar: {2} panels | {0}/{1}",
- new string[] {
- getNormalizedPowerString(solarPower(false),false),
- getNormalizedPowerString(solarpanelWatts,false),
- solarpanels.Count.ToString()
- });
- }
- if (reactors.Count > 0)
- {
- powerReport += string.Format(
- "\n *Reactors: {2} reactors | {0}/{1}",
- new string[] {
- getNormalizedPowerString(reactorPower(false),false),
- getNormalizedPowerString(reactorWatts,false),
- reactors.Count.ToString()
- });
- }
- debugOutput("status: " + StatusString);
- //Check comms status
- antennaString = "\n *No antennas found.";
- beaconString = "\n *No beacons found.";
- string tempshortCommsStatus = " OFFLINE.";
- float tempmaxAntennaRange = 0;
- if (antennas.Count > 0)
- {
- string antString = "";
- int workingAnts = 0;
- for (int i = 0; i < antennas.Count; i++)
- {
- var ant = (IMyRadioAntenna)antennas[i];
- antString += "\n *" + ant.DisplayNameText + " (" + ant.Radius + "m)";
- if (!ant.IsFunctional) { antString += " - Damaged"; }
- if (!ant.IsWorking) { antString += " - Offline"; }
- if (isHealthy(ant))
- {
- workingAnts++;
- if (ant.Radius > tempmaxAntennaRange) { tempmaxAntennaRange = ant.Radius; }
- tempshortCommsStatus = string.Format(" Comms Online ({0}m range, {1}/{2} antennas in use)", tempmaxAntennaRange, workingAnts, antennas.Count);
- debugOutput("Healthy antenna found. " + ant.DisplayNameText);
- }
- }
- antennaString = string.Format(
- "{1}",
- new string[] {
- antennas.Count.ToString(),
- antString
- });
- shortCommsStatus = tempshortCommsStatus;
- }
- if (shortCommsStatus.Contains("OFFLINE"))
- {
- StatusString += "\n >Communications offline.";
- shortStatus = "Core systems functional. Comms offline.";
- }
- if (beacons.Count > 0)
- {
- string beacString = "";
- for (int i = 0; i < beacons.Count; i++)
- {
- var beac = (IMyBeacon)beacons[i];
- beacString += "\n *" + beac.DisplayNameText + " (Beacon, " + beac.Radius + "m range)";
- if (!beac.IsFunctional) { beacString += " - Damaged"; }
- if (!beac.IsWorking) { beacString += " - Offline"; }
- }
- beaconString = string.Format(
- "{1}",
- new string[] {
- beacons.Count.ToString(),
- beacString
- });
- }
- debugOutput("Done.");
- }
- /// <summary>
- /// Updates all LCD panels with _Status suffix with useful information and messages.
- /// This is by far the heaviest function, but turning it off will also cripple the AI's functionality heavily.
- /// </summary>
- void updateStatusPanels()
- {
- //Fetch and prepare variables for use
- //Format information for displaying
- StringBuilder updateScreenBuilder = new StringBuilder();
- updateScreenBuilder.AppendFormat( //Status screen parts
- "{0} - {1}\nStatus: {2}{3}{4}",
- shipName,
- DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString(),
- shortStatus,
- damageReport,
- StatusString
- );
- //Show Assembler information if there are assemblers
- if (assemblers.Count > 0)
- {
- updateScreenBuilder.AppendFormat(
- "\nAssembly: {0}",
- string.Format(
- "{0} assemblers, {1} active.",
- new string[] {
- assemblers.Count.ToString(),
- activeAssemblers.ToString()
- })
- );
- }
- //Show Ore processing information if there are refineries
- if (refineries.Count > 0)
- {
- updateScreenBuilder.AppendFormat(
- "\nOre processing: {0}",
- string.Format(
- "{0} refineries, {1} active.",
- new string[] {
- refineries.Count.ToString(),
- activeRefineries.ToString()
- })
- );
- }
- //Show power information
- updateScreenBuilder.AppendFormat("\nPower: {0}", powerReport);
- //Show Comms information
- updateScreenBuilder.AppendFormat(
- "\nComms: {0}{1}{2}",
- shortCommsStatus,
- antennaString,
- beaconString
- );
- //Apply text to all screens with _status suffix
- for (int i = 0; i < statusPanels.Count; i++)
- {
- if (statusPanels[i].DisplayNameText.EndsWith("_status"))
- {
- IMyTextPanel screen = (IMyTextPanel)statusPanels[i];
- screen.WritePublicText(updateScreenBuilder.ToString());
- }
- }
- }
- void updateIconPanels()
- {
- if (codeBlue)
- {
- for (int i = 0; i < iconPanels.Count; i++)
- {
- debugOutput("Setting texture on " + iconPanels[i].DisplayNameText);
- iconPanels[i].ShowPublicTextOnScreen();
- iconPanels[i].ShowTextureOnScreen();
- iconPanels[i].AddImageToSelection("Construction");
- //THIS IS BROKEN UNTIL WE CAN REMOVE IMAGES. For fuck sake Keen Dx
- }
- }
- }
- /// <summary>
- /// Automatically increases/decreases antenna range to available power
- /// Attempting to not drain any battery
- /// </summary>
- /// <param name="minimumRangeMeters">Minimum amount of meters Antenna must be visible</param>
- void manageAnntenas(long minimumRangeMeters, long maximumRangeMeters)
- {
- if (antennas.Count < 1) { return; }
- if (maxPower < powerUsage) //If there's power being drained from the batteries
- {
- for (int i = 0; i < antennas.Count; i++)
- {
- var ant = (IMyRadioAntenna)antennas[i];
- if (ant.Radius > minimumRangeMeters && isHealthy(ant))
- {
- ant.ApplyAction("DecreaseRadius");
- }
- }
- }
- else if(maxPower -100 > powerUsage) //Only up the range if there's enough power overhead
- {
- for (int i = 0; i < antennas.Count; i++)
- {
- var ant = (IMyRadioAntenna)antennas[i];
- if (ant.Radius < maximumRangeMeters && isHealthy(ant))
- {
- ant.ApplyAction("IncreaseRadius");
- }
- }
- }
- }
- /// <summary>
- /// Automatically increases/decreases antenna range to available power
- /// Attempting to not drain any battery
- /// </summary>
- /// <param name="minimumRangeMeters">Minimum amount of meters Antenna must be visible</param>
- void manageBeacons(long minimumRangeMeters, long maximumRangeMeters)
- {
- if (antennas.Count < 1) { return; }
- if (maxPower < powerUsage) //If there's power being drained from the batteries
- {
- for (int i = 0; i < beacons.Count; i++)
- {
- var beacon = (IMyBeacon)beacons[i];
- if (beacon.Radius > minimumRangeMeters && isHealthy(beacon))
- {
- beacon.ApplyAction("DecreaseRadius");
- }
- }
- }
- else if (maxPower - 100 > powerUsage) //Only up the range if there's enough power overhead
- {
- for (int i = 0; i < beacons.Count; i++)
- {
- var beacon = (IMyBeacon)beacons[i];
- if (beacon.Radius < maximumRangeMeters && isHealthy(beacon))
- {
- beacon.ApplyAction("IncreaseRadius");
- }
- }
- }
- }
- /// <summary>
- /// Toggles refineries to make sure at least the given amount of energy in KWh is left in the batteries (default is 25%)
- /// Note: any value for mininmumKWh must be an ABSOLUTE value, e.g 500KWh, not in percentages. If you want percentages, edit the method.
- /// </summary>
- /// <param name="toggleOffset">Offset before refineries are turned on again to prevent togglestorm</param>
- /// <param name="minimumKWh">Least amount of KWh (e.g 500KWh) that must be left in batttery</param>
- void manageRefineries(int toggleOffset = 350, int minimumKWh = 0)
- {
- var power = getWattHourInBatteries();
- bool toggleRefineries = true;
- for (int i = 0; i < refineries.Count; i++)
- {
- if (minimumKWh <= 0)
- {
- if (power <= (getMaxWattHourInBatteries() / 4)) { refineries[i].ApplyAction("OnOff_Off"); }
- else if (power >= ((getMaxWattHourInBatteries() / 4) + toggleOffset)) { refineries[i].ApplyAction("OnOff_On"); }
- }
- else
- {
- if (power <= minimumKWh) { refineries[i].ApplyAction("OnOff_Off"); }
- else if (power >= (minimumKWh + toggleOffset)) { refineries[i].ApplyAction("OnOff_On"); }
- }
- }
- if (toggleRefineries)
- { refineries.ForEach(s => s.RequestEnable(true)); }
- else { refineries.ForEach(s => s.RequestEnable(false)); }
- }
- /// <summary>
- /// Works just like manageRefineries, except with Assemblers. Literally I used the same code with a quick case-sensitive replace.
- /// </summary>
- /// <param name="toggleOffset"></param>
- /// <param name="minimumKWh"></param>
- void manageAssemblers(int toggleOffset = 350, int minimumKWh = 0)
- {
- var power = getWattHourInBatteries();
- bool toggleAssemblers = true;
- for (int i = 0; i < assemblers.Count; i++)
- {
- if (minimumKWh <= 0)
- {
- if (power <= (getMaxWattHourInBatteries() / 4)) { assemblers[i].ApplyAction("OnOff_Off"); }
- else if (power >= ((getMaxWattHourInBatteries() / 4) + toggleOffset)) { assemblers[i].ApplyAction("OnOff_On"); }
- }
- else
- {
- if (power <= minimumKWh) { assemblers[i].ApplyAction("OnOff_Off"); }
- else if (power >= (minimumKWh + toggleOffset)) { assemblers[i].ApplyAction("OnOff_On"); }
- }
- }
- if (toggleAssemblers)
- { assemblers.ForEach(s => s.RequestEnable(true)); }
- else { assemblers.ForEach(s => s.RequestEnable(false)); }
- }
- /// <summary>
- /// Reactor management is really a matter of personal taste,
- /// but I personally want solar and battery power to be used before reactors become active.
- /// I also don't want reactors to charge batteries. which is the only thing you can toggle in this scheme
- /// If you don't like the reactor scheme, build your own.
- /// </summary>
- /// <param name="chargeBatteries">Whether or not to charge batteries using reactor power</param>
- void manageReactors(bool chargeBatteries = false)
- {
- //updateMisc(); //Might not be needed, if updateStatusPanel() is called first.
- if (batteries.Count < 1 && solarpanels.Count < 1)
- { return; } //It would be unwise to turn off our only known energy source.
- if (maxPower < powerUsage || solarpanels.Count < 1) //If Solar power doesn't cut it
- {
- if (wattHourInBatteries < (maxWattHourInBatteries / 4)) //If battery is low
- {
- debugOutput("Turning on reactors (maxpower<usage, watthoursinbats<maxwatthours/4)" );
- for (int i = 0; i < reactors.Count; i++)
- {
- reactors[i].ApplyAction("OnOff_On"); //Turn on all reactors
- }
- if (!chargeBatteries) //And if we don't wanna charge the batteries with reactor power
- {
- debugOutput("Turning off batteries (!chargebatteries)");
- for (int i = 0; i < batteries.Count; i++)
- {
- batteries[i].ApplyAction("OnOff_Off"); //Turn off all batteries
- debugOutput("Turning off batteries");
- }
- }
- }
- }
- else if (solarpanels.Count > 0)//If solar power is enough to charge the batteries and there are solar panels
- {
- debugOutput("Solar output high enough to charge batteries.");
- debugOutput("Turning off all reactors, and setting all batteries to charge");
- for (int i = 0; i < batteries.Count; i++)
- {
- batteries[i].ApplyAction("OnOff_On"); //Turn on all batteries
- }
- for (int i = 0; i < reactors.Count; i++)
- {
- reactors[i].ApplyAction("OnOff_Off"); //Turn off all reactors
- }
- }
- }
- /// <summary>
- /// Original battery management code written and tested by Stiggan and Malakeh in January 2015.
- /// </summary>
- void manageBatteries()
- {
- // Raise the usage by this much in kW to make batteries kick in earlier.
- int offset = 0;
- // Modify total on the reactors maximum to make batteries kick in earlier.
- float margin = 1.00f;
- maxPower = (int)(margin * ((float)maxPower));
- powerUsage += offset * 1000;
- if (maxPower > powerUsage)
- {
- toggleRandomBattery(); // Toggle on a battery to see if power is needed.
- chargeAllBatteries(); // Sets batteries to charge.
- }
- else
- {
- chargeAllBatteries(); // Sets batteries to discharge.
- }
- }
- /// <summary>
- /// This function automatically toggles turrets when the alertState goes to Red.
- /// </summary>
- void manageTurrets()
- {
- if (codeRed)
- {
- for (int i = 0; i < turrets.Count; i++)
- {
- turrets[i].ApplyAction("OnOff_On");
- }
- }
- else
- {
- for (int i = 0; i < turrets.Count; i++)
- {
- turrets[i].ApplyAction("OnOff_Off");
- }
- }
- }
- #endregion
- void Main()
- {
- currentInternalIteration++;
- updateLCDPanels(); //Must be first to execute, otherwise ai_storage and debugpanel are unavailable.
- debugOutput("Running internal Update Modules");
- updateModules();
- updateMiscWithBlacklist();
- updateBrokenBlocks();
- updateAssemblers();
- updateRefineries();
- updateSpeakers();
- updateStatusString();
- updateScreenModuleValues(); //This is optional if you don't have the Screen module.
- //updateIconPanels(); //A fix is underway: http://forums.keenswh.com/post/01071008-lcd-panel-image-api-unsuable-in-scripts-7318058?pid=1286386079
- debugOutput(string.Format("blue: {0} orange: {1} red: {2}", codeBlue, codeOrange, codeRed));
- debugOutput("Running External Modules");
- //Add and remove members from the script to activate their functionality. The void members right above Main() are available for use.
- if (!isScreenModulePresent)
- {
- debugOutput("No Screen module found, printing to status panels from core..");
- updateStatusPanels(); //Turn this off if you have the AI Screen Module.
- }
- debugOutput("Running Ship Management Modules");
- soundAlerts();
- manageAssemblers();
- manageRefineries();
- //manageBatteries(); //Powermanagement is broken and to be replaced.
- manageTurrets(); //Toggles turrets; Red means on, no alerts means off.
- manageAnntenas(500, 35000);
- manageBeacons(500, 35000);
- if (currentInternalIteration > 9) { currentInternalIteration = 0; }
- debugOutput("AI Iteration " + currentInternalIteration.ToString() + " finished.");
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment