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[]> ---@type table<InventorySlot, Barotrauma.Item[]>
local movableBySlot = {} local movableBySlot = {}
-- Get all the items and then we will sort them by condition and shit -- 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:") -- MyModGlobal.debugPrint("Checking item:")
-- dump(slots) -- dump(slots)
-- MyModGlobal.debugPrint(ititem.Prefab.Identifier.Value) -- 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 -- And tat that point we're done with that slot
if not moved then break end if not moved then break end
numMoved = numMoved + 1 numMoved = numMoved + 1
else -- else
MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value))) -- MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value)))
end end
end end
end end

View File

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