1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

changed callbacks + added global.task_queue_speed

This commit is contained in:
grilledham 2018-01-31 20:30:32 +00:00
parent fbee5b117c
commit 61947204e9

View File

@ -5,22 +5,20 @@
-- ======================================================= -- -- ======================================================= --
local Queue = require "utils.Queue" local Queue = require "utils.Queue"
local PriorityQueue = require "utils.PriorityQueue"
local Task = {} local Task = {}
local function set_new_next_async_callback_time() global.callbacks = global.callbacks or PriorityQueue.new()
global.next_async_callback_time = global.callbacks[1].time
for index, callback in pairs(global.callbacks) do
if callback.time < global.next_async_callback_time then
global.next_async_callback_time = callback.time
end
end
end
global.callbacks = {}
global.next_async_callback_time = -1 global.next_async_callback_time = -1
global.task_queue = global.task_queue or Queue.new() global.task_queue = global.task_queue or Queue.new()
global.total_task_weight = 0 global.total_task_weight = 0
global.task_queue_speed = 1
local function comp(a, b)
return a.time < b.time
end
local function on_tick() local function on_tick()
local queue = global.task_queue local queue = global.task_queue
for i = 1, get_task_per_tick() do for i = 1, get_task_per_tick() do
@ -37,24 +35,22 @@ local function on_tick()
end end
end end
end end
if game.tick == global.next_async_callback_time then
for index, callback in pairs(global.callbacks) do local callbacks = global.callbacks
if game.tick == callback.time then local callback = PriorityQueue.peek(callbacks)
pcall(callback.callback, callback.params) while callback ~= nil and game.tick >= callback.time do
table.remove(global.callbacks, index) local success, error = pcall(_G[callback.func_name], callback.params)
if #global.callbacks == 0 then if not success then
global.next_async_callback_time = -1 log(error)
else
set_new_next_async_callback_time()
end
end
end end
PriorityQueue.pop(callbacks, comp)
callback = PriorityQueue.peek(callbacks)
end end
end end
function get_task_per_tick() function get_task_per_tick()
local size = global.total_task_weight local size = global.total_task_weight
local apt = math.floor(math.log10(size + 1)) local apt = math.floor(math.log10(size + 1)) * global.task_queue_speed
if apt < 1 then if apt < 1 then
return 1 return 1
else else
@ -62,21 +58,16 @@ function get_task_per_tick()
end end
end end
function Task.set_timeout_in_ticks(ticks, callback, params) function Task.set_timeout_in_ticks(ticks, func_name, params)
local time = game.tick + ticks local time = game.tick + ticks
if global.next_async_callback_time == -1 or global.next_async_callback_time > time then local callback = {time = time, func_name = func_name, params = params}
global.next_async_callback_time = time PriorityQueue.push(global.callbacks, callback, comp)
end
if #global.callbacks == 0 then
end
table.insert(global.callbacks, {time = time, callback = callback, params = params})
end end
function Task.set_timeout(sec, callback, params) function Task.set_timeout(sec, func_name, params)
Task.set_timeout_in_ticks(60 * sec, callback, params) Task.set_timeout_in_ticks(60 * sec, func_name, params)
end end
function Task.queue_task(func_name, params, weight) function Task.queue_task(func_name, params, weight)
weight = weight or 1 weight = weight or 1
global.total_task_weight = global.total_task_weight + weight global.total_task_weight = global.total_task_weight + weight