-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fuel Gauge.cs
123 lines (100 loc) · 3.52 KB
/
Fuel Gauge.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
/*
* Fuel Gauge v4
* -----------
*
* 1. Put the name of the LCD Panel or Cockpit you want in the const string below.
* 2. If you used a cockpit set the TextSurfaceIndex to the screen you want. (0 = middle, 1 = left, 2 = right)
* 3. Enjoy!
*/
private const string LCDPanelName = "LCD Fuel Gauge";
private const int TextSurfaceIndex = 0;
List<IMyTerminalBlock> BlocksOnGridList;
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update100;
}
void Main(string argument)
{
float FuelCount = 0f;
string ERR_TXT = "";
if (BlocksOnGridList == null)
{
BlocksOnGridList = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(BlocksOnGridList, filterThis);
}
Dictionary<string, string> EngineDictionary = new Dictionary<string, string>();
List<IMyTextSurface> TextSerfaceList = new List<IMyTextSurface>();
if (BlocksOnGridList.Count == 0)
{
ERR_TXT += "grid has no blocks\n";
}
else
{
for (int i = 0; i < BlocksOnGridList.Count; i++)
{
if (BlocksOnGridList[i].HasInventory)
{
float tempFuel = countItem(BlocksOnGridList[i].GetInventory(0), "Ingot", "Fuel");
string tempBlockName = BlocksOnGridList[i].CustomName;
if (tempBlockName.ToLower().Contains("engine"))
{
string tempFuelString;
if (((IMyFunctionalBlock)BlocksOnGridList[i]).Enabled)
{
tempFuelString = string.Empty + (int)tempFuel;
}
else
{
tempFuelString = "off";
}
EngineDictionary.Add(tempBlockName, tempFuelString);
}
FuelCount = FuelCount + tempFuel;
}
if (BlocksOnGridList[i].CustomName == LCDPanelName)
{
IMyTextSurface TextSurface = null;
TextSurface = ((IMyTextSurfaceProvider)BlocksOnGridList[i]).GetSurface(TextSurfaceIndex);
if (TextSurface == null) TextSurface = (IMyTextSurface)BlocksOnGridList[i];
if (TextSurface != null) TextSerfaceList.Add(TextSurface);
}
}
}
if (ERR_TXT != "")
{
Echo("Script Errors:\n" + ERR_TXT + "(make sure block ownership is set correctly)");
return;
}
string outputString = "Fuel:\n" + (int)FuelCount + "";
foreach (KeyValuePair<string, string> entry in EngineDictionary)
{
outputString += "\n" + entry.Key + ": " + entry.Value;
}
foreach (IMyTextSurface tempTextSurface in TextSerfaceList)
{
tempTextSurface.ContentType = ContentType.TEXT_AND_IMAGE;
tempTextSurface.WriteText(outputString, false);
}
IMyTextSurface mesurface0 = Me.GetSurface(0);
mesurface0.ContentType = ContentType.TEXT_AND_IMAGE;
mesurface0.WriteText(outputString, false);
Echo(outputString);
}
bool filterThis(IMyTerminalBlock block)
{
return block.CubeGrid == Me.CubeGrid;
}
float countItem(IMyInventory inv, string itemType, string itemSubType)
{
List<MyInventoryItem> items = new List<MyInventoryItem>();
inv.GetItems(items, null);
float total = 0.0f;
for (int i = 0; i < items.Count; i++)
{
if (items[i].Type.TypeId.ToString().EndsWith(itemType) && items[i].Type.SubtypeId.ToString() == itemSubType)
{
total += (float)items[i].Amount;
}
}
return total;
}