-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
820 lines (682 loc) · 35.6 KB
/
Program.cs
File metadata and controls
820 lines (682 loc) · 35.6 KB
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
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage;
using VRage.Collections;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ObjectBuilders.Definitions;
using VRageMath;
namespace IngameScript
{
partial class Program : MyGridProgram
{
/*
* R e a d m e
* -----------
* Graphical inventory display for LCD by Space Engineers Script.
* 太空工程师,图形化显示库存脚本。
*
* @see <https://github.com/se-scripts/inventory-graphic>
* @author [Hi.James](https://space.bilibili.com/368005035)
* @author [li-guohao](https://github.com/li-guohao)
*/
const string version = "1.0.0";
MyIni _ini = new MyIni();
List<IMyCargoContainer> cargoContainers = new List<IMyCargoContainer>();
List<IMyTextPanel> panels = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Items_All = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Items_Ore = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Items_Ingot = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Items_Component = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Items_AmmoMagazine = new List<IMyTextPanel>();
List<IMyTextPanel> panels_Overall = new List<IMyTextPanel>();
List<IMyGasTank> oxygenTanks = new List<IMyGasTank>();
List<IMyGasTank> hydrogenTanks = new List<IMyGasTank>();
List<IMyPowerProducer> powerProducers = new List<IMyPowerProducer>();
List<IMyBatteryBlock> batteryList = new List<IMyBatteryBlock>();
List<string> spritesList = new List<string>();
Dictionary<string, string> translator = new Dictionary<string, string>();
const int itemAmountInEachScreen = 35, facilityAmountInEachScreen = 20;
const float itemBox_ColumnInterval_Float = 73, itemBox_RowInterval_Float = 102, amountBox_Height_Float = 24, facilityBox_RowInterval_Float = 25.5f;
const string information_Section = "Information";
const string translateList_Section = "Translate_List", length_Key = "Length";
int counter_ProgramRefresh = 0, counter_ShowItems = 0, counter_Panel = 0;
double counter_Logo = 0;
Color background_Color = new Color(0, 35, 45);
Color border_Color = new Color(0, 130, 255);
public struct ItemList
{
public string Name;
public double Amount;
}
ItemList[] itemList_All;
ItemList[] itemList_Ore;
ItemList[] itemList_Ingot;
ItemList[] itemList_Component;
ItemList[] itemList_AmmoMagazine;
public struct ComparisonTable
{
public string Name;
public string BluePrintName;
public double Amount;
public bool HasItem;
}
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Once | UpdateFrequency.Update10;
SetDefultConfiguration();
BuildTranslateDic();
}
public void DebugLCD(string text)
{
List<IMyTextPanel> debugPanel = new List<IMyTextPanel>();
GridTerminalSystem.GetBlocksOfType(debugPanel, b => b.IsSameConstructAs(Me) && b.CustomName == "DEBUGLCD");
if (debugPanel.Count == 0) return;
string temp = "";
foreach (var panel in debugPanel)
{
temp = "";
temp = panel.GetText();
}
foreach (var panel in debugPanel)
{
if (panel.ContentType != ContentType.TEXT_AND_IMAGE) panel.ContentType = ContentType.TEXT_AND_IMAGE;
panel.FontSize = 0.55f;
panel.Font = "LoadingScreen";
panel.WriteText(DateTime.Now.ToString(), false);
panel.WriteText("\n", true);
panel.WriteText(text, true);
panel.WriteText("\n", true);
panel.WriteText(temp, true);
}
}
public void SetDefultConfiguration()
{
// This time we _must_ check for failure since the user may have written invalid ini.
MyIniParseResult result;
if (!_ini.TryParse(Me.CustomData, out result))
throw new Exception(result.ToString());
// Initialize CustomData
string dataTemp;
dataTemp = Me.CustomData;
if (dataTemp == "" || dataTemp == null)
{
_ini.Set(information_Section, "LCD_Overall_Display", "LCD_Overall_Display | Fill In CustomName of Panel");
_ini.Set(information_Section, "LCD_Inventory_Display", "LCD_Inventory_Display:X | X=1,2,3... | Fill In CustomName of Panel");
_ini.Set(information_Section, "LCD_Ore_Inventory_Display", "LCD_Ore_Inventory_Display:X | X=1,2,3... | Fill In CustomName of Panel");
_ini.Set(information_Section, "LCD_Ingot_Inventory_Display", "LCD_Ingot_Inventory_Display:X | X=1,2,3... | Fill In CustomName of Panel");
_ini.Set(information_Section, "LCD_Component_Inventory_Display", "LCD_Component_Inventory_Display:X | X=1,2,3... | Fill In CustomName of Panel");
_ini.Set(information_Section, "LCD_AmmoMagazine_Inventory_Display", "LCD_AmmoMagazine_Inventory_Display:X | X=1,2,3... | Fill In CustomName of Panel");
_ini.Set(translateList_Section, length_Key, "1");
_ini.Set(translateList_Section, "1", "AH_BoreSight:More");
Me.CustomData = _ini.ToString();
}// End if
GridTerminalSystem.GetBlocksOfType(cargoContainers, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(panels, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(panels_Overall, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_Overall_Display"));
GridTerminalSystem.GetBlocksOfType(panels_Items_All, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_Inventory_Display:"));
GridTerminalSystem.GetBlocksOfType(panels_Items_Ore, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_Ore_Inventory_Display:"));
GridTerminalSystem.GetBlocksOfType(panels_Items_Ingot, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_Ingot_Inventory_Display:"));
GridTerminalSystem.GetBlocksOfType(panels_Items_Component, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_Component_Inventory_Display:"));
GridTerminalSystem.GetBlocksOfType(panels_Items_AmmoMagazine, b => b.IsSameConstructAs(Me) && b.CustomName.Contains("LCD_AmmoMagazine_Inventory_Display:"));
GridTerminalSystem.GetBlocksOfType(batteryList, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(powerProducers, b => b.IsSameConstructAs(Me));
GridTerminalSystem.GetBlocksOfType(oxygenTanks, b => b.IsSameConstructAs(Me) && !b.DefinitionDisplayNameText.ToString().Contains("Hydrogen") && !b.DefinitionDisplayNameText.ToString().Contains("氢气"));
GridTerminalSystem.GetBlocksOfType(hydrogenTanks, b => b.IsSameConstructAs(Me) && !b.DefinitionDisplayNameText.ToString().Contains("Oxygen") && !b.DefinitionDisplayNameText.ToString().Contains("氧气"));
// incase no screen
if (panels.Count < 1)
{
if (Me.SurfaceCount > 0)
{
Me.GetSurface(0).GetSprites(spritesList);
}
}
else
{
panels[0].GetSprites(spritesList);
}
}
public void GetConfiguration_from_CustomData(string section, string key, out string value)
{
// This time we _must_ check for failure since the user may have written invalid ini.
MyIniParseResult result;
if (!_ini.TryParse(Me.CustomData, out result))
throw new Exception(result.ToString());
string DefaultValue = "";
// Read the integer value. If it does not exist, return the default for this value.
value = _ini.Get(section, key).ToString(DefaultValue);
}
public void BuildTranslateDic()
{
string value;
GetConfiguration_from_CustomData(translateList_Section, length_Key, out value);
int length = Convert.ToInt16(value);
for (int i = 1; i <= length; i++)
{
GetConfiguration_from_CustomData(translateList_Section, i.ToString(), out value);
string[] result = value.Split(':');
translator.Add(result[0], result[1]);
}
}
public void ProgrammableBlockScreen()
{
// 512 X 320
IMyTextSurface panel = Me.GetSurface(0);
if (panel == null) return;
panel.ContentType = ContentType.SCRIPT;
MySpriteDrawFrame frame = panel.DrawFrame();
float x = 512 / 2, y1 = 205;
DrawLogo(frame, x, y1, 200);
PanelWriteText(frame, "Graphic Inventory Dispaly\nIn LCDs With Version " + version + "\nBy Hi.James and li-guohao.", x, y1 + 110, 1f, TextAlignment.CENTER);
frame.Dispose();
}
public void DrawLogo(MySpriteDrawFrame frame, float x, float y, float width)
{
MySprite sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width - 6, width - 6),
RotationOrScale = Convert.ToSingle(counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width / 2, width / 2),
RotationOrScale = Convert.ToSingle(2 * Math.PI - counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
sprite = new MySprite()
{
Type = SpriteType.TEXTURE,
Data = "Screen_LoadingBar",
Position = new Vector2(x, y),
Size = new Vector2(width / 4, width / 4),
RotationOrScale = Convert.ToSingle(Math.PI + counter_Logo / 360 * 2 * Math.PI),
Alignment = TextAlignment.CENTER,
};
frame.Add(sprite);
}
public void PanelWriteText(MySpriteDrawFrame frame, string text, float x, float y, float fontSize, TextAlignment alignment)
{
MySprite sprite = new MySprite()
{
Type = SpriteType.TEXT,
Data = text,
Position = new Vector2(x, y),
RotationOrScale = fontSize,
Color = Color.Coral,
Alignment = alignment,
FontId = "LoadingScreen"
};
frame.Add(sprite);
}
public void OverallDisplay()
{
foreach (var panel in panels_Overall)
{
if (panel.CustomData != "0") panel.CustomData = "0";
else panel.CustomData = "0.001";
if (panel.ContentType != ContentType.SCRIPT) panel.ContentType = ContentType.SCRIPT;
MySpriteDrawFrame frame = panel.DrawFrame();
DrawContentBox(panel, frame);
frame.Dispose();
}
}
public async void DrawContentBox(IMyTextPanel panel, MySpriteDrawFrame frame)
{
float x_Left = itemBox_ColumnInterval_Float / 2 + 1.5f, x_Right = itemBox_ColumnInterval_Float + 2 + (512 - itemBox_ColumnInterval_Float - 4) / 2, x_Title = 70, y_Title = itemBox_ColumnInterval_Float + 2 + Convert.ToSingle(panel.CustomData);
float progressBar_YCorrect = 0f, progressBarWidth = 512 - itemBox_ColumnInterval_Float - 6, progressBarHeight = itemBox_ColumnInterval_Float - 3;
// Title
DrawBox(frame, x_Left, x_Left + Convert.ToSingle(panel.CustomData), itemBox_ColumnInterval_Float, itemBox_ColumnInterval_Float, background_Color);
DrawBox(frame, 512 - x_Left, x_Left + Convert.ToSingle(panel.CustomData), itemBox_ColumnInterval_Float, itemBox_ColumnInterval_Float, background_Color);
DrawBox(frame, 512 / 2, x_Left + Convert.ToSingle(panel.CustomData), 512 - itemBox_ColumnInterval_Float * 2 - 4, itemBox_ColumnInterval_Float, background_Color);
PanelWriteText(frame, panels_Overall[0].GetOwnerFactionTag(), 512 / 2, 2 + Convert.ToSingle(panel.CustomData), 2.3f, TextAlignment.CENTER);
DrawLogo(frame, x_Left, x_Left + Convert.ToSingle(panel.CustomData), itemBox_ColumnInterval_Float);
DrawLogo(frame, 512 - x_Left, x_Left + Convert.ToSingle(panel.CustomData), itemBox_ColumnInterval_Float);
for (int i = 1; i <= 6; i++)
{
float y = i * itemBox_ColumnInterval_Float + itemBox_ColumnInterval_Float / 2 + 1.5f + Convert.ToSingle(panel.CustomData);
DrawBox(frame, x_Left, y, itemBox_ColumnInterval_Float, itemBox_ColumnInterval_Float, background_Color);
DrawBox(frame, x_Right, y, (512 - itemBox_ColumnInterval_Float - 4), itemBox_ColumnInterval_Float, background_Color);
}
// All Cargo
float y1 = itemBox_ColumnInterval_Float + itemBox_ColumnInterval_Float / 2 + 1.5f + Convert.ToSingle(panel.CustomData);
MySprite sprite = MySprite.CreateSprite("Textures\\FactionLogo\\Builders\\BuilderIcon_1.dds", new Vector2(x_Left, y1), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
string percentage_String, finalValue_String;
CalculateAll(out percentage_String, out finalValue_String);
ProgressBar(frame, x_Right, y1 + progressBar_YCorrect, progressBarWidth, progressBarHeight, percentage_String);
PanelWriteText(frame, cargoContainers.Count.ToString(), x_Title, y_Title, 0.55f, TextAlignment.RIGHT);
PanelWriteText(frame, percentage_String, x_Right, y_Title, 1.2f, TextAlignment.CENTER);
PanelWriteText(frame, finalValue_String, x_Right, y_Title + itemBox_ColumnInterval_Float / 2, 1.2f, TextAlignment.CENTER);
// H2
float y2 = y1 + itemBox_ColumnInterval_Float;
sprite = MySprite.CreateSprite("IconHydrogen", new Vector2(x_Left, y2), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
CalcualateGasTank(hydrogenTanks, out percentage_String, out finalValue_String);
PanelWriteText(frame, hydrogenTanks.Count.ToString(), x_Title, y_Title + itemBox_ColumnInterval_Float, 0.55f, TextAlignment.RIGHT);
ProgressBar(frame, x_Right, y2 + progressBar_YCorrect, progressBarWidth, progressBarHeight, percentage_String);
PanelWriteText(frame, percentage_String, x_Right, y_Title + itemBox_ColumnInterval_Float, 1.2f, TextAlignment.CENTER);
PanelWriteText(frame, finalValue_String, x_Right, y_Title + itemBox_ColumnInterval_Float + itemBox_ColumnInterval_Float / 2, 1.2f, TextAlignment.CENTER);
// O2
float y3 = y2 + itemBox_ColumnInterval_Float;
sprite = MySprite.CreateSprite("IconOxygen", new Vector2(x_Left, y3), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
CalcualateGasTank(oxygenTanks, out percentage_String, out finalValue_String);
PanelWriteText(frame, oxygenTanks.Count.ToString(), x_Title, y_Title + itemBox_ColumnInterval_Float * 2, 0.55f, TextAlignment.RIGHT);
ProgressBar(frame, x_Right, y3 + progressBar_YCorrect, progressBarWidth, progressBarHeight, percentage_String);
PanelWriteText(frame, percentage_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 2, 1.2f, TextAlignment.CENTER);
PanelWriteText(frame, finalValue_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 2 + itemBox_ColumnInterval_Float / 2, 1.2f, TextAlignment.CENTER);
// Battery
float y4 = y3 + itemBox_ColumnInterval_Float;
sprite = MySprite.CreateSprite("ColorfulIcons_BlockVariantGroup/BatteryGroup", new Vector2(x_Left, y4), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
CalcualateBattery(out percentage_String, out finalValue_String);
PanelWriteText(frame, batteryList.Count.ToString(), x_Title, y_Title + itemBox_ColumnInterval_Float * 3, 0.55f, TextAlignment.RIGHT);
ProgressBar(frame, x_Right, y4 + progressBar_YCorrect, progressBarWidth, progressBarHeight, percentage_String);
PanelWriteText(frame, percentage_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 3, 1.2f, TextAlignment.CENTER);
PanelWriteText(frame, finalValue_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 3 + itemBox_ColumnInterval_Float / 2, 1.2f, TextAlignment.CENTER);
// Power
float y5 = y4 + itemBox_ColumnInterval_Float;
sprite = MySprite.CreateSprite("IconEnergy", new Vector2(x_Left, y5), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
CalculatePowerProducer(out percentage_String, out finalValue_String);
PanelWriteText(frame, powerProducers.Count.ToString(), x_Title, y_Title + itemBox_ColumnInterval_Float * 4, 0.55f, TextAlignment.RIGHT);
ProgressBar(frame, x_Right, y5 + progressBar_YCorrect, progressBarWidth, progressBarHeight, percentage_String);
PanelWriteText(frame, percentage_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 4, 1.2f, TextAlignment.CENTER);
PanelWriteText(frame, finalValue_String, x_Right, y_Title + itemBox_ColumnInterval_Float * 4 + itemBox_ColumnInterval_Float / 2, 1.2f, TextAlignment.CENTER);
}
public void DrawBox(MySpriteDrawFrame frame, float x, float y, float width, float height, Color border_Color, Color background_Color)
{
//Echo($"width={width} | height={height}");
MySprite sprite;
sprite = MySprite.CreateSprite("SquareSimple", new Vector2(x, y), new Vector2(width - 1, height - 1));
sprite.Color = border_Color;
frame.Add(sprite);
sprite = MySprite.CreateSprite("SquareSimple", new Vector2(x, y), new Vector2(width - 3, height - 3));
sprite.Color = background_Color;
frame.Add(sprite);
}
public void DrawBox(MySpriteDrawFrame frame, float x, float y, float width, float height, Color background_Color)
{
MySprite sprite;
sprite = MySprite.CreateSprite("SquareSimple", new Vector2(x, y), new Vector2(width - 2, height - 2));
sprite.Color = background_Color;
frame.Add(sprite);
}
public void CalculateAll(out string percentage_String, out string finalValue_String)
{
double currentVolume_Double = 0, totalVolume_Double = 0;
foreach (var cargoContainer in cargoContainers)
{
currentVolume_Double += ((double)cargoContainer.GetInventory().CurrentVolume);
totalVolume_Double += ((double)cargoContainer.GetInventory().MaxVolume);
}
percentage_String = Math.Round(currentVolume_Double / totalVolume_Double * 100, 1).ToString() + "%";
finalValue_String = AmountUnitConversion(currentVolume_Double * 1000) + " L / " + AmountUnitConversion(totalVolume_Double * 1000) + " L";
}
public void ProgressBar(MySpriteDrawFrame frame, float x, float y, float width, float height, string ratio)
{
string[] ratiogroup = ratio.Split('%');
float ratio_Float = Convert.ToSingle(ratiogroup[0]);
float currentWidth = width * ratio_Float / 100;
float currentX = x - width / 2 + currentWidth / 2;
Color co = new Color(0, 0, 256);
if (ratio_Float == 0) return;
DrawBox(frame, currentX, y, currentWidth, height, co, co);
}
public string AmountUnitConversion(double amount)
{
double temp = 0;
string result = "";
if (amount >= 1000000000000000)
{
temp = Math.Round(amount / 1000000000000000, 1);
result = temp.ToString() + "KT";
}
else if (amount >= 1000000000000)
{
temp = Math.Round(amount / 1000000000000, 1);
result = temp.ToString() + "T";
}
else if (amount >= 1000000000)
{
temp = Math.Round(amount / 1000000000, 1);
result = temp.ToString() + "G";
}
else if (amount >= 1000000)
{
temp = Math.Round(amount / 1000000, 1);
result = temp.ToString() + "M";
}
else if (amount >= 1000)
{
temp = Math.Round(amount / 1000, 1);
result = temp.ToString() + "K";
}
else
{
temp = Math.Round(amount, 1);
result = temp.ToString();
}
return result;
}
public void CalcualateGasTank(List<IMyGasTank> tanks, out string percentage_String, out string finalValue_String)
{
double currentVolume_Double = 0, totalVolume_Double = 0;
foreach (var tank in tanks)
{
currentVolume_Double += tank.Capacity * tank.FilledRatio;
totalVolume_Double += tank.Capacity;
}
percentage_String = Math.Round(currentVolume_Double / totalVolume_Double * 100, 1).ToString() + "%";
finalValue_String = AmountUnitConversion(currentVolume_Double) + " L / " + AmountUnitConversion(totalVolume_Double) + " L";
}
public void CalcualateBattery(out string percentage_String, out string finalValue_String)
{
double currentStoredPower = 0, maxStoredPower = 0;
foreach (var battery in batteryList)
{
currentStoredPower += battery.CurrentStoredPower;
maxStoredPower += battery.MaxStoredPower;
}
percentage_String = Math.Round(currentStoredPower / maxStoredPower * 100, 1).ToString() + "%";
finalValue_String = AmountUnitConversion(currentStoredPower * 1000000) + " Wh / " + AmountUnitConversion(maxStoredPower * 1000000) + " Wh";
}
public void CalculatePowerProducer(out string percentage_String, out string finalValue_String)
{
double currentOutput = 0, totalOutput = 0;
foreach (var powerProducer in powerProducers)
{
currentOutput += powerProducer.CurrentOutput;
totalOutput += powerProducer.MaxOutput;
}
percentage_String = Math.Round(currentOutput / totalOutput * 100, 1).ToString() + "%";
finalValue_String = AmountUnitConversion(currentOutput * 1000000) + " W / " + AmountUnitConversion(totalOutput * 1000000) + " W";
}
public void transferItemsList(ItemList[] itemList, string tag)
{
int k = 0;
foreach (var item in itemList_All)
{
if (item.Name.IndexOf(tag) != -1)
{
itemList[k].Name = item.Name;
itemList[k].Amount = item.Amount;
k++;
}
}
}
public void GetAllItems()
{
Dictionary<string, double> allItems = new Dictionary<string, double>();
foreach (var cargoContainer in cargoContainers)
{
var items = new List<MyInventoryItem>();
cargoContainer.GetInventory().GetItems(items);
foreach (var item in items)
{
if (allItems.ContainsKey(item.Type.ToString())) allItems[item.Type.ToString()] += (double)item.Amount.RawValue;
else allItems.Add(item.Type.ToString(), (double)item.Amount.RawValue);
}
}
foreach (var cargoContainer in oxygenTanks)
{
var items = new List<MyInventoryItem>();
cargoContainer.GetInventory().GetItems(items);
foreach (var item in items)
{
if (allItems.ContainsKey(item.Type.ToString())) allItems[item.Type.ToString()] += (double)item.Amount.RawValue;
else allItems.Add(item.Type.ToString(), (double)item.Amount.RawValue);
}
}
foreach (var cargoContainer in hydrogenTanks)
{
var items = new List<MyInventoryItem>();
cargoContainer.GetInventory().GetItems(items);
foreach (var item in items)
{
if (allItems.ContainsKey(item.Type.ToString())) allItems[item.Type.ToString()] += (double)item.Amount.RawValue;
else allItems.Add(item.Type.ToString(), (double)item.Amount.RawValue);
}
}
itemList_All = new ItemList[allItems.Count];
int k = 0;
foreach (var key in allItems.Keys)
{
itemList_All[k].Name = key;
itemList_All[k].Amount = allItems[key];
k++;
}
itemList_Ore = new ItemList[LengthOfEachCategory("MyObjectBuilder_Ore")];
itemList_Ingot = new ItemList[LengthOfEachCategory("MyObjectBuilder_Ingot")];
itemList_AmmoMagazine = new ItemList[LengthOfEachCategory("MyObjectBuilder_AmmoMagazine")];
transferItemsList(itemList_Ore, "MyObjectBuilder_Ore");
transferItemsList(itemList_Ingot, "MyObjectBuilder_Ingot");
transferItemsList(itemList_AmmoMagazine, "MyObjectBuilder_AmmoMagazine");
itemList_Component = new ItemList[itemList_All.Length - itemList_Ore.Length - itemList_Ingot.Length - itemList_AmmoMagazine.Length];
k = 0;
foreach (var item in itemList_All)
{
if (item.Name.IndexOf("MyObjectBuilder_Ore") == -1 && item.Name.IndexOf("MyObjectBuilder_Ingot") == -1 && item.Name.IndexOf("MyObjectBuilder_AmmoMagazine") == -1)
{
itemList_Component[k].Name = item.Name;
itemList_Component[k].Amount = item.Amount;
k++;
}
}
}
public int LengthOfEachCategory(string tag)
{
Dictionary<string, double> keyValuePairs = new Dictionary<string, double>();
foreach (var item in itemList_All)
{
if (item.Name.IndexOf(tag) != -1)
{
keyValuePairs.Add(item.Name, item.Amount);
}
}
return keyValuePairs.Count;
}
public void DrawFullItemScreen(IMyTextPanel panel, MySpriteDrawFrame frame, string groupNumber, bool isEnoughScreen, ItemList[] itemList)
{
panel.WriteText("", false);
DrawBox(frame, 512 / 2, 512 / 2 + Convert.ToSingle(panel.CustomData), 520, 520, new Color(0, 0, 0));
for (int i = 0; i < itemAmountInEachScreen; i++)
{
int k = (Convert.ToInt16(groupNumber) - 1) * itemAmountInEachScreen + i;
int x = (i + 1) % 7;
if (x == 0) x = 7;
int y = Convert.ToInt16(Math.Ceiling(Convert.ToDecimal(Convert.ToDouble(i + 1) / 7)));
if (k > itemList.Length - 1)
{
return;
}
else
{
if (x == 7 && y == 5)
{
if (isEnoughScreen)
{
DrawSingleItemUnit(panel, frame, itemList[k].Name, itemList[k].Amount / 1000000, x, y);
}
else
{
double residus = itemList.Length - itemAmountInEachScreen * Convert.ToInt16(groupNumber) + 1;
DrawSingleItemUnit(panel, frame, "AH_BoreSight", residus, x, y);
}
}
else
{
DrawSingleItemUnit(panel, frame, itemList[k].Name, itemList[k].Amount / 1000000, x, y);
}
panel.WriteText(itemList[k].Name, true);
panel.WriteText("\n", true);
}
}
}
public void DrawSingleItemUnit(IMyTextPanel panel, MySpriteDrawFrame frame, string itemName, double amount, float x, float y)
{
// Picture box
float x1 = Convert.ToSingle((x - 1) * itemBox_ColumnInterval_Float + (itemBox_ColumnInterval_Float - 1) / 2 + 1.25f);
float y1 = Convert.ToSingle((y - 1) * itemBox_RowInterval_Float + (itemBox_RowInterval_Float - 1) / 2 + 1.5f) + Convert.ToSingle(panel.CustomData);
//DrawBox(frame, x1, y1, itemBox_ColumnInterval_Float, itemBox_RowInterval_Float, border_Color, background_Color);
DrawBox(frame, x1, y1, itemBox_ColumnInterval_Float, itemBox_RowInterval_Float, background_Color);
MySprite sprite = MySprite.CreateSprite(itemName, new Vector2(x1, y1 - 3), new Vector2(itemBox_ColumnInterval_Float - 2, itemBox_ColumnInterval_Float - 2));
frame.Add(sprite);
// Amount box
float y_Border_Amount = y1 + (itemBox_ColumnInterval_Float - 1) / 2 + amountBox_Height_Float / 2 - 1;
//DrawBox(frame, x1, y_Border_Amount, itemBox_ColumnInterval_Float, amountBox_Height_Float, border_Color, background_Color);
// Amount text
float x_Text_Amount = x1 + (itemBox_ColumnInterval_Float - 3) / 2 - 1;
float y_Text_Amount = y1 + itemBox_RowInterval_Float / 2 - amountBox_Height_Float;
PanelWriteText(frame, AmountUnitConversion(amount), x_Text_Amount, y_Text_Amount, 0.8f, TextAlignment.RIGHT);
// Name text
float x_Name = x1 - (itemBox_ColumnInterval_Float - 3) / 2 + 1;
float y_Name = y1 - (itemBox_RowInterval_Float - 3) / 2 + 1;
PanelWriteText(frame, TranslateName(itemName), x_Name, y_Name, 0.55f, TextAlignment.LEFT);
}
public string TranslateName(string name)
{
if (translator.ContainsKey(name))
{
return translator[name];
}
else
{
return ShortName(name);
}
}
public string ShortName(string name)
{
string[] temp = name.Split('/');
if (temp.Length == 2)
{
return temp[1];
}
else
{
return name;
}
}
public void ItemDivideInGroups(ItemList[] itemList, List<IMyTextPanel> panels_Items)
{
if (itemList.Length == 0 || panels_Items.Count == 0) return;
// get all panel numbers
int[] findMax = new int[panels_Items.Count];
int k = 0;
foreach (var panel in panels_Items)
{
// get current panel number
string[] arry = panel.CustomName.Split(':');
findMax[k] = Convert.ToInt16(arry[1]);
k++;
}
if (itemList.Length > FindMax(findMax) * itemAmountInEachScreen)
{
foreach (var panel in panels_Items)
{
if (panel.CustomData != "0") panel.CustomData = "0";
else panel.CustomData = "1";
panel.ContentType = ContentType.SCRIPT;
panel.BackgroundColor = Color.Black;
MySpriteDrawFrame frame = panel.DrawFrame();
string[] arry = panel.CustomName.Split(':');
if (Convert.ToInt16(arry[1]) < FindMax(findMax))
{
DrawFullItemScreen(panel, frame, arry[1], true, itemList);
}
else
{
DrawFullItemScreen(panel, frame, arry[1], false, itemList);
}
frame.Dispose();
}
}
else
{
foreach (var panel in panels_Items)
{
if (panel.CustomData != "0") panel.CustomData = "0";
else panel.CustomData = "1";
panel.ContentType = ContentType.SCRIPT;
panel.BackgroundColor = Color.Black;
MySpriteDrawFrame frame = panel.DrawFrame();
string[] arry = panel.CustomName.Split(':');
DrawFullItemScreen(panel, frame, arry[1], true, itemList);
frame.Dispose();
}
}
}
public int FindMax(int[] arry)
{
int p = 0;
for (int i = 0; i < arry.Length; i++)
{
if (i == 0) p = arry[i];
else if (arry[i] > p) p = arry[i];
}
return p;
}
public void Main(string argument, UpdateType updateSource)
{
Echo($"{DateTime.Now}");
Echo("Program is running.");
if (counter_ProgramRefresh != 1 || counter_ProgramRefresh != 6 || counter_ProgramRefresh != 11 || counter_ProgramRefresh != 16)
{
if (counter_Logo++ >= 360) counter_Logo = 0;
ProgrammableBlockScreen();
OverallDisplay();
}
if (counter_ProgramRefresh++ >= 21) counter_ProgramRefresh = 0;
// show items.
DateTime beforDT = System.DateTime.Now;
if (counter_ShowItems >= 7) counter_ShowItems = 1;
switch (counter_ShowItems.ToString())
{
case "1":
GetAllItems();
break;
case "2":
ItemDivideInGroups(itemList_All, panels_Items_All);
break;
case "3":
ItemDivideInGroups(itemList_Ore, panels_Items_Ore);
break;
case "4":
ItemDivideInGroups(itemList_Ingot, panels_Items_Ingot);
break;
case "5":
ItemDivideInGroups(itemList_Component, panels_Items_Component);
break;
case "6":
ItemDivideInGroups(itemList_AmmoMagazine, panels_Items_AmmoMagazine);
break;
}
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Echo("Total cost ms:" + ts.TotalMilliseconds);
DebugLCD("Total cost ms: " + ts.TotalMilliseconds);
counter_ShowItems++;
}
}
}