Implement proper quickstacking

This commit is contained in:
2025-03-29 22:16:06 +01:00
parent 3bc045cacf
commit 964f101865

View File

@@ -368,7 +368,7 @@ local function buildItemTree(inventory, itemTree, iter)
itemTree['empty'] = itemTree['empty'] or {} itemTree['empty'] = itemTree['empty'] or {}
itemTree['empty'][#itemTree['empty'] + 1] = { itemTree['empty'][#itemTree['empty'] + 1] = {
inventory = inventory, inventory = inventory,
slotIndex = slotIndex, slotIndex = slotIndex - 1,
maxFits = 60 maxFits = 60
} }
debugPrint("Added empty slot to itemTree at index: " .. slotIndex) debugPrint("Added empty slot to itemTree at index: " .. slotIndex)
@@ -376,8 +376,7 @@ local function buildItemTree(inventory, itemTree, iter)
---@type Barotrauma.Item ---@type Barotrauma.Item
local item = slot.items[1] local item = slot.items[1]
local identifier = item.Prefab.Identifier.Value local identifier = item.Prefab.Identifier.Value
debugPrint("Found item: " .. debugPrint("Found item: " .. item.Name .. " with identifier: " .. identifier)
item.Name .. " with identifier: " .. identifier .. ", max stack size: " .. maxStackSize)
itemTree[identifier] = itemTree[identifier] or {} itemTree[identifier] = itemTree[identifier] or {}
-- We DO want even slots with maxFits = 0 -- We DO want even slots with maxFits = 0
-- Because that indicates that we DO HAVE the item -- 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 -- And based on that we decide to move it
itemTree[identifier][#itemTree[identifier] + 1] = { itemTree[identifier][#itemTree[identifier] + 1] = {
inventory = inventory, inventory = inventory,
slotIndex = slotIndex, slotIndex = slotIndex - 1,
maxFits = slot.HowManyCanBePut(item.Prefab) maxFits = slot.HowManyCanBePut(item.Prefab)
} }
debugPrint("Added item to itemTree under identifier: " .. debugPrint("Added item to itemTree under identifier: " .. identifier)
identifier .. ", available fits: " .. (maxStackSize - #slot.items))
end end
end end
@@ -417,6 +415,48 @@ local function buildItemTree(inventory, itemTree, iter)
return itemTree return itemTree
end 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 -- Find the currently open container - improved version
local function getOpenContainer() local function getOpenContainer()
debugPrint("Attempting to find open container...") debugPrint("Attempting to find open container...")
@@ -575,6 +615,8 @@ local function quickStackItems(character)
local itemTree = buildItemTree(inventory, {}) local itemTree = buildItemTree(inventory, {})
DumpTable(itemTree) DumpTable(itemTree)
stackInventoryItems(inventory, itemTree)
-- Find all containers in player inventory, including nested ones -- Find all containers in player inventory, including nested ones
-- local containers = findAllContainers(inventory, {}) -- local containers = findAllContainers(inventory, {})