Implement toggleable queue

This commit is contained in:
2025-04-01 20:52:45 +02:00
parent a9490c039e
commit 355bfe4df6

View File

@@ -120,9 +120,9 @@ MyModGlobal.InventorySlot = {
end, end,
__tostring = function(self) __tostring = function(self)
return string.format( return string.format(
"InventorySlot(inventory=%s, slotIndex1=%d, slotIndex0=%d, item=%s, stackSize=%d, maxStackSize=%d)", "InventorySlot(inventory=%s, item=%s, stackSize=%d, maxStackSize=%d, slotIndex1=%d, slotIndex0=%d)",
tostring(self.inventory), self.slotIndex1, self.slotIndex0, tostring(self.item), self.stackSize, tostring(self.inventory), tostring(self.item), self.stackSize, self.maxStackSize, self.slotIndex1,
self.maxStackSize) self.slotIndex0)
end, end,
---@param self InventorySlot ---@param self InventorySlot
---@param predicate? fun(slot: InventorySlot): boolean ---@param predicate? fun(slot: InventorySlot): boolean
@@ -216,11 +216,13 @@ do
local itemMoveQueue = {} local itemMoveQueue = {}
---@type table<Barotrauma.Item, boolean> ---@type table<Barotrauma.Item, boolean>
local itemLookup = {} local itemLookup = {}
local rate = 100 local rate = 500
local perIteration = 6 local perIteration = 30
local noQueue = true
-- rate / 1000 is ms to seconds and *perIteraion is number of items per second -- rate / 1000 is ms to seconds and *perIteraion is number of items per second
local maxQueueSize = 10 * (1000 / rate * perIteration) local maxQueueSize = 10 * (1000 / rate * perIteration)
local function processQueue() local function processQueue()
if noQueue then return end
-- MyModGlobal.debugPrint("Processing queue") -- MyModGlobal.debugPrint("Processing queue")
Timer.Wait(processQueue, rate) Timer.Wait(processQueue, rate)
if not enabled then return end if not enabled then return end
@@ -234,7 +236,7 @@ do
moveRequest.allowCombine = moveRequest.allowCombine or false moveRequest.allowCombine = moveRequest.allowCombine or false
moveRequest.allowSwap = moveRequest.allowSwap or false moveRequest.allowSwap = moveRequest.allowSwap or false
local success = moveRequest.where.inventory.TryPutItem(moveRequest.what, moveRequest.where.slotIndex0, local success = moveRequest.where.inventory.TryPutItem(moveRequest.what, moveRequest.where.slotIndex0,
moveRequest.allowSwap, moveRequest.allowCombine, Character.Controlled, true) moveRequest.allowSwap, moveRequest.allowCombine, nil)
if not success then if not success then
MyModGlobal.debugPrint(string.format("Failed moving item from %s to %s", tostring(moveRequest.what), MyModGlobal.debugPrint(string.format("Failed moving item from %s to %s", tostring(moveRequest.what),
tostring(moveRequest.where:__tostring()))) tostring(moveRequest.where:__tostring())))
@@ -249,6 +251,17 @@ do
---@param allowSwap? boolean ---@param allowSwap? boolean
---@param allowCombine? boolean ---@param allowCombine? boolean
enqueueMove = function(what, where, allowSwap, allowCombine) enqueueMove = function(what, where, allowSwap, allowCombine)
allowCombine = allowCombine == true
allowSwap = allowSwap == true
if noQueue then
local success = where.inventory.TryPutItem(what, where.slotIndex0,
allowSwap, allowCombine, nil)
if not success then
MyModGlobal.debugPrint(string.format("Failed moving item from %s to %s", tostring(what),
tostring(where:__tostring())))
end
where:pretendMoved(what)
else
if #itemMoveQueue >= maxQueueSize then if #itemMoveQueue >= maxQueueSize then
MyModGlobal.debugPrint("Queue is full, skipping move") MyModGlobal.debugPrint("Queue is full, skipping move")
return return
@@ -269,6 +282,7 @@ do
-- We will very optimistically pretend that this will 100% for sure work -- We will very optimistically pretend that this will 100% for sure work
where:pretendMoved(what) where:pretendMoved(what)
end end
end
end end