Advertisement
NickNDS

Door Sealer

Aug 6th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.10 KB | None | 0 0
  1. public string
  2. ventGroupPrefix = "[vent", // Vent and door custom data prefix key
  3. doorOpenKeyword = "open"; // Door custom data control keyword, makes door open when oxygen is at or above oxygenLevelForOpening
  4.  
  5. public double
  6. rescanDelayInSeconds = 10, // Time in seconds before scans for new vents and doors
  7. oxygenLevelForOpening = 0.98, // Oxygen level for opening door
  8. oxygenLevelForClosing = 0.0; // Oxygen level for closing door
  9.  
  10. public bool doubleCheckBeforeOpeningDoor = true; // Check that the door wasn't closed by another group before opening
  11.  
  12. // End of user configuration
  13.  
  14. public SortedList<string, VentGroup> ventGroups = new SortedList<string, VentGroup>();
  15.  
  16. public List<IMyTerminalBlock> closedDoors = new List<IMyTerminalBlock>();
  17.  
  18. public TimeSpan rescanSpan = new TimeSpan(0, 0, 0);
  19.  
  20. public int currentVentGroupIndex = 0;
  21.  
  22. public Program()
  23. {
  24.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  25.     ScanForVentGroups();
  26.     Save();
  27. }
  28.  
  29. public void Save() { }
  30.  
  31. public void Main(string argument, UpdateType updateSource)
  32. {
  33.     rescanSpan += Runtime.TimeSinceLastRun;
  34.     Echo("Vent Groups: " + ventGroups.Values.Count);
  35.    
  36.     if (rescanSpan.TotalSeconds >= rescanDelayInSeconds)
  37.     {
  38.         rescanSpan = new TimeSpan(0, 0, 0);
  39.         ScanForVentGroups();
  40.     } else if (ventGroups.Values.Count > 0) CheckVents();
  41.     else Echo("No Current Vent Group");
  42.     Save();
  43. }
  44.  
  45. public void CheckVents()
  46. {
  47.     if (currentVentGroupIndex >= ventGroups.Values.Count) currentVentGroupIndex = 0;
  48.     Echo("Current Vent Group: " + (currentVentGroupIndex + 1) + ": " + ventGroups.Keys[currentVentGroupIndex]);
  49.     Echo("Doors: " + ventGroups.Values[currentVentGroupIndex].doorList.Count);
  50.     Echo("Vents: " + ventGroups.Values[currentVentGroupIndex].ventList.Count);
  51.     bool open = false, close = false, firstRun = ventGroups.Values[currentVentGroupIndex].firstRun;
  52.     while (ventGroups.Values[currentVentGroupIndex].ventList.Count > 0)
  53.     {
  54.         if (!ValidBlock(ventGroups.Values[currentVentGroupIndex].ventList[0])) ventGroups.Values[currentVentGroupIndex].ventList.RemoveAt(0);
  55.         else {
  56.             double oxygenLevel = ((IMyAirVent)ventGroups.Values[currentVentGroupIndex].ventList[0]).GetOxygenLevel();
  57.             if (oxygenLevel >= oxygenLevelForOpening) open = true;
  58.             else if (oxygenLevel <= oxygenLevelForClosing) close = true;
  59.             break;
  60.         }
  61.     }
  62.     if (firstRun || (open && !ventGroups.Values[currentVentGroupIndex].open) || (close && ventGroups.Values[currentVentGroupIndex].open))
  63.     {
  64.         ventGroups.Values[currentVentGroupIndex].firstRun = false;
  65.         ventGroups.Values[currentVentGroupIndex].open = open && !close;
  66.         for (int i = 0; i < ventGroups.Values[currentVentGroupIndex].doorList.Count; i += 0)
  67.         {
  68.             if (!ValidBlock(ventGroups.Values[currentVentGroupIndex].doorList[i])) ventGroups.Values[currentVentGroupIndex].doorList.RemoveAt(i);
  69.             else {
  70.                 if (close) {
  71.                     OperateDoor(ventGroups.Values[currentVentGroupIndex].doorList[i]);
  72.                     if (!ventGroups.Values[currentVentGroupIndex].closedDoorList.Contains(ventGroups.Values[currentVentGroupIndex].doorList[i]))
  73.                         ventGroups.Values[currentVentGroupIndex].closedDoorList.Add(ventGroups.Values[currentVentGroupIndex].doorList[i]);
  74.                 }
  75.                 else if (ventGroups.Values[currentVentGroupIndex].doorList[i].CustomData.ToLower().Contains(doorOpenKeyword.ToLower()))
  76.                     OperateDoor(ventGroups.Values[currentVentGroupIndex].doorList[i], true);
  77.                 i++;
  78.             }
  79.         }
  80.     }
  81.     currentVentGroupIndex++;
  82. }
  83.  
  84. public void OperateDoor(IMyTerminalBlock door, bool open = false)
  85. {
  86.     if (!open)
  87.     {
  88.         ((IMyDoor)door).CloseDoor();
  89.         if (doubleCheckBeforeOpeningDoor && !ventGroups.Values[currentVentGroupIndex].closedDoorList.Contains(door))
  90.             ventGroups.Values[currentVentGroupIndex].closedDoorList.Add(door);
  91.     } else {
  92.         bool canOpen = true;
  93.         if (doubleCheckBeforeOpeningDoor) {
  94.             ventGroups.Values[currentVentGroupIndex].closedDoorList.Remove(door);
  95.             for (int i = 0; i < ventGroups.Values.Count; i++)
  96.             {
  97.                 if (i != currentVentGroupIndex)
  98.                 {
  99.                     if (ventGroups.Values[i].closedDoorList.Contains(door))
  100.                     {
  101.                         canOpen = false;
  102.                         break;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.         if (canOpen) ((IMyDoor)door).OpenDoor();
  108.     }
  109. }
  110.  
  111. public bool ValidBlock(IMyTerminalBlock block)
  112. {
  113.     return block.CubeGrid.CubeExists(block.Position);
  114. }
  115.  
  116. public void ScanForVentGroups()
  117. {
  118.     for (int i = 0; i < ventGroups.Values.Count; i++)
  119.     {
  120.         ventGroups.Values[i].doorList.Clear();
  121.         ventGroups.Values[i].ventList.Clear();
  122.     }
  123.     List<IMyTerminalBlock> ventList = new List<IMyTerminalBlock>(), doorList = new List<IMyTerminalBlock>();
  124.     GridTerminalSystem.GetBlocksOfType<IMyDoor>(doorList, d => d.CustomData.ToLower().Contains(ventGroupPrefix.ToLower()));
  125.     GridTerminalSystem.GetBlocksOfType<IMyAirVent>(ventList, v => v.CustomData.ToLower().Contains(ventGroupPrefix.ToLower()));
  126.     for (int i = 0; i < doorList.Count; i++)
  127.         AddDoor(doorList[i]);
  128.     for (int i = 0; i < ventList.Count; i++)
  129.         AddVent(ventList[i]);
  130.     for (int i = 0; i < ventGroups.Values.Count; i += 0)
  131.         if (ventGroups.Values[i].doorList.Count == 0 && ventGroups.Values[i].ventList.Count == 0) ventGroups.RemoveAt(i);
  132.         else i++;
  133. }
  134.  
  135. public void AddDoor(IMyTerminalBlock door)
  136. {
  137.     List<string> keys = new List<string>();
  138.     GetKeys(door, ref keys);
  139.     for (int i = 0; i < keys.Count; i++)
  140.     {
  141.         if (!ventGroups.ContainsKey(keys[i]))
  142.         {
  143.             VentGroup ventGroup = new VentGroup();
  144.             ventGroup.doorList.Add(door);
  145.             ventGroups[keys[i]] = ventGroup;
  146.         } else ventGroups[keys[i]].doorList.Add(door);
  147.     }
  148. }
  149.  
  150. public void AddVent(IMyTerminalBlock vent)
  151. {
  152.     List<string> keys = new List<string>();
  153.     GetKeys(vent, ref keys);
  154.     for (int i = 0; i < keys.Count; i++)
  155.     {
  156.         if (!ventGroups.ContainsKey(keys[i]))
  157.         {
  158.             VentGroup ventGroup = new VentGroup();
  159.             ventGroup.ventList.Add(vent);
  160.             ventGroups[keys[i]] = ventGroup;
  161.         } else ventGroups[keys[i]].ventList.Add(vent);
  162.     }
  163. }
  164.  
  165. public void GetKeys(IMyTerminalBlock block, ref List<string> keys)
  166. {
  167.     string key = "", cData = block.CustomData;
  168.     while (cData.Contains("]"))
  169.     {
  170.         cData = cData.Substring(cData.IndexOf("[") + 1);
  171.         key = cData.Substring(0, cData.IndexOf("]"));
  172.         if (!keys.Contains(key)) keys.Add(key);
  173.         cData = cData.Substring(cData.IndexOf("]") + 1);
  174.     }
  175. }
  176.  
  177. public class VentGroup
  178. {
  179.     public
  180.     List<IMyTerminalBlock> ventList = new List<IMyTerminalBlock>(),
  181.     doorList = new List<IMyTerminalBlock>(),
  182.     closedDoorList = new List<IMyTerminalBlock>();
  183.     public bool open = true, firstRun = true;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement