1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-20 03:29:47 +02:00

Update and rename Task.lua to task.lua

This commit is contained in:
Gerkiz 2019-03-11 17:47:01 +01:00 committed by GitHub
parent f7b1dff52b
commit ee744dbe1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,13 @@
-- Threading simulation module -- Threading simulation module
-- Task.sleep() -- Task.sleep()
-- @author Valansch -- @author Valansch and Grilledham
-- github: https://github.com/Valansch/RedMew -- github: https://github.com/Refactorio/RedMew
-- ======================================================= -- -- ======================================================= --
local Queue = require 'utils.Queue' local Queue = require 'utils.queue'
local PriorityQueue = require 'utils.PriorityQueue' local PriorityQueue = require 'utils.priority_queue'
local Event = require 'utils.event' local Event = require 'utils.event'
local Token = require 'utils.global_token' local Token = require 'utils.token'
local Task = {} local Task = {}
@ -21,6 +21,18 @@ local function comp(a, b)
return a.time < b.time return a.time < b.time
end end
global.tpt = global.task_queue_speed
local function get_task_per_tick()
if game.tick % 300 == 0 then
local size = global.total_task_weight
global.tpt = math.floor(math.log10(size + 1)) * global.task_queue_speed
if global.tpt < 1 then
global.tpt = 1
end
end
return global.tpt
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
@ -29,7 +41,11 @@ local function on_tick()
-- result is error if not success else result is a boolean for if the task should stay in the queue. -- result is error if not success else result is a boolean for if the task should stay in the queue.
local success, result = pcall(Token.get(task.func_token), task.params) local success, result = pcall(Token.get(task.func_token), task.params)
if not success then if not success then
log(result) if _DEBUG then
error(result)
else
log(result)
end
Queue.pop(queue) Queue.pop(queue)
global.total_task_weight = global.total_task_weight - task.weight global.total_task_weight = global.total_task_weight - task.weight
elseif not result then elseif not result then
@ -42,37 +58,52 @@ local function on_tick()
local callbacks = global.callbacks local callbacks = global.callbacks
local callback = PriorityQueue.peek(callbacks) local callback = PriorityQueue.peek(callbacks)
while callback ~= nil and game.tick >= callback.time do while callback ~= nil and game.tick >= callback.time do
local success, error = pcall(Token.get(callback.func_token), callback.params) local success, result = pcall(Token.get(callback.func_token), callback.params)
if not success then if not success then
log(error) if _DEBUG then
error(result)
else
log(result)
end
end end
PriorityQueue.pop(callbacks, comp) PriorityQueue.pop(callbacks, comp)
callback = PriorityQueue.peek(callbacks) callback = PriorityQueue.peek(callbacks)
end end
end end
global.tpt = global.task_queue_speed --- Allows you to set a timer (in ticks) after which the tokened function will be run with params given as an argument
function get_task_per_tick() -- Cannot be called before init
if game.tick % 300 == 0 then -- @param ticks <number>
local size = global.total_task_weight -- @param func_token <number> a token for a function store via the token system
global.tpt = math.floor(math.log10(size + 1)) * global.task_queue_speed -- @param params <any> the argument to send to the tokened function
if global.tpt < 1 then
global.tpt = 1
end
end
return global.tpt
end
function Task.set_timeout_in_ticks(ticks, func_token, params) function Task.set_timeout_in_ticks(ticks, func_token, params)
if not game then
error('cannot call when game is not available', 2)
end
local time = game.tick + ticks local time = game.tick + ticks
local callback = {time = time, func_token = func_token, params = params} local callback = {time = time, func_token = func_token, params = params}
PriorityQueue.push(global.callbacks, callback, comp) PriorityQueue.push(global.callbacks, callback, comp)
end end
--- Allows you to set a timer (in seconds) after which the tokened function will be run with params given as an argument
-- Cannot be called before init
-- @param sec <number>
-- @param func_token <number> a token for a function store via the token system
-- @param params <any> the argument to send to the tokened function
function Task.set_timeout(sec, func_token, params) function Task.set_timeout(sec, func_token, params)
if not game then
error('cannot call when game is not available', 2)
end
Task.set_timeout_in_ticks(60 * sec, func_token, params) Task.set_timeout_in_ticks(60 * sec, func_token, params)
end end
--- Queueing allows you to split up heavy tasks which don't need to be completed in the same tick.
-- Queued tasks are generally run 1 per tick. If the queue backs up, more tasks will be processed per tick.
-- @param func_token <number> a token for a function stored via the token system
-- If this function returns `true` it will run again the next tick, delaying other queued tasks (see weight)
-- @param params <any> the argument to send to the tokened function
-- @param weight <number> (defaults to 1) weight is the number of ticks a task is expected to take.
-- Ex. if the task is expected to repeat multiple times (ie. the function returns true and loops several ticks)
function Task.queue_task(func_token, params, weight) function Task.queue_task(func_token, 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