From b440127e54e4fcc587a81aa0c99b530647e343e1 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 29 Mar 2025 23:03:10 +0100 Subject: [PATCH] Clean up building item tree to only use bag --- QuickStackToBag/Lua/Autorun/init.lua | 131 +++++++++++++++------------ 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index d537692..044a00a 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -41,55 +41,44 @@ end ---@param inventory Barotrauma.ItemInventory ---@param itemTree table ---@return table -local function buildItemTree(inventory, itemTree, iter) - iter = iter or 0 +local function buildItemTree(inventory, itemTree) itemTree = itemTree or {} if not inventory or not inventory.slots then - -- debugPrint("Inventory is nil or has no slots, returning empty itemTree") + debugPrint("Inventory is nil or has no slots, returning empty itemTree") return itemTree end -- One slot can have one item but multiple of it -- The number of an item in a slot is #slot.items for slotIndex, slot in ipairs(inventory.slots) do - -- This iteration is done only to skip the player inventory as a potential destination - -- Since it will always be 0th iteration and there's no point stacking - -- Items from the player inventory to itself - -- debugPrint("Building item tree for inventory at iteration: " .. iter .. ", slot index: " .. slotIndex) - -- debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items") - if iter > 0 then - if #slot.items == 0 then - -- debugPrint("Slot " .. slotIndex .. " is empty, adding to itemTree as 'empty'") - itemTree['empty'] = itemTree['empty'] or {} - itemTree['empty'][#itemTree['empty'] + 1] = { - inventory = inventory, - slotIndex = slotIndex - 1, - maxFits = 60 - } - -- debugPrint("Added empty slot to itemTree at index: " .. slotIndex) - else - ---@type Barotrauma.Item - local item = slot.items[1] - local identifier = item.Prefab.Identifier.Value - -- debugPrint("Found item: " .. item.Name .. " with identifier: " .. identifier) - itemTree[identifier] = itemTree[identifier] or {} - -- We DO want even slots with maxFits = 0 - -- Because that indicates that we DO HAVE the item - -- At all - -- And based on that we decide to move it - itemTree[identifier][#itemTree[identifier] + 1] = { - inventory = inventory, - slotIndex = slotIndex - 1, - maxFits = slot.HowManyCanBePut(item.Prefab) - } - -- debugPrint("Added item to itemTree under identifier: " .. identifier) - end - end - - - if #slot.items > 0 then + debugPrint("Building item tree for inventory at slot index: " .. slotIndex) + debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items") + if #slot.items == 0 then + debugPrint("Slot " .. slotIndex .. " is empty, adding to itemTree as 'empty'") + itemTree['empty'] = itemTree['empty'] or {} + itemTree['empty'][#itemTree['empty'] + 1] = { + inventory = inventory, + slotIndex = slotIndex - 1, + maxFits = 60 + } + debugPrint("Added empty slot to itemTree at index: " .. slotIndex) + else ---@type Barotrauma.Item local item = slot.items[1] + local identifier = item.Prefab.Identifier.Value + debugPrint("Found item: " .. item.Name .. " with identifier: " .. identifier) + itemTree[identifier] = itemTree[identifier] or {} + -- We DO want even slots with maxFits = 0 + -- Because that indicates that we DO HAVE the item + -- At all + -- And based on that we decide to move it + itemTree[identifier][#itemTree[identifier] + 1] = { + inventory = inventory, + slotIndex = slotIndex - 1, + maxFits = slot.HowManyCanBePut(item.Prefab) + } + debugPrint("Added item to itemTree under identifier: " .. identifier) + local tags = item.Prefab.Tags local shouldSuss = false for tag in tags do @@ -100,13 +89,13 @@ local function buildItemTree(inventory, itemTree, iter) end if shouldSuss then - -- debugPrint("Searching inside " .. item.Name .. " for nested containers") - buildItemTree(item.OwnInventory, itemTree, iter + 1) + debugPrint("Searching inside " .. item.Name .. " for nested containers") + buildItemTree(item.OwnInventory, itemTree) end end end - -- debugPrint("Completed building item tree for current inventory iteration: " .. iter) + debugPrint("Completed building item tree") return itemTree end @@ -135,6 +124,7 @@ local function stackInventoryItems(inventory, itemTree) ---@type Barotrauma.Item local item = slot.items[1] local identifier = item.Prefab.Identifier.Value + debugPrint("Item at slot " .. slotIndex .. " is " .. identifier) ---@type ItemLocation[] local locations = itemTree[identifier] @@ -211,31 +201,52 @@ local function quickStackItems(character) return end - local itemTree = buildItemTree(inventory, {}) + local itemTree = {} + itemTree = buildItemTree(inventory, itemTree) itemTree = sortItemtreeBySlots(itemTree) - stackInventoryItems(inventory, itemTree) + debugPrint("Preparing to stack items into the bag...") + local bagSlot = inventory.slots[8] + if bagSlot then + debugPrint("Bag slot found at index 8 with " .. #bagSlot.items .. " items.") + if #bagSlot.items > 0 then + local item = bagSlot.items[1] + debugPrint("Found item in bag slot: " .. item.Name) + if item and item.OwnInventory then + debugPrint("Item has its own inventory, building item tree for it...") + itemTree = buildItemTree(item.OwnInventory, itemTree) + else + debugPrint("Item does not have its own inventory.") + end + else + debugPrint("Bag slot is empty.") + end + else + debugPrint("No bag slot found at index 8.") + end + + stackInventoryItems(item.OwnInventory, itemTree) local openContainerInventory = getOpenContainer() if openContainerInventory then stackInventoryItems(openContainerInventory, itemTree) end - local handItems = {} - for _, slotIndex in ipairs(CONFIG.HAND_SLOTS) do - local slot = inventory.slots[slotIndex] - debugPrint("Checking hand slot index: " .. slotIndex) - if slot then - debugPrint("Hand slot " .. slotIndex .. " found with " .. #slot.items .. " items.") - if #slot.items > 0 then - handItems[#handItems + 1] = slot.items[1] - debugPrint("Added item " .. slot.items[1].Name .. " from hand slot " .. slotIndex .. " to handItems.") - else - debugPrint("Hand slot " .. slotIndex .. " is empty.") - end - else - debugPrint("Hand slot " .. slotIndex .. " does not exist.") - end - end + --local handItems = {} + --for _, slotIndex in ipairs(CONFIG.HAND_SLOTS) do + -- local slot = inventory.slots[slotIndex] + -- debugPrint("Checking hand slot index: " .. slotIndex) + -- if slot then + -- debugPrint("Hand slot " .. slotIndex .. " found with " .. #slot.items .. " items.") + -- if #slot.items > 0 then + -- handItems[#handItems + 1] = slot.items[1] + -- debugPrint("Added item " .. slot.items[1].Name .. " from hand slot " .. slotIndex .. " to handItems.") + -- else + -- debugPrint("Hand slot " .. slotIndex .. " is empty.") + -- end + -- else + -- debugPrint("Hand slot " .. slotIndex .. " does not exist.") + -- end + --end end -- Hook into player control to listen for key press