i should leave this peace of oxygen etc etc Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
60 lines
1.5 KiB
Lua
60 lines
1.5 KiB
Lua
local PriorityQueue = require "lib.utils.priority_queue"
|
|
|
|
local sortInitiative = function(id_a, id_b)
|
|
local res = Tree.level.characters[id_a]:try(Tree.behaviors.stats, function(astats)
|
|
local res = Tree.level.characters[id_b]:try(Tree.behaviors.stats, function(bstats)
|
|
return astats.initiative > bstats.initiative
|
|
end)
|
|
return res
|
|
end)
|
|
return res or false
|
|
end
|
|
|
|
--- @class TurnOrder
|
|
--- @field characterIds Id[]
|
|
--- @field walked PriorityQueue
|
|
--- @field notWalked PriorityQueue
|
|
--- @field isTurnsEnabled boolean
|
|
local turnOrder = {}
|
|
turnOrder.__index = turnOrder
|
|
|
|
local function new()
|
|
return setmetatable({
|
|
walked = PriorityQueue.new(sortInitiative),
|
|
notWalked = PriorityQueue.new(sortInitiative),
|
|
isTurnsEnabled = true
|
|
}, turnOrder)
|
|
end
|
|
|
|
function turnOrder:next()
|
|
local cur = self.notWalked:pop()
|
|
self.walked:insert(cur)
|
|
end
|
|
|
|
function turnOrder:reorder()
|
|
local _walked, _notWalked = PriorityQueue.new(sortInitiative), PriorityQueue.new(sortInitiative)
|
|
local charId = self.walked:pop()
|
|
while charId do
|
|
_walked:insert(charId)
|
|
charId = self.walked:pop()
|
|
end
|
|
|
|
charId = self.notWalked:pop()
|
|
while charId do
|
|
_notWalked:insert(charId)
|
|
charId = self.notWalked:pop()
|
|
end
|
|
end
|
|
|
|
--- @param count number
|
|
function turnOrder:getOrder(count)
|
|
|
|
end
|
|
|
|
--- @param id Id
|
|
function turnOrder:add(id)
|
|
table.insert(self.characterIds, id)
|
|
end
|
|
|
|
return { new = new }
|