From 27e49408a42ae5e37f40f4c57c9358e330d3f8b4 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 3 Feb 2019 02:02:54 -0500 Subject: [PATCH 1/2] Push global to Global --- features/admin_commands.lua | 22 +++++++++---- features/gui/paint.lua | 23 +++++++++---- features/reactor_meltdown.lua | 62 ++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 38 deletions(-) diff --git a/features/admin_commands.lua b/features/admin_commands.lua index 9e239174..83336c11 100644 --- a/features/admin_commands.lua +++ b/features/admin_commands.lua @@ -1,5 +1,6 @@ local Task = require 'utils.task' local Token = require 'utils.token' +local Global = require 'utils.global' local UserGroups = require 'features.user_groups' local Report = require 'features.report' local Utils = require 'utils.core' @@ -10,7 +11,16 @@ local Command = require 'utils.command' local format = string.format --- A table of players with tpmode turned on -global.tp_players = {} +local tp_players = {} + +Global.register( + { + tp_players = tp_players + }, + function(tbl) + tp_players = tbl.tp_players + end +) --- Sends a message to all online admins local function admin_chat(args, player) @@ -199,11 +209,11 @@ local function teleport_location(_, player) Game.player_print('Teleporting to your selected entity.') end ---- If a player is in the global.tp_players list, remove ghosts they place and teleport them to that position +--- If a player is in the tp_players list, remove ghosts they place and teleport them to that position local function built_entity(event) local index = event.player_index - if global.tp_players[index] then + if tp_players[index] then local entity = event.created_entity if not entity or not entity.valid or entity.type ~= 'entity-ghost' then @@ -218,13 +228,13 @@ end --- Adds/removes players from the tp_players table (admin only) local function toggle_tp_mode(_, player) local index = player.index - local toggled = global.tp_players[index] + local toggled = tp_players[index] if toggled then - global.tp_players[index] = nil + tp_players[index] = nil Game.player_print('tp mode is now off') else - global.tp_players[index] = true + tp_players[index] = true Game.player_print('tp mode is now on - place a ghost entity to teleport there.') end end diff --git a/features/gui/paint.lua b/features/gui/paint.lua index e5d6c8a9..2b913376 100644 --- a/features/gui/paint.lua +++ b/features/gui/paint.lua @@ -1,6 +1,7 @@ local Event = require 'utils.event' local Gui = require 'utils.gui' local Game = require 'utils.game' +local Global = require 'utils.global' local brush_tool = 'refined-hazard-concrete' @@ -39,13 +40,23 @@ local filter_element_name = Gui.uid_name() local filters_table_name = Gui.uid_name() local filter_table_close_button_name = Gui.uid_name() -global.paint_brushes_by_player = {} +local paint_brushes_by_player = {} + +Global.register( + { + paint_brushes_by_player = paint_brushes_by_player + }, + function(tbl) + paint_brushes_by_player = tbl.paint_brushes_by_player + end +) + local function player_build_tile(event) if event.item.name ~= brush_tool then return end - local replace_tile = global.paint_brushes_by_player[event.player_index] + local replace_tile = paint_brushes_by_player[event.player_index] if not replace_tile then return end @@ -130,7 +141,7 @@ local function toggle(event) caption = 'Paint Brush' } - local tooltip = global.paint_brushes_by_player[event.player_index] or '' + local tooltip = paint_brushes_by_player[event.player_index] or '' local brush = main_frame.add({type = 'flow'}).add { @@ -156,7 +167,7 @@ Gui.on_click( filter_button_name, function(event) if event.button == defines.mouse_button_type.right then - global.paint_brushes_by_player[event.player_index] = nil + paint_brushes_by_player[event.player_index] = nil local element = event.element element.sprite = 'utility/pump_cannot_connect_icon' element.tooltip = '' @@ -174,7 +185,7 @@ Gui.on_click( brush.sprite = 'utility/pump_cannot_connect_icon' brush.tooltip = '' - global.paint_brushes_by_player[event.player_index] = nil + paint_brushes_by_player[event.player_index] = nil end ) @@ -185,7 +196,7 @@ Gui.on_click( local frame = Gui.get_data(element) local filter_button = Gui.get_data(frame) - global.paint_brushes_by_player[event.player_index] = element.tooltip + paint_brushes_by_player[event.player_index] = element.tooltip filter_button.sprite = element.sprite filter_button.tooltip = element.tooltip diff --git a/features/reactor_meltdown.lua b/features/reactor_meltdown.lua index 07fcb0e2..aec98213 100644 --- a/features/reactor_meltdown.lua +++ b/features/reactor_meltdown.lua @@ -6,10 +6,25 @@ local Event = require 'utils.event' local Game = require 'utils.game' local Command = require 'utils.command' +local Global = require 'utils.global' + +local primitives = {reactors_enabled = {global.config.reactor_meltdown.on_by_default}} +local wastelands = {} +local reactors = {} + +Global.register( + { + primitives = primitives, + wastelands = wastelands, + reactors = reactors + }, + function(tbl) + primitives = tbl.primitives + wastelands = tbl.wastelands + reactors = tbl.reactors + end +) -global.reactors_enabled = global.config.reactor_meltdown.on_by_default -global.wastelands = {} -global.reactors = {} local wasteland_duration_seconds = 300 local function spawn_wasteland(surface, position) @@ -51,7 +66,7 @@ local function spawn_wasteland(surface, position) end local function entity_destroyed(event) - if not global.reactors_enabled or not event.entity.valid or event.entity.name ~= 'nuclear-reactor' then + if not primitives.reactors_enabled or not event.entity.valid or event.entity.name ~= 'nuclear-reactor' then return end @@ -60,7 +75,7 @@ local function entity_destroyed(event) if reactor.temperature > 700 then reactor.surface.create_entity {name = 'atomic-rocket', position = reactor.position, target = reactor, speed = 1} spawn_wasteland(reactor.surface, reactor.position) - global.wastelands[reactor.position.x .. '/' .. reactor.position.y] = { + wastelands[reactor.position.x .. '/' .. reactor.position.y] = { position = reactor.position, surface_id = reactor.surface.index, creation_time = game.tick @@ -70,18 +85,13 @@ end local function alert(reactor) for _, p in pairs(game.players) do - p.add_custom_alert( - reactor, - {type = 'item', name = 'nuclear-reactor'}, - string.format('Reactor at %s°C', math.floor(reactor.temperature)), - true - ) + p.add_custom_alert(reactor, {type = 'item', name = 'nuclear-reactor'}, string.format('Reactor at %s°C', math.floor(reactor.temperature)), true) end end local function check_reactors() for _ in pairs(game.surfaces) do - for i, reactor in pairs(global.reactors) do + for i, reactor in pairs(reactors) do if reactor.valid then if reactor.temperature > 800 then alert(reactor) @@ -97,17 +107,17 @@ local function check_reactors() speed = 1 } spawn_wasteland(reactor.surface, reactor.position) - global.wastelands[reactor.position.x .. '/' .. reactor.position.y] = { + wastelands[reactor.position.x .. '/' .. reactor.position.y] = { position = reactor.position, surface_id = reactor.surface.index, creation_time = game.tick } - table.remove(global.reactors, i) + table.remove(reactors, i) else reactor.health = 500 - (reactor.temperature - 800) * 2.5 end else - table.remove(global.reactors, i) + table.remove(reactors, i) end end --global.last_reactor_warning = last_reactor_warning @@ -115,21 +125,21 @@ local function check_reactors() end local function check_wastelands() - for index, wl in pairs(global.wastelands) do + for index, wl in pairs(wastelands) do local age = game.tick - wl.creation_time wl.last_checked = wl.last_checked or 0 if (game.tick - wl.last_checked) > 899 then wl.last_checked = game.tick spawn_wasteland(game.surfaces[wl.surface_id], wl.position) if age > wasteland_duration_seconds * 60 - 1 then - global.wastelands[index] = nil - local reactors = + wastelands[index] = nil + local wasteland_reactors = game.surfaces[wl.surface_id].find_entities_filtered { position = wl.position, name = 'nuclear-reactor' } - if reactors[1] then - reactors[1].destroy() + if wasteland_reactors[1] then + wasteland_reactors[1].destroy() end end end @@ -137,7 +147,7 @@ local function check_wastelands() end local function on_tick() - if global.reactors_enabled then + if primitives.reactors_enabled then check_wastelands() check_reactors() end @@ -147,14 +157,14 @@ local function entity_build(event) if not event.created_entity.valid then return end - if event.created_entity.name == 'nuclear-reactor' and event.created_entity.surface.name ~= "antigrief" then - table.insert(global.reactors, event.created_entity) + if event.created_entity.name == 'nuclear-reactor' and event.created_entity.surface.name ~= 'antigrief' then + table.insert(reactors, event.created_entity) end end local function reactor_toggle() - global.reactors_enabled = not global.reactors_enabled - if global.reactors_enabled then + primitives.reactors_enabled = not primitives.reactors_enabled + if primitives.reactors_enabled then game.print('Reactor meltdown activated.') else game.print('Reactor meltdown deactivated.') @@ -162,7 +172,7 @@ local function reactor_toggle() end local function is_meltdown() - if global.reactors_enabled then + if primitives.reactors_enabled then Game.player_print('Reactor meltdown is enabled.') else Game.player_print('Reactor meltdown is disabled.') From 42f3553bc09259942f2b9cde85b65b79f27b3404 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 3 Feb 2019 02:05:06 -0500 Subject: [PATCH 2/2] Contain within global.namespace --- features/nuke_control.lua | 61 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 6ecce459..ea498234 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -75,38 +75,41 @@ local function item_not_sanctioned(item) return (name:find('capsule') or name == 'cliff-explosives' or name == 'raw-fish' or name == 'discharge-defense-remote') end -global.entities_allowed_to_bomb = { - ['stone-wall'] = true, - ['transport-belt'] = true, - ['fast-transport-belt'] = true, - ['express-transport-belt'] = true, - ['construction-robot'] = true, - ['player'] = true, - ['gun-turret'] = true, - ['laser-turret'] = true, - ['flamethrower-turret'] = true, - ['rail'] = true, - ['rail-chain-signal'] = true, - ['rail-signal'] = true, - ['tile-ghost'] = true, - ['entity-ghost'] = true, - ['gate'] = true, - ['electric-pole'] = true, - ['small-electric-pole'] = true, - ['medium-electric-pole'] = true, - ['big-electric-pole'] = true, - ['logistic-robot'] = true, - ['defender'] = true, - ['destroyer'] = true, - ['distractor'] = true +global.nuke_control = { + entities_allowed_to_bomb = { + ['stone-wall'] = true, + ['transport-belt'] = true, + ['fast-transport-belt'] = true, + ['express-transport-belt'] = true, + ['construction-robot'] = true, + ['player'] = true, + ['gun-turret'] = true, + ['laser-turret'] = true, + ['flamethrower-turret'] = true, + ['rail'] = true, + ['rail-chain-signal'] = true, + ['rail-signal'] = true, + ['tile-ghost'] = true, + ['entity-ghost'] = true, + ['gate'] = true, + ['electric-pole'] = true, + ['small-electric-pole'] = true, + ['medium-electric-pole'] = true, + ['big-electric-pole'] = true, + ['logistic-robot'] = true, + ['defender'] = true, + ['destroyer'] = true, + ['distractor'] = true + }, + players_warned = {} } +local entities_allowed_to_bomb = global.nuke_control.entities_allowed_to_bomb +local players_warned = global.nuke_control.players_warned local function entity_allowed_to_bomb(entity) - return global.entities_allowed_to_bomb[entity.name] + return entities_allowed_to_bomb[entity.name] end -global.players_warned = {} - local function on_capsule_used(event) local item = event.item local player = Game.get_player_by_index(event.player_index) @@ -143,12 +146,12 @@ local function on_capsule_used(event) end end if count > 8 then - if global.players_warned[event.player_index] then + if players_warned[event.player_index] then if nuke_control.enable_autoban then Server.ban_sync(player.name, format('Damaged %i entities with %s. This action was performed automatically. If you want to contest this ban please visit redmew.com/discord.', count, item.name), '