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-20 17:34:17 +02:00
|
|
|
local Debug = require'map_gen.Diggy.Debug'
|
2018-09-20 20:04:24 +02:00
|
|
|
local Task = require 'utils.Task'
|
|
|
|
local Token = require 'utils.global_token'
|
2018-09-08 18:29:27 +02:00
|
|
|
|
|
|
|
-- this
|
|
|
|
local DiggyCaveCollapse = {}
|
|
|
|
|
2018-09-20 20:04:24 +02:00
|
|
|
local config = {}
|
2018-09-20 17:34:17 +02:00
|
|
|
|
|
|
|
DiggyCaveCollapse.events = {
|
|
|
|
--[[--
|
|
|
|
When stress at certain position is above the collapse threshold
|
|
|
|
- position LuaPosition
|
|
|
|
- surface LuaSurface
|
|
|
|
]]
|
|
|
|
on_collapse_triggered = script.generate_event_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
local function create_collapse_template(positions, surface)
|
|
|
|
local entities = {}
|
|
|
|
local tiles = {}
|
|
|
|
for _, position in pairs(positions) do
|
|
|
|
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'})
|
|
|
|
end
|
|
|
|
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
|
|
|
|
pcall(function() entity.die() end)
|
|
|
|
pcall(function() entity.destroy() end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for key,entity in pairs(entities) do
|
|
|
|
if not entity.valid then
|
|
|
|
entities[key] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tiles, entities
|
|
|
|
end
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
--[[--
|
|
|
|
@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-20 17:34:17 +02:00
|
|
|
local max_value
|
2018-09-11 22:15:02 +02:00
|
|
|
Mask.blur(position.x, position.y, strength, function (x, y, fraction)
|
2018-09-20 20:04:24 +02:00
|
|
|
max_value = max_value or StressMap.add(surface, {x = x, y = y}, fraction)
|
2018-09-08 18:29:27 +02:00
|
|
|
end)
|
|
|
|
|
2018-09-20 17:34:17 +02:00
|
|
|
if max_value then
|
|
|
|
script.raise_event(DiggyCaveCollapse.events.on_collapse_triggered, {surface = surface, position = position})
|
|
|
|
end
|
|
|
|
end
|
2018-09-12 23:13:54 +02:00
|
|
|
|
2018-09-20 17:34:17 +02:00
|
|
|
local function collapse(surface, position)
|
|
|
|
local positions = {}
|
|
|
|
|
2018-09-20 20:04:24 +02:00
|
|
|
Mask.blur(position.x, position.y, config.collapse_threshold_total_strength, function(x,y, value)
|
2018-09-20 17:34:17 +02:00
|
|
|
StressMap.check_stress_in_threshold(surface, {x=x,y=y}, value, function(_, position)
|
|
|
|
table.insert(positions, position)
|
|
|
|
end)
|
2018-09-13 20:42:12 +02:00
|
|
|
|
2018-09-11 22:15:02 +02:00
|
|
|
end)
|
2018-09-20 17:34:17 +02:00
|
|
|
local tiles, entities = create_collapse_template(positions, surface)
|
|
|
|
Template.insert(surface, tiles, entities)
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
|
2018-09-20 20:04:24 +02:00
|
|
|
local on_collapse_timeout_finished = Token.register(function(params)
|
|
|
|
collapse(params.surface, params.position)
|
|
|
|
end)
|
|
|
|
|
2018-09-20 17:34:17 +02:00
|
|
|
|
2018-09-21 11:44:40 +02:00
|
|
|
function spawn_cracking_sound_text(surface, position)
|
|
|
|
local text = config.cracking_sounds[math.random(1, #config.cracking_sounds)]
|
|
|
|
|
|
|
|
local color = {
|
|
|
|
r= 1,
|
|
|
|
g= math.random(1, 100) / 100,
|
|
|
|
b=0
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 1, #text do
|
|
|
|
local x_offset = (i - #text / 2 - 1) / 3
|
|
|
|
local char = text:sub(i,i)
|
|
|
|
surface.create_entity{
|
|
|
|
name = 'flying-text',
|
|
|
|
color = color,
|
|
|
|
text = char ,
|
|
|
|
position = {x = position.x + x_offset, y = position.y - ((i + 1) % 2) / 4}
|
|
|
|
}.active = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
--[[--
|
|
|
|
Registers all event handlers.]
|
|
|
|
|
2018-09-21 11:44:40 +02:00
|
|
|
@param global_config Table {@see Diggy.Config}.
|
2018-09-08 18:29:27 +02:00
|
|
|
]]
|
2018-09-21 11:44:40 +02:00
|
|
|
function DiggyCaveCollapse.register(global_config)
|
|
|
|
config = global_config.features.DiggyCaveCollapse
|
|
|
|
local support_beam_entities = config.support_beam_entities;
|
2018-09-12 20:38:19 +02:00
|
|
|
|
2018-09-20 17:34:17 +02:00
|
|
|
Event.add(DiggyCaveCollapse.events.on_collapse_triggered, function(event)
|
2018-09-21 11:44:40 +02:00
|
|
|
spawn_cracking_sound_text(event.surface, event.position)
|
|
|
|
|
2018-09-25 17:58:35 +02:00
|
|
|
Task.set_timeout(
|
2018-09-25 18:11:37 +02:00
|
|
|
math.random(config.collapse_delay_min * 10, config.collapse_delay_max * 10) / 10,
|
2018-09-25 17:58:35 +02:00
|
|
|
on_collapse_timeout_finished,
|
|
|
|
{surface = event.surface, position = event.position}
|
|
|
|
)
|
2018-09-20 17:34:17 +02:00
|
|
|
end)
|
|
|
|
|
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
|