Clean up building item tree to only use bag

This commit is contained in:
2025-03-29 23:03:10 +01:00
parent b849d05187
commit b440127e54

View File

@@ -41,55 +41,44 @@ end
---@param inventory Barotrauma.ItemInventory ---@param inventory Barotrauma.ItemInventory
---@param itemTree table<string, ItemLocation[]> ---@param itemTree table<string, ItemLocation[]>
---@return table ---@return table
local function buildItemTree(inventory, itemTree, iter) local function buildItemTree(inventory, itemTree)
iter = iter or 0
itemTree = itemTree or {} itemTree = itemTree or {}
if not inventory or not inventory.slots then 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 return itemTree
end end
-- One slot can have one item but multiple of it -- One slot can have one item but multiple of it
-- The number of an item in a slot is #slot.items -- The number of an item in a slot is #slot.items
for slotIndex, slot in ipairs(inventory.slots) do for slotIndex, slot in ipairs(inventory.slots) do
-- This iteration is done only to skip the player inventory as a potential destination debugPrint("Building item tree for inventory at slot index: " .. slotIndex)
-- Since it will always be 0th iteration and there's no point stacking debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items")
-- Items from the player inventory to itself if #slot.items == 0 then
-- debugPrint("Building item tree for inventory at iteration: " .. iter .. ", slot index: " .. slotIndex) debugPrint("Slot " .. slotIndex .. " is empty, adding to itemTree as 'empty'")
-- debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items") itemTree['empty'] = itemTree['empty'] or {}
if iter > 0 then itemTree['empty'][#itemTree['empty'] + 1] = {
if #slot.items == 0 then inventory = inventory,
-- debugPrint("Slot " .. slotIndex .. " is empty, adding to itemTree as 'empty'") slotIndex = slotIndex - 1,
itemTree['empty'] = itemTree['empty'] or {} maxFits = 60
itemTree['empty'][#itemTree['empty'] + 1] = { }
inventory = inventory, debugPrint("Added empty slot to itemTree at index: " .. slotIndex)
slotIndex = slotIndex - 1, else
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
---@type Barotrauma.Item ---@type Barotrauma.Item
local item = slot.items[1] 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 tags = item.Prefab.Tags
local shouldSuss = false local shouldSuss = false
for tag in tags do for tag in tags do
@@ -100,13 +89,13 @@ local function buildItemTree(inventory, itemTree, iter)
end end
if shouldSuss then if shouldSuss then
-- debugPrint("Searching inside " .. item.Name .. " for nested containers") debugPrint("Searching inside " .. item.Name .. " for nested containers")
buildItemTree(item.OwnInventory, itemTree, iter + 1) buildItemTree(item.OwnInventory, itemTree)
end end
end end
end end
-- debugPrint("Completed building item tree for current inventory iteration: " .. iter) debugPrint("Completed building item tree")
return itemTree return itemTree
end end
@@ -135,6 +124,7 @@ local function stackInventoryItems(inventory, itemTree)
---@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("Item at slot " .. slotIndex .. " is " .. identifier)
---@type ItemLocation[] ---@type ItemLocation[]
local locations = itemTree[identifier] local locations = itemTree[identifier]
@@ -211,31 +201,52 @@ local function quickStackItems(character)
return return
end end
local itemTree = buildItemTree(inventory, {}) local itemTree = {}
itemTree = buildItemTree(inventory, itemTree)
itemTree = sortItemtreeBySlots(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() local openContainerInventory = getOpenContainer()
if openContainerInventory then if openContainerInventory then
stackInventoryItems(openContainerInventory, itemTree) stackInventoryItems(openContainerInventory, itemTree)
end end
local handItems = {} --local handItems = {}
for _, slotIndex in ipairs(CONFIG.HAND_SLOTS) do --for _, slotIndex in ipairs(CONFIG.HAND_SLOTS) do
local slot = inventory.slots[slotIndex] -- local slot = inventory.slots[slotIndex]
debugPrint("Checking hand slot index: " .. slotIndex) -- debugPrint("Checking hand slot index: " .. slotIndex)
if slot then -- if slot then
debugPrint("Hand slot " .. slotIndex .. " found with " .. #slot.items .. " items.") -- debugPrint("Hand slot " .. slotIndex .. " found with " .. #slot.items .. " items.")
if #slot.items > 0 then -- if #slot.items > 0 then
handItems[#handItems + 1] = slot.items[1] -- handItems[#handItems + 1] = slot.items[1]
debugPrint("Added item " .. slot.items[1].Name .. " from hand slot " .. slotIndex .. " to handItems.") -- debugPrint("Added item " .. slot.items[1].Name .. " from hand slot " .. slotIndex .. " to handItems.")
else -- else
debugPrint("Hand slot " .. slotIndex .. " is empty.") -- debugPrint("Hand slot " .. slotIndex .. " is empty.")
end -- end
else -- else
debugPrint("Hand slot " .. slotIndex .. " does not exist.") -- debugPrint("Hand slot " .. slotIndex .. " does not exist.")
end -- end
end --end
end end
-- Hook into player control to listen for key press -- Hook into player control to listen for key press