-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStorageSystemManager.cs
226 lines (196 loc) · 7.96 KB
/
StorageSystemManager.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
namespace Bulldozer
{
/// <summary>Helps finding items on local planet from player, storage boxes and logistics stations</summary>
public class StorageSystemManager
{
private static StorageSystemManager _instance;
private readonly PlanetFactory _factory;
private readonly Player _player;
private StorageSystemManager(PlanetFactory factory, Player player)
{
_factory = factory;
_player = player;
}
private static StorageSystemManager GetInstance()
{
return new StorageSystemManager(GameMain.mainPlayer.factory, GameMain.mainPlayer);
}
private int CountLocalStationItems(int itemId)
{
var result = 0;
for (int i = 1; i < _factory.transport.stationCursor; i++)
{
var station = _factory.transport.stationPool[i];
if (station?.id != i)
continue;
foreach (var store in station.storage)
{
if (store.itemId != itemId)
{
continue;
}
if (store.itemId != itemId)
continue;
result += store.count;
}
}
return result;
}
private int CountLocalStorageItems(int itemId)
{
var result = 0;
if (_factory.factoryStorage?.storagePool == null)
{
return result;
}
for (int i = 0; i < _factory.factoryStorage.storageCursor; i++)
{
var storageComponent = _factory.factoryStorage.storagePool[i];
if (storageComponent == null || storageComponent.id <= 0)
continue;
result += storageComponent.GetItemCount(itemId);
}
return result;
}
public static (int itemsRemoved, bool successful) RemoveItems(int itemId, int count)
{
var itemsRemoved = GetInstance().DoRemove(itemId, count);
return (itemsRemoved, itemsRemoved >= count);
}
private int DoRemove(int itemId, int count)
{
var removed = 0;
var (removedFromLocation, successful) = RemoveFromPlayer(itemId, count);
if (successful)
{
return removedFromLocation;
}
removed += removedFromLocation;
(removedFromLocation, successful) = RemoveFromStorage(itemId, count - removed);
if (successful)
return count;
removed += removedFromLocation;
(removedFromLocation, successful) = RemoveFromStations(itemId, count - removed);
return removed + removedFromLocation;
}
private (int, bool successful) RemoveFromStorage(int itemId, int count)
{
var removed = 0;
for (int i = 0; i < _factory.factoryStorage.storageCursor; i++)
{
var storageComponent = _factory.factoryStorage.storagePool[i];
if (storageComponent?.id != i)
continue;
var remaining = count - removed;
var itemIdRef = itemId;
var remainRef = remaining;
storageComponent.TakeTailItems(ref itemIdRef, ref remainRef, out _);
removed += remainRef;
if (removed >= count)
return (removed, true);
}
return (removed, removed >= count);
}
private (int removed, bool successful) RemoveFromStations(int itemId, int count)
{
var removed = 0;
for (int i = 0; i < _factory.transport.stationCursor; i++)
{
var station = _factory.transport.stationPool[i];
if (station?.id != i)
continue;
var itemRef = itemId;
var countRef = count - removed;
station.TakeItem(ref itemRef, ref countRef, out _);
if (countRef > 0)
{
removed += countRef;
if (removed >= count)
return (removed, true);
}
}
return (removed, removed >= count);
}
private (int, bool) RemoveFromPlayer(int itemId, int count)
{
var amountToRemove = count;
var itemIdRef = itemId;
GameMain.mainPlayer.package.TakeTailItems(ref itemIdRef, ref amountToRemove, out _);
return amountToRemove == count ? (amountToRemove, true) : (amountToRemove, false);
}
public static (string message, bool hasEnoughCapacity, int remainingToRemove) BuildRemovalMessage(int itemId, int count)
{
var result = GetInstance().DoBuildRemovalMessage(itemId, count);
return (result.Item1.Trim(), Math.Max(0, result.Item2) <= 0, result.Item2);
}
private (string, int) DoBuildRemovalMessage(int itemId, int count)
{
if (_player?.package == null)
{
Log.logger.LogWarning("player not set");
return ("Failure while counting items", count);
}
var playerItemCount = _player.package.GetItemCount(itemId);
var itemName = GetItemName(itemId);
if (playerItemCount >= count)
return ($"{count} of {itemName} will be removed from your inventory (leaving {playerItemCount - count})", 0);
var message = "";
var remainingToRemove = count;
if (playerItemCount > 0)
{
message += $"{playerItemCount} of {itemName} will be removed from your inventory ({count - playerItemCount} needed after). ";
remainingToRemove -= playerItemCount;
}
var localStorageItems = 0;
try
{
localStorageItems = CountLocalStorageItems(itemId);
}
catch (Exception e)
{
Log.logger.LogWarning($"failure while trying to count items from local storage {e.Message}");
Log.logger.LogWarning(e.StackTrace);
}
if (localStorageItems > remainingToRemove)
{
message += $"\n{remainingToRemove} of {itemName} will be removed from storage ({localStorageItems} total). ";
remainingToRemove -= localStorageItems;
return (message, remainingToRemove);
}
if (localStorageItems > 0)
{
message += $"\nAll {localStorageItems} of {itemName} will be removed from storage.";
remainingToRemove -= localStorageItems;
}
if (remainingToRemove <= 0)
return (message, remainingToRemove);
var stationItems = 0;
try
{
stationItems = CountLocalStationItems(itemId);
}
catch (Exception e)
{
Log.logger.LogWarning($"failure while trying to count items from local logistics {e.Message}");
Log.logger.LogWarning(e.StackTrace);
}
if (stationItems > remainingToRemove)
{
message += $"\n{remainingToRemove} of {itemName} will be removed from local logistics stations ({stationItems} total). ";
remainingToRemove -= stationItems;
return (message, remainingToRemove);
}
if (stationItems > 0)
{
message += $"\nAll {stationItems} of {itemName} will be removed from local logistics stations.";
remainingToRemove -= stationItems;
}
return (message, remainingToRemove);
}
private static string GetItemName(int itemId)
{
return LDB.items.Select(itemId).Name.Translate();
}
}
}