Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Auto Door/Airlock Script v13 - 4/10/2016 ///
- /// PUBLIC RELEASE ///
- _______________________________________________________________________
- ///DESCRIPTION///
- This script will close opened doors after 3 seconds (default).
- The duration that a door is allowed to be open can be modified lower
- down in the code (line 62).
- This script also supports an INFINITE number of airlock systems.
- _______________________________________________________________________
- ///TIMER SETUP///
- <REQUIRED>: Make a timer and set it to run this script and run itself every 1 second
- (Optional): If you want the program to be more precise, make the timer
- "Trigger Now" itself
- _______________________________________________________________________
- ///AUTO DOOR CLOSER///
- The script will fetch ALL doors on the grid and automatically close any
- door that has been open for 3 seconds. Doors can also be excluded from
- this feature.
- Excluding Doors:
- * Add the tag "[Excluded]" anywhere in the door(s) name.
- * Airtight Hangar Doors with their default names will be excluded by default.
- _______________________________________________________________________
- ///AIRLOCKS///
- This script supports the optional feature of simple airlock systems.
- Airlock systems are composed of AT LEAST one Interior Door AND one Exterior Door.
- The airlock status light does NOT affect the functionality of the doors
- so if you don't have space for one, don't fret :)
- Airlock system names should follow these patterns:
- * Interior Airlock Doors: "[Prefix] Airlock Interior"
- * Exterior Airlock Doors: "[Prefix] Airlock Exterior"
- * Airlock Status Lights: "[Prefix] Airlock Light"
- You can make the [Prefix] what ever you wish, but in order for doors in an airlock
- system to be linked by the script, they MUST have the same prefix.
- _____________________________________________________________________
- If you have any questions, comments, or concerns, feel free to leave a comment on
- the workshop page: http://steamcommunity.com/sharedfiles/filedetails/?id=416932930
- - Whiplash141 :)
- _____________________________________________________________________
- */
- //Door open duration (in seconds)
- const double door_open_duration = 3;
- //Door exclusion string
- const string door_exclude_string = "[Excluded]";
- //Airlock Light Settings
- Color alarmColor = new Color(255,40,40); //color of alarm light
- Color regularColor = new Color (80,160,255); //color of regular light
- Color lightColor;
- float alarmBlinkLength = 50f; //alarm blink length in %
- float regularBlinkLength = 100f; //regular blink length in %
- float lightBlinkLength;
- float blinkInterval = .8f; // blink interval in seconds
- string targetLights;
- //====================================================================
- //MAIN CODE BELOW: Do NOT edit if you don't know that you are doing :P
- //====================================================================
- Dictionary<IMyTerminalBlock, double> dictDoors = new Dictionary<IMyTerminalBlock, double>();
- void Main()
- {
- AutoDoors();
- Airlocks(); //you can comment out one of these if you do not want them active
- }
- void AutoDoors()
- {
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyDoor>(blocks);
- for(int i = 0; i < blocks.Count; i++)
- {
- //removes Airtight Hangar Doors
- if ( blocks[i].CustomName.Contains("Airtight Hangar Door") )
- {
- //Echo("Removing " + blocks[i].CustomName);
- continue;
- }
- else if( blocks[i].CustomName.Contains( door_exclude_string ) ) //removes excluded doors
- {
- //Echo("Removing " + blocks[i].CustomName);
- continue;
- }
- else
- {
- var thisDoor = blocks[i] as IMyDoor; //checks if current block is a door
- if( dictDoors.ContainsKey( thisDoor ) ) //checks dict for said door
- {
- if( thisDoor.OpenRatio == 0 )
- {
- dictDoors.Remove( thisDoor ); //ignores door if closed
- }
- else
- {
- double doorCount;
- dictDoors.TryGetValue( thisDoor, out doorCount ); //pulls current time count
- dictDoors.Remove( thisDoor ); //removes old time count
- if( doorCount + ElapsedTime.TotalSeconds < door_open_duration )
- {
- dictDoors.Add( thisDoor, doorCount + ElapsedTime.TotalSeconds ); //adds new time count
- }
- else
- {
- thisDoor.ApplyAction( "Open_Off" ); //closes door if duration has past
- }
- }
- }else{
- if( thisDoor.OpenRatio > 0 ) //if door isnt in dict, we add it at a time count of zero
- {
- dictDoors.Add( thisDoor, 0 );
- }
- }
- }
- }
- }
- void Airlocks()
- {
- List<IMyTerminalBlock> airlockDoors = new List<IMyTerminalBlock>();
- List<string> airlockNames = new List<string>();
- List<string> airlockNamesNoDupes = new List<string>();
- string airlockInteriorName;
- string airlockExteriorName;
- string airlockLightName;
- string currentAirlock;
- bool isInteriorClosed;
- bool isExteriorClosed;
- //Fetch all airlock names
- GridTerminalSystem.SearchBlocksOfName( "Airlock Interior", airlockDoors, isDoor );//lists all blocks with proper name
- for ( int i = 0 ; i < airlockDoors.Count ; i++ )
- {
- airlockNames.Add( airlockDoors[i].CustomName );//adds name to string list
- }
- //ignores duplicate names
- for ( int i = 0 ; i < airlockNames.Count ; i++ )
- {
- if( !( airlockNamesNoDupes.Contains( airlockNames[i] ) ) ) //if we dont have this name, add it
- airlockNamesNoDupes.Add( airlockNames[i] );
- }
- //Evaluate each unique airlock name
- for ( int i = 0 ; i < airlockNamesNoDupes.Count ; i++ )
- {
- airlockInteriorName = airlockNamesNoDupes[i]; //makes Interior door name
- airlockExteriorName = airlockInteriorName.Replace( "Interior", "Exterior" ); //makes Exterior door name
- airlockLightName = airlockInteriorName.Replace( "Interior", "Light" ); //makes Airlock Light name
- List<IMyTerminalBlock> airlockInteriorList = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> airlockExteriorList = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName( airlockInteriorName, airlockInteriorList ); //get all interior doors
- GridTerminalSystem.SearchBlocksOfName( airlockExteriorName, airlockExteriorList ); //get all exterior doors
- //Start checking airlock status
- if( airlockInteriorList.Count != 0 && airlockExteriorList.Count != 0 ) //if we have both door types
- {
- //fetch name of airlock group and write to console
- currentAirlock = airlockInteriorName.Replace( " Airlock Interior", "" ); /*W=h*I/p=l/A.s/H/1/4/1/*/
- Echo ("Airlock Group '" + currentAirlock + "' found");
- //we assume the airlocks are closed until proven otherwise
- isInteriorClosed = true;
- isExteriorClosed = true;
- //Door Interior Check
- for( int j = 0 ; j < airlockInteriorList.Count ; j++ )
- {
- var airlockInterior = airlockInteriorList[j] as IMyDoor;
- if( airlockInterior.OpenRatio > 0 )
- {
- Lock(airlockExteriorName);
- LightColorChanger( true, airlockLightName );
- isInteriorClosed = false;
- break;
- //if any doors yield false, bool will persist until comparison
- }
- }
- //Door Exterior Check
- for( int j = 0 ; j < airlockExteriorList.Count ; j++ )
- {
- var airlockExterior = airlockExteriorList[j] as IMyDoor;
- if( airlockExterior.OpenRatio > 0 )
- {
- Lock(airlockInteriorName);
- LightColorChanger( true, airlockLightName );
- isExteriorClosed = false;
- break;
- }
- }
- //if all Interior & Exterior doors closed
- if( isInteriorClosed && isExteriorClosed )
- {
- LightColorChanger( false, airlockLightName);
- }
- //if all Interior doors closed
- if( isInteriorClosed )
- {
- Unlock( airlockExteriorName );
- }
- //if all Exterior doors closed
- if( isExteriorClosed )
- {
- Unlock( airlockInteriorName );
- }
- }
- }
- }
- bool isDoor( IMyTerminalBlock testBlock )
- {
- var testDoor = testBlock as IMyDoor;
- return testDoor != null;
- }
- void Lock( string lockName )
- {
- //locks all doors with the inputed name
- List<IMyTerminalBlock> lock_door_list = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName( lockName, lock_door_list );
- for(int i = 0 ; i < lock_door_list.Count ; i++)
- {
- IMyDoor lock_door = lock_door_list[i] as IMyDoor;
- if( lock_door.Open )
- lock_door.ApplyAction("Open_Off");
- if( lock_door.OpenRatio == 0 && lock_door.GetValue<bool>("OnOff") == true )
- lock_door.ApplyAction("OnOff_Off");
- }
- Echo(lockName + " is locked");
- }
- void Unlock( string unlockName )
- {
- //unlocks all doors with inputed name
- List<IMyTerminalBlock> unlock_door_list = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName( unlockName, unlock_door_list );
- for(int i = 0 ; i < unlock_door_list.Count ; i++)
- {
- IMyDoor unlock_door = unlock_door_list[i] as IMyDoor;
- if( unlock_door.GetValue<bool>("OnOff") == false )
- unlock_door.ApplyAction("OnOff_On");
- }
- Echo(unlockName + " is unlocked");
- }
- void LightColorChanger( bool alarm, string lightName )
- {
- //applies our status colors to the airlock lights
- if( alarm )
- {
- lightColor = alarmColor;
- lightBlinkLength = alarmBlinkLength;
- }else{
- lightColor = regularColor;
- lightBlinkLength = regularBlinkLength;
- }
- List<IMyTerminalBlock> listLights = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName( lightName, listLights );
- for( int i=0 ; i< listLights.Count ; i++)
- {
- var Light = listLights[i] as IMyLightingBlock;
- Light.SetValue( "Color", lightColor );
- Light.SetValue( "Blink Lenght", lightBlinkLength );
- Light.SetValue( "Blink Interval", blinkInterval );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment