From 355bfe4df6c4b72dc6453d70610215f0403ba758 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 1 Apr 2025 20:52:45 +0200 Subject: [PATCH] Implement toggleable queue --- CykaQuick/Lua/Cyka/utils.lua | 62 ++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/CykaQuick/Lua/Cyka/utils.lua b/CykaQuick/Lua/Cyka/utils.lua index f5b46d1..fa9b14a 100644 --- a/CykaQuick/Lua/Cyka/utils.lua +++ b/CykaQuick/Lua/Cyka/utils.lua @@ -120,9 +120,9 @@ MyModGlobal.InventorySlot = { end, __tostring = function(self) return string.format( - "InventorySlot(inventory=%s, slotIndex1=%d, slotIndex0=%d, item=%s, stackSize=%d, maxStackSize=%d)", - tostring(self.inventory), self.slotIndex1, self.slotIndex0, tostring(self.item), self.stackSize, - self.maxStackSize) + "InventorySlot(inventory=%s, item=%s, stackSize=%d, maxStackSize=%d, slotIndex1=%d, slotIndex0=%d)", + tostring(self.inventory), tostring(self.item), self.stackSize, self.maxStackSize, self.slotIndex1, + self.slotIndex0) end, ---@param self InventorySlot ---@param predicate? fun(slot: InventorySlot): boolean @@ -216,11 +216,13 @@ do local itemMoveQueue = {} ---@type table local itemLookup = {} - local rate = 100 - local perIteration = 6 + local rate = 500 + local perIteration = 30 + local noQueue = true -- rate / 1000 is ms to seconds and *perIteraion is number of items per second local maxQueueSize = 10 * (1000 / rate * perIteration) local function processQueue() + if noQueue then return end -- MyModGlobal.debugPrint("Processing queue") Timer.Wait(processQueue, rate) if not enabled then return end @@ -234,7 +236,7 @@ do moveRequest.allowCombine = moveRequest.allowCombine or false moveRequest.allowSwap = moveRequest.allowSwap or false 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 MyModGlobal.debugPrint(string.format("Failed moving item from %s to %s", tostring(moveRequest.what), tostring(moveRequest.where:__tostring()))) @@ -249,25 +251,37 @@ do ---@param allowSwap? boolean ---@param allowCombine? boolean enqueueMove = function(what, where, allowSwap, allowCombine) - if #itemMoveQueue >= maxQueueSize then - MyModGlobal.debugPrint("Queue is full, skipping move") - return + 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 + MyModGlobal.debugPrint("Queue is full, skipping move") + return + end + if itemLookup[what] then + MyModGlobal.debugPrint("Item is already in the queue, skipping move") + return + end + MyModGlobal.debugPrint(string.format("Enqueuing move from %s to %s, now in queue %d/%d", tostring(what), + tostring(where:__tostring()), #itemMoveQueue, maxQueueSize)) + table.insert(itemMoveQueue, { + what = what, + where = where, + allowSwap = allowSwap or false, + allowCombine = allowCombine ~= false, + }) + itemLookup[what] = true + -- We will very optimistically pretend that this will 100% for sure work + where:pretendMoved(what) end - if itemLookup[what] then - MyModGlobal.debugPrint("Item is already in the queue, skipping move") - return - end - MyModGlobal.debugPrint(string.format("Enqueuing move from %s to %s, now in queue %d/%d", tostring(what), - tostring(where:__tostring()), #itemMoveQueue, maxQueueSize)) - table.insert(itemMoveQueue, { - what = what, - where = where, - allowSwap = allowSwap or false, - allowCombine = allowCombine ~= false, - }) - itemLookup[what] = true - -- We will very optimistically pretend that this will 100% for sure work - where:pretendMoved(what) end end