-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path气密门.cs
370 lines (367 loc) · 16.2 KB
/
气密门.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
private const string GroupAll="AirLocker";
private const string Group_Inner_All="Inner";
private const string Group_Outer_All="Outer";
private const string AtomsphereOxygonDetector_NM="Atomsphere Oxygon Detector";
private const string Inner_Dpd="Danger";
private const string Inner_Pd="Arrow";
private const string Outer_Dpd="Cross";
private const string Outer_Pd="Arrow";
private const string AutoCloseDoor_NM="AutoClose";
private static readonly string GroupAirlockerVent_NM=$"{GroupAll} Vent";
private static readonly string GroupInner=$"{GroupAll} {Group_Inner_All}";
private static readonly string GroupOuter=$"{GroupAll} {Group_Outer_All}";
private static string Cmd_TriggerRoomState="TriggerRoomState";
Program()
{
oxygonManager = new OxygonManager(GridTerminalSystem);
AirLockerSys.AtomsphereOxygonDetector = GridTerminalSystem.GetBlockWithName(AtomsphereOxygonDetector_NM) as IMyAirVent;
if(AirLockerSys.AtomsphereOxygonDetector == null)
throw new Exception("No Atomsphere Oxygon Detector Refered!");
GetGroups();
Runtime.UpdateFrequency = UpdateFrequency.Update10 | UpdateFrequency.Update100;
}
void Main(string argument, UpdateType updateSource)
{
if(updateSource.HasFlag(UpdateType.Terminal) || updateSource.HasFlag(UpdateType.Trigger))
SetConsoleCommade(argument);
else if(updateSource.HasFlag(UpdateType.Update10))
{
RoomAirManager();
AutoCloseDoorRunner();
oxygonManager.Running();
}
}
void GetGroups()
{
string[] GroupNames=Me.CustomData.Split(new string[]{"\n","\r",",",";","." },StringSplitOptions.RemoveEmptyEntries);
if(GroupNames == null)
throw new Exception("No Air Lock Room Need to be Managed!");
foreach(var GroupName in GroupNames)
{
Get_AirLockerGroup(GroupName);
AutoCloseDoor_GroupCreater(GroupName);
}
}
void Get_AirLockerGroup(string GroupName)
{
if(!GroupName.Contains($"-{GroupAll}"))
return;
GroupName = GroupName.Replace(" ", "");
GroupName = GroupName.Replace("\t", "");
GroupName = GroupName.Replace($"-{GroupAll}", "");
var RoomEquipmentGroup = GridTerminalSystem.GetBlockGroupWithName(GroupName);
if(RoomEquipmentGroup == null)
throw new Exception("No Air Locker Group Refered!");
var AirLocker=new AirLockerSys(RoomEquipmentGroup);
AirLockers.Add(AirLocker);
SetConsoleCommade += (string cmd) => { if(!cmd.StartsWith(AirLocker.Name)) return; string _cmd= cmd.TrimStart(AirLocker.Name.ToArray()); _cmd = _cmd.TrimStart(' '); AirLocker.SetConsoleCommade(_cmd); };
RoomAirManager += () => { AirLocker.RoomAirManager(); };
}
void AutoCloseDoor_GroupCreater(string GroupName)
{
if(!GroupName.Contains($"-{AutoCloseDoor_NM}"))
return;
GroupName = GroupName.Replace(" ", "");
GroupName = GroupName.Replace("\t", "");
GroupName = GroupName.Replace($"-{AutoCloseDoor_NM}", "");
var RoomEquipmentGroup = GridTerminalSystem.GetBlockGroupWithName(GroupName);
if(RoomEquipmentGroup == null)
throw new Exception("No Auto Close Door Group Refered!");
var doors=new List<IMyDoor>();
RoomEquipmentGroup.GetBlocksOfType(doors);
foreach(var door in doors)
{
var door_auto=new AutoCloseDoor(door);
AutoCloseDoorRunner += () => { door_auto.Running(); };
AutoClosers.Add(door_auto);
}
}
delegate void ActionRef<T>(ref T value);
delegate void ActionRef<T1, T2>(T1 value1, ref T2 value2);
public enum AirLockState { Idle, Start, Pressure, Depressure, Init }
private readonly List<AirLockerSys> AirLockers=new List<AirLockerSys>();
private readonly List<AutoCloseDoor> AutoClosers=new List<AutoCloseDoor>();
private OxygonManager oxygonManager;
private Action<string> SetConsoleCommade=(string cmd)=>{ };
private Action RoomAirManager=()=>{ };
private Action AutoCloseDoorRunner=()=>{ };
class OxygonManager
{
private float Oxygon_Total;
private float Oxygon_Current;
public OxygonManager(IMyGridTerminalSystem GridTerminalSystem)
{
List<IMyGasTank> OxygonTanks=new List<IMyGasTank>();
GridTerminalSystem.GetBlocksOfType(OxygonTanks, (IMyGasTank tank) => { return ( tank.BlockDefinition.SubtypeId == "OxygenTankSmall" || tank.BlockDefinition.SubtypeId == "" ); });
foreach(var tank in OxygonTanks)
{
Statistic_Oxygon_Total += (ref float Total) => { Total += tank.Capacity; };
Statistic_Oxygon_Current += (ref float Total) => { Total += ( tank.Capacity * (float) tank.FilledRatio ); };
}
List<IMyGasGenerator> GasGenerators=new List<IMyGasGenerator>();
GridTerminalSystem.GetBlocksOfType(GasGenerators);
foreach(var GasGenerator in GasGenerators)
{
Enabled_H2O2Generator += (bool OnOff) => { GasGenerator.Enabled = OnOff; };
}
}
public void Running()
{
Oxygon_Total = 0;
Oxygon_Current = 0;
Statistic_Oxygon_Total(ref Oxygon_Total);
Statistic_Oxygon_Current(ref Oxygon_Current);
Enabled_H2O2Generator(( Oxygon_Current / Oxygon_Total ) < 0.8f);
}
private ActionRef<float> Statistic_Oxygon_Total=(ref float Total)=>{ };
private ActionRef<float> Statistic_Oxygon_Current=(ref float Total)=>{ };
private Action<bool> Enabled_H2O2Generator=(bool OnOff)=>{ };
}
class AirLockerSys
{
public AirLockerSys(IMyBlockGroup RoomEquipmentGroup)
{
Name = RoomEquipmentGroup.Name;
List<IMyDoor> Doors=new List<IMyDoor>();
RoomEquipmentGroup.GetBlocksOfType(Doors);
foreach(var door in Doors)
{
DoorGroupAction += (string GroupName, bool Open) => { if(!door.CustomName.Contains(GroupName)) return; if(Open) door.OpenDoor(); else door.CloseDoor(); };
DoorGroupActionTrigger += (string GroupName) => { if(!door.CustomName.Contains(GroupName)) return; door.ToggleDoor(); };
DoorGroupLocker += (string GroupName, bool Lock) => { if(!door.CustomName.Contains(GroupName)) return; door.Enabled = !Lock; };
if(door.CustomName.Contains(GroupInner))
{
Room_InnerDoorState_Open += (ref bool Open) => { Open = Open && ( door.Status == DoorStatus.Open ); };
Room_InnerDoorState_Close += (ref bool Close) => { Close = Close && ( door.Status == DoorStatus.Closed ); };
}
else if(door.CustomName.Contains(GroupOuter))
{
Room_OuterDoorState_Open += (ref bool Open) => { Open = Open && ( door.Status == DoorStatus.Open ); };
Room_OuterDoorState_Close += (ref bool Close) => { Close = Close && ( door.Status == DoorStatus.Closed ); };
}
}
List<IMyAirVent> AirVentsLocker=new List<IMyAirVent>();
RoomEquipmentGroup.GetBlocksOfType(AirVentsLocker, (IMyAirVent vent) => vent.CustomName.Contains(GroupAirlockerVent_NM));
stateShow = new StateShowSys(RoomEquipmentGroup);
foreach(var vent in AirVentsLocker)
{
Room_CanPressurize += (ref bool CanPressurize) => { CanPressurize = ( CanPressurize && vent.CanPressurize ); };
Room_Pressurize += (bool Pressurize) => { vent.Depressurize = !Pressurize; };
Room_AirVentOnOff += (bool OnOff) => { vent.Enabled = OnOff; };
Room_Pressurized += (ref bool Pressurized) => { Pressurized = ( Pressurized && ( vent.GetOxygenLevel() > 0.9f ) ); };
Room_Depressurized += (ref bool Depressurized) => { Depressurized = ( Depressurized && ( VRageMath.MathHelper.RoundOn2(vent.GetOxygenLevel()) <= 0.00f ) ); };
}
DoorGroupLocker(GroupAll, false);
DoorGroupAction(GroupAll, false);
RoomState = AirLockState.Init;
}
public void SetConsoleCommade(string cmd)
{
if(RoomState != AirLockState.Idle || CommadeLines.Count != 0) return;
string[] cmdline=cmd.Split(Splites,StringSplitOptions.RemoveEmptyEntries);
if(cmdline == null) return;
CommadeLines.AddArray(cmdline);
}
public void RoomAirManager()
{
if(AtomsphereOxygonDetector.GetOxygenLevel() > 0.75f)
{
Room_AirVentOnOff(false);
DoorGroupLocker(GroupAll, false);
stateShow.SetImage_InnerLcds(Inner_Pd);
stateShow.SetImage_OuterLcds(Outer_Pd);
DoorManage();
}
else
{
bool Pressureable=true;
bool Pressurized=true;
bool Depressurized=true;
bool open_outer=true;
bool open_inner=true;
bool close_outer=true;
bool close_inner=true;
Room_CanPressurize(ref Pressureable);
Room_Pressurized(ref Pressurized);
Room_Depressurized(ref Depressurized);
Room_InnerDoorState_Open(ref open_inner);
Room_OuterDoorState_Open(ref open_outer);
Room_InnerDoorState_Close(ref close_inner);
Room_OuterDoorState_Close(ref close_outer);
Room_AirVentOnOff(true);
if(Pressurized)
stateShow.SetImage_InnerLcds(Inner_Pd);
else
stateShow.SetImage_InnerLcds(Inner_Dpd);
if(Depressurized)
stateShow.SetImage_OuterLcds(Outer_Pd);
else
stateShow.SetImage_OuterLcds(Outer_Dpd);
if(RoomState == AirLockState.Init)
{
DoorGroupLocker(GroupAll, false);
DoorGroupAction(GroupAll, false);
Room_Pressurize(true);
if(close_inner && close_outer)
RoomState = AirLockState.Idle;
return;
}
switch(RoomState)
{
case AirLockState.Start:
if(!Pressurized)
{
DoorGroupLocker(GroupOuter, false);
DoorGroupAction(GroupOuter, false);
if(close_outer)
{
RoomState = AirLockState.Pressure;
DoorGroupLocker(GroupOuter, true);
}
}
else
{
DoorGroupLocker(GroupInner, false);
DoorGroupAction(GroupInner, false);
if(close_inner)
{
RoomState = AirLockState.Depressure;
DoorGroupLocker(GroupInner, true);
}
}
return;
case AirLockState.Pressure:
Room_Pressurize(true);
if(Pressurized)
RoomState = AirLockState.Idle;
return;
case AirLockState.Depressure:
Room_Pressurize(false);
if(Depressurized)
RoomState = AirLockState.Idle;
return;
default:
DoorGroupLocker(GroupInner, !Pressurized);
DoorGroupLocker(GroupOuter, !Depressurized);
break;
}
DoorManage();
}
}
private void DoorManage()
{
if(CommadeLines.Count <= 0) return;
if(CommadeLines[0] == Cmd_TriggerRoomState)
{
RoomState = AirLockState.Start;
CommadeLines.RemoveAt(0);
return;
}
else
{
switch(CommadeLines[0])
{
case "Open": DoorGroupAction(CommadeLines[1], true); break;
case "Close": DoorGroupAction(CommadeLines[1], false); break;
case "Toggle": DoorGroupActionTrigger(CommadeLines[1]); break;
case "Lock": DoorGroupLocker(CommadeLines[1], true); break;
case "Unlock": DoorGroupLocker(CommadeLines[1], false); break;
default: break;
}
CommadeLines.Clear();
return;
}
}
public AirLockState RoomState { get; private set; } = AirLockState.Idle;
public string Name { get; private set; }
public static IMyAirVent AtomsphereOxygonDetector;
public readonly Action<string,bool> DoorGroupAction=(string GroupName,bool Open)=>{ };
public readonly Action<string> DoorGroupActionTrigger=(string GroupName)=>{ };
public readonly Action<string,bool> DoorGroupLocker=(string GroupName,bool Lock)=>{ };
private readonly Action<bool> Room_Pressurize=(bool Pressurize)=>{ };
private readonly Action<bool> Room_AirVentOnOff=(bool OnOff)=>{ };
private readonly StateShowSys stateShow;
private readonly ActionRef<bool> Room_InnerDoorState_Open=(ref bool Open)=>{ Open=(Open&&true); };
private readonly ActionRef<bool> Room_OuterDoorState_Open=(ref bool Open)=>{ Open=(Open&&true); };
private readonly ActionRef<bool> Room_InnerDoorState_Close=(ref bool Close)=>{ Close=(Close&&true); };
private readonly ActionRef<bool> Room_OuterDoorState_Close=(ref bool Close)=>{ Close=(Close&&true); };
private readonly ActionRef<bool> Room_CanPressurize=(ref bool CanPressurize)=>{CanPressurize=(CanPressurize&&true); };
private readonly ActionRef<bool> Room_Pressurized=(ref bool Pressurized)=>{Pressurized=(Pressurized&&true); };
private readonly ActionRef<bool> Room_Depressurized=(ref bool Depressurized)=>{Depressurized=(Depressurized&&true); };
private readonly List<string> CommadeLines=new List<string>();
}
class StateShowSys
{
public StateShowSys(IMyBlockGroup RoomEquipmentGroup)
{
List<IMyButtonPanel> Buttons=new List<IMyButtonPanel>();
RoomEquipmentGroup.GetBlocksOfType(Buttons);
List<IMyTextPanel> TextPanels=new List<IMyTextPanel>();
RoomEquipmentGroup.GetBlocksOfType(TextPanels);
foreach(var Lcd in Buttons)
{
if(Lcd is IMyTextSurfaceProvider)
{
if(( Lcd as IMyTextSurfaceProvider ).SurfaceCount > 0)
{
if(Lcd.CustomName.Contains(Group_Inner_All))
SetImage_InnerLcds += (string ID) =>
{
var surface = (Lcd as IMyTextSurfaceProvider).GetSurface(0);
surface.ClearImagesFromSelection();
surface.AddImageToSelection(ID);
};
else if(Lcd.CustomName.Contains(Group_Outer_All))
SetImage_OuterLcds += (string ID) =>
{
var surface = (Lcd as IMyTextSurfaceProvider).GetSurface(0);
surface.ClearImagesFromSelection();
surface.AddImageToSelection(ID);
};
}
}
}
foreach(var Lcd in TextPanels)
{
if(Lcd.CustomName.Contains(Group_Inner_All))
SetImage_InnerLcds += (string ID) =>
{
Lcd.ClearImagesFromSelection();
Lcd.AddImageToSelection(ID);
};
else if(Lcd.CustomName.Contains(Group_Outer_All))
SetImage_OuterLcds += (string ID) =>
{
Lcd.ClearImagesFromSelection();
Lcd.AddImageToSelection(ID);
};
}
}
public readonly Action<string> SetImage_InnerLcds=(string ID)=>{ };
public readonly Action<string> SetImage_OuterLcds=(string ID)=>{ };
}
class AutoCloseDoor
{
public readonly IMyDoor Door;
private int Timer;
private const int LastTime=30;
public AutoCloseDoor(IMyDoor Door)
{
this.Door = Door;
Timer = LastTime;
Door.CloseDoor();
}
public void Running()
{
if(Door.Status == DoorStatus.Closed)
{
Timer = LastTime;
return;
}
if(Timer > 0)
Timer--;
else
Door.CloseDoor();
}
}
private static readonly string[] Splites=new string[]{" ","\t" };