2017-07-23 14:51:20 +02:00
|
|
|
-- Threading simulation module
|
|
|
|
-- Thread.sleep()
|
|
|
|
-- @author Valansch
|
|
|
|
-- github: https://github.com/Valansch/RedMew
|
|
|
|
-- ======================================================= --
|
|
|
|
|
2018-01-26 01:31:47 +02:00
|
|
|
local Queue = require "utils.Queue"
|
2017-07-23 14:51:20 +02:00
|
|
|
|
|
|
|
local Thread = {}
|
|
|
|
|
|
|
|
local function set_new_next_async_callback_time()
|
|
|
|
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
|
2018-01-26 01:31:47 +02:00
|
|
|
global.actions_queue = global.actions_queue or Queue.new()
|
2017-07-23 14:51:20 +02:00
|
|
|
local function on_tick()
|
2018-01-26 01:31:47 +02:00
|
|
|
local queue = global.actions_queue
|
|
|
|
for i = 1, get_actions_per_tick() do
|
|
|
|
local action = Queue.peek(queue)
|
|
|
|
if action ~= nil then
|
2018-01-15 19:24:48 +02:00
|
|
|
function call(params)
|
2018-01-26 01:31:47 +02:00
|
|
|
return _G[action.action](params)
|
2018-01-15 19:24:48 +02:00
|
|
|
end
|
2018-01-26 01:31:47 +02:00
|
|
|
local success, result = pcall(call, action.params) -- result is error if not success else result is a boolean for if the action should stay in the queue.
|
2018-01-15 19:24:48 +02:00
|
|
|
if not success then
|
|
|
|
log(result)
|
2018-01-26 01:31:47 +02:00
|
|
|
Queue.pop(queue)
|
2018-01-15 19:24:48 +02:00
|
|
|
elseif not result then
|
2018-01-26 01:31:47 +02:00
|
|
|
Queue.pop(queue)
|
2018-01-15 19:24:48 +02:00
|
|
|
end
|
2017-10-23 13:24:05 +02:00
|
|
|
end
|
2017-10-06 02:13:56 +02:00
|
|
|
end
|
2017-07-23 14:51:20 +02:00
|
|
|
if game.tick == global.next_async_callback_time then
|
|
|
|
for index, callback in pairs(global.callbacks) do
|
|
|
|
if game.tick == callback.time then
|
|
|
|
pcall(callback.callback, callback.params)
|
|
|
|
table.remove(global.callbacks, index)
|
|
|
|
if #global.callbacks == 0 then
|
|
|
|
global.next_async_callback_time = -1
|
|
|
|
else
|
|
|
|
set_new_next_async_callback_time()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-26 01:31:47 +02:00
|
|
|
function get_actions_per_tick()
|
|
|
|
local size = Queue.size(global.actions_queue)
|
|
|
|
local apt = math.floor(math.log10(size + 1))
|
2017-10-25 13:00:45 +02:00
|
|
|
if apt < 1 then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return apt
|
|
|
|
end
|
2017-10-23 13:24:05 +02:00
|
|
|
end
|
|
|
|
|
2017-10-06 02:13:56 +02:00
|
|
|
function Thread.set_timeout_in_ticks(ticks, callback, params)
|
|
|
|
local time = game.tick + ticks
|
2017-07-23 14:51:20 +02:00
|
|
|
if global.next_async_callback_time == -1 or global.next_async_callback_time > time then
|
|
|
|
global.next_async_callback_time = time
|
|
|
|
end
|
|
|
|
if #global.callbacks == 0 then
|
|
|
|
end
|
|
|
|
table.insert(global.callbacks, {time = time, callback = callback, params = params})
|
|
|
|
end
|
|
|
|
|
2017-10-06 02:13:56 +02:00
|
|
|
function Thread.set_timeout(sec, callback, params)
|
|
|
|
Thread.set_timeout_in_ticks(60 * sec, callback, params)
|
|
|
|
end
|
|
|
|
|
2018-01-26 01:31:47 +02:00
|
|
|
|
|
|
|
function Thread.queue_action(action, params)
|
|
|
|
local queue = global.actions_queue
|
|
|
|
Queue.push(queue, {action = action, params = params})
|
2017-10-06 02:13:56 +02:00
|
|
|
end
|
|
|
|
|
2017-07-23 14:51:20 +02:00
|
|
|
Event.register(defines.events.on_tick, on_tick)
|
|
|
|
|
|
|
|
return Thread
|