mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-18 03:21:47 +02:00
commit
ccae893130
@ -1,4 +1,7 @@
|
||||
--Author: Valansch
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function is_depleted(drill)
|
||||
local position = drill.position
|
||||
local area = {}
|
||||
@ -33,4 +36,4 @@ local function on_resource_depleted(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_resource_depleted, on_resource_depleted)
|
||||
Event.add(defines.events.on_resource_depleted, on_resource_depleted)
|
||||
|
8
band.lua
8
band.lua
@ -2,7 +2,7 @@
|
||||
-- Version 0.1.6
|
||||
-- https://github.com/Befzz/factorio_random/tree/master/scenarios/befzz_test
|
||||
|
||||
-- Requires event.lua to work ( https://github.com/3RaGaming/utils )
|
||||
local Event = require "utils.event"
|
||||
|
||||
-- SETTINGS
|
||||
local option_band_change_interval = 60 * 3 -- in ticks
|
||||
@ -411,6 +411,6 @@ local function on_gui_elem_changed(event)
|
||||
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_gui_elem_changed, on_gui_elem_changed)
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_joined_game, create_band_gui)
|
||||
Event.add(defines.events.on_gui_elem_changed, on_gui_elem_changed)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_joined_game, create_band_gui)
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
if not global.score_biter_total_kills then global.score_biter_total_kills = 0 end
|
||||
|
||||
local function biter_kill_counter(event)
|
||||
@ -6,4 +8,4 @@ local function biter_kill_counter(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_entity_died, biter_kill_counter)
|
||||
Event.add(defines.events.on_entity_died, biter_kill_counter)
|
@ -1,5 +1,4 @@
|
||||
require "util"
|
||||
require "utils.event"
|
||||
require "config"
|
||||
require "utils.utils"
|
||||
require "utils.list_utils"
|
||||
@ -23,6 +22,7 @@ require "tasklist"
|
||||
require "autodeconstruct"
|
||||
require "corpse_util"
|
||||
require "reactor_meltdown"
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function player_joined(event)
|
||||
local player = game.players[event.player_index]
|
||||
@ -64,5 +64,5 @@ function hodor(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_player_created, player_joined)
|
||||
Event.register(defines.events.on_console_chat, hodor)
|
||||
Event.add(defines.events.on_player_created, player_joined)
|
||||
Event.add(defines.events.on_console_chat, hodor)
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
local ttl = 15*60*60
|
||||
local function on_init()
|
||||
global.corpse_util = {}
|
||||
@ -28,6 +30,6 @@ local function remove_corpse_marks()
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(-1, on_init)
|
||||
Event.register(defines.events.on_player_died, mark_corpse)
|
||||
Event.register(defines.events.on_tick, remove_corpse_marks)
|
||||
Event.on_init(on_init)
|
||||
Event.add(defines.events.on_player_died, mark_corpse)
|
||||
Event.add(defines.events.on_tick, remove_corpse_marks)
|
||||
|
@ -1,5 +1,5 @@
|
||||
local Task = require "utils.Task"
|
||||
require "utils.event"
|
||||
local Event = require "utils.event"
|
||||
|
||||
function player_print(str)
|
||||
if game.player then
|
||||
@ -256,7 +256,7 @@ local function built_entity(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_built_entity, built_entity)
|
||||
Event.add(defines.events.on_built_entity, built_entity)
|
||||
|
||||
local function toggle_tp_mode(cmd)
|
||||
if not game.player or not (game.player.admin or is_mod(game.player.name)) then
|
||||
|
@ -17,6 +17,8 @@ make pet faster
|
||||
make pet follow you moar
|
||||
--]]
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
function spawn_market(cmd)
|
||||
if not game.player or not game.player.admin then
|
||||
cant_run(cmd.name)
|
||||
@ -402,7 +404,7 @@ function fish_player_crafted_item(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_pre_player_mined_item, pre_player_mined_item)
|
||||
Event.register(defines.events.on_entity_died, fish_drop_entity_died)
|
||||
Event.register(defines.events.on_market_item_purchased, market_item_purchased)
|
||||
Event.register(defines.events.on_player_crafted_item, fish_player_crafted_item)
|
||||
Event.add(defines.events.on_pre_player_mined_item, pre_player_mined_item)
|
||||
Event.add(defines.events.on_entity_died, fish_drop_entity_died)
|
||||
Event.add(defines.events.on_market_item_purchased, market_item_purchased)
|
||||
Event.add(defines.events.on_player_crafted_item, fish_player_crafted_item)
|
||||
|
6
info.lua
6
info.lua
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function create_info_button(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
@ -78,5 +80,5 @@ local function on_gui_click(event)
|
||||
end
|
||||
|
||||
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_joined_game, create_info_button)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_joined_game, create_info_button)
|
||||
|
@ -1,6 +1,7 @@
|
||||
--Author: MewMew
|
||||
-- !! ATTENTION !!
|
||||
-- Use water only in starting area as map setting!!!
|
||||
local Event = require "utils.event"
|
||||
require "map_gen.shared.perlin_noise"
|
||||
|
||||
wreck_item_pool = {}
|
||||
@ -485,4 +486,4 @@ local function on_tick()
|
||||
end
|
||||
|
||||
|
||||
Event.register(defines.events.on_tick, on_tick) --]]
|
||||
Event.add(defines.events.on_tick, on_tick) --]]
|
@ -1,5 +1,7 @@
|
||||
--Author: Valansch
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
local wrech_items_module = require "map_gen.misc.wreck_items"
|
||||
|
||||
local resource_types = {"copper-ore", "iron-ore", "coal", "stone", "uranium-ore", "crude-oil"}
|
||||
@ -158,4 +160,4 @@ end
|
||||
|
||||
commands.add_command("linkchests", "Select a chest to link to another. Run this command again to select the other one.", linkchests)
|
||||
commands.add_command("linkportals", "Select a portal to link to another. Run this command again to select the other one.", linkportals)
|
||||
Event.register(defines.events.on_tick, dim_on_tick)
|
||||
Event.add(defines.events.on_tick, dim_on_tick)
|
||||
|
@ -1,5 +1,6 @@
|
||||
require "map_gen.shared.perlin_noise"
|
||||
local Task = require "utils.Task"
|
||||
local Event = require "utils.event"
|
||||
|
||||
local block_size = 1 -- in tiles
|
||||
local start_size = 64 -- in blocks
|
||||
@ -171,4 +172,4 @@ local function on_tick()
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_tick, on_tick)
|
||||
Event.add(defines.events.on_tick, on_tick)
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
mymodule = {}
|
||||
|
||||
local function rot_pos(pos, rot)
|
||||
@ -68,7 +70,7 @@ local function on_init()
|
||||
connect_line({ 4, 21}, { 15, 21})
|
||||
end
|
||||
|
||||
Event.register(-1, on_init)
|
||||
Event.on_init(on_init)
|
||||
|
||||
local function build_intersection(type, origin, rot)
|
||||
local surface = game.surfaces[1]
|
||||
|
@ -1,4 +1,6 @@
|
||||
Event.register(-1,function()
|
||||
local Event = require "utils.event"
|
||||
|
||||
Event.on_init(function()
|
||||
|
||||
global.players = {}
|
||||
|
||||
@ -9,7 +11,7 @@ Event.register(-1,function()
|
||||
|
||||
end)
|
||||
|
||||
Event.register(defines.events.on_player_created, function(event)
|
||||
Event.add(defines.events.on_player_created, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
--player.print("Info: PVP server mod 'Bearded Snails' (c) byte");
|
||||
guiNewPlayer(player.gui.left);
|
||||
@ -27,7 +29,7 @@ Event.register(defines.events.on_player_created, function(event)
|
||||
player.force.chart(player.surface, {{player.position.x - 200, player.position.y - 200}, {player.position.x + 200, player.position.y + 200}})
|
||||
end)
|
||||
|
||||
Event.register(defines.events.on_player_respawned, function(event)
|
||||
Event.add(defines.events.on_player_respawned, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
player.insert{name="heavy-armor", count=1}
|
||||
@ -38,7 +40,7 @@ Event.register(defines.events.on_player_respawned, function(event)
|
||||
player.character.character_running_speed_modifier = 0.5
|
||||
end)
|
||||
|
||||
Event.register(defines.events.on_rocket_launched, function(event)
|
||||
Event.add(defines.events.on_rocket_launched, function(event)
|
||||
local force = event.rocket.force
|
||||
if event.rocket.get_item_count("satellite") > 0 then
|
||||
if global.satellite_sent == nil then
|
||||
@ -66,7 +68,7 @@ Event.register(defines.events.on_rocket_launched, function(event)
|
||||
end
|
||||
end)
|
||||
|
||||
Event.register(defines.events.on_gui_click, function(event)
|
||||
Event.add(defines.events.on_gui_click, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
local gui = player.gui.left;
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
map_gen_decoratives = false -- Generate our own decoratives
|
||||
map_gen_rows_per_tick = 8 -- Inclusive integer between 1 and 32. Used for map_gen_threaded, higher numbers will generate map quicker but cause more lag.
|
||||
|
||||
@ -37,7 +39,7 @@ local function research_finished(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_research_finished, research_finished)
|
||||
Event.add(defines.events.on_research_finished, research_finished)
|
||||
|
||||
local function max_axis_distance(world_x, world_y, target_x, target_y)
|
||||
local x = math.abs(world_x - target_x)
|
||||
|
@ -1,4 +1,5 @@
|
||||
local Task = require "utils.Task"
|
||||
local Event = require "utils.event"
|
||||
|
||||
local mines_factor = 4
|
||||
|
||||
@ -30,4 +31,4 @@ local death_messages = {"Went exploring, and didn't bring a minesweeping kit.","
|
||||
local function player_died()
|
||||
game.print(death_messages[math.random(1, #death_messages)])
|
||||
end
|
||||
Event.register(defines.events.on_player_died, player_died)
|
||||
Event.add(defines.events.on_player_died, player_died)
|
||||
|
@ -5,6 +5,8 @@ If you want to add your own module, just add it to the others
|
||||
in this file and your run_*type*_module(event) function will be called.
|
||||
--]]
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
--combined--
|
||||
--require "map_gen.combined.island_resort"
|
||||
--require "map_gen.combined.red_planet_v2"
|
||||
@ -107,4 +109,4 @@ local on_chunk_generated = function(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_chunk_generated, on_chunk_generated)
|
||||
Event.add(defines.events.on_chunk_generated, on_chunk_generated)
|
||||
|
@ -1,4 +1,4 @@
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
function allowed_to_nuke(player)
|
||||
if type(player) == "table" then
|
||||
@ -117,8 +117,8 @@ local function on_capsule_used(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_player_ammo_inventory_changed, ammo_changed)
|
||||
Event.register(defines.events.on_player_deconstructed_area, on_player_deconstructed_area)
|
||||
--Event.register(defines.events.on_player_mined_entity, on_player_mined_item)
|
||||
Event.register(defines.events.on_player_used_capsule, on_capsule_used)
|
||||
Event.add(defines.events.on_player_ammo_inventory_changed, ammo_changed)
|
||||
Event.add(defines.events.on_player_deconstructed_area, on_player_deconstructed_area)
|
||||
--Event.add(defines.events.on_player_mined_entity, on_player_mined_item)
|
||||
Event.add(defines.events.on_player_used_capsule, on_capsule_used)
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function on_tick()
|
||||
walk_on_tick()
|
||||
if game.tick % 60 == 0 then
|
||||
@ -15,4 +17,4 @@ local function on_tick()
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_tick, on_tick)
|
||||
Event.add(defines.events.on_tick, on_tick)
|
||||
|
@ -16,6 +16,8 @@ things to do (maybe)
|
||||
make it sorted by time played
|
||||
--]]
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
local symbol_asc = "▲"
|
||||
local symbol_desc = "▼"
|
||||
|
||||
@ -412,6 +414,6 @@ local function player_list_on_player_died(event)
|
||||
end
|
||||
|
||||
|
||||
Event.register(defines.events.on_player_joined_game, on_player_joined_game)
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_died, player_list_on_player_died)
|
||||
Event.add(defines.events.on_player_joined_game, on_player_joined_game)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_died, player_list_on_player_died)
|
||||
|
8
poll.lua
8
poll.lua
@ -3,6 +3,8 @@
|
||||
-- by MewMew -- with some help from RedLabel, Klonan, Morcup, BrainClot
|
||||
----------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function create_poll_gui(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
@ -317,6 +319,6 @@ function poll_on_second()
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_joined_game, create_poll_gui)
|
||||
Event.register(defines.events.on_player_joined_game, poll_sync_for_new_joining_player)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_joined_game, create_poll_gui)
|
||||
Event.add(defines.events.on_player_joined_game, poll_sync_for_new_joining_player)
|
||||
|
@ -3,6 +3,8 @@
|
||||
--
|
||||
--a reactors loses 2 damage per second at 1000°C
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
global.wastelands = {}
|
||||
global.reactors = {}
|
||||
local wasteland_duration_seconds = 300
|
||||
@ -114,9 +116,9 @@ local function entity_build(event)
|
||||
end
|
||||
end
|
||||
|
||||
script.on_nth_tick(67, on_tick)
|
||||
Event.register(defines.events.on_player_mined_entity, entity_destroyed)
|
||||
Event.register(defines.events.on_robot_mined_entity, entity_destroyed)
|
||||
Event.register(defines.events.on_entity_died, entity_destroyed)
|
||||
Event.register(defines.events.on_built_entity, entity_build)
|
||||
Event.register(defines.events.on_robot_built_entity, entity_build)
|
||||
Event.on_nth_tick(60, on_tick)
|
||||
Event.add(defines.events.on_player_mined_entity, entity_destroyed)
|
||||
Event.add(defines.events.on_robot_mined_entity, entity_destroyed)
|
||||
Event.add(defines.events.on_entity_died, entity_destroyed)
|
||||
Event.add(defines.events.on_built_entity, entity_build)
|
||||
Event.add(defines.events.on_robot_built_entity, entity_build)
|
||||
|
10
score.lua
10
score.lua
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
if not global.score_rockets_launched then global.score_rockets_launched = 0 end
|
||||
|
||||
local function create_score_gui(event)
|
||||
@ -92,7 +94,7 @@ end
|
||||
|
||||
|
||||
|
||||
Event.register(defines.events.on_entity_died, refresh_score)
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_joined_game, create_score_gui)
|
||||
Event.register(defines.events.on_rocket_launched, rocket_launched)
|
||||
Event.add(defines.events.on_entity_died, refresh_score)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_joined_game, create_score_gui)
|
||||
Event.add(defines.events.on_rocket_launched, rocket_launched)
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
global.player_spawns = {} -- player_index to spawn_name
|
||||
global.spawns = {} -- spawn_name to x, y, player_online_count
|
||||
|
||||
@ -243,9 +245,9 @@ local function print_players_for_spawn_command(cmd)
|
||||
print_players_for_spawn(ps[1])
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_player_joined_game, player_joined_game)
|
||||
Event.register(defines.events.on_player_left_game, player_left_game)
|
||||
Event.register(defines.events.on_player_respawned, player_respawned)
|
||||
Event.add(defines.events.on_player_joined_game, player_joined_game)
|
||||
Event.add(defines.events.on_player_left_game, player_left_game)
|
||||
Event.add(defines.events.on_player_respawned, player_respawned)
|
||||
|
||||
commands.add_command("tpspawn", "<player> <spawn_name> teleports a player to the spawn point (Admins only)", tp_spawn_command)
|
||||
commands.add_command("changespawn", "<player> <spawn_name> changes the spawn point for a player (Admins only)", change_spawn_command)
|
||||
|
@ -3,6 +3,8 @@
|
||||
-- based on MewMew's Poll
|
||||
----------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function create_tasklist_gui(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
@ -199,6 +201,6 @@ local function on_gui_click(event)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_gui_click, on_gui_click)
|
||||
Event.register(defines.events.on_player_joined_game, create_tasklist_gui)
|
||||
Event.register(defines.events.on_player_joined_game, tasklist_sync_for_new_joining_player)
|
||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||
Event.add(defines.events.on_player_joined_game, create_tasklist_gui)
|
||||
Event.add(defines.events.on_player_joined_game, tasklist_sync_for_new_joining_player)
|
||||
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
local function player_built_entity(event)
|
||||
|
||||
if event.created_entity.name == "train-stop" then
|
||||
@ -13,5 +15,5 @@ local function player_built_entity(event)
|
||||
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_built_entity, player_built_entity)
|
||||
Event.register(defines.events.on_robot_built_entity, player_built_entity)
|
||||
Event.add(defines.events.on_built_entity, player_built_entity)
|
||||
Event.add(defines.events.on_robot_built_entity, player_built_entity)
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
local Queue = require "utils.Queue"
|
||||
local PriorityQueue = require "utils.PriorityQueue"
|
||||
local Event = require "utils.event"
|
||||
|
||||
local Task = {}
|
||||
|
||||
@ -76,6 +77,6 @@ function Task.queue_task(func_name, params, weight)
|
||||
Queue.push(global.task_queue, {func_name = func_name, params = params, weight = weight})
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_tick, on_tick)
|
||||
Event.add(defines.events.on_tick, on_tick)
|
||||
|
||||
return Task
|
||||
|
265
utils/event.lua
265
utils/event.lua
@ -1,131 +1,152 @@
|
||||
--Event Capture
|
||||
--A 3Ra Gaming revision, original from Factorio-Stdlib by Afforess
|
||||
-- @module Event
|
||||
local Event = {}
|
||||
|
||||
function fail_if_missing(var, msg)
|
||||
if not var then
|
||||
if msg then
|
||||
error(msg, 3)
|
||||
else
|
||||
error("Missing value", 3)
|
||||
end
|
||||
end
|
||||
return false
|
||||
local debug_mode = false
|
||||
|
||||
local init_event_name = -1
|
||||
local load_event_name = -2
|
||||
|
||||
local control_stage = true
|
||||
|
||||
local event_handlers = {}-- map of event_name to handlers[]
|
||||
local on_nth_tick_event_handlers = {}-- map of nth_tick to handlers[]
|
||||
|
||||
local function call_handlers(handlers, event)
|
||||
if debug_mode then
|
||||
for _, handler in ipairs(handlers) do
|
||||
handler(event)
|
||||
end
|
||||
else
|
||||
for _, handler in ipairs(handlers) do
|
||||
local success, error = pcall(handler, event)
|
||||
if not success then
|
||||
log(error)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Event = {
|
||||
_registry = {},
|
||||
core_events = {
|
||||
init = -1,
|
||||
load = -2,
|
||||
configuration_changed = -3,
|
||||
_register = function(id)
|
||||
if id == Event.core_events.init then
|
||||
script.on_init(function()
|
||||
Event.dispatch({ name = Event.core_events.init, tick = game.tick })
|
||||
end)
|
||||
elseif id == Event.core_events.load then
|
||||
script.on_load(function()
|
||||
Event.dispatch({ name = Event.core_events.load, tick = -1 })
|
||||
end)
|
||||
elseif id == Event.core_events.configuration_changed then
|
||||
script.on_configuration_changed(function(data)
|
||||
Event.dispatch({ name = Event.core_events.configuration_changed, tick = game.tick, data = data })
|
||||
end)
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
--- Registers a function for a given event
|
||||
-- @param event or array containing events to register
|
||||
-- @param handler Function to call when event is triggered
|
||||
-- @return #Event
|
||||
function Event.register(event, handler)
|
||||
fail_if_missing(event, "missing event argument")
|
||||
|
||||
if type(event) == "number" then
|
||||
event = { event }
|
||||
end
|
||||
|
||||
for _, event_id in pairs(event) do
|
||||
fail_if_missing(event_id, "missing event id")
|
||||
if handler == nil then
|
||||
Event._registry[event_id] = nil
|
||||
script.on_event(event_id, nil)
|
||||
else
|
||||
if not Event._registry[event_id] then
|
||||
Event._registry[event_id] = {}
|
||||
|
||||
if event_id >= 0 then
|
||||
script.on_event(event_id, Event.dispatch)
|
||||
else
|
||||
Event.core_events._register(event_id)
|
||||
end
|
||||
end
|
||||
table.insert(Event._registry[event_id], handler)
|
||||
end
|
||||
end
|
||||
return Event
|
||||
local function on_event(event)
|
||||
local handlers = event_handlers[event.name]
|
||||
call_handlers(handlers, event)
|
||||
end
|
||||
|
||||
--- Calls the registerd handlers
|
||||
-- @param event LuaEvent as created by game.raise_event
|
||||
function Event.dispatch(event)
|
||||
fail_if_missing(event, "missing event argument")
|
||||
if Event._registry[event.name] then
|
||||
for _, handler in pairs(Event._registry[event.name]) do
|
||||
local metatbl = { __index = function(tbl, key) if key == '_handler' then return handler else return rawget(tbl, key) end end }
|
||||
setmetatable(event, metatbl)
|
||||
local success, err = pcall(handler, event)
|
||||
if not success then
|
||||
-- may be nil in on_load
|
||||
local identifier = event.name
|
||||
for i,v in pairs(defines.events) do
|
||||
if v == event.name then
|
||||
identifier = i
|
||||
end
|
||||
end
|
||||
err = string.gsub(err, "\n", " : ")
|
||||
if event.name < -1 or global.last_error ~= identifier then
|
||||
print("output$Error in event "..identifier..": \""..err.."\".")
|
||||
log("Error in event "..identifier..": \""..err.."\".")
|
||||
if event.name > -2 then
|
||||
global.last_error = identifier
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local function on_init()
|
||||
local handlers = event_handlers[init_event_name]
|
||||
call_handlers(handlers)
|
||||
end
|
||||
|
||||
--- Removes the handler from the event
|
||||
-- @param event event or array containing events to remove the handler
|
||||
-- @param handler to remove
|
||||
-- @return #Event
|
||||
function Event.remove(event, handler)
|
||||
fail_if_missing(event, "missing event argument")
|
||||
fail_if_missing(handler, "missing handler argument")
|
||||
|
||||
if type(event) == "number" then
|
||||
event = { event }
|
||||
end
|
||||
|
||||
for _, event_id in pairs(event) do
|
||||
fail_if_missing(event_id, "missing event id")
|
||||
if Event._registry[event_id] then
|
||||
for i = #Event._registry[event_id], 1, -1 do
|
||||
if Event._registry[event_id][i] == handler then
|
||||
table.remove(Event._registry[event_id], i)
|
||||
end
|
||||
end
|
||||
if #Event._registry[event_id] == 0 then
|
||||
Event._registry[event_id] = nil
|
||||
script.on_event(event_id, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
return Event
|
||||
local function on_load()
|
||||
local handlers = event_handlers[load_event_name]
|
||||
call_handlers(handlers)
|
||||
end
|
||||
|
||||
local function on_nth_tick_event(event)
|
||||
local handlers = on_nth_tick_event_handlers[event.nth_tick]
|
||||
call_handlers(handlers, event)
|
||||
end
|
||||
|
||||
function Event.add(event_name, handler)
|
||||
local handlers = event_handlers[event_name]
|
||||
if not handlers then
|
||||
event_handlers[event_name] = {handler}
|
||||
script.on_event(event_name, on_event)
|
||||
else
|
||||
table.insert(handlers, handler)
|
||||
end
|
||||
end
|
||||
|
||||
function Event.on_init(handler)
|
||||
local handlers = event_handlers[init_event_name]
|
||||
if not handlers then
|
||||
event_handlers[init_event_name] = {handler}
|
||||
script.on_init(on_init)
|
||||
else
|
||||
table.insert(handlers, handler)
|
||||
end
|
||||
end
|
||||
|
||||
function Event.on_load(handler)
|
||||
local handlers = event_handlers[load_event_name]
|
||||
if not handlers then
|
||||
event_handlers[load_event_name] = {handler}
|
||||
script.on_load(on_load)
|
||||
else
|
||||
table.insert(handlers, handler)
|
||||
end
|
||||
end
|
||||
|
||||
function Event.on_nth_tick(tick, handler)
|
||||
local handlers = on_nth_tick_event_handlers[tick]
|
||||
if not handlers then
|
||||
on_nth_tick_event_handlers[tick] = {handler}
|
||||
script.on_nth_tick(tick, on_nth_tick_event)
|
||||
else
|
||||
table.insert(handlers, handler)
|
||||
end
|
||||
end
|
||||
|
||||
local Token = require "utils.global_token"
|
||||
global.event_tokens = {}
|
||||
|
||||
function Event.add_removable(event_name, token)
|
||||
local event_tokens = global.event_tokens
|
||||
|
||||
local tokens = event_tokens[event_name]
|
||||
if not tokens then
|
||||
event_tokens[event_name] = {token}
|
||||
else
|
||||
table.insert(tokens, token)
|
||||
end
|
||||
|
||||
if not control_stage then
|
||||
local handler = Token.get(token)
|
||||
Event.add(event_name, handler)
|
||||
end
|
||||
end
|
||||
|
||||
local function remove(t, e)
|
||||
for i, v in ipairs(t) do
|
||||
if v == e then
|
||||
table.remove(t, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Event.remove_removable(event_name, token)
|
||||
local event_tokens = global.event_tokens
|
||||
|
||||
local tokens = event_tokens[event_name]
|
||||
|
||||
if not tokens then
|
||||
return
|
||||
end
|
||||
|
||||
local handler = Token.get(token)
|
||||
local handlers = event_handlers[event_name]
|
||||
|
||||
remove(tokens, token)
|
||||
remove(handlers, handler)
|
||||
|
||||
if #handlers == 0 then
|
||||
script.on_event(event_name, nil)
|
||||
end
|
||||
end
|
||||
|
||||
local function add_token_handlers()
|
||||
control_stage = false
|
||||
|
||||
local event_tokens = global.event_tokens
|
||||
|
||||
for event_name, tokens in pairs(event_tokens) do
|
||||
for _, token in ipairs(tokens) do
|
||||
local handler = Token.get(token)
|
||||
Event.add(event_name, handler)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Event.on_init(add_token_handlers)
|
||||
Event.on_load(add_token_handlers)
|
||||
|
||||
return Event
|
||||
|
20
utils/global_token.lua
Normal file
20
utils/global_token.lua
Normal file
@ -0,0 +1,20 @@
|
||||
local Token = {}
|
||||
|
||||
local tokens = {}
|
||||
|
||||
local counter = 1
|
||||
|
||||
function Token.register(var)
|
||||
local c = counter
|
||||
|
||||
tokens[c] = var
|
||||
counter = c + 1
|
||||
|
||||
return c
|
||||
end
|
||||
|
||||
function Token.get(token_id)
|
||||
return tokens[token_id]
|
||||
end
|
||||
|
||||
return Token
|
@ -3,6 +3,8 @@
|
||||
-- define debug_status to 1 or nil in the control.lua, before statement require("utils")
|
||||
-- define also debug_file and debug_mod_name
|
||||
|
||||
local Event = require "utils.event"
|
||||
|
||||
colors = {
|
||||
white = { r = 1, g = 1, b = 1 },
|
||||
black = { r = 0, g = 0, b = 0 },
|
||||
@ -494,7 +496,7 @@ function print_admins(msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
function check_name(function_name)
|
||||
for i,v in pairs(global.scenario.custom_functions) do
|
||||
if v.name == function_name:lower() then
|
||||
@ -511,7 +513,7 @@ function add_global_event(event, func, name)
|
||||
if not name then p("Missing name parameter") return end
|
||||
if check_name(name) then p("Function name \""..name.."\" already in use.") return end
|
||||
table.insert(global.scenario.custom_functions, {event = event, name = name, func = func})
|
||||
Event.register(event, func)
|
||||
Event.add(event, func)
|
||||
end
|
||||
|
||||
function remove_global_event(name)
|
||||
@ -524,8 +526,9 @@ function remove_global_event(name)
|
||||
end
|
||||
end
|
||||
|
||||
Event.register(-2, function()
|
||||
Event.add(-2, function()
|
||||
for i,v in pairs(global.scenario.custom_functions) do
|
||||
Event.register(v.event, v.func)
|
||||
Event.add(v.event, v.func)
|
||||
end
|
||||
end)
|
||||
]]
|
@ -1,3 +1,5 @@
|
||||
local Event = require "utils.event"
|
||||
|
||||
function walk_distance_on_second()
|
||||
local last_positions = global.scenario.variables.player_positions
|
||||
local d_x = 0
|
||||
@ -30,4 +32,4 @@ local function init_player_position(event)
|
||||
end
|
||||
end
|
||||
|
||||
-- Event.register(defines.events.on_player_joined_game, init_player_position)
|
||||
-- Event.add(defines.events.on_player_joined_game, init_player_position)
|
||||
|
Loading…
x
Reference in New Issue
Block a user