-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPressureChamberControlSystem.cs
227 lines (198 loc) · 7.91 KB
/
PressureChamberControlSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using Sandbox.ModAPI.Ingame;
public class Class1
{
IMyGridTerminalSystem GridTerminalSystem = null;
#region CodeEditor
//************************ Pressure Chamber Control System ************************\\
// Build a small room with two doors and one vent.
// Build a Timer Block and configure it so that the first action points to this Programmable Block -> "Run".
// Enter the names of the blocks in the variables below.
// For an easy use, you should build 3 Button Panels. One next to each door (outside) plus one in the center
// of the Pressure Chamber.
//***** Configuration *****\\
//Name of the Inner Door
const string DOOR_INSIDE = "Door (Pressure Chamber Inside)";
//Name of the Outer Door
const string DOOR_OUTSIDE = "Door (Pressure Chamber Outside)";
//Name of the Vent
const string VENT = "Air Vent (Pressure Chamber)";
//Name of the Timer block
const string TIMER_BLOCK = "Timer Block (Pressure Chamber)";
//Name of the LCD Panel the PB will print its messages onto. (OPTIONAL)
const string LOG_PANEL = "LCD Log (Pressure Chamber)";
//Set this to false, if you don't want the PB to power off doors.
//This prevents manual open/close and accidental loss of air.
const bool DISABLE_DOORS = true;
//Don't wait longer than 3 seconds (180 ticks) for depressurization.
//Low values will result in a loss of air if the vent is too slow or your Oxygen Tanks are full.
const int MAX_WAIT_DEPRESSURIZING = 180;
//Don't wait longer than 3 seconds (180 ticks) for pressurization.
//Low values probably won't harm you, as you will receive oxygen
//from the main room if the door opens.
//This is mostly useful if your tanks are empty.
//Set to 0 if you want the inner door to open immediately.
const int MAX_WAIT_PRESSURIZING = 180;
//The number of frames until the door is fully closed.
//You probably don't need to change this.
const int DOOR_ANIMATION_DURATION = 70;
//***** DO NOT MODIFY BELOW THIS LINE *****\\
const string OPEN_DOOR = "Open_On";
const string CLOSE_DOOR = "Open_Off";
const string ENABLE = "OnOff_On";
const string DISABLE = "OnOff_Off";
const string DE_PRESSURIZE = "Depressurize_On";
const string PRESSURIZE = "Depressurize_Off";
public Func<bool> wait = null;
public List<Func<Func<bool>>> actions = new List<Func<Func<bool>>>();
public int currentAction = 0;
public IMyTimerBlock timerBlock = null;
public IMyTextPanel logPanel = null;
public void nextTick()
{
var tb = GridTerminalSystem.GetBlockWithName(TIMER_BLOCK) as IMyTimerBlock;
timerBlock.ApplyAction("TriggerNow");
}
public void c_log(string message)
{
if (logPanel == null) { return; }
if (message == null)
{
logPanel.WritePublicText("");
}
else
{
logPanel.WritePublicText(message + "\n", true);
}
logPanel.ShowPublicTextOnScreen();
}
public Func<bool> waitForTicks(long ticks)
{
long count = 0;
return () =>
{
return count++ >= ticks;
};
}
public Func<bool> waitForPressure(IMyAirVent vent, bool pressurizing)
{
Func<bool> maxWait = waitForTicks(pressurizing ? MAX_WAIT_PRESSURIZING : MAX_WAIT_DEPRESSURIZING);
return () =>
{
double ox = vent.GetOxygenLevel();
if (maxWait()) {
c_log((pressurizing ? "" : "de")+"pressurization_wait_timout ("+(ox*100)+"% oxygen lost)");
return true;
}
if (pressurizing)
{
if (ox >= 0.95)
{
return true;
}
}
else
{
if (ox <= 0.0001)
{
return true;
}
}
return false;
};
}
public class BlockAction
{
protected readonly string action;
protected readonly Func<bool> wait;
protected readonly IMyTerminalBlock[] blocks;
public BlockAction(string action, Func<bool> wait, params IMyTerminalBlock[] blocks)
{
this.action = action;
this.wait = wait;
this.blocks = blocks;
}
public Func<bool> execute()
{
foreach (IMyTerminalBlock block in blocks)
{
block.ApplyAction(action);
}
return wait;
}
}
public Func<Func<bool>> logWrapper(string message, Func<Func<bool>> action)
{
return () =>
{
c_log("Executing: " + message);
return action();
};
}
public void addDoorAction(string msg, Func<Func<bool>> action)
{
if (DISABLE_DOORS)
{
actions.Add(logWrapper(msg, action));
}
}
void Main()
{
if (wait != null)
{
if (!wait())
{
nextTick();
return;
}
wait = null;
}
if (actions.Count == 0)
{
var logPanelBlock = GridTerminalSystem.GetBlockWithName(LOG_PANEL);
logPanel = (logPanelBlock != null && logPanelBlock is IMyTextPanel)
? (IMyTextPanel)logPanelBlock
: null;
timerBlock = GridTerminalSystem.GetBlockWithName(TIMER_BLOCK) as IMyTimerBlock;
var door_outside = GridTerminalSystem.GetBlockWithName(DOOR_OUTSIDE) as IMyDoor;
var door_inside = GridTerminalSystem.GetBlockWithName(DOOR_INSIDE) as IMyDoor;
var vent = GridTerminalSystem.GetBlockWithName(VENT) as IMyAirVent;
c_log(null);
addDoorAction("enable_doors", new BlockAction(ENABLE, waitForTicks(0), door_outside, door_inside).execute);
actions.Add(logWrapper("close_doors", new BlockAction(CLOSE_DOOR, waitForTicks(DOOR_ANIMATION_DURATION), door_outside, door_inside).execute));
addDoorAction("disable_doors", new BlockAction(DISABLE, null, door_outside, door_inside).execute);
actions.Add(logWrapper("enable_vent", new BlockAction(ENABLE, waitForTicks(0), vent).execute));
if (door_outside.Open)
{
actions.Add(logWrapper("pressurize", new BlockAction(PRESSURIZE, waitForPressure(vent, true), vent).execute));
addDoorAction("enable_inner_door", new BlockAction(ENABLE, waitForTicks(0), door_inside).execute);
actions.Add(logWrapper("disable_vent", new BlockAction(DISABLE, waitForTicks(0), vent).execute));
actions.Add(logWrapper("open_inner_door", new BlockAction(OPEN_DOOR, waitForTicks(DOOR_ANIMATION_DURATION), door_inside).execute));
addDoorAction("disable_inner_door", new BlockAction(DISABLE, null, door_inside).execute);
}
else
{
actions.Add(logWrapper("depressurize", new BlockAction(DE_PRESSURIZE, waitForPressure(vent, false), vent).execute));
addDoorAction("enable_outer_door", new BlockAction(ENABLE, waitForTicks(0), door_outside).execute);
actions.Add(logWrapper("disable_vent", new BlockAction(DISABLE, waitForTicks(0), vent).execute));
actions.Add(logWrapper("open_outer_door", new BlockAction(OPEN_DOOR, waitForTicks(DOOR_ANIMATION_DURATION), door_outside).execute));
addDoorAction("disable_outer_door", new BlockAction(DISABLE, null, door_outside).execute);
}
}
while (wait == null && currentAction < actions.Count)
{
wait = actions[currentAction++]();
}
if (currentAction < actions.Count)
{
nextTick();
}
else
{
currentAction = 0;
actions.Clear();
}
}
#endregion
}