Advertisement
Whiplash141

Auto Door Closing Script - Experimental Version

Apr 14th, 2015
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. //Auto Door Closing Script [EXPERIMENTAL VERSION]
  2. //
  3. //--------------------------------SETUP--------------------------------
  4. //Ignores all doors with the name "Airtight Hangar Door"
  5. //Make a timer and set it to run this script and trigger itself instantly
  6. //--------------------------Adding Exceptions--------------------------
  7. //Simply add the tag "[Excluded]" without the quotes to exclude a door
  8. //---------------------Airlock Functionality---------------------------
  9. //Script can support 4 airlock doors
  10. //Airlock names should include the following:
  11. //  * Stbd Airlock 1 and Stbd Airlock 2 , Port Airlock 1 and Port Airlock 2
  12. //  * Port doors and Stbd doors operate independently from each other
  13. //  * If Airlock 1 is open Airlock 2 will not open until its counterpart is closed
  14. //
  15. //This Code is experimental in the sense that it runs continuously. If the doors
  16. //do NOT stay open for the predicted [3] seconds, please contact me so I can try
  17. //and fix it :D
  18. //  -Whiplash141
  19.  
  20. //-------------------------Code Start----------------------------------
  21. //Our lists
  22.     Dictionary<IMyTerminalBlock, int> dict = new Dictionary<IMyTerminalBlock, int>();
  23.     List<IMyTerminalBlock> hangarDoors = new List<IMyTerminalBlock>();
  24.     List<IMyTerminalBlock> excludedDoors = new List <IMyTerminalBlock>();
  25.  
  26.     const int MAX_SECONDS = 180; //1 second = 60, adjust as u see fit
  27.  
  28. void Main() {
  29.  
  30.    List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();    
  31.    GridTerminalSystem.GetBlocksOfType<IMyDoor>(blocks);  
  32.    GridTerminalSystem.SearchBlocksOfName("Airtight Hangar Door",hangarDoors);
  33.    //Here is the exception part
  34.    GridTerminalSystem.SearchBlocksOfName("[Excluded]",excludedDoors); //change [Excluded] to what u want  
  35.  
  36.     for(int i = 0; i < blocks.Count; i++) {
  37.  
  38.         var door = blocks[i] as IMyDoor;
  39.        
  40. //I have no clue if this works
  41.         for (int j = 0; j < hangarDoors.Count; j++ ){      
  42.                    
  43.               IMyDoor hangarDoor = hangarDoors[j] as IMyDoor;        
  44.               dict.Remove(hangarDoor);
  45.                 }
  46.              
  47.                 for(int k = 0; k < excludedDoors.Count; k++){
  48.                     IMyDoor excludedDoor = excludedDoors[k] as IMyDoor;
  49.                     dict.Remove(excludedDoor);
  50.               }
  51.  
  52.        
  53.  
  54.         if(dict.ContainsKey(door)) {
  55.            
  56.             if(!door.Open) {
  57.  
  58.                 dict.Remove(door);
  59.  
  60.             } else {
  61.  
  62.                 int doorCount;
  63.  
  64.                 dict.TryGetValue(door, out doorCount);
  65.                 dict.Remove(door);
  66.  
  67.                 if(doorCount++ < MAX_SECONDS)
  68.                     dict.Add(door, doorCount);
  69.                 else
  70.                     door.GetActionWithName("Open_Off").Apply(door);
  71.             }
  72.  
  73.         } else {
  74.  
  75.             if(door.Open)
  76.                 dict.Add(door,0);
  77.         }
  78.     }
  79. //----------End of auto door close part of script---------------
  80.  
  81. //----------Start Airlock Script--------------------------------
  82. //Make Airlock Lists
  83.     List<IMyTerminalBlock> doorList1 = new List<IMyTerminalBlock>();
  84.     List<IMyTerminalBlock> doorList2 = new List<IMyTerminalBlock>();
  85.     List<IMyTerminalBlock> doorList3 = new List<IMyTerminalBlock>();
  86.     List<IMyTerminalBlock> doorList4 = new List<IMyTerminalBlock>();
  87.    
  88.     GridTerminalSystem.SearchBlocksOfName("Port Airlock 1",doorList1);
  89.     GridTerminalSystem.SearchBlocksOfName("Port Airlock 2",doorList2);
  90.     GridTerminalSystem.SearchBlocksOfName("Stbd Airlock 1",doorList3);
  91.     GridTerminalSystem.SearchBlocksOfName("Stbd Airlock 2",doorList4);
  92.  
  93. //Airlock Check 1 (Port)
  94. if(doorList1.Count != 0 && doorList2.Count != 0)
  95. {
  96.     for(int k = 0; k < doorList1.Count; k++)
  97.     {
  98.         IMyDoor door1 = doorList1[k] as IMyDoor;
  99.        
  100.         for(int j = 0; j < doorList2.Count; j++)
  101.         {
  102.             IMyDoor door2 = doorList2[j] as IMyDoor;
  103.        
  104.    
  105.         if(door1.Open)
  106.             {
  107.             door2.GetActionWithName("Open_Off").Apply(door2);
  108.             door2.GetActionWithName("OnOff_Off").Apply(door2);
  109.             }
  110.         else
  111.             {
  112.             door2.GetActionWithName("OnOff_On").Apply(door2);
  113.             }
  114.  
  115.         if(door2.Open)  
  116.             {  
  117.             door1.GetActionWithName("Open_Off").Apply(door1);  
  118.             door1.GetActionWithName("OnOff_Off").Apply(door1);  
  119.             }
  120.         else  
  121.             {  
  122.             door1.GetActionWithName("OnOff_On").Apply(door1);  
  123.             }
  124.         }
  125.     }
  126. }
  127. //Airlock Check 2(Stbd)
  128. if(doorList3.Count != 0 && doorList4.Count != 0)
  129. {
  130.     for(int k = 0; k < doorList3.Count; k++){    
  131.         IMyDoor door3 = doorList3[k] as IMyDoor;
  132.        
  133.         for(int j = 0; j < doorList4.Count; j++){
  134.             IMyDoor door4 = doorList4[j] as IMyDoor;
  135.        
  136.  
  137.         if(door3.Open)  
  138.             {  
  139.             door4.GetActionWithName("Open_Off").Apply(door4);  
  140.             door4.GetActionWithName("OnOff_Off").Apply(door4);  
  141.             }  
  142.         else  
  143.             {  
  144.             door4.GetActionWithName("OnOff_On").Apply(door4);  
  145.             }  
  146.  
  147.         if(door4.Open)  
  148.             {  
  149.             door3.GetActionWithName("Open_Off").Apply(door3);  
  150.             door3.GetActionWithName("OnOff_Off").Apply(door3);  
  151.             }  
  152.         else  
  153.             {  
  154.             door3.GetActionWithName("OnOff_On").Apply(door3);  
  155.             }
  156.         }
  157.     }  
  158. }
  159. } //the void main bracket
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement