2018-09-08 18:29:27 +02:00
|
|
|
--[[-- info
|
|
|
|
Provides the ability to collapse caves when digging.
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- dependencies
|
|
|
|
require 'utils.list_utils'
|
|
|
|
|
|
|
|
local Event = require 'utils.event'
|
2018-09-14 22:12:55 +02:00
|
|
|
local Template = require 'map_gen.Diggy.Template'
|
|
|
|
local Mask = require 'map_gen.Diggy.Mask'
|
2018-09-19 18:55:07 +02:00
|
|
|
local StressMap = require 'map_gen.Diggy.StressMap'
|
2018-09-08 18:29:27 +02:00
|
|
|
|
|
|
|
-- this
|
|
|
|
local DiggyCaveCollapse = {}
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
@param surface LuaSurface
|
|
|
|
@param position Position with x and y
|
2018-09-19 18:55:07 +02:00
|
|
|
@param strength positive increases stress, negative decreases stress
|
2018-09-08 18:29:27 +02:00
|
|
|
]]
|
2018-09-19 18:55:07 +02:00
|
|
|
local function update_stress_map(surface, position, strength)
|
2018-09-11 22:15:02 +02:00
|
|
|
Mask.blur(position.x, position.y, strength, function (x, y, fraction)
|
2018-09-19 18:55:07 +02:00
|
|
|
StressMap.add(surface, {x = x, y = y}, fraction)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
StressMap.process_maxed_values_buffer(surface, function (positions)
|
2018-09-12 23:13:54 +02:00
|
|
|
local entities = {}
|
|
|
|
local tiles = {}
|
|
|
|
|
|
|
|
for _, position in pairs(positions) do
|
2018-09-13 20:42:12 +02:00
|
|
|
table.insert(entities, {position = {x = position.x, y = position.y - 1}, name = 'sand-rock-big'})
|
|
|
|
table.insert(entities, {position = {x = position.x + 1, y = position.y}, name = 'sand-rock-big'})
|
|
|
|
table.insert(entities, {position = {x = position.x, y = position.y + 1}, name = 'sand-rock-big'})
|
|
|
|
table.insert(entities, {position = {x = position.x - 1, y = position.y}, name = 'sand-rock-big'})
|
|
|
|
table.insert(tiles, {position = {x = position.x, y = position.y}, name = 'out-of-map'})
|
2018-09-12 23:13:54 +02:00
|
|
|
end
|
|
|
|
|
2018-09-13 20:42:12 +02:00
|
|
|
for _, new_spawn in pairs({entities, tiles}) do
|
|
|
|
for _, tile in pairs(new_spawn) do
|
|
|
|
for _, entity in pairs(surface.find_entities_filtered({position = tile.position})) do
|
2018-09-14 21:42:58 +02:00
|
|
|
pcall(function() entity.die() end)
|
|
|
|
pcall(function() entity.destroy() end)
|
2018-09-13 20:42:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Template.insert(surface, tiles, entities, false)
|
2018-09-11 22:15:02 +02:00
|
|
|
end)
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Registers all event handlers.]
|
|
|
|
|
|
|
|
@param config Table {@see Diggy.Config}.
|
|
|
|
]]
|
|
|
|
function DiggyCaveCollapse.register(config)
|
2018-09-12 20:38:19 +02:00
|
|
|
local support_beam_entities = config.features.DiggyCaveCollapse.support_beam_entities;
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
Event.add(defines.events.on_robot_built_entity, function(event)
|
2018-09-12 20:38:19 +02:00
|
|
|
local strength = support_beam_entities[event.created_entity.name]
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-12 20:38:19 +02:00
|
|
|
if (not strength) then
|
2018-09-08 18:29:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.created_entity.surface, {
|
2018-09-08 18:29:27 +02:00
|
|
|
x = event.created_entity.position.x,
|
|
|
|
y = event.created_entity.position.y,
|
2018-09-11 22:15:02 +02:00
|
|
|
}, -1 * strength)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
Event.add(defines.events.on_built_entity, function(event)
|
2018-09-12 20:38:19 +02:00
|
|
|
local strength = support_beam_entities[event.created_entity.name]
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-13 23:07:38 +02:00
|
|
|
|
2018-09-12 20:38:19 +02:00
|
|
|
if (not strength) then
|
2018-09-08 18:29:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.created_entity.surface, {
|
2018-09-08 18:29:27 +02:00
|
|
|
x = event.created_entity.position.x,
|
|
|
|
y = event.created_entity.position.y,
|
2018-09-11 22:15:02 +02:00
|
|
|
}, -1 * strength)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
2018-09-11 22:15:02 +02:00
|
|
|
Event.add(Template.events.on_placed_entity, function(event)
|
2018-09-12 20:38:19 +02:00
|
|
|
local strength = support_beam_entities[event.entity.name]
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-13 23:07:38 +02:00
|
|
|
|
2018-09-12 20:38:19 +02:00
|
|
|
if (not strength) then
|
2018-09-08 18:29:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.entity.surface, {
|
2018-09-08 18:29:27 +02:00
|
|
|
x = event.entity.position.x,
|
|
|
|
y = event.entity.position.y,
|
2018-09-11 22:15:02 +02:00
|
|
|
}, -1 * strength)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
Event.add(defines.events.on_entity_died, function(event)
|
2018-09-12 20:38:19 +02:00
|
|
|
local strength = support_beam_entities[event.entity.name]
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-12 20:38:19 +02:00
|
|
|
if (not strength) then
|
2018-09-08 18:29:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.entity.surface, {
|
2018-09-11 22:15:02 +02:00
|
|
|
x = event.entity.position.x,
|
|
|
|
y = event.entity.position.y,
|
|
|
|
}, strength)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
Event.add(defines.events.on_player_mined_entity, function(event)
|
2018-09-12 20:38:19 +02:00
|
|
|
local strength = support_beam_entities[event.entity.name]
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-12 20:38:19 +02:00
|
|
|
if (not strength) then
|
2018-09-08 18:29:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.entity.surface, {
|
2018-09-11 22:15:02 +02:00
|
|
|
x = event.entity.position.x,
|
|
|
|
y = event.entity.position.y,
|
|
|
|
}, strength)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
2018-09-13 23:07:38 +02:00
|
|
|
|
|
|
|
Event.add(Template.events.on_void_removed, function(event)
|
|
|
|
local strength = support_beam_entities['out-of-map']
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.surface, {
|
2018-09-13 23:07:38 +02:00
|
|
|
x = event.old_tile.position.x,
|
|
|
|
y = event.old_tile.position.y,
|
|
|
|
}, strength)
|
|
|
|
end)
|
|
|
|
|
|
|
|
Event.add(Template.events.on_void_added, function(event)
|
|
|
|
local strength = support_beam_entities['out-of-map']
|
|
|
|
|
2018-09-19 18:55:07 +02:00
|
|
|
update_stress_map(event.surface, {
|
2018-09-13 23:07:38 +02:00
|
|
|
x = event.old_tile.position.x,
|
|
|
|
y = event.old_tile.position.y,
|
|
|
|
}, -1 * strength)
|
|
|
|
end)
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Initializes the Feature.
|
|
|
|
|
|
|
|
@param config Table {@see Diggy.Config}.
|
|
|
|
]]
|
|
|
|
function DiggyCaveCollapse.initialize(config)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return DiggyCaveCollapse
|