diff --git a/custom_commands.lua b/custom_commands.lua index b01222e3..05abfa40 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -448,17 +448,6 @@ local function pool() end end -local function reactor_toggle() - if not game.player or game.player.admin then - global.reactors_enabled = not global.reactors_enabled - if global.reactors_enabled then - game.print("Reactor meltdown activated.") - else - game.print("Reactor meltdown deactivated.") - end - end -end - local old_add_command = commands.add_command commands.add_command = function(name, desc, func) old_add_command(name, desc, function(cmd) @@ -491,4 +480,3 @@ commands.add_command("zoom", " Sets your zoom.", zoom) commands.add_command("all-tech", "researches all technologies", function() if game.player and game.player.admin then game.player.force.research_all_technologies() end end) commands.add_command("hax", "Toggles your hax", function() if game.player and game.player.admin then game.player.cheat_mode = not game.player.cheat_mode end end) commands.add_command("pool", "Spawns a pool", pool) -commands.add_command("meltdown", "Toggles if reactors blow up", reactor_toggle) diff --git a/reactor_meltdown.lua b/reactor_meltdown.lua index 162cd785..a3f8feff 100644 --- a/reactor_meltdown.lua +++ b/reactor_meltdown.lua @@ -5,22 +5,25 @@ local Event = require "utils.event" +global.reactors_enabled = false global.wastelands = {} global.reactors = {} local wasteland_duration_seconds = 300 local function entity_destroyed(event) - if event.entity.name == "nuclear-reactor" then - local reactor = event.entity - if event.entity.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] = {position = reactor.position, surface_id = reactor.surface.index, creation_time = game.tick} - end + if not global.reactors_enabled or not event.entity.valid or not event.entity.name == "nuclear-reactor" then + return + end + + local reactor = event.entity + 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] = {position = reactor.position, surface_id = reactor.surface.index, creation_time = game.tick} end end -function spawn_wasteland(surface, position) +local function spawn_wasteland(surface, position) local positions = { {0, 0}, {0, 12}, @@ -102,7 +105,6 @@ local function check_wastelands() end end -global.reactors_enabled = true local function on_tick() if global.reactors_enabled then check_wastelands() @@ -119,6 +121,19 @@ local function entity_build(event) end end +local function reactor_toggle() + if not game.player or game.player.admin then + global.reactors_enabled = not global.reactors_enabled + if global.reactors_enabled then + game.print("Reactor meltdown activated.") + else + game.print("Reactor meltdown deactivated.") + end + end +end + +commands.add_command("meltdown", "Toggles if reactors blow up", reactor_toggle) + 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)