diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index 8b4e868..3e07a62 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -44,29 +44,29 @@ end 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 - debugPrint("Building item tree for inventory at slot index: " .. slotIndex) - debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items") + -- 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'") + -- 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) + -- 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) + -- 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 @@ -77,7 +77,7 @@ local function buildItemTree(inventory, itemTree) slotIndex = slotIndex - 1, maxFits = slot.HowManyCanBePut(item.Prefab) } - debugPrint("Added item to itemTree under identifier: " .. identifier) + -- debugPrint("Added item to itemTree under identifier: " .. identifier) local tags = item.Prefab.Tags local shouldSuss = false @@ -89,13 +89,13 @@ local function buildItemTree(inventory, itemTree) end if shouldSuss then - debugPrint("Searching inside " .. item.Name .. " for nested containers") + -- debugPrint("Searching inside " .. item.Name .. " for nested containers") buildItemTree(item.OwnInventory, itemTree) end end end - debugPrint("Completed building item tree") + -- debugPrint("Completed building item tree") return itemTree end @@ -173,10 +173,13 @@ local enqueueInventory ---@return Barotrauma.Item[], string enqueueItem = function(item, queue) queue = queue or {} + -- debugPrint("Enqueuing item: " .. item.Prefab.Identifier.Value) -- Debug log for item being enqueued if item.OwnInventory then + -- debugPrint("Item has its own inventory, enqueuing inventory...") -- Debug log for own inventory queue = enqueueInventory(item.OwnInventory, queue) end queue[#queue + 1] = item + -- debugPrint("Item enqueued. Current queue size: " .. #queue) -- Debug log for current queue size return queue end @@ -185,6 +188,7 @@ end ---@return Barotrauma.Item[], string enqueueSlot = function(slot, queue) queue = queue or {} + -- debugPrint("Enqueuing slot with " .. #slot.items .. " items.") -- Debug log for slot items count -- We don't want to shadow queue local err -- If the slot is empty there's nothing to iterate @@ -192,9 +196,10 @@ enqueueSlot = function(slot, queue) for _, item in ipairs(slot.items) do queue, err = enqueueItem(item, queue) if err then - print("Error enqueuing item: " .. err) + -- debugPrint("Error enqueuing item: " .. err) -- Debug log for error enqueuing item end end + -- debugPrint("Finished enqueuing slot. Current queue size: " .. #queue) -- Debug log for current queue size return queue end @@ -203,14 +208,15 @@ end ---@return Barotrauma.Item[], string[] enqueueInventory = function(inventory, queue) queue = queue or {} - -- We don't want to shadow queue + -- debugPrint("Enqueuing inventory with " .. #inventory.slots .. " slots.") -- Debug log for inventory slots count local err for _, slot in ipairs(inventory.slots) do queue, err = enqueueSlot(slot, queue) if err then - print("Error enqueuing slot: " .. err) + -- debugPrint("Error enqueuing slot: " .. err) -- Debug log for error enqueuing slot end end + -- debugPrint("Finished enqueuing inventory. Current queue size: " .. #queue) -- Debug log for current queue size return queue end @@ -291,20 +297,29 @@ end -- This is a bit fucking sucky..... -- But I really don't know better -- Maybe it will be fine... ----@return Barotrauma.ItemInventory -local function getOpenContainer() +---@return Barotrauma.Item[] +local function getOpenContainers() debugPrint("Attempting to find open container...") + local containers = {} for item in Item.ItemList do ---@cast item Barotrauma.Item - if item and item.OwnInventory then - if item.OwnInventory.visualSlots and #item.OwnInventory.visualSlots > 0 then - return item.OwnInventory - end + local isok = true + isok = isok and item ~= nil + isok = isok and item.OwnInventory ~= nil + isok = isok and item.OwnInventory.visualSlots ~= nil + isok = isok and #item.OwnInventory.visualSlots > 0 + -- I don't know what rootContainer is + -- It seems to be the parent of the current item...? + -- Maybe the world object... + -- Either way - static objects that we may open have it + -- And our own inventory does not + -- So it's a good selector for now + isok = isok and item.rootContainer ~= nil + if isok then + containers[#containers + 1] = item end end - - debugPrint("No open container found") - return nil + return containers end -- We would like to fill larger stacks first @@ -326,15 +341,15 @@ end ---@return table, string local function tryBuildItemTree(inventory) local itemTree = {} - debugPrint("Preparing to stack items into the bag...") + -- 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.") + -- 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) + -- 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...") + -- debugPrint("Item has its own inventory, building item tree for it...") itemTree = buildItemTree(item.OwnInventory, itemTree) else return itemTree, "Bag does not have its own inventory." @@ -374,9 +389,14 @@ local function quickStackItems(character) -- toMove = enqueueInventory(inventory, toMove) - local openContainerInventory = getOpenContainer() - if openContainerInventory then - toMove = enqueueInventory(openContainerInventory, toMove) + local openContainers = getOpenContainers() + for _, container in ipairs(openContainers) do + debugPrint(string.format("Enqueuing inventory %s with %d slots", container.Name, + #container.OwnInventory.slots)) + local before = #toMove + toMove = enqueueInventory(container.OwnInventory, toMove) + local after = #toMove + debugPrint("Enqueued " .. after - before .. " items from the open container") end local errors = tryMoveItems(toMove, itemTree)