|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows.Forms; |
| 7 | + |
| 8 | +namespace gridproject |
| 9 | +{ |
| 10 | + public enum Item |
| 11 | + { |
| 12 | + None, Cobblestone, Wood, Stone, Charcoal, Coal, Plank, WoodenPickaxe, Stick, IronOre, IronIngot, CraftingTable, Chest, StonePickaxe, Furnace, WoodenShovel, Dirt, GoldOre, Diamond, IronPickaxe, |
| 13 | + Bedrock, DiamondPickaxe, DebugStick, GoldIngot, StoneShovel, IronShovel, GoldenShovel, DiamondShovel, GoldenPickaxe, WoodenAxe, StoneAxe, IronAxe, GoldenAxe, DiamondAxe, CoolCraftingTable, |
| 14 | + Locator, NetheriteScrap, NetheriteIngot, |
| 15 | + |
| 16 | + StoneCore, IronCore, CoalCore, WoodCore, GoldCore, DiamondCore, |
| 17 | + HemisphereA, HemisphereB, Perforator, Activator, |
| 18 | + |
| 19 | + TheFinale, |
| 20 | + |
| 21 | + Landfill, Emerald, CoolChest, DirtReactor |
| 22 | + } |
| 23 | + |
| 24 | + public enum AccessLevel |
| 25 | + { |
| 26 | + Low, Mid, High |
| 27 | + } |
| 28 | + |
| 29 | + public class ItemStack |
| 30 | + { |
| 31 | + public Item Item; |
| 32 | + public int Count; |
| 33 | + |
| 34 | + public ItemStack(Item item, int count) |
| 35 | + { |
| 36 | + Item = item; |
| 37 | + Count = count; |
| 38 | + } |
| 39 | + |
| 40 | + public ItemStack Single => new ItemStack(Item, 1); |
| 41 | + } |
| 42 | + |
| 43 | + public class Inventory |
| 44 | + { |
| 45 | + |
| 46 | + |
| 47 | + private List<ItemStack> Items = new List<ItemStack>(); |
| 48 | + public int Capacity { get; private set; } |
| 49 | + |
| 50 | + public Inventory(int capacity, AccessLevel accessLevel) |
| 51 | + { |
| 52 | + Capacity = capacity; |
| 53 | + AccessLevel = accessLevel; |
| 54 | + |
| 55 | + for (int i = 0; i < capacity; i++) |
| 56 | + { |
| 57 | + Items.Add(new ItemStack(Item.None, 0)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public Inventory(int capacity, AccessLevel accessLevel, Item[] filterItems) : this(capacity, accessLevel) |
| 62 | + { |
| 63 | + FilterItems = filterItems; |
| 64 | + UseFilter = true; |
| 65 | + } |
| 66 | + |
| 67 | + public AccessLevel AccessLevel; |
| 68 | + |
| 69 | + public Item[] FilterItems = new Item[1]; |
| 70 | + public bool UseFilter = false; |
| 71 | + |
| 72 | + public int Occupied => Items.Where(i => i.Item != Item.None).Count(); |
| 73 | + public int ItemCount(Item item) |
| 74 | + { |
| 75 | + var itemStacks = Items.Where(s => s.Item == item).ToList(); |
| 76 | + if (itemStacks.Count <= 0) return -1; |
| 77 | + int totalItems = 0; |
| 78 | + foreach (var st in itemStacks) |
| 79 | + totalItems += st.Count; |
| 80 | + |
| 81 | + return totalItems; |
| 82 | + } |
| 83 | + public int InsertItem(ItemStack stackt) |
| 84 | + { |
| 85 | + ItemStack stack = new ItemStack(stackt.Item, stackt.Count); |
| 86 | + if (stack.Count <= 0) return -1; |
| 87 | + if (stack.Item == Item.None) return -1; |
| 88 | + if (!FilterItems.Contains(stack.Item) && UseFilter) return -1; |
| 89 | + |
| 90 | + var itemStack = Items.Find(i => i.Item == stack.Item && i.Count < Const.StackAmount); |
| 91 | + |
| 92 | + if (itemStack == null && Occupied == Capacity) return -1; |
| 93 | + |
| 94 | + if (itemStack == null) |
| 95 | + { |
| 96 | + |
| 97 | + var emptyStack = Items.Find(s => s.Item == Item.None); |
| 98 | + emptyStack.Item = stack.Item; |
| 99 | + if(stack.Count > Const.StackAmount) |
| 100 | + { |
| 101 | + emptyStack.Count = Const.StackAmount; |
| 102 | + return InsertItem(new ItemStack(stack.Item, stack.Count - Const.StackAmount)); |
| 103 | + } |
| 104 | + emptyStack.Count = stack.Count; |
| 105 | + return 0; |
| 106 | + } |
| 107 | + else if(Occupied == Capacity) |
| 108 | + { |
| 109 | + if(itemStack.Count + stack.Count > Const.StackAmount) |
| 110 | + { |
| 111 | + var oldCount = itemStack.Count; |
| 112 | + itemStack.Count = Const.StackAmount; |
| 113 | + var ret = InsertItem(new ItemStack(stack.Item, itemStack.Count + stack.Count - Const.StackAmount)); |
| 114 | + if(ret < 0) |
| 115 | + { |
| 116 | + return Const.StackAmount - oldCount; |
| 117 | + } |
| 118 | + } |
| 119 | + itemStack.Count += stack.Count; |
| 120 | + return 0; |
| 121 | + } |
| 122 | + |
| 123 | + if (itemStack.Count + stack.Count > Const.StackAmount) |
| 124 | + { |
| 125 | + var newInsert = itemStack.Count + stack.Count - Const.StackAmount; |
| 126 | + itemStack.Count = Const.StackAmount; |
| 127 | + return InsertItem(new ItemStack(stack.Item, newInsert)); |
| 128 | + } |
| 129 | + itemStack.Count += stack.Count; |
| 130 | + |
| 131 | + return 0; |
| 132 | + } |
| 133 | + public int RemoveItem(ItemStack stack) |
| 134 | + { |
| 135 | + if (stack.Count <= 0) return -1; |
| 136 | + if (stack.Item == Item.None) return -1; |
| 137 | + |
| 138 | + var itemStacks = Items.Where(s => s.Item == stack.Item).ToList(); |
| 139 | + if (itemStacks.Count <= 0) return -1; |
| 140 | + int totalItems = 0; |
| 141 | + foreach (var st in itemStacks) |
| 142 | + totalItems += st.Count; |
| 143 | + |
| 144 | + if (totalItems < stack.Count) return -1; |
| 145 | + |
| 146 | + foreach (var st in itemStacks) |
| 147 | + { |
| 148 | + if(stack.Count > st.Count) |
| 149 | + { |
| 150 | + stack.Count -= st.Count; |
| 151 | + st.Count = 0; |
| 152 | + st.Item = Item.None; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + st.Count -= stack.Count; |
| 157 | + if (st.Count == 0) st.Item = Item.None; |
| 158 | + |
| 159 | + return 0; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return -1; |
| 164 | + } |
| 165 | + |
| 166 | + public bool TransferInventories(Inventory anotherInventory, ItemStack giveaway) |
| 167 | + { |
| 168 | + if(giveaway.Count == 64) |
| 169 | + { |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + ItemStack giveawayRemove = new ItemStack(giveaway.Item, giveaway.Count); |
| 174 | + ItemStack giveawayInsert = new ItemStack(giveaway.Item, giveaway.Count); |
| 175 | + |
| 176 | + var itemsBefore = anotherInventory.ItemCount(giveaway.Item); |
| 177 | + |
| 178 | + |
| 179 | + if ((int)AccessLevel < (int)anotherInventory.AccessLevel) return false; |
| 180 | + |
| 181 | + var removeError = RemoveItem(giveawayRemove); |
| 182 | + if (removeError < 0) |
| 183 | + { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + var insertError = anotherInventory.InsertItem(giveawayInsert); |
| 188 | + if(anotherInventory.AccessLevel < AccessLevel) |
| 189 | + { |
| 190 | + Player.PointsCount += (anotherInventory.ItemCount(giveaway.Item) - itemsBefore) * Const.ItemReceivePoints[(int)giveaway.Item]; |
| 191 | + } |
| 192 | + if(insertError < 0) |
| 193 | + { |
| 194 | + InsertItem(giveaway); |
| 195 | + return false; |
| 196 | + } |
| 197 | + if (insertError > 0) |
| 198 | + { |
| 199 | + InsertItem(new ItemStack(giveaway.Item, insertError)); |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + public bool ContainsItem(ItemStack stack) |
| 207 | + { |
| 208 | + var error = RemoveItem(stack); |
| 209 | + |
| 210 | + if (error >= 0) InsertItem(stack); |
| 211 | + |
| 212 | + return error >= 0; |
| 213 | + } |
| 214 | + |
| 215 | + public ItemStack GetSlot(int slot) |
| 216 | + { |
| 217 | + if (slot >= Capacity) return null; |
| 218 | + return Items[slot]; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments