-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirlock.cs
1549 lines (1425 loc) · 61.2 KB
/
Airlock.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System;
using VRage.Collections;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ObjectBuilders.Definitions;
using VRage.Game;
using VRage;
using VRageMath;
namespace IngameScript
{
partial class Program
{
/// <summary>
/// Represents a single airlock assembly and all of its component blocks
/// and configuration properties.
/// </summary>
public class Airlock
{
#region Constants, Enums, Statics
public const string ROLE_NAME_OUTERDOOR = "OuterDoor";
public const string ROLE_NAME_INNERDOOR = "InnerDoor";
public const string ROLE_NAME_FILLVENT = "FillVent";
public const string ROLE_NAME_DRAINVENT = "DrainVent";
public const string ROLE_NAME_DRAINTANK = "DrainTank";
public const string ROLE_NAME_HABBAROMETER = "HabitatBarometer";
public const string ROLE_NAME_VACBAROMETER = "VacuumBarometer";
public const string ROLE_NAME_PRESSURELIGHT = "PressureLight";
/// <summary>
/// The possible modes of operation (i.e., target states) an Airlock
/// may be in.
/// </summary>
/// <remarks>
/// <para>
/// An Airlock in a given mode is not guaranteed to be in exactly
/// the state described for that mode. But it should be working to
/// get there.
/// </para>
/// </remarks>
public enum Mode
{
/// <summary>
/// Target pressure matches the Habitat. InnerDoors open,
/// OuterDoors locked.
/// </summary>
OpenToHabitat = 1
,
/// <summary>
/// Target pressure matches the Vacuum. OuterDoors open,
/// InnerDoors locked.
/// </summary>
OpenToVacuum = 2
,
/// <summary>
/// Target pressure 0%, all doors locked.
/// </summary>
/// <remarks>
/// <para>
/// This is not normal operation. It's included in case a
/// defense-minded engineer wants to be able to trap intruders
/// between locked doors with nothing to breathe. And, hey, no
/// one said you can't line your airlock chambers with Interior
/// Turrets...
/// </para>
/// </remarks>
LockDown = 99
}
/// <summary>
/// The possible target pressure values for the airlock chamber.
/// </summary>
public enum PressureTarget
{
/// <summary>Zero pressure</summary>
Empty
/// <summary>The same pressure reported by the <see cref="VacuumBarometers"/></summary>
, Vacuum
/// <summary>The same pressure reported by the <see cref="HabitatBarometers"/></summary>
, Habitat
/// <summary>100% pressure</summary>
, Full
}
/// <summary>
/// The possible target states for <see cref="InnerDoors"/> and <see cref="OuterDoors"/>.
/// </summary>
public enum DoorTarget
{
/// <summary>Passable and manually operable</summary>
Open
/// <summary>Passable and NOT manually operable</summary>
, LockedOpen
/// <summary>Impassable, airtight, and manually operable</summary>
, Closed
/// <summary>Impassable, airtight, and NOT manually operable</summary>
, LockedClosed
}
private readonly Color COLOR_SAFE = Color.Green;
private readonly Color COLOR_UNSAFE = Color.Red;
private readonly Color COLOR_TRANSITION = Color.Yellow;
private const float TRANSITION_BLINK_INTERVAL_SECONDS = 0.25f;
private const float TRANSITION_BLINK_LENGTH = 50.0f;
#endregion Constants, Enums, Statics
#region Basic Properties
/// <summary>
/// Name of the airlock
/// </summary>
/// <remarks>
/// This is both functional, as it appears in the configuration data
/// to associate separate blocks into a unified airlock, and
/// cosmetic, as it may appear in-game on informational
/// readouts.
/// </remarks>
public string Name { get; set; }
private Mode _targetMode;
/// <summary>
/// The Airlock's Mode of operation. It's either there, or it's
/// working toward it.
/// </summary>
/// <remarks>
/// <para>
/// Setting this will also set <see cref="CurrentPressureTarget"/>,
/// <see cref="CurrentInnerDoorsTarget"/>, and <see
/// cref="CurrentOuterDoorsTarget"/>.
/// </para>
/// </remarks>
public Mode TargetMode {
get { return this._targetMode; }
private set {
this._targetMode = value;
switch (this._targetMode)
{
case Mode.OpenToHabitat:
this.CurrentPressureTarget = PressureTarget.Habitat;
this.CurrentInnerDoorsTarget = DoorTarget.Open;
this.CurrentOuterDoorsTarget = DoorTarget.LockedClosed;
break;
case Mode.OpenToVacuum:
this.CurrentPressureTarget = PressureTarget.Vacuum;
this.CurrentInnerDoorsTarget = DoorTarget.LockedClosed;
this.CurrentOuterDoorsTarget = DoorTarget.Open;
break;
case Mode.LockDown:
this.CurrentPressureTarget = PressureTarget.Empty;
this.CurrentInnerDoorsTarget = DoorTarget.LockedClosed;
this.CurrentOuterDoorsTarget = DoorTarget.LockedClosed;
break;
default:
break;
}
this._lastTransitionEnded = DateTime.MinValue;
this.SetPressureLightsTransition();
}
}
private Mode? _queuedMode = null;
/// <summary>
/// The Airlock's next Mode of operation. It will transition to this
/// after <see cref="TargetMode"/> has been reached and enough time
/// has passed.
/// </summary>
public Mode? QueuedMode
{
get { return this._queuedMode; }
private set { this._queuedMode = value; }
}
private DateTime _lastTransitionEnded = DateTime.MinValue;
private DateTime _doNotTransitionBefore = DateTime.MinValue;
private PressureTarget? _currentPressureTarget;
/// <summary>
/// The pressure that the Airlock is currently working toward, or
/// <code>null</code> if the current pressure is acceptable.
/// </summary>
public PressureTarget? CurrentPressureTarget
{
get { return this._currentPressureTarget; }
private set
{
this._currentPressureTarget = value;
this.EnableFillVents(); // If something disabled the FillVents, they need to be re-enabled to serve as barometers.
}
}
/// <summary>
/// Returns <code>true</code> if the chamber's pressure is correct
/// for <see cref="CurrentPressureTarget"/>.
/// </summary>
/// <remarks>
/// <para>
/// This is manually set as pressure is evaluated during the update
/// loop. The property itself takes no readings.
/// </para>
/// </remarks>
private bool IsPressureAtTarget { get; set; }
private DoorTarget? _currentInnerDoorsTarget;
/// <summary>
/// The desired DoorTarget of the <see cref="InnerDoors"/>, or
/// <code>null</code> if their current states are acceptable.
/// </summary>
public DoorTarget? CurrentInnerDoorsTarget
{
get { return this._currentInnerDoorsTarget; }
private set { this._currentInnerDoorsTarget = value; }
}
private DoorTarget? _currentOuterDoorsTarget;
/// <summary>
/// The desired DoorTarget of the <see cref="OuterDoors"/>, or
/// <code>null</code> if their current states are acceptable.
/// </summary>
public DoorTarget? CurrentOuterDoorsTarget
{
get { return this._currentOuterDoorsTarget; }
private set { this._currentOuterDoorsTarget = value; }
}
private StringBuilder _badConfigReport = new StringBuilder();
/// <summary>
/// Text explaining how this airlock is misconfigured.
/// </summary>
public string BadConfigReport
{
get
{
return this._badConfigReport.ToString();
}
}
#endregion Basic Properties
#region Mandatory Blocks
#region Mandatory Blocks / OuterDoors
private readonly IList<IMyDoor> _outerDoors;
/// <summary>
/// Returns references to all of this airlock's "outer doors":
/// passable doors that can seal the airlock chamber from the
/// vacuum.
/// </summary>
/// <remarks>
/// <para>Eligible block types are the Airtight Hangar Door, the
/// "Door" (the halves pull straight out into the walls), and the
/// Sliding Door (halves rotate against the walls). Blast Doors are
/// not airtight in any combination, thus not eligible. Exotic
/// constructs that somehow manage to create a seal with
/// piston/rotor subgrids are not recognized by this script.</para>
/// <para>The returned collection is immutable, though the doors
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyDoor> OuterDoors
{
get
{
return new List<IMyDoor>(_outerDoors);
}
}
/// <summary>
/// Associates a door with this airlock as an Outer Door
/// </summary>
public void AddOuterDoor(IMyDoor newDoor)
{
if (null == newDoor) throw new ArgumentNullException("newDoor");
this._outerDoors.Add(newDoor);
}
/// <summary>
/// Enable and open all <see cref="OuterDoors"/>.
/// </summary>
private void OpenOuterDoors()
{
foreach (IMyDoor thisDoor in this.OuterDoors)
{
thisDoor.Enabled = true;
thisDoor.OpenDoor();
}
}
/// <summary>
/// Enable and close all <see cref="OuterDoors"/>.
/// </summary>
private void CloseOuterDoors()
{
foreach (IMyDoor thisDoor in this.OuterDoors)
{
thisDoor.Enabled = true;
thisDoor.CloseDoor();
}
}
/// <summary>
/// Enable all <see cref="OuterDoors"/>.
/// </summary>
private void UnlockOuterDoors()
{
foreach (IMyDoor thisDoor in this.OuterDoors)
{
thisDoor.Enabled = true;
}
}
/// <summary>
/// Disable all <see cref="OuterDoors"/> that are not in the process
/// of opening or closing.
/// </summary>
private void LockOuterDoors()
{
foreach (IMyDoor thisDoor in this.OuterDoors.Where(d =>
d.Status != DoorStatus.Closing
&&
d.Status != DoorStatus.Opening
))
{
thisDoor.Enabled = false;
}
}
#endregion Mandatory Blocks / OuterDoors
#region Mandatory Blocks / InnerDoors
private readonly IList<IMyDoor> _innerDoors;
/// <summary>
/// Returns references to all of this airlock's "inner doors":
/// passable doors that can seal the airlock chamber from the
/// habitat.
/// </summary>
/// <remarks>
/// <para>Eligible block types are the Airtight Hangar Door, the
/// "Door" (the halves pull straight out into the walls), and the
/// Sliding Door (halves rotate against the walls). Blast Doors are
/// not airtight in any combination, thus not eligible. Exotic
/// constructs that somehow manage to create a seal with
/// piston/rotor subgrids are not recognized by this script.</para>
/// <para>The returned collection is immutable, though the doors
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyDoor> InnerDoors
{
get
{
return new List<IMyDoor>(_innerDoors);
}
}
/// <summary>
/// Associates a door with this airlock as an Inner Door
/// </summary>
public void AddInnerDoor(IMyDoor newDoor)
{
if (null == newDoor) throw new ArgumentNullException("newDoor");
this._innerDoors.Add(newDoor);
}
/// <summary>
/// Enable and open all <see cref="InnerDoors"/>.
/// </summary>
private void OpenInnerDoors()
{
foreach (IMyDoor thisDoor in this.InnerDoors)
{
thisDoor.Enabled = true;
thisDoor.OpenDoor();
}
}
/// <summary>
/// Enable and close all <see cref="InnerDoors"/>.
/// </summary>
private void CloseInnerDoors()
{
foreach (IMyDoor thisDoor in this.InnerDoors)
{
thisDoor.Enabled = true;
thisDoor.CloseDoor();
}
}
/// <summary>
/// Enable all <see cref="InnerDoors"/>.
/// </summary>
private void UnlockInnerDoors()
{
foreach (IMyDoor thisDoor in this.InnerDoors)
{
thisDoor.Enabled = true;
}
}
/// <summary>
/// Disable all <see cref="InnerDoors"/> that are not in the process
/// of opening or closing.
/// </summary>
private void LockInnerDoors()
{
foreach (IMyDoor thisDoor in this.InnerDoors.Where(d =>
d.Status != DoorStatus.Closing
&&
d.Status != DoorStatus.Opening
))
{
thisDoor.Enabled = false;
}
}
#endregion Mandatory Blocks / InnerDoors
#region Mandatory Blocks / FillVents
private readonly IList<IMyAirVent> _fillVents;
/// <summary>
/// Returns references to all of this airlock's "fill vents": Air
/// Vents that are connected to main oxygen supplies and NOT to the
/// drain tanks.
/// </summary>
/// <remarks>
/// <para>The returned collection is immutable, though the vents
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyAirVent> FillVents
{
get
{
return new List<IMyAirVent>(_fillVents);
}
}
/// <summary>
/// Associates a vent with this airlock as a Fill Vent
/// </summary>
public void AddFillVent(IMyAirVent newVent)
{
if (null == newVent) throw new ArgumentNullException("newVent");
this._fillVents.Add(newVent);
}
/// <summary>
/// Ensures that all <see cref="FillVents"/> are <see
/// cref="IMyFunctionalBlock.Enabled"/>.
/// </summary>
/// <remarks>
/// <para>
/// FillVents should never become disabled during the normal course
/// of airlock operation. This is called at key moments as a
/// precaution, as the FillVents are crucial as barometers for the
/// chamber. If they get turned off, lots of stuff breaks.
/// </para>
/// </remarks>
private void EnableFillVents()
{
foreach (IMyAirVent thisVent in this.FillVents)
{
thisVent.Enabled = true;
}
}
// Never disable the FillVents. Learned that the hard way.
//private void DisableFillVents()
//{
// foreach (IMyAirVent thisVent in this.FillVents)
// {
// thisVent.Enabled = false;
// }
//}
/// <summary>
/// Set all <see cref="FillVents"/> to pressurize the chamber. Does
/// nothing to <see cref="DrainVents"/>.
/// </summary>
private void PressurizeWithFillVents()
{
this.EnableFillVents();
foreach (IMyAirVent thisVent in this.FillVents)
{
thisVent.Depressurize = false;
}
}
/// <summary>
/// Set all <see cref="FillVents"/> to depressurize the chamber.
/// Does nothing to <see cref="DrainVents"/>.
/// </summary>
private void DepressurizeWithFillVents()
{
this.EnableFillVents();
foreach (IMyAirVent thisVent in this.FillVents)
{
thisVent.Depressurize = true;
}
}
#endregion Mandatory Blocks / FillVents
#region Mandatory Blocks / DrainVents
private readonly IList<IMyAirVent> _drainVents;
/// <summary>
/// Returns references to all of this airlock's "drain vents": Air
/// Vents that are connected to drainage tanks and NOTHING ELSE.
/// </summary>
/// <remarks>
/// <para>The returned collection is immutable, though the vents
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyAirVent> DrainVents
{
get
{
return new List<IMyAirVent>(_drainVents);
}
}
/// <summary>
/// Associates a vent with this airlock as a Drain Vent
/// </summary>
public void AddDrainVent(IMyAirVent newVent)
{
if (null == newVent) throw new ArgumentNullException("newVent");
this._drainVents.Add(newVent);
}
/// <summary>
/// Ensures that all <see cref="DrainVents"/> are <see
/// cref="IMyFunctionalBlock.Enabled"/>.
/// </summary>
private void EnableDrainVents()
{
foreach (IMyAirVent thisVent in this.DrainVents)
{
thisVent.Enabled = true;
}
}
/// <summary>
/// Disables all <see cref="DrainVents"/>.
/// </summary>
/// <remarks>
/// <para>
/// This is more a power-saving measure than anything else.
/// </para>
/// </remarks>
private void DisableDrainVents()
{
foreach (IMyAirVent thisVent in this.DrainVents)
{
thisVent.Enabled = false;
}
}
/// <summary>
/// Set all <see cref="DrainVents"/> to pressurize the chamber. Does
/// nothing to <see cref="FillVents"/>.
/// </summary>
private void PressurizeWithDrainVents()
{
this.EnableDrainVents();
foreach (IMyAirVent thisVent in this.DrainVents)
{
thisVent.Depressurize = false;
}
}
/// <summary>
/// Set all <see cref="DrainVents"/> to depressurize the chamber.
/// Does nothing to <see cref="FillVents"/>.
/// </summary>
private void DepressurizeWithDrainVents()
{
this.EnableDrainVents();
foreach (IMyAirVent thisVent in this.DrainVents)
{
thisVent.Depressurize = true;
}
}
#endregion Mandatory Blocks / DrainVents
#region Mandatory Blocks / DrainTanks
private readonly IList<IMyGasTank> _drainTanks;
/// <summary>
/// Returns references to all of this airlock's "drain tanks": Air
/// Vents that are connected to drainage tanks and NOTHING ELSE.
/// </summary>
/// <remarks>
/// <para>The returned collection is immutable, though the vents
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyGasTank> DrainTanks
{
get
{
return new List<IMyGasTank>(_drainTanks);
}
}
/// <summary>
/// Associates an oxygen tank with this airlock as a Drain Tank
/// </summary>
/// <remarks>
/// IMyGasTank objects are not necessarily oxygen tanks. If
/// <paramref name="newTank"/> is not, an Exception will be thrown.
/// </remarks>
public void AddDrainTank(IMyGasTank newTank)
{
if (null == newTank) throw new ArgumentNullException("newTank");
if (!newTank.IsForOxygen())
{
throw new Exception(
"Attempted to add a non-oxygen tank as an airlock Draink Tank."
+ $"Block: {newTank.CustomName}"
);
}
this._drainTanks.Add(newTank);
}
#endregion Mandatory Blocks / DrainTanks
#region Mandatory Blocks / HabitatBarometers
private readonly IList<IMyAirVent> _habBarometers;
/// <summary>
/// Returns references to all of this airlock's "habitat
/// barometers": Air Vents that face the habitat and are used to
/// read the habitat's oxygen pressure.
/// </summary>
/// <remarks>
/// <para>It should not matter whether these vents are dedicated to
/// the airlock or part of the main life support system. There can
/// be any number of vents in the collection, but the pressure will
/// only ever be read from the first one found to be working.</para>
/// <para>The returned collection is immutable, though the vents
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyAirVent> HabitatBarometers
{
get
{
return new List<IMyAirVent>(_habBarometers);
}
}
/// <summary>
/// Associates a vent with this airlock as a Habitat Barometer
/// </summary>
public void AddHabitatBarometer(IMyAirVent newVent)
{
if (null == newVent) throw new ArgumentNullException("newVent");
this._habBarometers.Add(newVent);
}
#endregion Mandatory Blocks / HabitatBarometers
#region Mandatory Blocks / VacuumBarometers
private readonly IList<IMyAirVent> _vacBarometers;
/// <summary>
/// Returns references to all of this airlock's "vacuum barometers":
/// Air Vents that face the vacuum and are used to read the vacuum's
/// oxygen pressure.
/// </summary>
/// <remarks>
/// <para>It would be normal for these blocks to be detached from
/// all oxygen supplies and mostly non-functional, as they are used
/// ONLY for reading pressure. There can be any number of vents in
/// the collection, but the pressure will only ever be read from the
/// first one found to be working.</para>
/// <para>The returned collection is immutable, though the vents
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyAirVent> VacuumBarometers
{
get
{
return new List<IMyAirVent>(_vacBarometers);
}
}
/// <summary>
/// Associates a vent with this airlock as a Vacuum Barometer
/// </summary>
public void AddVacuumBarometer(IMyAirVent newVent)
{
if (null == newVent) throw new ArgumentNullException("newVent");
this._vacBarometers.Add(newVent);
}
#endregion Mandatory Blocks / VacuumBarometers
#endregion Mandatory Blocks
#region Optional Blocks
#region Optional Blocks / PressureLights
private readonly IList<IMyLightingBlock> _pressureLights;
/// <summary>
/// Returns references to all of this airlock's lights that indicate
/// chamber pressure.
/// </summary>
/// <remarks>
/// <para>The returned collection is immutable, though the lights
/// themselves can be manipulated by reference.</para>
/// </remarks>
public IEnumerable<IMyLightingBlock> PressureLights
{
get
{
return new List<IMyLightingBlock>(_pressureLights);
}
}
private void AddPressureLight(IMyLightingBlock newLight)
{
if (null == newLight) throw new ArgumentNullException("newLight");
this._pressureLights.Add(newLight);
}
private void SetPressureLights(
Color newColor
, float newBlinkIntervalSeconds
, float newBlinkLength
)
{
if (null == newColor) throw new ArgumentNullException("newColor");
//if (newBlinkLength < 0.0f || newBlinkLength > 1.0f)
//{
// throw new ArgumentException("newBlinkLength must be between 0 and 1.", "newBlinkLength");
//}
foreach (IMyLightingBlock thisLight in this.PressureLights)
{
if (thisLight.Color != newColor)
thisLight.Color = newColor;
if (
thisLight.BlinkIntervalSeconds - newBlinkIntervalSeconds > 0.001f
||
thisLight.BlinkIntervalSeconds - newBlinkIntervalSeconds < -0.001f
)
thisLight.BlinkIntervalSeconds = newBlinkIntervalSeconds;
if (
thisLight.BlinkLength - newBlinkLength > 0.001f
||
thisLight.BlinkLength - newBlinkLength < -0.001f
)
thisLight.BlinkLength = newBlinkLength;
}
}
private void SetPressureLightsLow()
{
this.SetPressureLights(COLOR_UNSAFE, 0.0f, 50.0f);
}
private void SetPressureLightsHigh()
{
this.SetPressureLights(COLOR_SAFE, 0.0f, 50.0f);
}
private void SetPressureLightsTransition()
{
this.SetPressureLights(
COLOR_TRANSITION
, TRANSITION_BLINK_INTERVAL_SECONDS
, TRANSITION_BLINK_LENGTH
);
}
#endregion Optional Blocks / PressureLights
#endregion Optional Blocks
#region Constructors and Factory Methods
private Airlock()
{
this._outerDoors = new List<IMyDoor>();
this._innerDoors = new List<IMyDoor>();
this._fillVents = new List<IMyAirVent>();
this._drainVents = new List<IMyAirVent>();
this._drainTanks = new List<IMyGasTank>();
this._habBarometers = new List<IMyAirVent>();
this._vacBarometers = new List<IMyAirVent>();
this._pressureLights = new List<IMyLightingBlock>();
this.TargetMode = Mode.OpenToHabitat;
this.CurrentPressureTarget = null;
this.CurrentInnerDoorsTarget = null;
this.CurrentOuterDoorsTarget = null;
}
/// <summary>
/// Create a new instance of the Airlock class with a given name.
/// </summary>
/// <param name="name"><see cref="Name"/> of the airlock</param>
public Airlock(string name) : this()
{
this.Name = name;
}
/// <summary>
/// Search a collection of blocks and create a dictionary of Airlock
/// objects keyed by Name out of the relevant blocks.
/// </summary>
/// <param name="allBlocks">The collection of blocks. The more this
/// can be narrowed beforehand, the less work this method has to do,
/// but DiscoverAllAirlocks will effectively sort through a
/// completely unfiltered set.</param>
/// <param name="iniParser">Optional reference to a <see
/// cref="MyIni"/> object that will be used to parse blocks'
/// CustomData. If absent, the method will create its own.</param>
/// <param name="iniSectionName">Optional name of the INI section
/// header that appears in a block's CustomData before airlock
/// configuration data. If absent, <see
/// cref="Program.INI_SECTION_NAME"/> will be used.</param>
/// <returns>A dictionary of all discrete Airlock objects found,
/// keyed by their Names.</returns>
/// <remarks>DiscoverAllAirlocks does not check whether the found
/// Airlocks are complete or functional. However, it will throw
/// useful exceptions when there is easily-spotted bad configuration
/// data.</remarks>
public static IDictionary<string, Airlock> DiscoverAllAirlocks(
IEnumerable<IMyTerminalBlock> allBlocks
, MyIni iniParser = null
, string iniSectionName = null
)
{
if (null == iniParser) iniParser = new MyIni();
if (null == iniSectionName) iniSectionName = Program.INI_SECTION_NAME;
// Note the use of CurrentCulture. Airlock names are expected to
// be user-readable, so the code should obey the user's language
// rules. But they should still be case-insensitive as that's
// probably a common assumption.
Dictionary<string, Airlock> foundAirlocks
= new Dictionary<string, Airlock>(StringComparer.CurrentCultureIgnoreCase);
MyIniParseResult parseResult;
string airlockName;
Airlock thisAirlock;
string roleName;
foreach (IMyTerminalBlock thisBlock in allBlocks)
{
if (!iniParser.TryParse(thisBlock.CustomData, iniSectionName, out parseResult))
{
throw new Exception(
$"Failed to parse airlock configuration data."
+ $"\nBlock: {thisBlock.CustomName}"
+ $"\nParse result: {parseResult.ToString()}"
);
}
// Parse the airlock name and get a new or existing reference to the airlock
airlockName = iniParser.Get(iniSectionName, INI_AIRLOCK_KEY).ToString(); // TODO This should not reference Program.INI_AIRLOCK_KEY directly.
if (string.IsNullOrEmpty(airlockName))
{
throw new Exception(
$"Failed to parse airlock configuration data because no airlock name was given."
+ $"\nBlock: {thisBlock.CustomName}"
);
}
if (foundAirlocks.ContainsKey(airlockName))
{
thisAirlock = foundAirlocks[airlockName];
}
else
{
thisAirlock = new Airlock(airlockName);
foundAirlocks.Add(airlockName, thisAirlock);
}
roleName = iniParser.Get(iniSectionName, INI_ROLE_KEY).ToString(); // TODO This should not reference Program.INI_ROLE_KEY directly.
if (string.IsNullOrEmpty(roleName))
{
throw new Exception(
$"Failed to parse airlock configuration data because no role was given."
+ $"\nBlock: {thisBlock.CustomName}"
);
}
bool wrongTypeForRole = false;
switch (roleName)
{
case Airlock.ROLE_NAME_OUTERDOOR:
if (!(thisBlock is IMyDoor))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddOuterDoor(thisBlock as IMyDoor);
break;
case Airlock.ROLE_NAME_INNERDOOR:
if (!(thisBlock is IMyDoor))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddInnerDoor(thisBlock as IMyDoor);
break;
case Airlock.ROLE_NAME_FILLVENT:
if (!(thisBlock is IMyAirVent))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddFillVent(thisBlock as IMyAirVent);
break;
case Airlock.ROLE_NAME_DRAINVENT:
if (!(thisBlock is IMyAirVent))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddDrainVent(thisBlock as IMyAirVent);
break;
case Airlock.ROLE_NAME_DRAINTANK:
if (!(thisBlock is IMyGasTank))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddDrainTank(thisBlock as IMyGasTank);
break;
case Airlock.ROLE_NAME_HABBAROMETER:
if (!(thisBlock is IMyAirVent))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddHabitatBarometer(thisBlock as IMyAirVent);
break;
case Airlock.ROLE_NAME_VACBAROMETER:
if (!(thisBlock is IMyAirVent))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddVacuumBarometer(thisBlock as IMyAirVent);
break;
case Airlock.ROLE_NAME_PRESSURELIGHT:
if (!(thisBlock is IMyLightingBlock))
{
wrongTypeForRole = true;
break;
}
thisAirlock.AddPressureLight(thisBlock as IMyLightingBlock);
break;
default:
throw new Exception(
$"Failed to parse airlock configuration data because a block's role was not recognized."
+ $"\nBlock: {thisBlock.CustomName}"
+ $"\nRole: {roleName}"
);
} // switch (roleName)
if (wrongTypeForRole)
{
throw new Exception(
$"Failed to parse airlock configuration data because a block is the wrong type for its role."
+ $"\nBlock: {thisBlock.CustomName}"
+ $"\nRole: {roleName}"
);
}
} // foreach thisBlock
return foundAirlocks;
}
#endregion Constructors and Factory Methods
#region Serialization
/// <summary>
/// Writes the values of the <see cref="TargetMode"/> and <see
/// cref="QueuedMode"/> properties to a provided <see cref="MyIni"/>
/// object for persistent storage.