-
Notifications
You must be signed in to change notification settings - Fork 10
/
ItemInventory.cs
200 lines (180 loc) · 5.18 KB
/
ItemInventory.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
using MechTransfer.ContainerAdapters;
using System;
using System.Collections.Generic;
using Terraria;
namespace MechTransfer
{
public class ItemCatalog
{
private List<ContainerAdapter> container = new List<ContainerAdapter>();
private List<object> identifier = new List<object>();
private List<int> amount = new List<int>();
private List<int> amountTook = new List<int>();
public void RegisterItem(ContainerAdapter c, Tuple<Item, object> i)
{
container.Add(c);
identifier.Add(i.Item2);
amount.Add(i.Item1.stack);
amountTook.Add(0);
}
private int TotalSingle(int i)
{
return amount[i] - amountTook[i];
}
public int Total()
{
int total = 0;
for (int i = 0; i < amount.Count; i++)
{
total += TotalSingle(i);
}
return total;
}
public void Take(int many)
{
int left = many;
for (int i = 0; i < amount.Count; i++)
{
int single = TotalSingle(i);
if (single >= left)
{
amountTook[i] += left;
return;
}
else
{
amountTook[i] += single;
left -= single;
}
}
}
public void Commit()
{
for (int i = 0; i < amount.Count; i++)
{
if (amountTook[i] > 0)
container[i].TakeItem(identifier[i], amountTook[i]);
}
}
public void CommitAlchemy()
{
for (int i = 0; i < amount.Count; i++)
{
for (int j = 0; j < amountTook[i]; j++)
{
if (Main.rand.Next(3) != 0)
container[i].TakeItem(identifier[i], 1);
}
}
}
public void Reset()
{
for (int i = 0; i < amount.Count; i++)
{
amountTook[i] = 0;
}
}
}
public class ItemInventory
{
private Dictionary<int, ItemCatalog> catalogs = new Dictionary<int, ItemCatalog>();
public void Commit(bool alchemy)
{
if (alchemy)
{
foreach (var item in catalogs)
{
item.Value.CommitAlchemy();
}
}
else
{
foreach (var item in catalogs)
{
item.Value.Commit();
}
}
}
public void Reset()
{
foreach (var item in catalogs)
{
item.Value.Reset();
}
}
public void Clear()
{
catalogs.Clear();
}
public void RegisterContainer(ContainerAdapter container)
{
foreach (var item in container.EnumerateItems())
{
if (!item.Item1.IsAir)
{
if (!catalogs.ContainsKey(item.Item1.type))
catalogs.Add(item.Item1.type, new ItemCatalog());
catalogs[item.Item1.type].RegisterItem(container, item);
}
}
}
public int ItemCount(int type)
{
ItemCatalog catalog;
if (catalogs.TryGetValue(type, out catalog))
{
return catalog.Total();
}
else
{
return 0;
}
}
public void TakeItem(Item take)
{
catalogs[take.type].Take(take.stack);
}
public bool TryTakeItem(Item take)
{
ItemCatalog catalog;
if (catalogs.TryGetValue(take.type, out catalog))
{
if (catalog.Total() >= take.stack)
{
catalog.Take(take.stack);
return true;
}
else
{
return false;
}
}
return false;
}
public bool TryTakeIngredient(Recipe recipe, Item ingredient)
{
int need = ingredient.stack;
int type = ingredient.type;
foreach (var item in catalogs)
{
// useWood, useSand, useIronBar, useFragment and usePressurePlate are no longer accessible,
// but tModLoader convers to item groups now, so this should be fine.
if (recipe.AcceptedByItemGroups(item.Key, type) || item.Key == type)
{
int total = item.Value.Total();
if (total >= need)
{
item.Value.Take(need);
return true;
}
else
{
item.Value.Take(total);
need -= total;
}
}
}
return false;
}
}
}