-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventorySpace.cs
96 lines (75 loc) · 3.15 KB
/
InventorySpace.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
using Sandbox.Definitions;
using Sandbox.Engine;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using VRage;
namespace TestScript
{
public class InventorySpace : IngameScript
{
public static StringBuilder debug = new StringBuilder();
const string debugName = "Debug";
const string antennaName = "Working";
const string stop = "xIn#0";
const int K = 1000;
void Main()
{
// initialize
VRage.MyFixedPoint totalVolume = 0;
VRage.MyFixedPoint totalMaxVolume = 0;
var blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyInventoryOwner>(blocks, FilterInventoryOwner);
if (blocks.Count == 0)
throw new Exception("Did not find any cargo container.");
for (int i = 0; i < blocks.Count; ++i)
{
var invOwner = blocks[i] as IMyInventoryOwner;
for (int j = 0; j < invOwner.InventoryCount; ++j)
{
var inv = invOwner.GetInventory(j);
totalVolume += inv.CurrentVolume;
totalMaxVolume += inv.MaxVolume;
}
}
blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyBeacon>(blocks, FilterAntenna);
GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks, FilterAntenna);
if (blocks.Count == 0)
throw new Exception("Did not find the specified antenna");
var antenna = blocks[0];
StringBuilder sb = new StringBuilder();
sb.Append(antennaName).Append(" - ");
sb.Append((long)(totalVolume * K)).Append(" / ").Append((long)(totalMaxVolume * K));
sb.Append(" (").Append(VRageMath.MathHelper.RoundOn2(100 * (float)(totalVolume * K) / (float)(totalMaxVolume * K))).Append("%)");
//antenna.SetCustomName(sb.ToString());
if (totalVolume == totalMaxVolume)
{
IMyTerminalBlock block = GridTerminalSystem.GetBlockWithName(stop);
if (block == null)
throw new Exception("Could not find block with name: '" + stop + "'");
block.SetCustomName(block.CustomName + "full,");
block.ApplyAction("Run");
}
Echo(sb.ToString());
debug.Clear();
}
private bool FilterInventoryOwner(IMyTerminalBlock arg)
{
return arg != null && !(arg is IMyReactor);
}
private bool FilterAntenna(IMyTerminalBlock arg)
{
return arg != null && arg.CustomName.Contains(antennaName);
}
void Debug(String message)
{
var list = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName(debugName, list);
if (list.Count > 0)
list[0].SetCustomName(debugName + ":\n\r" + message);
}
}
}