-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEasyAPI.debug.cs
2199 lines (1822 loc) · 57.8 KB
/
EasyAPI.debug.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
/************************************************************************************
EasyAPI - Documentation: http://steamcommunity.com/sharedfiles/filedetails/?id=381043
*************************************************************************************/
public class Example : EasyAPI
{
public Example(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime)
{
// Start your code here
}
}
/*********************************************/
/*** Advanced users only beyond this point ***/
/*********************************************/
Example state;
public Program()
{
state = new Example(GridTerminalSystem, Me, Echo, Runtime.TimeSinceLastRun);
Runtime.UpdateFrequency = UpdateFrequency.Update100;
}
public void Main(string argument, UpdateType updateType)
{
if(state == null)
{
state = new Example(GridTerminalSystem, Me, Echo, Runtime.TimeSinceLastRun);
}
// Set the minimum time between ticks here to prevent lag.
// To utilize onSingleTap and onDoubleTap, set the minimum time to the same
// time period of the timer running this script (e.g. 1 * EasyAPI.Seconds).
state.Tick(100 * EasyAPI.Milliseconds, argument);
}
/**************************************************/
/*** EasyAPI class. Extend for easier scripting ***/
/**************************************************/
public abstract class EasyAPI
{
private long start = 0; // Time at start of program
private long clock = 0; // Current time in ticks
private long delta = 0; // Time since last call to Tick in ticks
public EasyBlock Self; // Reference to the Programmable Block that is running this script
public IMyGridTerminalSystem GridTerminalSystem;
public Action<string> Echo;
public TimeSpan ElapsedTime;
static public IMyGridTerminalSystem grid;
/*** Events ***/
private Dictionary<string,List<Action>> ArgumentActions;
private Dictionary<string,List<Action<int, string[]>>> CommandActions;
private List<EasyInterval> Schedule;
private List<EasyInterval> Intervals;
private EasyCommands commands;
/*** Overridable lifecycle methods ***/
public virtual void onRunThrottled(float intervalTranspiredPercentage) {}
public virtual void onTickStart() {}
public virtual void onTickComplete() {}
public virtual bool onSingleTap() { return false; }
public virtual bool onDoubleTap() { return false; }
private int InterTickRunCount = 0;
/*** Cache ***/
public EasyBlocks Blocks;
/*** Constants ***/
public const long Microseconds = 10; // Ticks (100ns)
public const long Milliseconds = 1000 * Microseconds;
public const long Seconds = 1000 * Milliseconds;
public const long Minutes = 60 * Seconds;
public const long Hours = 60 * Minutes;
public const long Days = 24 * Hours;
public const long Years = 365 * Days;
/*** Constructor ***/
public EasyAPI(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime, string commandArgument = "EasyCommand")
{
this.clock = this.start = DateTime.Now.Ticks;
this.delta = 0;
this.GridTerminalSystem = EasyAPI.grid = grid;
this.Echo = echo;
this.ElapsedTime = elapsedTime;
this.ArgumentActions = new Dictionary<string,List<Action>>();
this.CommandActions = new Dictionary<string,List<Action<int, string[]>>>();
this.Schedule = new List<EasyInterval>();
this.Intervals = new List<EasyInterval>();
this.commands = new EasyCommands(this);
// Get the Programmable Block that is running this script (thanks to LordDevious and LukeStrike)
this.Self = new EasyBlock(me);
this.Reset();
}
public void AddEvent(EasyEvent e)
{
EasyEvent.add(e);
}
public void AddEvent(EasyBlock block, Func<EasyBlock, bool> evnt, Func<EasyBlock, bool> action, bool onChange = false)
{
this.AddEvent(new EasyEvent(block, evnt, action, onChange));
}
public void AddEvents(EasyBlocks blocks, Func<EasyBlock, bool> evnt, Func<EasyBlock, bool> action, bool onChange = false)
{
for(int i = 0; i < blocks.Count(); i++)
{
this.AddEvent(new EasyEvent(blocks.GetBlock(i), evnt, action, onChange));
}
}
// Get messages sent to this block
public List<EasyMessage> GetMessages()
{
var mymessages = new List<EasyMessage>();
var parts = this.Self.Name().Split('\0');
if(parts.Length > 1)
{
for(int n = 1; n < parts.Length; n++)
{
EasyMessage m = new EasyMessage(parts[n]);
mymessages.Add(m);
}
// Delete the messages once they are received
this.Self.SetName(parts[0]);
}
return mymessages;
}
// Clear messages sent to this block
public void ClearMessages()
{
var parts = this.Self.Name().Split('\0');
if(parts.Length > 1)
{
// Delete the messages
this.Self.SetName(parts[0]);
}
}
public EasyMessage ComposeMessage(String Subject, String Message)
{
return new EasyMessage(this.Self, Subject, Message);
}
/*** Execute one tick of the program (interval is the minimum time between ticks) ***/
public void Tick(long interval = 0, string argument = "")
{
/*** Handle Arguments ***/
if(argument != "")
{
if(this.ArgumentActions.ContainsKey(argument))
{
for(int n = 0; n < this.ArgumentActions[argument].Count; n++)
{
this.ArgumentActions[argument][n]();
}
}
else if(this.CommandActions.Count > 0)
{
int argc = 0;
var matches = System.Text.RegularExpressions.Regex.Matches(argument, @"(?<match>[^\s""]+)|""(?<match>[^""]*)""");
string[] argv = new string[matches.Count];
for(int n = 0; n < matches.Count; n++)
{
argv[n] = matches[n].Groups["match"].Value;
}
argc = argv.Length;
if(argc > 0 && this.CommandActions.ContainsKey(argv[0]))
{
for(int n = 0; n < this.CommandActions[argv[0]].Count; n++)
{
this.CommandActions[argv[0]][n](argc, argv);
}
}
}
else if(argument.Length > 12 && argument.Substring(0, 12) == "EasyCommand ")
{
this.commands.handle(argument.Substring(12));
}
}
long now = DateTime.Now.Ticks;
if(this.clock > this.start && now - this.clock < interval) {
InterTickRunCount++;
float transpiredPercentage = ((float)((double)(now - this.clock) / interval));
onRunThrottled(transpiredPercentage);
return; // Don't run until the minimum time between ticks
}
if(InterTickRunCount == 1) {
if(onSingleTap()) {
return; // Override has postponed this Tick to next Run
}
} else if(InterTickRunCount > 1) {
if(onDoubleTap()) {
return; // Override has postponed this Tick to next Run
}
}
InterTickRunCount = 0;
onTickStart();
long lastClock = this.clock;
this.clock = now;
this.delta = this.clock - lastClock;
/*** Handle Events ***/
EasyEvent.handle();
/*** Handle Intervals ***/
for(int n = 0; n < this.Intervals.Count; n++)
{
if(this.clock >= this.Intervals[n].time)
{
long time = this.clock + this.Intervals[n].interval - (this.clock - this.Intervals[n].time);
(this.Intervals[n].action)();
this.Intervals[n] = new EasyInterval(time, this.Intervals[n].interval, this.Intervals[n].action); // reset time interval
}
}
/*** Handle Schedule ***/
for(int n = 0; n < this.Schedule.Count; n++)
{
if(this.clock >= this.Schedule[n].time)
{
(this.Schedule[n].action)();
Schedule.Remove(this.Schedule[n]);
}
}
onTickComplete();
}
public long GetDelta() {return this.delta;}
public long GetClock() {return clock;}
public void On(string argument, Action callback)
{
if(!this.ArgumentActions.ContainsKey(argument))
{
this.ArgumentActions.Add(argument, new List<Action>());
}
this.ArgumentActions[argument].Add(callback);
}
public void OnCommand(string argument, Action<int, string[]> callback)
{
if(!this.CommandActions.ContainsKey(argument))
{
this.CommandActions.Add(argument, new List<Action<int, string[]>>());
}
this.CommandActions[argument].Add(callback);
}
/*** Call a function at the specified time ***/
public void At(long time, Action callback)
{
long t = this.start + time;
Schedule.Add(new EasyInterval(t, 0, callback));
}
/*** Call a function every interval of time ***/
public void Every(long time, Action callback)
{
Intervals.Add(new EasyInterval(this.clock + time, time, callback));
}
/*** Call a function in "time" seconds ***/
public void In(long time, Action callback)
{
this.At(this.clock - this.start + time, callback);
}
/*** Resets the clock and refreshes the blocks. ***/
public void Reset()
{
this.start = this.clock;
this.ClearMessages(); // clear messages
this.Refresh();
}
/*** Refreshes blocks. If you add or remove blocks, call this. ***/
public void Refresh()
{
List<IMyTerminalBlock> kBlocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocks(kBlocks);
Blocks = new EasyBlocks(kBlocks);
}
}
public class EasyBlocks
{
public List<EasyBlock> Blocks;
// Constructor with IMyTerminalBlock list
public EasyBlocks(List<IMyTerminalBlock> TBlocks)
{
this.Blocks = new List<EasyBlock>();
for(int i = 0; i < TBlocks.Count; i++)
{
EasyBlock Block = new EasyBlock(TBlocks[i]);
this.Blocks.Add(Block);
}
}
// Constructor with EasyBlock list
public EasyBlocks(List<EasyBlock> Blocks)
{
this.Blocks = Blocks;
}
public EasyBlocks()
{
this.Blocks = new List<EasyBlock>();
}
// Get number of blocks in list
public int Count()
{
return this.Blocks.Count;
}
// Get a specific block from the list
public EasyBlock GetBlock(int i)
{
return this.Blocks[i];
}
/*********************/
/*** Block Filters ***/
/*********************/
/*** Interface Filters ***/
public EasyBlocks WithInterface<T>() where T: class
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
T block = this.Blocks[i].Block as T;
if(block != null)
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
/*** Type Filters ***/
public EasyBlocks OfType(String Type)
{
return TypeFilter("==", Type);
}
public EasyBlocks NotOfType(String Type)
{
return TypeFilter("!=", Type);
}
public EasyBlocks OfTypeLike(String Type)
{
return TypeFilter("~", Type);
}
public EasyBlocks NotOfTypeLike(String Type)
{
return TypeFilter("!~", Type);
}
public EasyBlocks OfTypeRegex(String Pattern)
{
return TypeFilter("R", Pattern);
}
public EasyBlocks NotOfTypeRegex(String Pattern)
{
return TypeFilter("!R", Pattern);
}
protected EasyBlocks TypeFilter(String op, String Type)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
if(EasyCompare(op, this.Blocks[i].Type(), Type))
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
/*** Name Filters ***/
public EasyBlocks Named(String Name)
{
return NameFilter("==", Name);
}
public EasyBlocks NotNamed(String Name)
{
return NameFilter("!=", Name);
}
public EasyBlocks NamedLike(String Name)
{
return NameFilter("~", Name);
}
public EasyBlocks NotNamedLike(String Name)
{
return NameFilter("!~", Name);
}
public EasyBlocks NamedRegex(String Pattern)
{
return NameFilter("R", Pattern);
}
public EasyBlocks NotNamedRegex(String Pattern)
{
return NameFilter("!R", Pattern);
}
protected EasyBlocks NameFilter(String op, String Name)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
if(EasyCompare(op, this.Blocks[i].Name(), Name))
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
/*** Group Filters ***/
public EasyBlocks InGroupsNamed(String Group)
{
return GroupFilter("==", Group);
}
public EasyBlocks InGroupsNotNamed(String Group)
{
return GroupFilter("!=", Group);
}
public EasyBlocks InGroupsNamedLike(String Group)
{
return GroupFilter("~", Group);
}
public EasyBlocks InGroupsNotNamedLike(String Group)
{
return GroupFilter("!~", Group);
}
public EasyBlocks InGroupsNamedRegex(String Pattern)
{
return GroupFilter("R", Pattern);
}
public EasyBlocks InGroupsNotNamedRegex(String Pattern)
{
return GroupFilter("!R", Pattern);
}
public EasyBlocks GroupFilter(String op, String Group)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
List<IMyBlockGroup> groups = new List<IMyBlockGroup>();
EasyAPI.grid.GetBlockGroups(groups);
List<IMyBlockGroup> matchedGroups = new List<IMyBlockGroup>();
List<IMyTerminalBlock> groupBlocks = new List<IMyTerminalBlock>();
for(int n = 0; n < groups.Count; n++)
{
if(EasyCompare(op, groups[n].Name, Group))
{
matchedGroups.Add(groups[n]);
}
}
for(int n = 0; n < matchedGroups.Count; n++)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
IMyTerminalBlock block = this.Blocks[i].Block;
matchedGroups[n].GetBlocks(groupBlocks);
for(int j = 0; j < groupBlocks.Count; j++)
{
if(block == groupBlocks[j])
{
FilteredList.Add(this.Blocks[i]);
}
}
groupBlocks.Clear();
}
}
return new EasyBlocks(FilteredList);
}
/*** Sensor Filters ***/
public EasyBlocks SensorsActive(bool isActive = true)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
if(this.Blocks[i].Type() == "Sensor" && ((IMySensorBlock)this.Blocks[i].Block).IsActive == isActive)
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
public EasyBlocks RoomPressure(String op, Single percent)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
if(this.Blocks[i].RoomPressure(op, percent))
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
/*** Advanced Filters ***/
public EasyBlocks FilterBy(Func<EasyBlock, bool> action)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
for(int i = 0; i < this.Blocks.Count; i++)
{
if(action(this.Blocks[i]))
{
FilteredList.Add(this.Blocks[i]);
}
}
return new EasyBlocks(FilteredList);
}
/*** Other ***/
public EasyBlocks First()
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
if(this.Blocks.Count > 0)
{
FilteredList.Add(Blocks[0]);
}
return new EasyBlocks(FilteredList);
}
public EasyBlocks Add(EasyBlock Block)
{
this.Blocks.Add(Block);
return this;
}
public EasyBlocks Plus(EasyBlocks Blocks)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
FilteredList.AddRange(this.Blocks);
for(int i = 0; i < Blocks.Count(); i++)
{
if(!FilteredList.Contains(Blocks.GetBlock(i)))
{
FilteredList.Add(Blocks.GetBlock(i));
}
}
return new EasyBlocks(FilteredList);
}
public EasyBlocks Minus(EasyBlocks Blocks)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
FilteredList.AddRange(this.Blocks);
for(int i = 0; i < Blocks.Count(); i++)
{
FilteredList.Remove(Blocks.GetBlock(i));
}
return new EasyBlocks(FilteredList);
}
public static EasyBlocks operator +(EasyBlocks a, EasyBlocks b)
{
return a.Plus(b);
}
public static EasyBlocks operator -(EasyBlocks a, EasyBlocks b)
{
return a.Minus(b);
}
public EasyBlocks Plus(EasyBlock Block)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
FilteredList.AddRange(this.Blocks);
if(!FilteredList.Contains(Block))
{
FilteredList.Add(Block);
}
return new EasyBlocks(FilteredList);
}
public EasyBlocks Minus(EasyBlock Block)
{
List<EasyBlock> FilteredList = new List<EasyBlock>();
FilteredList.AddRange(this.Blocks);
FilteredList.Remove(Block);
return new EasyBlocks(FilteredList);
}
public static EasyBlocks operator +(EasyBlocks a, EasyBlock b)
{
return a.Plus(b);
}
public static EasyBlocks operator -(EasyBlocks a, EasyBlock b)
{
return a.Minus(b);
}
/*** Operations ***/
public EasyBlocks FindOrFail(string message)
{
if(this.Count() == 0) throw new Exception(message);
return this;
}
public EasyBlocks Run(EasyAPI api, string type = "public")
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].Run(api, type);
}
return this;
}
public EasyBlocks SendMessage(EasyMessage message)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].SendMessage(message);
}
return this;
}
public EasyBlocks ApplyAction(String Name)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].ApplyAction(Name);
}
return this;
}
public EasyBlocks SetProperty<T>(String PropertyId, T value, int bleh = 0)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].SetProperty<T>(PropertyId, value);
}
return this;
}
public EasyBlocks SetFloatValue(String PropertyId, float value, int bleh = 0)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].SetFloatValue(PropertyId, value);
}
return this;
}
public T GetProperty<T>(String PropertyId, int bleh = 0)
{
return this.Blocks[0].GetProperty<T>(PropertyId);
}
public EasyBlocks On()
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].On();
}
return this;
}
public EasyBlocks Off()
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].Off();
}
return this;
}
public EasyBlocks Toggle()
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].Toggle();
}
return this;
}
public EasyBlocks RunPB(string argument = "")
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].RunPB(argument);
}
return this;
}
public EasyBlocks WritePublicText(string text)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].WritePublicText(text);
}
return this;
}
public EasyBlocks WriteCustomData(string text)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].WriteCustomData(text);
}
return this;
}
public EasyBlocks WritePublicTitle(string text)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].WritePublicTitle(text);
}
return this;
}
public EasyBlocks WritePrivateTitle(string text)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].WritePublicTitle(text);
}
return this;
}
public EasyBlocks AppendPublicText(string text)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].AppendPublicText(text);
}
return this;
}
public EasyInventory Items()
{
return new EasyInventory(this.Blocks);
}
public string DebugDump(bool throwIt = true)
{
String output = "\n";
for(int i = 0; i < this.Blocks.Count; i++)
{
output += this.Blocks[i].Type() + ": " + this.Blocks[i].Name() + "\n";
}
if(throwIt)
throw new Exception(output);
else
return output;
}
public string DebugDumpActions(bool throwIt = true)
{
String output = "\n";
for(int i = 0; i < this.Blocks.Count; i++)
{
output += "[ " + this.Blocks[i].Type() + ": " + this.Blocks[i].Name() + " ]\n";
output += "*** ACTIONS ***\n";
List<ITerminalAction> actions = this.Blocks[i].GetActions();
for(int j = 0; j < actions.Count; j++)
{
output += actions[j].Id + ":" + actions[j].Name + "\n";
}
output += "\n\n";
}
if(throwIt)
throw new Exception(output);
else
return output;
}
public string DebugDumpProperties(bool throwIt = true)
{
String output = "\n";
for(int i = 0; i < this.Blocks.Count; i++)
{
output += "[ " + this.Blocks[i].Type() + ": " + this.Blocks[i].Name() + " ]\n";
output += "*** PROPERTIES ***\n";
List<ITerminalProperty> properties = this.Blocks[i].GetProperties();
for(int j = 0; j < properties.Count; j++)
{
output += properties[j].TypeName + ": " + properties[j].Id + "\n";
}
output += "\n\n";
}
if(throwIt)
throw new Exception(output);
else
return output;
}
/*** Events ***/
public EasyBlocks AddEvent(Func<EasyBlock, bool> evnt, Func<EasyBlock, bool> action, bool onChange = false)
{
for(int i = 0; i < this.Blocks.Count; i++)
{
this.Blocks[i].AddEvent(evnt, action, onChange);
}
return this;
}
private bool EasyCompare(String op, String a, String b)
{
switch(op)
{
case "==":
return (a == b);
case "!=":
return (a != b);
case "~":
return a.Contains(b);
case "!~":
return !a.Contains(b);
case "R":
System.Text.RegularExpressions.Match m = (new System.Text.RegularExpressions.Regex(b)).Match(a);
while(m.Success)
{
return true;
}
return false;
case "!R":
return !EasyCompare("R", a, b);
}
return false;
}
}
public struct EasyBlock
{
public IMyTerminalBlock Block;
private IMySlimBlock slim;
public EasyBlock(IMyTerminalBlock Block)
{
this.Block = Block;
this.slim = null;
}
public IMySlimBlock Slim()
{
if(this.slim == null)
{
this.slim = this.Block.CubeGrid.GetCubeBlock(this.Block.Position);
}
return this.slim;
}
public String Type()
{
return this.Block.DefinitionDisplayNameText;
}
public Single Damage()
{
return this.CurrentDamage() / this.MaxIntegrity() * (Single)100.0;
}
public Single CurrentDamage()
{
return this.Slim().CurrentDamage;
}
public Single MaxIntegrity()
{
return this.Slim().MaxIntegrity;
}
public bool Open()
{
IMyDoor door = Block as IMyDoor;
if(door != null)
{
return door.Status == DoorStatus.Open;
}
return false;
}
public String Name()
{
return this.Block.CustomName;
}