Make maybe little better open storage detection

This commit is contained in:
2025-03-29 23:36:02 +01:00
parent e99d9b5c49
commit 2e23215493

View File

@@ -44,29 +44,29 @@ end
local function buildItemTree(inventory, itemTree) local function buildItemTree(inventory, itemTree)
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
debugPrint("Building item tree for inventory at slot index: " .. slotIndex) -- debugPrint("Building item tree for inventory at slot index: " .. slotIndex)
debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items") -- debugPrint("Slot " .. slotIndex .. " has " .. #slot.items .. " items")
if #slot.items == 0 then 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'] or {}
itemTree['empty'][#itemTree['empty'] + 1] = { itemTree['empty'][#itemTree['empty'] + 1] = {
inventory = inventory, inventory = inventory,
slotIndex = slotIndex - 1, 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)
else else
---@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: " .. item.Name .. " with identifier: " .. identifier) -- debugPrint("Found item: " .. item.Name .. " with identifier: " .. identifier)
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
@@ -77,7 +77,7 @@ local function buildItemTree(inventory, itemTree)
slotIndex = slotIndex - 1, slotIndex = slotIndex - 1,
maxFits = slot.HowManyCanBePut(item.Prefab) 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 tags = item.Prefab.Tags
local shouldSuss = false local shouldSuss = false
@@ -89,13 +89,13 @@ local function buildItemTree(inventory, itemTree)
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) buildItemTree(item.OwnInventory, itemTree)
end end
end end
end end
debugPrint("Completed building item tree") -- debugPrint("Completed building item tree")
return itemTree return itemTree
end end
@@ -173,10 +173,13 @@ local enqueueInventory
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string
enqueueItem = function(item, queue) enqueueItem = function(item, queue)
queue = queue or {} queue = queue or {}
-- debugPrint("Enqueuing item: " .. item.Prefab.Identifier.Value) -- Debug log for item being enqueued
if item.OwnInventory then if item.OwnInventory then
-- debugPrint("Item has its own inventory, enqueuing inventory...") -- Debug log for own inventory
queue = enqueueInventory(item.OwnInventory, queue) queue = enqueueInventory(item.OwnInventory, queue)
end end
queue[#queue + 1] = item queue[#queue + 1] = item
-- debugPrint("Item enqueued. Current queue size: " .. #queue) -- Debug log for current queue size
return queue return queue
end end
@@ -185,6 +188,7 @@ end
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string
enqueueSlot = function(slot, queue) enqueueSlot = function(slot, queue)
queue = queue or {} queue = queue or {}
-- debugPrint("Enqueuing slot with " .. #slot.items .. " items.") -- Debug log for slot items count
-- We don't want to shadow queue -- We don't want to shadow queue
local err local err
-- If the slot is empty there's nothing to iterate -- 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 for _, item in ipairs(slot.items) do
queue, err = enqueueItem(item, queue) queue, err = enqueueItem(item, queue)
if err then if err then
print("Error enqueuing item: " .. err) -- debugPrint("Error enqueuing item: " .. err) -- Debug log for error enqueuing item
end end
end end
-- debugPrint("Finished enqueuing slot. Current queue size: " .. #queue) -- Debug log for current queue size
return queue return queue
end end
@@ -203,14 +208,15 @@ end
---@return Barotrauma.Item[], string[] ---@return Barotrauma.Item[], string[]
enqueueInventory = function(inventory, queue) enqueueInventory = function(inventory, queue)
queue = queue or {} 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 local err
for _, slot in ipairs(inventory.slots) do for _, slot in ipairs(inventory.slots) do
queue, err = enqueueSlot(slot, queue) queue, err = enqueueSlot(slot, queue)
if err then if err then
print("Error enqueuing slot: " .. err) -- debugPrint("Error enqueuing slot: " .. err) -- Debug log for error enqueuing slot
end end
end end
-- debugPrint("Finished enqueuing inventory. Current queue size: " .. #queue) -- Debug log for current queue size
return queue return queue
end end
@@ -291,20 +297,29 @@ end
-- This is a bit fucking sucky..... -- This is a bit fucking sucky.....
-- But I really don't know better -- But I really don't know better
-- Maybe it will be fine... -- Maybe it will be fine...
---@return Barotrauma.ItemInventory ---@return Barotrauma.Item[]
local function getOpenContainer() local function getOpenContainers()
debugPrint("Attempting to find open container...") debugPrint("Attempting to find open container...")
local containers = {}
for item in Item.ItemList do for item in Item.ItemList do
---@cast item Barotrauma.Item ---@cast item Barotrauma.Item
if item and item.OwnInventory then local isok = true
if item.OwnInventory.visualSlots and #item.OwnInventory.visualSlots > 0 then isok = isok and item ~= nil
return item.OwnInventory isok = isok and item.OwnInventory ~= nil
end 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
end end
return containers
debugPrint("No open container found")
return nil
end end
-- We would like to fill larger stacks first -- We would like to fill larger stacks first
@@ -326,15 +341,15 @@ end
---@return table<string, ItemLocation[]>, string ---@return table<string, ItemLocation[]>, string
local function tryBuildItemTree(inventory) local function tryBuildItemTree(inventory)
local itemTree = {} local itemTree = {}
debugPrint("Preparing to stack items into the bag...") -- debugPrint("Preparing to stack items into the bag...")
local bagSlot = inventory.slots[8] local bagSlot = inventory.slots[8]
if bagSlot then 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 if #bagSlot.items > 0 then
local item = bagSlot.items[1] 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 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) itemTree = buildItemTree(item.OwnInventory, itemTree)
else else
return itemTree, "Bag does not have its own inventory." return itemTree, "Bag does not have its own inventory."
@@ -374,9 +389,14 @@ local function quickStackItems(character)
-- toMove = enqueueInventory(inventory, toMove) -- toMove = enqueueInventory(inventory, toMove)
local openContainerInventory = getOpenContainer() local openContainers = getOpenContainers()
if openContainerInventory then for _, container in ipairs(openContainers) do
toMove = enqueueInventory(openContainerInventory, toMove) 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 end
local errors = tryMoveItems(toMove, itemTree) local errors = tryMoveItems(toMove, itemTree)