Whiplash141

Whip's Auto Door and Airlock v13 [SpaceEngineers]

Mar 30th, 2015
2,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.87 KB | None | 0 0
  1.  
  2. /*          
  3. /// Whip's Auto Door/Airlock Script v13 - 4/10/2016 ///  
  4. /// PUBLIC RELEASE ///        
  5. _______________________________________________________________________        
  6. ///DESCRIPTION///  
  7.  
  8.     This script will close opened doors after 3 seconds (default).  
  9.     The duration that a door is allowed to be open can be modified lower  
  10.     down in the code (line 62).
  11.  
  12. This script also supports an INFINITE number of airlock systems.      
  13. _______________________________________________________________________        
  14. ///TIMER SETUP///      
  15.  
  16.     <REQUIRED>: Make a timer and set it to run this script and run itself every 1 second
  17.  
  18.     (Optional): If you want the program to be more precise, make the timer
  19.     "Trigger Now" itself
  20. _______________________________________________________________________        
  21. ///AUTO DOOR CLOSER///    
  22.  
  23.     The script will fetch ALL doors on the grid and automatically close any
  24.     door that has been open for 3 seconds. Doors can also be excluded from  
  25.     this feature.  
  26.  
  27. Excluding Doors:      
  28.     * Add the tag "[Excluded]" anywhere in the door(s) name.  
  29.     * Airtight Hangar Doors with their default names will be excluded by default.        
  30. _______________________________________________________________________        
  31. ///AIRLOCKS///        
  32.  
  33.     This script supports the optional feature of simple airlock systems.
  34.     Airlock systems are composed of AT LEAST one Interior Door AND one Exterior Door.
  35.     The airlock status light does NOT affect the functionality of the doors
  36.     so if you don't have space for one, don't fret :)  
  37.  
  38. Airlock system names should follow these patterns:  
  39.  
  40.     * Interior Airlock Doors: "[Prefix] Airlock Interior"  
  41.  
  42.     * Exterior Airlock Doors: "[Prefix] Airlock Exterior"  
  43.  
  44.     * Airlock Status Lights: "[Prefix] Airlock Light"  
  45.  
  46.     You can make the [Prefix] what ever you wish, but in order for doors in an airlock  
  47.     system to be linked by the script, they MUST have the same prefix.
  48. _____________________________________________________________________  
  49.  
  50. If you have any questions, comments, or concerns, feel free to leave a comment on          
  51. the workshop page: http://steamcommunity.com/sharedfiles/filedetails/?id=416932930        
  52. - Whiplash141   :)  
  53. _____________________________________________________________________    
  54. */        
  55.  
  56. //Door open duration (in seconds)
  57.     const double door_open_duration = 3;
  58.  
  59. //Door exclusion string
  60.     const string door_exclude_string = "[Excluded]";
  61.  
  62. //Airlock Light Settings
  63.     Color alarmColor = new Color(255,40,40); //color of alarm light        
  64.     Color regularColor = new Color (80,160,255); //color of regular light
  65.     Color lightColor;
  66.     float alarmBlinkLength = 50f;  //alarm blink length in %
  67.     float regularBlinkLength = 100f; //regular blink length in %
  68.     float lightBlinkLength;                                      
  69.     float blinkInterval = .8f; // blink interval in seconds
  70.     string targetLights;
  71.    
  72. //====================================================================
  73. //MAIN CODE BELOW: Do NOT edit if you don't know that you are doing :P
  74. //====================================================================
  75.  
  76.     Dictionary<IMyTerminalBlock, double> dictDoors = new Dictionary<IMyTerminalBlock, double>();
  77.  
  78. void Main()          
  79. {
  80.     AutoDoors();
  81.     Airlocks(); //you can comment out one of these if you do not want them active
  82. }
  83.  
  84. void AutoDoors()
  85. {
  86.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  87.     GridTerminalSystem.GetBlocksOfType<IMyDoor>(blocks);
  88.  
  89.     for(int i = 0; i < blocks.Count; i++)
  90.     {
  91.  
  92.     //removes Airtight Hangar Doors
  93.         if ( blocks[i].CustomName.Contains("Airtight Hangar Door") )
  94.         {
  95.                 //Echo("Removing " + blocks[i].CustomName);
  96.                 continue;
  97.         }
  98.         else if( blocks[i].CustomName.Contains( door_exclude_string ) ) //removes excluded doors
  99.         {
  100.                 //Echo("Removing " + blocks[i].CustomName);
  101.                 continue;
  102.         }
  103.         else
  104.         {
  105.             var thisDoor = blocks[i] as IMyDoor; //checks if current block is a door
  106.             if( dictDoors.ContainsKey( thisDoor ) ) //checks dict for said door
  107.             {
  108.                 if( thisDoor.OpenRatio == 0 )
  109.                 {
  110.                     dictDoors.Remove( thisDoor ); //ignores door if closed
  111.                 }
  112.                 else  
  113.                 {
  114.                     double doorCount;
  115.                     dictDoors.TryGetValue( thisDoor, out doorCount ); //pulls current time count
  116.                     dictDoors.Remove( thisDoor ); //removes old time count
  117.  
  118.                     if( doorCount + ElapsedTime.TotalSeconds < door_open_duration )
  119.                     {
  120.                         dictDoors.Add( thisDoor, doorCount + ElapsedTime.TotalSeconds ); //adds new time count
  121.                     }
  122.                     else
  123.                     {
  124.                         thisDoor.ApplyAction( "Open_Off" ); //closes door if duration has past
  125.                     }
  126.                 }
  127.             }else{
  128.                 if( thisDoor.OpenRatio > 0 ) //if door isnt in dict, we add it at a time count of zero
  129.                 {
  130.                     dictDoors.Add( thisDoor, 0 );
  131.                 }
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. void Airlocks()
  138. {
  139.     List<IMyTerminalBlock> airlockDoors = new List<IMyTerminalBlock>();
  140.     List<string> airlockNames = new List<string>();
  141.     List<string> airlockNamesNoDupes = new List<string>();
  142.     string airlockInteriorName;
  143.     string airlockExteriorName;
  144.     string airlockLightName;
  145.     string currentAirlock;
  146.     bool isInteriorClosed;
  147.     bool isExteriorClosed;
  148.  
  149. //Fetch all airlock names
  150.     GridTerminalSystem.SearchBlocksOfName( "Airlock Interior", airlockDoors, isDoor );//lists all blocks with proper name
  151.     for ( int i = 0 ; i < airlockDoors.Count ; i++ )
  152.     {
  153.         airlockNames.Add( airlockDoors[i].CustomName );//adds name to string list  
  154.     }
  155.  
  156. //ignores duplicate names
  157.     for ( int i = 0 ; i < airlockNames.Count ; i++ )
  158.     {
  159.         if( !( airlockNamesNoDupes.Contains( airlockNames[i] ) ) ) //if we dont have this name, add it
  160.             airlockNamesNoDupes.Add( airlockNames[i] );
  161.     }
  162.  
  163. //Evaluate each unique airlock name  
  164.     for ( int i = 0 ; i < airlockNamesNoDupes.Count ; i++ )    
  165.     {
  166.         airlockInteriorName = airlockNamesNoDupes[i]; //makes Interior door name  
  167.         airlockExteriorName = airlockInteriorName.Replace( "Interior", "Exterior" ); //makes Exterior door name
  168.         airlockLightName = airlockInteriorName.Replace( "Interior", "Light" ); //makes Airlock Light name
  169.  
  170.         List<IMyTerminalBlock> airlockInteriorList = new List<IMyTerminalBlock>();
  171.  
  172.         List<IMyTerminalBlock> airlockExteriorList = new List<IMyTerminalBlock>();
  173.  
  174.         GridTerminalSystem.SearchBlocksOfName( airlockInteriorName, airlockInteriorList ); //get all interior doors
  175.         GridTerminalSystem.SearchBlocksOfName( airlockExteriorName, airlockExteriorList ); //get all exterior doors
  176.  
  177.     //Start checking airlock status  
  178.         if( airlockInteriorList.Count != 0 && airlockExteriorList.Count != 0 ) //if we have both door types  
  179.         {
  180.         //fetch name of airlock group and write to console
  181.             currentAirlock = airlockInteriorName.Replace( " Airlock Interior", "" );                                                                                                                                                /*W=h*I/p=l/A.s/H/1/4/1/*/
  182.             Echo ("Airlock Group '" + currentAirlock + "' found");  
  183.  
  184.         //we assume the airlocks are closed until proven otherwise      
  185.             isInteriorClosed = true;  
  186.             isExteriorClosed = true;  
  187.  
  188.         //Door Interior Check  
  189.             for( int j = 0 ; j < airlockInteriorList.Count ; j++ )        
  190.             {        
  191.                 var airlockInterior = airlockInteriorList[j] as IMyDoor;        
  192.                  
  193.                 if( airlockInterior.OpenRatio > 0 )          
  194.                 {          
  195.                     Lock(airlockExteriorName);    
  196.                     LightColorChanger( true, airlockLightName );    
  197.                     isInteriorClosed = false;
  198.                     break;
  199.                         //if any doors yield false, bool will persist until comparison  
  200.                 }        
  201.             }      
  202.  
  203.         //Door Exterior Check          
  204.             for( int j = 0 ; j < airlockExteriorList.Count ; j++ )        
  205.             {
  206.                 var airlockExterior = airlockExteriorList[j] as IMyDoor;
  207.  
  208.                 if( airlockExterior.OpenRatio > 0 )
  209.                 {
  210.                     Lock(airlockInteriorName);
  211.                     LightColorChanger( true, airlockLightName );
  212.                     isExteriorClosed = false;
  213.                     break;
  214.                 }
  215.             }
  216.  
  217.         //if all Interior & Exterior doors closed
  218.             if( isInteriorClosed && isExteriorClosed )
  219.             {
  220.                 LightColorChanger( false, airlockLightName);
  221.             }
  222.  
  223.         //if all Interior doors closed
  224.             if( isInteriorClosed )
  225.             {
  226.                 Unlock( airlockExteriorName );
  227.             }
  228.  
  229.         //if all Exterior doors closed    
  230.             if( isExteriorClosed )
  231.             {
  232.                 Unlock( airlockInteriorName );
  233.             }
  234.         }
  235.     }
  236. }
  237.  
  238. bool isDoor( IMyTerminalBlock testBlock )
  239. {
  240.     var testDoor = testBlock as IMyDoor;
  241.     return testDoor != null;
  242. }
  243.  
  244. void Lock( string lockName )        
  245. {        
  246. //locks all doors with the inputed name      
  247.     List<IMyTerminalBlock> lock_door_list = new List<IMyTerminalBlock>();        
  248.     GridTerminalSystem.SearchBlocksOfName( lockName, lock_door_list );        
  249.            
  250.     for(int i = 0 ; i < lock_door_list.Count ; i++)        
  251.     {        
  252.         IMyDoor lock_door = lock_door_list[i] as IMyDoor;        
  253.         if( lock_door.Open )
  254.             lock_door.ApplyAction("Open_Off");
  255.         if( lock_door.OpenRatio == 0 && lock_door.GetValue<bool>("OnOff") == true )
  256.             lock_door.ApplyAction("OnOff_Off");        
  257.     }        
  258.     Echo(lockName + " is locked");        
  259. }        
  260.  
  261. void Unlock( string unlockName )        
  262. {    
  263. //unlocks all doors with inputed name    
  264.     List<IMyTerminalBlock> unlock_door_list = new List<IMyTerminalBlock>();        
  265.     GridTerminalSystem.SearchBlocksOfName( unlockName, unlock_door_list );        
  266.            
  267.     for(int i = 0 ; i < unlock_door_list.Count ; i++)        
  268.     {        
  269.         IMyDoor unlock_door = unlock_door_list[i] as IMyDoor;        
  270.         if( unlock_door.GetValue<bool>("OnOff") == false )
  271.             unlock_door.ApplyAction("OnOff_On");        
  272.     }        
  273.     Echo(unlockName + " is unlocked");      
  274. }    
  275.  
  276. void LightColorChanger( bool alarm, string lightName )      
  277. {
  278. //applies our status colors to the airlock lights      
  279.         if( alarm )  
  280.         {            
  281.             lightColor = alarmColor;      
  282.             lightBlinkLength = alarmBlinkLength;      
  283.         }else{
  284.             lightColor = regularColor;      
  285.             lightBlinkLength = regularBlinkLength;    
  286.         }            
  287.  
  288.     List<IMyTerminalBlock> listLights = new List<IMyTerminalBlock>();            
  289.     GridTerminalSystem.SearchBlocksOfName( lightName, listLights );        
  290.     for( int i=0 ; i< listLights.Count ; i++)                      
  291.     {      
  292.         var Light = listLights[i] as IMyLightingBlock;                                            
  293.             Light.SetValue( "Color", lightColor );                
  294.             Light.SetValue( "Blink Lenght", lightBlinkLength );                
  295.             Light.SetValue( "Blink Interval", blinkInterval );                
  296.     }          
  297. }
Advertisement
Add Comment
Please, Sign In to add comment