-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManaged_Refinery.cs
53 lines (42 loc) · 1.7 KB
/
Managed_Refinery.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
///<summary> Managed Refinery script makes sure that every refinery always has the same amount of ressources in production
///<author> Cranc - Debauchery Engineer Tea Party
void Main()
{
var refinery_list = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyRefinery>(refinery_list);
List<KeyValuePair<IMyTerminalBlock, double>> ref_list;
ref_list = new List<KeyValuePair<IMyTerminalBlock, double>>();
for (int i = 0; i < refinery_list.Count; i++ ){
ref_list.Add(new KeyValuePair<IMyTerminalBlock, double>(refinery_list[i], GetMassOfInventory(refinery_list[i])));
}
List<KeyValuePair<IMyTerminalBlock, double>> mySortList = ref_list;
if( mySortList != null) {
mySortList.Sort((x,y)=>y.Value.CompareTo(x.Value));
int end = mySortList.Count - 1;
for (int start = 0; start < end; start++){
var dif = mySortList[start].Value - mySortList[end].Value;
dif /= 2;
var inv_one = mySortList[start].Key.GetInventory(0);
var inv_two = mySortList[end].Key.GetInventory(0);
if( inv_one.IsConnectedTo(inv_two)) {
var theresult = inv_one.TransferItemTo(
inv_two,
0,
stackIfPossible: true,
amount: (VRage.MyFixedPoint)dif
);
}else{
throw new Exception("inventorys not connected");
}
end--;
}
}else{
throw new Exception("no refinery in list");
}
}
///<param name="myref"> refinery block
double GetMassOfInventory(IMyTerminalBlock myref)
{
var input_inv = myref.GetInventory(0);
return (double)(input_inv.CurrentMass);
}