1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/features/reactor_meltdown.lua

209 lines
6.1 KiB
Lua
Raw Normal View History

2018-11-11 09:20:07 +02:00
--Reactors melt down if:
--temperature is at 1000°C and health is 0 or reactor is picked up
--
--a reactors loses 2 damage per second at 1000°C
local Event = require 'utils.event'
local Game = require 'utils.game'
local Command = require 'utils.command'
2019-02-03 09:02:54 +02:00
local Global = require 'utils.global'
local Ranks = require 'resources.ranks'
2019-02-03 09:02:54 +02:00
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
)
2018-11-11 09:20:07 +02:00
local wasteland_duration_seconds = 300
local function spawn_wasteland(surface, position)
local positions = {
{0, 0},
{0, 12},
{0, -12},
{12, 0},
{-12, 0},
{-8.5, 8.5},
{-8.5, -8.5},
{8.5, -8.5},
{8.5, 8.5},
{4, 4},
{-4, 4},
{-4, -4},
{4, -4},
{13, 7.5},
{-13, 7.5},
{-13, -7.5},
{13, -7.5},
{7.5, 13},
{-7.5, 13},
{-7.5, -13},
{7.5, -13},
{0, 15},
{-15, 0},
{15, 0},
{0, -15}
}
for _, rel_position in pairs(positions) do
surface.create_entity {
name = 'poison-capsule',
position = position,
target = {position.x + rel_position[1], position.y + rel_position[2]},
speed = 0.4
}
end
end
local function entity_destroyed(event)
2019-02-03 09:02:54 +02:00
if not primitives.reactors_enabled or not event.entity.valid or event.entity.name ~= 'nuclear-reactor' then
2018-11-11 09:20:07 +02:00
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)
2019-02-03 09:02:54 +02:00
wastelands[reactor.position.x .. '/' .. reactor.position.y] = {
2018-11-11 09:20:07 +02:00
position = reactor.position,
surface_id = reactor.surface.index,
creation_time = game.tick
}
end
end
local function alert(reactor)
for _, p in pairs(game.players) do
2019-02-03 09:02:54 +02:00
p.add_custom_alert(reactor, {type = 'item', name = 'nuclear-reactor'}, string.format('Reactor at %s°C', math.floor(reactor.temperature)), true)
2018-11-11 09:20:07 +02:00
end
end
local function check_reactors()
for _ in pairs(game.surfaces) do
2019-02-03 09:02:54 +02:00
for i, reactor in pairs(reactors) do
2018-11-11 09:20:07 +02:00
if reactor.valid then
if reactor.temperature > 800 then
alert(reactor)
end
if reactor.temperature == 1000 then
reactor.force = 'enemy'
reactor.destructible = false
reactor.health = 0
reactor.surface.create_entity {
name = 'atomic-rocket',
position = reactor.position,
target = reactor,
speed = 1
}
spawn_wasteland(reactor.surface, reactor.position)
2019-02-03 09:02:54 +02:00
wastelands[reactor.position.x .. '/' .. reactor.position.y] = {
2018-11-11 09:20:07 +02:00
position = reactor.position,
surface_id = reactor.surface.index,
creation_time = game.tick
}
2019-02-03 09:02:54 +02:00
table.remove(reactors, i)
2018-11-11 09:20:07 +02:00
else
reactor.health = 500 - (reactor.temperature - 800) * 2.5
end
else
2019-02-03 09:02:54 +02:00
table.remove(reactors, i)
2018-11-11 09:20:07 +02:00
end
end
--global.last_reactor_warning = last_reactor_warning
2018-11-11 09:20:07 +02:00
end
end
local function check_wastelands()
2019-02-03 09:02:54 +02:00
for index, wl in pairs(wastelands) do
2018-11-11 09:20:07 +02:00
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
2019-02-03 09:02:54 +02:00
wastelands[index] = nil
local wasteland_reactors =
2018-11-11 09:20:07 +02:00
game.surfaces[wl.surface_id].find_entities_filtered {
position = wl.position,
name = 'nuclear-reactor'
}
2019-02-03 09:02:54 +02:00
if wasteland_reactors[1] then
wasteland_reactors[1].destroy()
2018-11-11 09:20:07 +02:00
end
end
end
end
end
local function on_tick()
2019-02-03 09:02:54 +02:00
if primitives.reactors_enabled then
2018-11-11 09:20:07 +02:00
check_wastelands()
check_reactors()
end
end
local function entity_build(event)
if not event.created_entity.valid then
return
end
2019-02-03 09:02:54 +02:00
if event.created_entity.name == 'nuclear-reactor' and event.created_entity.surface.name ~= 'antigrief' then
table.insert(reactors, event.created_entity)
2018-11-11 09:20:07 +02:00
end
end
local function reactor_toggle()
2019-02-03 09:02:54 +02:00
primitives.reactors_enabled = not primitives.reactors_enabled
if primitives.reactors_enabled then
game.print('Reactor meltdown activated.')
else
game.print('Reactor meltdown deactivated.')
2018-11-11 09:20:07 +02:00
end
end
local function is_meltdown()
2019-02-03 09:02:54 +02:00
if primitives.reactors_enabled then
2018-11-11 09:20:07 +02:00
Game.player_print('Reactor meltdown is enabled.')
else
Game.player_print('Reactor meltdown is disabled.')
end
end
Command.add(
'meltdown',
{
description = 'Toggles if reactors blow up',
required_rank = Ranks.admin,
allowed_by_server = true,
log_command = true
},
reactor_toggle
)
2018-11-11 09:20:07 +02:00
Command.add(
'is-meltdown',
{
description = 'Prints if meltdown is enabled',
allowed_by_server = true
},
is_meltdown
)
2018-11-11 09:20:07 +02:00
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)