From cde3eda8f8b2e680bcb3710d8311668b8e24fad0 Mon Sep 17 00:00:00 2001 From: przemo1232 <79700515+przemo1232@users.noreply.github.com> Date: Tue, 13 Aug 2024 00:07:58 +0200 Subject: [PATCH] remove flying text shenanigans --- lib/data-stage.lua | 12 ------- lib/events.lua | 80 ++++++++++++---------------------------------- 2 files changed, 21 insertions(+), 71 deletions(-) diff --git a/lib/data-stage.lua b/lib/data-stage.lua index f186fa3..a996b12 100644 --- a/lib/data-stage.lua +++ b/lib/data-stage.lua @@ -523,17 +523,5 @@ py.disallow_effectivity = function(recipe_categories) end end -local delays = {} -for i = 0, 20 do - local n = 2 ^ i - delays[#delays + 1] = { - name = 'py-ticked-script-delay-' .. n, - type = 'flying-text', - time_to_live = n, - speed = 0, - } -end -data:extend(delays) - ---@diagnostic disable-next-line: duplicate-set-field py.on_event = function() end diff --git a/lib/events.lua b/lib/events.lua index 1e262e4..4f6ad17 100644 --- a/lib/events.lua +++ b/lib/events.lua @@ -1,22 +1,9 @@ -if data then - local delays = {} - for _, n in pairs(powers_of_two) do - delays[#delays + 1] = { - name = 'py-ticked-script-delay-' .. n, - type = 'flying-text', - time_to_live = n, - speed = 0, - } - end - data:extend(delays) - return -end - local events = {} ---Drop-in replacement for script.on_event however it supports multiple handlers per event. You can also use 'on_built' 'on_destroyed' and 'on_init' as shortcuts for multiple events. ---@param event defines.events|defines.events[]|string ---@param f function +---@diagnostic disable-next-line: duplicate-set-field py.on_event = function(event, f) if event == 'on_built' then py.on_event({defines.events.on_built_entity, @@ -58,11 +45,6 @@ local function one_function_from_many(functions) end end -local powers_of_two = {} -for i = 0, 20 do - powers_of_two[i] = 2 ^ i -end - local finalized = false py.finalize_events = function() if finalized then error('Events already finalized') end @@ -108,47 +90,27 @@ for event, _ in pairs(gui_events) do py.on_event(event, process_gui_event) end -py.delayed_functions = {} ----use this to execute a script after a delay ----example: ----py.delayed_functions.my_delayed_func = function(param1, param2, param3) ... end ----py.execute_later('my_delayed_func', 60, param1, param2, param3) ----The above code will execute my_delayed_func after waiting for 60 ticks ----@param function_key string ----@param ticks integer ----@param ... any -function py.execute_later(function_key, ticks, ...) - if ticks < 1 or ticks % 1 ~= 0 then error('Invalid delay: ' .. ticks) end - local highest = 1 - for _, n in pairs(powers_of_two) do - if n <= ticks then - highest = n - else break end - end - local flying_text = game.surfaces.nauvis.create_entity{ - name = 'py-ticked-script-delay-' .. highest, - position = {0, 0}, - create_build_effect_smoke = false, - text = '' - } - if not flying_text then error() end - global._delayed_functions = global._delayed_functions or {} - global._delayed_functions[script.register_on_entity_destroyed(flying_text)] = {function_key, ticks - highest, {...}} -end -py.on_event(defines.events.on_entity_destroyed, function(event) - if not global._delayed_functions then return end - local data = global._delayed_functions[event.registration_number] - if not data then return end - global._delayed_functions[event.registration_number] = nil +---@type table> +py.on_tick = {} - local function_key = data[1] - local ticks = data[2] +-- use this to register functions that run at a specific tick +-- can replace on_nth_tick if you register the same function for a later tick +-- pass parameters in a list +---@param tick uint +---@param func function +---@param params any[]? +function py.register_tick_event(tick, func, params) + params = params or {} + if type(params) ~= 'table' then params = {params} end + py.on_tick[tick] = py.on_tick[tick] or {} + py.on_tick[tick][func] = params +end - if ticks == 0 then - local f = py.delayed_functions[function_key] - if not f then error('No function found for key: ' .. function_key) end - f(table.unpack(data[3])) - else - py.execute_later(function_key, ticks, table.unpack(data[3])) +py.on_event(defines.events.on_tick, function(event) + local tick = event.tick + if not py.on_tick[tick] then return end + for func, params in pairs(py.on_tick[tick]) do + func(table.unpack(params)) end + py.on_tick[tick] = nil end) \ No newline at end of file