-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerFunctions.cs
More file actions
148 lines (124 loc) · 5.66 KB
/
ContainerFunctions.cs
File metadata and controls
148 lines (124 loc) · 5.66 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
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 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
{
private void fillDictWithResources(List<IMyTerminalBlock> cargoContainers,
ref Dictionary<string, int> resources, string resourceName)
{
foreach (var cargo in cargoContainers)
{
IMyInventory cargoInventory = cargo.GetInventory();
int itemCount = cargoInventory.ItemCount;
for (int i = 0; i < itemCount; i++)
{
MyInventoryItem? item = cargoInventory.GetItemAt(i);
string itemName = item.Value.Type.SubtypeId;
string itemSubtype = item.Value.Type.TypeId;
int itemAmount = item.Value.Amount.ToIntSafe();
// Adds up the total amount of a certain Ore
if (itemSubtype.Contains(resourceName))
{
if (resources.ContainsKey(itemName))
resources[itemName] += itemAmount;
else
{
resources.Add(itemName, itemAmount);
resources = resources.OrderBy(x => x.Key).ToDictionary(x =>x.Key, y=>y.Value);
}
}
}
}
}
private void fillDictWithResourcesTwoInventory(List<IMyTerminalBlock> cargoContainers,
ref Dictionary<string, int> resources, string resourceName, int inventoryIndex)
{
foreach (var cargo in cargoContainers)
{
IMyInventory cargoInventory = cargo.GetInventory(inventoryIndex);
int itemCount = cargoInventory.ItemCount;
for (int i = 0; i < itemCount; i++)
{
MyInventoryItem? item = cargoInventory.GetItemAt(i);
string itemName = item.Value.Type.SubtypeId;
string itemSubtype = item.Value.Type.TypeId;
int itemAmount = item.Value.Amount.ToIntSafe();
// Adds up the total amount of a certain Ore
if (itemSubtype.Contains(resourceName))
{
if (resources.ContainsKey(itemName))
resources[itemName] += itemAmount;
else
{
resources.Add(itemName, itemAmount);
resources = resources.OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
}
}
}
}
}
private void addMissingComponentToAssemblerQueue(Dictionary<string, int> inventoryDict,
Dictionary<string, int> requestDict, List<IMyAssembler> assemblers)
{
Dictionary<string, int> itemsToAddToQueue = new Dictionary<string, int>();
IMyAssembler removeFromList = null;
// Get the amount of items to add to the queue
foreach (KeyValuePair<string, int> request in requestDict)
{
int requestAmount = request.Value - inventoryDict[request.Key];
if (requestAmount > 0)
itemsToAddToQueue.Add(request.Key, requestAmount / assemblers.Count);
}
foreach(KeyValuePair<string, int> request in itemsToAddToQueue)
{
Echo($"Requesting fromeach: {request.Key} = {request.Value}");
}
// If assembler not producing the item - add it to the queue
foreach(IMyAssembler assembler in assemblers)
{
if (assembler.CustomName.Contains("Survival"))
removeFromList = assembler;
List<MyProductionItem> assemblerProductionQueue = new List<MyProductionItem>();
assembler.GetQueue(assemblerProductionQueue);
foreach(KeyValuePair<string, int> requestQueueItem in itemsToAddToQueue)
{
float amountToAddToQueue;
int amountAlreadyInQueue = 0;
// If already in production queue then add a different amount to the queue
foreach(MyProductionItem productionQueueItem in assemblerProductionQueue)
{
if (productionQueueItem.BlueprintId.ToString().Contains(requestQueueItem.Key))
amountAlreadyInQueue += productionQueueItem.Amount.ToIntSafe();
}
amountToAddToQueue = requestQueueItem.Value - amountAlreadyInQueue;
if (amountToAddToQueue > 0)
{
MyDefinitionId definitionId = MyDefinitionId.Parse(subIdToBlueprintsDict[requestQueueItem.Key]);
assembler.AddQueueItem(definitionId, amountToAddToQueue);
}
}
}
// Removes survival Kits
if(removeFromList != null)
assemblers.Remove(removeFromList);
}
}
}