From 964f101865af9abdd2384c996776a86d18925d99 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 29 Mar 2025 22:16:06 +0100 Subject: [PATCH] Implement proper quickstacking --- QuickStackToBag/Lua/Autorun/init.lua | 54 ++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index 22e6800..7828ea2 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -368,7 +368,7 @@ local function buildItemTree(inventory, itemTree, iter) itemTree['empty'] = itemTree['empty'] or {} itemTree['empty'][#itemTree['empty'] + 1] = { inventory = inventory, - slotIndex = slotIndex, + slotIndex = slotIndex - 1, maxFits = 60 } debugPrint("Added empty slot to itemTree at index: " .. slotIndex) @@ -376,8 +376,7 @@ local function buildItemTree(inventory, itemTree, iter) ---@type Barotrauma.Item local item = slot.items[1] local identifier = item.Prefab.Identifier.Value - debugPrint("Found item: " .. - item.Name .. " with identifier: " .. identifier .. ", max stack size: " .. maxStackSize) + 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 @@ -385,11 +384,10 @@ local function buildItemTree(inventory, itemTree, iter) -- And based on that we decide to move it itemTree[identifier][#itemTree[identifier] + 1] = { inventory = inventory, - slotIndex = slotIndex, + slotIndex = slotIndex - 1, maxFits = slot.HowManyCanBePut(item.Prefab) } - debugPrint("Added item to itemTree under identifier: " .. - identifier .. ", available fits: " .. (maxStackSize - #slot.items)) + debugPrint("Added item to itemTree under identifier: " .. identifier) end end @@ -417,6 +415,48 @@ local function buildItemTree(inventory, itemTree, iter) return itemTree end +local function stackInventoryItems(inventory, itemTree) + debugPrint("Starting to stack inventory items...") + for slotIndex, slot in ipairs(inventory.slots) do + debugPrint("Checking slot index: " .. slotIndex) + -- Cannot stack items if there are no items... + if #slot.items > 0 then + ---@type Barotrauma.Item + local item = slot.items[1] + local identifier = item.Prefab.Identifier.Value + local toMove = #slot.items + debugPrint("Items to move: " .. toMove) + ---@type ItemLocation[] + local locations = itemTree[identifier] + if locations then + debugPrint("Found locations for identifier: " .. identifier) + for _, location in ipairs(locations) do + local moveHere = math.min(location.maxFits, toMove) + debugPrint("Attempting to move " .. + moveHere .. " items to inventory at slot index: " .. location.slotIndex) + if moveHere > 0 then + for _, itemToMove in ipairs(slot.items) do + location.inventory.TryPutItem(itemToMove, location.slotIndex, false, true, nil) + toMove = toMove - 1 + location.maxFits = location.maxFits - 1 + if location.maxFits <= 0 then location.maxFits = 0 end + if toMove <= 0 then break end + end + debugPrint("Moved " .. moveHere .. " items. Remaining to move: " .. toMove) + else + debugPrint("No items to move for this location.") + end + end + else + debugPrint("No locations found for identifier: " .. identifier) + end + else + debugPrint("Slot index " .. slotIndex .. " is empty.") + end + end + debugPrint("Completed stacking inventory items.") +end + -- Find the currently open container - improved version local function getOpenContainer() debugPrint("Attempting to find open container...") @@ -575,6 +615,8 @@ local function quickStackItems(character) local itemTree = buildItemTree(inventory, {}) DumpTable(itemTree) + stackInventoryItems(inventory, itemTree) + -- Find all containers in player inventory, including nested ones -- local containers = findAllContainers(inventory, {})