Skill items with few slots when looking for items to reload with

Don't want to unload one tool for another
This commit is contained in:
2025-03-30 22:02:00 +02:00
parent 068f72082a
commit a239f9cc36
2 changed files with 40 additions and 13 deletions

View File

@@ -44,7 +44,14 @@ local function getItemsPerSlot(from, slots)
---@type table<InventorySlot, Barotrauma.Item[]>
local movableBySlot = {}
-- Get all the items and then we will sort them by condition and shit
utils.enqueueInventory(from, {}, function(ititem)
utils.enqueueInventory(from, {}, function(ititem, inventoryRef)
-- We don't want to take oxygen out of our diving suit to load our plasma cutter
-- Most loadable items have 1 capacity
-- But some have 2 or 3 (coil speargun)
if inventoryRef and inventoryRef.Capacity < 4 then
MyModGlobal.debugPrint(string.format("Skipping small inventory %s", tostring(inventoryRef)))
return false
end
-- MyModGlobal.debugPrint("Checking item:")
-- dump(slots)
-- MyModGlobal.debugPrint(ititem.Prefab.Identifier.Value)
@@ -169,8 +176,8 @@ local function tryReloadSlot(slot, preferMinCondition)
-- And tat that point we're done with that slot
if not moved then break end
numMoved = numMoved + 1
else
MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value)))
-- else
-- MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value)))
end
end
end

View File

@@ -7,11 +7,16 @@ local enqueueSlot
local enqueueInventory
local _
---@alias FilterPredicate fun(item: Barotrauma.Item, inventoryRef?: Barotrauma.ItemInventory, slotRef: Barotrauma.ItemInventory.Slot): boolean
---@param item Barotrauma.Item
---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean
---@param predicate? FilterPredicate
---@param loadRefs? boolean
---@param slotRef? Barotrauma.ItemInventory.Slot
---@param inventoryRef? Barotrauma.ItemInventory
---@return Barotrauma.Item[], string?
enqueueItem = function(item, queue, predicate)
enqueueItem = function(item, queue, predicate, loadRefs, slotRef, inventoryRef)
queue = queue or {}
predicate = predicate or function() return true end
-- debugPrint(string.format("Enqueuing item: %s", item.Prefab.Identifier.Value))
@@ -20,7 +25,7 @@ enqueueItem = function(item, queue, predicate)
-- No, not yet...
if not item then return queue, "No item" end
local ok, stop = predicate(item)
local ok, stop = predicate(item, inventoryRef, slotRef)
if ok then
queue[#queue + 1] = item
end
@@ -30,7 +35,11 @@ enqueueItem = function(item, queue, predicate)
-- Only machines have multiple
-- So inventrorY should be fine here
-- debugPrint("Item has its own inventory, enqueuing inventory...")
if loadRefs then
queue, _ = enqueueInventory(item.OwnInventory, queue, predicate, loadRefs)
else
queue, _ = enqueueInventory(item.OwnInventory, queue, predicate)
end
-- if err then
-- debugPrint(string.format("Error enqueuing inventory: %s", err))
-- end
@@ -41,9 +50,11 @@ end
---@param slot Barotrauma.ItemInventory.Slot
---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean
---@param predicate? FilterPredicate
---@param loadRefs? boolean
---@param inventoryRef? Barotrauma.ItemInventory
---@return Barotrauma.Item[], string?
enqueueSlot = function(slot, queue, predicate)
enqueueSlot = function(slot, queue, predicate, loadRefs, inventoryRef)
queue = queue or {}
predicate = predicate or function() return true end
-- debugPrint(string.format("Enqueuing slot with %d items.", #slot.items))
@@ -56,7 +67,11 @@ enqueueSlot = function(slot, queue, predicate)
for _, item in ipairs(slot.items) do
-- Only the final leaf nodes decide upon the predicate
if loadRefs then
queue, err = enqueueItem(item, queue, predicate, loadRefs, inventoryRef, slot)
else
queue, err = enqueueItem(item, queue, predicate)
end
if err then
return queue, err
end
@@ -67,9 +82,10 @@ end
---@param inventory Barotrauma.ItemInventory
---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean
---@param predicate? FilterPredicate
---@param loadRefs? boolean
---@return Barotrauma.Item[], string?
enqueueInventory = function(inventory, queue, predicate)
enqueueInventory = function(inventory, queue, predicate, loadRefs)
queue = queue or {}
predicate = predicate or function() return true end
-- debugPrint(string.format("Enqueuing inventory with %d slots.", #inventory.slots))
@@ -79,7 +95,11 @@ enqueueInventory = function(inventory, queue, predicate)
for _, slot in ipairs(inventory.slots) do
-- Only the final leaf nodes decide upon the predicate
if loadRefs then
queue, err = enqueueSlot(slot, queue, predicate, loadRefs, inventory)
else
queue, err = enqueueSlot(slot, queue, predicate)
end
if err then
return queue, err
end