Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string
- ventGroupPrefix = "[vent", // Vent and door custom data prefix key
- doorOpenKeyword = "open"; // Door custom data control keyword, makes door open when oxygen is at or above oxygenLevelForOpening
- public double
- rescanDelayInSeconds = 10, // Time in seconds before scans for new vents and doors
- oxygenLevelForOpening = 0.98, // Oxygen level for opening door
- oxygenLevelForClosing = 0.0; // Oxygen level for closing door
- public bool doubleCheckBeforeOpeningDoor = true; // Check that the door wasn't closed by another group before opening
- // End of user configuration
- public SortedList<string, VentGroup> ventGroups = new SortedList<string, VentGroup>();
- public List<IMyTerminalBlock> closedDoors = new List<IMyTerminalBlock>();
- public TimeSpan rescanSpan = new TimeSpan(0, 0, 0);
- public int currentVentGroupIndex = 0;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- ScanForVentGroups();
- Save();
- }
- public void Save() { }
- public void Main(string argument, UpdateType updateSource)
- {
- rescanSpan += Runtime.TimeSinceLastRun;
- Echo("Vent Groups: " + ventGroups.Values.Count);
- if (rescanSpan.TotalSeconds >= rescanDelayInSeconds)
- {
- rescanSpan = new TimeSpan(0, 0, 0);
- ScanForVentGroups();
- } else if (ventGroups.Values.Count > 0) CheckVents();
- else Echo("No Current Vent Group");
- Save();
- }
- public void CheckVents()
- {
- if (currentVentGroupIndex >= ventGroups.Values.Count) currentVentGroupIndex = 0;
- Echo("Current Vent Group: " + (currentVentGroupIndex + 1) + ": " + ventGroups.Keys[currentVentGroupIndex]);
- Echo("Doors: " + ventGroups.Values[currentVentGroupIndex].doorList.Count);
- Echo("Vents: " + ventGroups.Values[currentVentGroupIndex].ventList.Count);
- bool open = false, close = false, firstRun = ventGroups.Values[currentVentGroupIndex].firstRun;
- while (ventGroups.Values[currentVentGroupIndex].ventList.Count > 0)
- {
- if (!ValidBlock(ventGroups.Values[currentVentGroupIndex].ventList[0])) ventGroups.Values[currentVentGroupIndex].ventList.RemoveAt(0);
- else {
- double oxygenLevel = ((IMyAirVent)ventGroups.Values[currentVentGroupIndex].ventList[0]).GetOxygenLevel();
- if (oxygenLevel >= oxygenLevelForOpening) open = true;
- else if (oxygenLevel <= oxygenLevelForClosing) close = true;
- break;
- }
- }
- if (firstRun || (open && !ventGroups.Values[currentVentGroupIndex].open) || (close && ventGroups.Values[currentVentGroupIndex].open))
- {
- ventGroups.Values[currentVentGroupIndex].firstRun = false;
- ventGroups.Values[currentVentGroupIndex].open = open && !close;
- for (int i = 0; i < ventGroups.Values[currentVentGroupIndex].doorList.Count; i += 0)
- {
- if (!ValidBlock(ventGroups.Values[currentVentGroupIndex].doorList[i])) ventGroups.Values[currentVentGroupIndex].doorList.RemoveAt(i);
- else {
- if (close) {
- OperateDoor(ventGroups.Values[currentVentGroupIndex].doorList[i]);
- if (!ventGroups.Values[currentVentGroupIndex].closedDoorList.Contains(ventGroups.Values[currentVentGroupIndex].doorList[i]))
- ventGroups.Values[currentVentGroupIndex].closedDoorList.Add(ventGroups.Values[currentVentGroupIndex].doorList[i]);
- }
- else if (ventGroups.Values[currentVentGroupIndex].doorList[i].CustomData.ToLower().Contains(doorOpenKeyword.ToLower()))
- OperateDoor(ventGroups.Values[currentVentGroupIndex].doorList[i], true);
- i++;
- }
- }
- }
- currentVentGroupIndex++;
- }
- public void OperateDoor(IMyTerminalBlock door, bool open = false)
- {
- if (!open)
- {
- ((IMyDoor)door).CloseDoor();
- if (doubleCheckBeforeOpeningDoor && !ventGroups.Values[currentVentGroupIndex].closedDoorList.Contains(door))
- ventGroups.Values[currentVentGroupIndex].closedDoorList.Add(door);
- } else {
- bool canOpen = true;
- if (doubleCheckBeforeOpeningDoor) {
- ventGroups.Values[currentVentGroupIndex].closedDoorList.Remove(door);
- for (int i = 0; i < ventGroups.Values.Count; i++)
- {
- if (i != currentVentGroupIndex)
- {
- if (ventGroups.Values[i].closedDoorList.Contains(door))
- {
- canOpen = false;
- break;
- }
- }
- }
- }
- if (canOpen) ((IMyDoor)door).OpenDoor();
- }
- }
- public bool ValidBlock(IMyTerminalBlock block)
- {
- return block.CubeGrid.CubeExists(block.Position);
- }
- public void ScanForVentGroups()
- {
- for (int i = 0; i < ventGroups.Values.Count; i++)
- {
- ventGroups.Values[i].doorList.Clear();
- ventGroups.Values[i].ventList.Clear();
- }
- List<IMyTerminalBlock> ventList = new List<IMyTerminalBlock>(), doorList = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyDoor>(doorList, d => d.CustomData.ToLower().Contains(ventGroupPrefix.ToLower()));
- GridTerminalSystem.GetBlocksOfType<IMyAirVent>(ventList, v => v.CustomData.ToLower().Contains(ventGroupPrefix.ToLower()));
- for (int i = 0; i < doorList.Count; i++)
- AddDoor(doorList[i]);
- for (int i = 0; i < ventList.Count; i++)
- AddVent(ventList[i]);
- for (int i = 0; i < ventGroups.Values.Count; i += 0)
- if (ventGroups.Values[i].doorList.Count == 0 && ventGroups.Values[i].ventList.Count == 0) ventGroups.RemoveAt(i);
- else i++;
- }
- public void AddDoor(IMyTerminalBlock door)
- {
- List<string> keys = new List<string>();
- GetKeys(door, ref keys);
- for (int i = 0; i < keys.Count; i++)
- {
- if (!ventGroups.ContainsKey(keys[i]))
- {
- VentGroup ventGroup = new VentGroup();
- ventGroup.doorList.Add(door);
- ventGroups[keys[i]] = ventGroup;
- } else ventGroups[keys[i]].doorList.Add(door);
- }
- }
- public void AddVent(IMyTerminalBlock vent)
- {
- List<string> keys = new List<string>();
- GetKeys(vent, ref keys);
- for (int i = 0; i < keys.Count; i++)
- {
- if (!ventGroups.ContainsKey(keys[i]))
- {
- VentGroup ventGroup = new VentGroup();
- ventGroup.ventList.Add(vent);
- ventGroups[keys[i]] = ventGroup;
- } else ventGroups[keys[i]].ventList.Add(vent);
- }
- }
- public void GetKeys(IMyTerminalBlock block, ref List<string> keys)
- {
- string key = "", cData = block.CustomData;
- while (cData.Contains("]"))
- {
- cData = cData.Substring(cData.IndexOf("[") + 1);
- key = cData.Substring(0, cData.IndexOf("]"));
- if (!keys.Contains(key)) keys.Add(key);
- cData = cData.Substring(cData.IndexOf("]") + 1);
- }
- }
- public class VentGroup
- {
- public
- List<IMyTerminalBlock> ventList = new List<IMyTerminalBlock>(),
- doorList = new List<IMyTerminalBlock>(),
- closedDoorList = new List<IMyTerminalBlock>();
- public bool open = true, firstRun = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement