-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaser Welder Reload.cs
More file actions
155 lines (139 loc) · 4.43 KB
/
Copy pathLaser Welder Reload.cs
File metadata and controls
155 lines (139 loc) · 4.43 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
/*
* Laser Welder Reload v2
* -----------
*
* 1. Ensure all components are in one cargo container.
* 2. Put the name of the cargo container in the const string below.
* 3. Group all of your welders in one group by themselves.
* 4. Put that group name in the const string below.
* 5. Have a timer block run this script once every second.
* 6. Enjoy!
*/
private const string SingleCargoContainerName = "Base-Only XL Cargo Container Components";
private const string WelderGroupName = "Laser Welders";
static readonly Dictionary<string, int> ComponentDict = new Dictionary<string, int>
{
{"BulletproofGlass", 10},
{"Computer", 100},
{"Construction", 150},
{"Detector", 10},
{"Display", 100},
{"Girder", 100},
{"GravityGenerator", 10},
{"InteriorPlate", 150},
{"LargeTube", 50},
{"Medical", 5},
{"MetalGrid", 150},
{"Motor", 100},
{"PowerCell", 25},
{"RadioCommunication", 10},
{"Reactor", 100},
{"SmallTube", 100},
{"SolarCell", 5},
{"SteelPlate", 350},
{"Superconductor", 25},
{"Thrust", 100}
};
public void Main(string argument, UpdateType updateSource)
{
string ERR_TXT = "";
List<IMyTerminalBlock> l0 = new List<IMyTerminalBlock>();
IMyCargoContainer v0 = null;
GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(l0);
if (l0.Count == 0)
{
ERR_TXT += "no Large Cargo Container blocks found\n";
}
else
{
for (int i = 0; i < l0.Count; i++)
{
if (l0[i].CustomName == SingleCargoContainerName)
{
v0 = (IMyCargoContainer)l0[i];
break;
}
}
if (v0 == null)
{
ERR_TXT += "no Large Cargo Container block named " + SingleCargoContainerName + " found\n";
}
}
List<IMyTerminalBlock> v1 = new List<IMyTerminalBlock>();
if (GridTerminalSystem.GetBlockGroupWithName(WelderGroupName) != null)
{
GridTerminalSystem.GetBlockGroupWithName(WelderGroupName).GetBlocksOfType<IMyShipWelder>(v1);
if (v1.Count == 0)
{
ERR_TXT += "group LG lasers has no Welder blocks\n";
}
}
else
{
ERR_TXT += "group LG lasers not found\n";
}
if (ERR_TXT != "")
{
Echo("Script Errors:\n" + ERR_TXT + "(make sure block ownership is set correctly)");
return;
}
else { Echo(""); }
int currentLaserWelder = 0;
int.TryParse(Me.CustomData, out currentLaserWelder);
if (currentLaserWelder > (v1.Count - 1))
{
currentLaserWelder = 0;
Me.CustomData = "0";
}
Me.CustomData = "" + (currentLaserWelder + 1);
for (int i = ComponentDict.Count - 1; i >= 0; i--)
{
KeyValuePair<string, int> Component = ComponentDict.ElementAt(i);
string ComponentKey = Component.Key;
int ComponentValue = Component.Value;
float currentItemAmount = countItem(v1[currentLaserWelder].GetInventory(0), "Component", ComponentKey);
float itemAmountToAdd = ComponentValue - currentItemAmount;
if (itemAmountToAdd > 0)
{
transfer(v0.GetInventory(0), v1[currentLaserWelder].GetInventory(0), "Component", ComponentKey, itemAmountToAdd);
}
}
Echo(v1[currentLaserWelder].CustomName);
}
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;
}
void transfer(IMyInventory a, IMyInventory b, string type, string sType, float amount)
{
List<MyInventoryItem> items = new List<MyInventoryItem>();
a.GetItems(items);
float left = amount;
for (int i = items.Count - 1; i >= 0; i--)
{
if (left > 0 && items[i].Type.TypeId.ToString().EndsWith(type) && items[i].Type.SubtypeId.ToString() == sType)
{
if ((float)items[i].Amount > left)
{
a.TransferItemTo(b, i, null, true, (VRage.MyFixedPoint)amount);
left = 0;
break;
}
else
{
left -= (float)items[i].Amount;
a.TransferItemTo(b, i, null, true, null);
}
}
}
}