Space Engineers

Space Engineers

EasyAPI
LukeStrike 2016 年 6 月 12 日 下午 9:12
EasyAPI By examples (01): Automatic doors
This is a simple example to show us how EasyAPI (code name ESY) is powerful. What we want here is when an outer door is open, we close all the other outer and inner doors. To keep O2 inside the whole ship. And when an inner door is open, we want to be sure that all the other doors are also closed. For sure you can add pieces of code to lock them, start warnings etc (in case of attack thru sensors) but it is not the point here.

Code will follow.
LKS
最後修改者:LukeStrike; 2016 年 6 月 24 日 下午 2:50
< >
目前顯示第 1-5 則留言,共 5
LukeStrike 2016 年 6 月 12 日 下午 9:17 
Tested with:

/************************************************************************************
EasyAPI - Documentation: https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=381043
*************************************************************************************/
// This script was made for the LKS_Hydros but can be applied to similar ships
// That means just one group for outer doors, one for inner doors, and one for all the doors
// Feel free to rename the groups accordingly.
//

public static string AIRLOCK_DOORS = "HY 1- Doors"; // your doors group
public static string AIRLOCK_OUTER_DOORS = "HY 1- Doors (Outer)"; // your outer doors group
public static string AIRLOCK_INNER_DOORS = "HY 1- Doors (Inner)"; // your inner doors group
public static bool AIRLOCK_AUTOCLOSE = true; // set it to true if you want the door closing automaticaly after a delay
public static long AIRLOCK_AUTOCLOSEDELAY = 3 * EasyAPI.Seconds; // autoclose delay set to 3 seconds, change it if you want

public class Example : EasyAPI
{
public Example(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime)
{
// if an outer door is open we close all the other (outer/inner) doors
Blocks.InGroupsNamed(AIRLOCK_OUTER_DOORS).AddEvent(
delegate(EasyBlock thisdoor)
{
return thisdoor.Open();
},
delegate(EasyBlock thisdoor)
{
Blocks.InGroupsNamed(AIRLOCK_DOORS).Minus(thisdoor).ApplyAction("Open_Off");
if(AIRLOCK_AUTOCLOSE) // autoclose the outer door that was open, if any
{
In(AIRLOCK_AUTOCLOSEDELAY, delegate()
{
thisdoor.ApplyAction("Open_Off");
});
}

return true;
},
true
);

// if an inner door is open we close all the other (outer/inner) doors
Blocks.InGroupsNamed(AIRLOCK_INNER_DOORS).AddEvent(
delegate(EasyBlock thisdoor)
{
return thisdoor.Open();
},
delegate(EasyBlock thisdoor)
{
Blocks.InGroupsNamed(AIRLOCK_DOORS).Minus(thisdoor).ApplyAction("Open_Off");
if(AIRLOCK_AUTOCLOSE) // autoclose the inner door that was open, if any
{
In(AIRLOCK_AUTOCLOSEDELAY, delegate()
{
thisdoor.ApplyAction("Open_Off");
});
}

return true;
},
true
);
}
}

/*********************************************/
/*** Advanced users only beyond this point ***/
/*********************************************/
...
最後修改者:LukeStrike; 2018 年 11 月 7 日 上午 5:19
LukeStrike 2016 年 6 月 12 日 下午 9:19 
As you can see the door you just opened can be also automaticaly closed behind you after a delay. Just change the AIRLOCK_AUTOCLOSE and AIRLOCK_AUTOCLOSEDELAY the way you want.

Enjoy,
LKS
LukeStrike 2016 年 6 月 12 日 下午 9:32 
Of course this is just an example. Maybe you do not want to close all the outer doors when opening an inner door (because outer doors have their own airlock system for example). Up to you to adapt this code to your needs ;)

LKS
Kri 2016 年 6 月 13 日 上午 6:04 
Here's another spin at the airlock doors script. It is based on the example door close script on EasyAPI GitHub page.

By default it will auto close any door after 2-3sec.
If you name two or more doors with exactly same name this script will make sure only one door of the same name can be open at one time by turning off the other(s).

public class Airlocks : EasyAPI { EasyBlocks doors; public Airlocks(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime) { DoorAutoClose(); } public void DoorAutoClose() { doors = Blocks.OfTypeLike("Door").NotOfType("Airtight Hangar Door"); In(1 * EasyAPI.Seconds, delegate() { // In one second, create the events. doors.AddEvent( delegate(EasyBlock block) { // When a door is opened return block.Open(); }, doorOpened, true // only trigger event when the condition (door open) goes from false to true ).AddEvent( delegate(EasyBlock block) { return !block.Open(); }, doorClosed, true ); }); } public bool doorOpened(EasyBlock block) { // Do the following In(2 * EasyAPI.Seconds, delegate() { // In 2 seconds block.ApplyAction("Open_Off"); // close the door }); EasyBlocks paired_doors = doors.Named(block.Name()) - block; paired_doors.Off(); return true; } public bool doorClosed(EasyBlock block) { EasyBlocks paired_doors = doors.Named(block.Name()) - block; paired_doors.On(); return true; } } Airlocks state; void Main(string argument) { if(state == null) { state = new Airlocks(GridTerminalSystem, Me, Echo, ElapsedTime); } state.Tick(100 * EasyAPI.Milliseconds, argument); }
最後修改者:Kri; 2016 年 6 月 13 日 上午 6:53
LukeStrike 2018 年 11 月 7 日 上午 5:37 
In my example this is the same function for either inner/outer doors ... of course you can simplify the code and apply it to the whole group AIRLOCK_DOORS but I decided to split inner/outer doors in case of you want to add some functions to them (activate inner turrets, trigger an alarm, etc)

This is your code, not mine ;)
LKS
< >
目前顯示第 1-5 則留言,共 5
每頁顯示: 1530 50