|
| 1 | + |
| 2 | +local module = {} |
| 3 | + |
| 4 | + |
| 5 | +--#region Global data |
| 6 | +local players_data |
| 7 | +--#endregion |
| 8 | + |
| 9 | + |
| 10 | +--#region Constants |
| 11 | +local ABS = math.abs |
| 12 | +--#endregion |
| 13 | + |
| 14 | + |
| 15 | +--#region Functions of events |
| 16 | + |
| 17 | +local function on_player_created(event) |
| 18 | + local player = game.get_player(event.player_index) |
| 19 | + if not (player and player.valid) then return end |
| 20 | + |
| 21 | + player.print("Events of example scenario works fine. Also, scripts of example mod has been disabled!") |
| 22 | +end |
| 23 | + |
| 24 | +local function on_game_created_from_scenario(event) |
| 25 | + -- ######## |
| 26 | + -- Some examples of code what you can do with this event below |
| 27 | + -- Some interaction with game ignores (i.q: game.print("loaded scenario")) |
| 28 | + -- ######## |
| 29 | + |
| 30 | + -- disable_recipes() |
| 31 | + |
| 32 | + -- if global.biters_destination == nil then |
| 33 | + -- local target = game.get_entity_by_tag("target") |
| 34 | + -- global.biters_destination = target.position |
| 35 | + -- biters_destination = global.biters_destination |
| 36 | + -- if global.target_id == nil and target then |
| 37 | + -- global.target_id = script.register_on_entity_destroyed(target) |
| 38 | + -- end |
| 39 | + -- end |
| 40 | + |
| 41 | + -- local biters_spawn |
| 42 | + -- biters_spawn = game.get_entity_by_tag("biters_spawn_1") |
| 43 | + -- global.biters_spawn_position_1 = global.biters_spawn_position_1 or biters_spawn.position |
| 44 | + -- biters_spawn_position_1 = global.biters_spawn_position_1 |
| 45 | +end |
| 46 | + |
| 47 | +--#endregion |
| 48 | + |
| 49 | + |
| 50 | +--#region Commands |
| 51 | + |
| 52 | +local function delete_example_UI_command(cmd) |
| 53 | + if cmd.player_index == 0 then -- server |
| 54 | + print("Deleted UIs") |
| 55 | + else |
| 56 | + local player = game.get_player(cmd.player_index) |
| 57 | + if not player.admin then |
| 58 | + player.print({"command-output.parameters-require-admin"}) |
| 59 | + return |
| 60 | + end |
| 61 | + player.print("Deleted UIs") |
| 62 | + end |
| 63 | + |
| 64 | + for _, player in pairs(game.players) do |
| 65 | + if player.valid then |
| 66 | + -- destroy_UIs(player) |
| 67 | + end |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +--#endregion |
| 72 | + |
| 73 | + |
| 74 | +--#region Pre-game stage |
| 75 | + |
| 76 | +local function link_data() |
| 77 | + players_data = global.players |
| 78 | +end |
| 79 | + |
| 80 | +local function update_global_data() |
| 81 | + global.players = global.players or {} |
| 82 | + |
| 83 | + for player_index, player in pairs(game.players) do |
| 84 | + -- delete UIs |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | + |
| 89 | +module.on_init = (function() |
| 90 | + update_global_data() |
| 91 | + link_data() |
| 92 | +end) |
| 93 | + |
| 94 | +module.on_load = (function() |
| 95 | + link_data() |
| 96 | +end) |
| 97 | + |
| 98 | +module.on_configuration_changed = (function() |
| 99 | + update_global_data() |
| 100 | + link_data() |
| 101 | +end) |
| 102 | +module.update_global_data_on_disabling = update_global_data -- for safe disabling of this mod |
| 103 | + |
| 104 | +--#endregion |
| 105 | + |
| 106 | + |
| 107 | +---@type table<number|string, function> |
| 108 | +-- [optional] |
| 109 | +-- All events of https://lua-api.factorio.com/latest/events.html#All%20events except on_nth_tick |
| 110 | +module.events = { |
| 111 | + [defines.events.on_game_created_from_scenario] = on_game_created_from_scenario, |
| 112 | + -- [defines.events.on_gui_click] = on_gui_click, |
| 113 | + [defines.events.on_player_created] = on_player_created, |
| 114 | + -- [defines.events.on_player_joined_game] = on_player_joined_game, |
| 115 | + -- [defines.events.on_player_left_game] = on_player_left_game, |
| 116 | + -- [defines.events.on_player_removed] = delete_player_data, |
| 117 | + -- [defines.events.on_player_changed_surface] = clear_player_data, |
| 118 | + -- [defines.events.on_player_respawned] = clear_player_data, |
| 119 | + -- [defines.events.on_gui_value_changed] = on_gui_value_changed, -- please, don't use it. It impacts UPS significantly |
| 120 | +} |
| 121 | + |
| 122 | +---@type table<number, function> |
| 123 | +-- [optional] |
| 124 | +-- module.on_nth_tick = { |
| 125 | +-- [50] = function() |
| 126 | +-- for player_index, _ in pairs(game.connected_players) do |
| 127 | +-- pcall(update_stuff, player_index) |
| 128 | +-- end |
| 129 | +-- end, |
| 130 | +-- } |
| 131 | + |
| 132 | + |
| 133 | +---@type table<string, function> |
| 134 | +-- [optional] |
| 135 | +-- Check "command-wrapper" folder |
| 136 | +module.commands = { |
| 137 | + delete_example_UI = delete_example_UI_command, |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +return module |
0 commit comments