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,37 +41,32 @@ end
---@param inventory Barotrauma.ItemInventory
---@param itemTree table<string, ItemLocation[]>
---@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
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
@@ -82,14 +77,8 @@ local function buildItemTree(inventory, itemTree, iter)
slotIndex = slotIndex - 1,
maxFits = slot.HowManyCanBePut(item.Prefab)
}
-- debugPrint("Added item to itemTree under identifier: " .. identifier)
end
end
debugPrint("Added item to itemTree under identifier: " .. identifier)
if #slot.items > 0 then
---@type Barotrauma.Item
local item = slot.items[1]
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