From 2d29d35f96f4bedbe38d8c8be9a5a2e630416dd1 Mon Sep 17 00:00:00 2001 From: Kitsune-Jesus Date: Sun, 1 Feb 2026 23:26:14 +0100 Subject: [PATCH 1/2] fix: implement character death cleanup in turn order and queues --- lib/level/level.lua | 1 + lib/level/turn_order.lua | 15 +++++++++++++++ lib/utils/priority_queue.lua | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/lib/level/level.lua b/lib/level/level.lua index 9e6950c..0dcbdb6 100644 --- a/lib/level/level.lua +++ b/lib/level/level.lua @@ -40,6 +40,7 @@ end function level:update(dt) utils.each(self.deadIds, function(id) self.characters[id] = nil + self.turnOrder:remove(id) end) self.deadIds = {} diff --git a/lib/level/turn_order.lua b/lib/level/turn_order.lua index f03c836..a448409 100644 --- a/lib/level/turn_order.lua +++ b/lib/level/turn_order.lua @@ -108,4 +108,19 @@ function turnOrder:add(id) self.actedQueue:insert(id) -- новые персонажи по умолчанию попадают в очередь следующего хода end +--- Удалить персонажа из очереди хода (например, при смерти) +--- @param id Id +function turnOrder:remove(id) + if self.current == id then + self.current = self.pendingQueue:pop() + if not self.current then + self:endRound() + end + return + end + + self.actedQueue:remove(id) + self.pendingQueue:remove(id) +end + return { new = new } diff --git a/lib/utils/priority_queue.lua b/lib/utils/priority_queue.lua index 0097604..7aca181 100644 --- a/lib/utils/priority_queue.lua +++ b/lib/utils/priority_queue.lua @@ -115,4 +115,29 @@ function PriorityQueue:copy() }, PriorityQueue) end +--- Удалить элемент из очереди. +--- Осторожно: O(n), так как нужно найти индекс элемента. +---@param x any +function PriorityQueue:remove(x) + local index = nil + for i, v in ipairs(self.data) do + if v == x then + index = i + break + end + end + + if not index then return end + + local n = #self.data + if index == n then + self.data[n] = nil + else + self.data[index] = self.data[n] + self.data[n] = nil + self:_sift_up(index) + self:_sift_down(index) + end +end + return PriorityQueue From 752fe00910aa962030bde02334129f777182ab65 Mon Sep 17 00:00:00 2001 From: Kitsune-Jesus Date: Sun, 1 Feb 2026 23:36:09 +0100 Subject: [PATCH 2/2] refactor: implement turnOrder:remove by filtering/rebuilding queues instead of O(n) PriorityQueue:remove --- lib/level/turn_order.lua | 14 ++++++++++++-- lib/utils/priority_queue.lua | 25 ------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/lib/level/turn_order.lua b/lib/level/turn_order.lua index a448409..de1b52e 100644 --- a/lib/level/turn_order.lua +++ b/lib/level/turn_order.lua @@ -119,8 +119,18 @@ function turnOrder:remove(id) return end - self.actedQueue:remove(id) - self.pendingQueue:remove(id) + local function filterQueue(q, targetId) + local newQ = PriorityQueue.new(initiativeComparator) + for _, val in ipairs(q.data) do + if val ~= targetId then + newQ:insert(val) + end + end + return newQ + end + + self.actedQueue = filterQueue(self.actedQueue, id) + self.pendingQueue = filterQueue(self.pendingQueue, id) end return { new = new } diff --git a/lib/utils/priority_queue.lua b/lib/utils/priority_queue.lua index 7aca181..0097604 100644 --- a/lib/utils/priority_queue.lua +++ b/lib/utils/priority_queue.lua @@ -115,29 +115,4 @@ function PriorityQueue:copy() }, PriorityQueue) end ---- Удалить элемент из очереди. ---- Осторожно: O(n), так как нужно найти индекс элемента. ----@param x any -function PriorityQueue:remove(x) - local index = nil - for i, v in ipairs(self.data) do - if v == x then - index = i - break - end - end - - if not index then return end - - local n = #self.data - if index == n then - self.data[n] = nil - else - self.data[index] = self.data[n] - self.data[n] = nil - self:_sift_up(index) - self:_sift_down(index) - end -end - return PriorityQueue