Add predicate filter to enqueue

To effectively make a search or filter
This commit is contained in:
2025-03-30 00:17:16 +01:00
parent 89ea5ca2b5
commit 21be3eeec2

View File

@@ -155,9 +155,11 @@ local _
---@param item Barotrauma.Item ---@param item Barotrauma.Item
---@param queue Barotrauma.Item[] ---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string
enqueueItem = function(item, queue) enqueueItem = function(item, queue, predicate)
queue = queue or {} queue = queue or {}
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))
-- local err -- local err
if item.OwnInventory then if item.OwnInventory then
@@ -170,23 +172,26 @@ enqueueItem = function(item, queue)
-- debugPrint(string.format("Error enqueuing inventory: %s", err)) -- debugPrint(string.format("Error enqueuing inventory: %s", err))
-- end -- end
end end
queue[#queue + 1] = item if predicate(item) then queue[#queue + 1] = item end
-- debugPrint(string.format("Item enqueued. Current queue size: %d", #queue)) -- debugPrint(string.format("Item enqueued. Current queue size: %d", #queue))
return queue, nil return queue, nil
end 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
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string
enqueueSlot = function(slot, queue) enqueueSlot = function(slot, queue, predicate)
queue = queue or {} queue = queue or {}
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))
-- 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
-- And we will naturally return queue as is -- And we will naturally return queue as is
for _, item in ipairs(slot.items) do for _, item in ipairs(slot.items) do
queue, _ = enqueueItem(item, queue) -- Only the final leaf nodes decide upon the predicate
queue, _ = enqueueItem(item, queue, predicate)
-- if err then -- if err then
-- debugPrint(string.format("Error enqueuing item: %s", err)) -- debugPrint(string.format("Error enqueuing item: %s", err))
-- end -- end
@@ -197,13 +202,16 @@ end
---@param inventory Barotrauma.ItemInventory ---@param inventory Barotrauma.ItemInventory
---@param queue Barotrauma.Item[] ---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean
---@return Barotrauma.Item[] ---@return Barotrauma.Item[]
enqueueInventory = function(inventory, queue) enqueueInventory = function(inventory, queue, predicate)
queue = queue or {} queue = queue or {}
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))
-- local err -- local err
for _, slot in ipairs(inventory.slots) do for _, slot in ipairs(inventory.slots) do
queue, _ = enqueueSlot(slot, queue) -- Only the final leaf nodes decide upon the predicate
queue, _ = enqueueSlot(slot, queue, predicate)
-- if err then -- if err then
-- debugPrint(string.format("Error enqueuing slot: %s", err)) -- debugPrint(string.format("Error enqueuing slot: %s", err))
-- end -- end