-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cs
221 lines (179 loc) · 6.77 KB
/
Inventory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace gridproject
{
public enum Item
{
None, Cobblestone, Wood, Stone, Charcoal, Coal, Plank, WoodenPickaxe, Stick, IronOre, IronIngot, CraftingTable, Chest, StonePickaxe, Furnace, WoodenShovel, Dirt, GoldOre, Diamond, IronPickaxe,
Bedrock, DiamondPickaxe, DebugStick, GoldIngot, StoneShovel, IronShovel, GoldenShovel, DiamondShovel, GoldenPickaxe, WoodenAxe, StoneAxe, IronAxe, GoldenAxe, DiamondAxe, CoolCraftingTable,
Locator, NetheriteScrap, NetheriteIngot,
StoneCore, IronCore, CoalCore, WoodCore, GoldCore, DiamondCore,
HemisphereA, HemisphereB, Perforator, Activator,
TheFinale,
Landfill, Emerald, CoolChest, DirtReactor
}
public enum AccessLevel
{
Low, Mid, High
}
public class ItemStack
{
public Item Item;
public int Count;
public ItemStack(Item item, int count)
{
Item = item;
Count = count;
}
public ItemStack Single => new ItemStack(Item, 1);
}
public class Inventory
{
private List<ItemStack> Items = new List<ItemStack>();
public int Capacity { get; private set; }
public Inventory(int capacity, AccessLevel accessLevel)
{
Capacity = capacity;
AccessLevel = accessLevel;
for (int i = 0; i < capacity; i++)
{
Items.Add(new ItemStack(Item.None, 0));
}
}
public Inventory(int capacity, AccessLevel accessLevel, Item[] filterItems) : this(capacity, accessLevel)
{
FilterItems = filterItems;
UseFilter = true;
}
public AccessLevel AccessLevel;
public Item[] FilterItems = new Item[1];
public bool UseFilter = false;
public int Occupied => Items.Where(i => i.Item != Item.None).Count();
public int ItemCount(Item item)
{
var itemStacks = Items.Where(s => s.Item == item).ToList();
if (itemStacks.Count <= 0) return -1;
int totalItems = 0;
foreach (var st in itemStacks)
totalItems += st.Count;
return totalItems;
}
public int InsertItem(ItemStack stackt)
{
ItemStack stack = new ItemStack(stackt.Item, stackt.Count);
if (stack.Count <= 0) return -1;
if (stack.Item == Item.None) return -1;
if (!FilterItems.Contains(stack.Item) && UseFilter) return -1;
var itemStack = Items.Find(i => i.Item == stack.Item && i.Count < Const.StackAmount);
if (itemStack == null && Occupied == Capacity) return -1;
if (itemStack == null)
{
var emptyStack = Items.Find(s => s.Item == Item.None);
emptyStack.Item = stack.Item;
if(stack.Count > Const.StackAmount)
{
emptyStack.Count = Const.StackAmount;
return InsertItem(new ItemStack(stack.Item, stack.Count - Const.StackAmount));
}
emptyStack.Count = stack.Count;
return 0;
}
else if(Occupied == Capacity)
{
if(itemStack.Count + stack.Count > Const.StackAmount)
{
var oldCount = itemStack.Count;
itemStack.Count = Const.StackAmount;
var ret = InsertItem(new ItemStack(stack.Item, itemStack.Count + stack.Count - Const.StackAmount));
if(ret < 0)
{
return Const.StackAmount - oldCount;
}
}
itemStack.Count += stack.Count;
return 0;
}
if (itemStack.Count + stack.Count > Const.StackAmount)
{
var newInsert = itemStack.Count + stack.Count - Const.StackAmount;
itemStack.Count = Const.StackAmount;
return InsertItem(new ItemStack(stack.Item, newInsert));
}
itemStack.Count += stack.Count;
return 0;
}
public int RemoveItem(ItemStack stack)
{
if (stack.Count <= 0) return -1;
if (stack.Item == Item.None) return -1;
var itemStacks = Items.Where(s => s.Item == stack.Item).ToList();
if (itemStacks.Count <= 0) return -1;
int totalItems = 0;
foreach (var st in itemStacks)
totalItems += st.Count;
if (totalItems < stack.Count) return -1;
foreach (var st in itemStacks)
{
if(stack.Count > st.Count)
{
stack.Count -= st.Count;
st.Count = 0;
st.Item = Item.None;
}
else
{
st.Count -= stack.Count;
if (st.Count == 0) st.Item = Item.None;
return 0;
}
}
return -1;
}
public bool TransferInventories(Inventory anotherInventory, ItemStack giveaway)
{
if(giveaway.Count == 64)
{
}
ItemStack giveawayRemove = new ItemStack(giveaway.Item, giveaway.Count);
ItemStack giveawayInsert = new ItemStack(giveaway.Item, giveaway.Count);
var itemsBefore = anotherInventory.ItemCount(giveaway.Item);
if ((int)AccessLevel < (int)anotherInventory.AccessLevel) return false;
var removeError = RemoveItem(giveawayRemove);
if (removeError < 0)
{
return false;
}
var insertError = anotherInventory.InsertItem(giveawayInsert);
if(anotherInventory.AccessLevel < AccessLevel)
{
Player.PointsCount += (anotherInventory.ItemCount(giveaway.Item) - itemsBefore) * Const.ItemReceivePoints[(int)giveaway.Item];
}
if(insertError < 0)
{
InsertItem(giveaway);
return false;
}
if (insertError > 0)
{
InsertItem(new ItemStack(giveaway.Item, insertError));
return false;
}
return true;
}
public bool ContainsItem(ItemStack stack)
{
var error = RemoveItem(stack);
if (error >= 0) InsertItem(stack);
return error >= 0;
}
public ItemStack GetSlot(int slot)
{
if (slot >= Capacity) return null;
return Items[slot];
}
}
}