mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
optimized collapse: collapses now happen every 80 ticks
This commit is contained in:
parent
ba739a8563
commit
5c6f92a5bc
@ -48,15 +48,19 @@ local stress_threshold_causing_collapse = 0.9
|
|||||||
local deconstruction_alert_message_shown = {}
|
local deconstruction_alert_message_shown = {}
|
||||||
local stress_map_storage = {}
|
local stress_map_storage = {}
|
||||||
local new_tile_map = {}
|
local new_tile_map = {}
|
||||||
|
local collapse_positions_storage = {}
|
||||||
|
|
||||||
|
|
||||||
Global.register({
|
Global.register({
|
||||||
new_tile_map = new_tile_map,
|
new_tile_map = new_tile_map,
|
||||||
stress_map_storage = stress_map_storage,
|
stress_map_storage = stress_map_storage,
|
||||||
deconstruction_alert_message_shown = deconstruction_alert_message_shown,
|
deconstruction_alert_message_shown = deconstruction_alert_message_shown,
|
||||||
|
collapse_positions_storage = collapse_positions_storage,
|
||||||
}, function(tbl)
|
}, function(tbl)
|
||||||
new_tile_map = tbl.new_tile_map
|
new_tile_map = tbl.new_tile_map
|
||||||
stress_map_storage = tbl.stress_map_storage
|
stress_map_storage = tbl.stress_map_storage
|
||||||
deconstruction_alert_message_shown = tbl.deconstruction_alert_message_shown
|
deconstruction_alert_message_shown = tbl.deconstruction_alert_message_shown
|
||||||
|
collapse_positions_storage = tbl.collapse_positions_storage
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local defaultValue = 0
|
local defaultValue = 0
|
||||||
@ -70,20 +74,12 @@ DiggyCaveCollapse.events = {
|
|||||||
on_collapse_triggered = script.generate_event_name()
|
on_collapse_triggered = script.generate_event_name()
|
||||||
}
|
}
|
||||||
|
|
||||||
local function create_collapse_template(positions, surface)
|
local function create_collapse_template(map, surface)
|
||||||
local entities = {}
|
local entities = {}
|
||||||
local tiles = {}
|
local tiles = {}
|
||||||
local map = {}
|
|
||||||
for _, position in pairs(positions) do
|
|
||||||
local x = position.x
|
|
||||||
local y = position.y
|
|
||||||
map[x] = map[x] or {}
|
|
||||||
map[x][y] = map[x][y] or true
|
|
||||||
insert(tiles, {position = {x = x, y = y}, name = 'out-of-map'})
|
|
||||||
end
|
|
||||||
|
|
||||||
for x,y_tbl in pairs(map) do
|
for x,y_tbl in pairs(map) do
|
||||||
for y,_ in pairs(y_tbl) do
|
for y,_ in pairs(y_tbl) do
|
||||||
|
insert(tiles, {position = {x = x, y = y}, name = 'out-of-map'})
|
||||||
if not map[x] or not map[x][y - 1] then
|
if not map[x] or not map[x][y - 1] then
|
||||||
insert(entities, {position = {x = x, y = y - 1}, name = 'sand-rock-big'})
|
insert(entities, {position = {x = x, y = y - 1}, name = 'sand-rock-big'})
|
||||||
end
|
end
|
||||||
@ -106,10 +102,9 @@ local function create_collapse_template(positions, surface)
|
|||||||
for _, entity in pairs(find_entities_filtered({position = tile.position})) do
|
for _, entity in pairs(find_entities_filtered({position = tile.position})) do
|
||||||
pcall(function()
|
pcall(function()
|
||||||
local strength = support_beam_entities[entity.name]
|
local strength = support_beam_entities[entity.name]
|
||||||
local position = entity.position
|
|
||||||
|
|
||||||
entity.die()
|
entity.die()
|
||||||
if strength then
|
if strength then
|
||||||
|
local position = entity.position
|
||||||
stress_map_blur_add(surface, position, strength)
|
stress_map_blur_add(surface, position, strength)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@ -120,32 +115,6 @@ local function create_collapse_template(positions, surface)
|
|||||||
return tiles, entities
|
return tiles, entities
|
||||||
end
|
end
|
||||||
|
|
||||||
local function collapse(args)
|
|
||||||
local position = args.position
|
|
||||||
local surface = args.surface
|
|
||||||
local positions = {}
|
|
||||||
local tiles = {}
|
|
||||||
local entities
|
|
||||||
mask_disc_blur(
|
|
||||||
position.x, position.y,
|
|
||||||
config.collapse_threshold_total_strength,
|
|
||||||
function(x, y, value)
|
|
||||||
stress_map_check_stress_in_threshold(
|
|
||||||
surface,
|
|
||||||
{x = x, y = y},
|
|
||||||
value,
|
|
||||||
function(_, position)
|
|
||||||
insert(positions, position)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
tiles, entities = create_collapse_template(positions, surface)
|
|
||||||
Template.insert(surface, tiles, entities)
|
|
||||||
end
|
|
||||||
|
|
||||||
local on_collapse_timeout_finished = Token.register(collapse)
|
|
||||||
|
|
||||||
local function spawn_cracking_sound_text(surface, position)
|
local function spawn_cracking_sound_text(surface, position)
|
||||||
local text = config.cracking_sounds[random(1, #config.cracking_sounds)]
|
local text = config.cracking_sounds[random(1, #config.cracking_sounds)]
|
||||||
|
|
||||||
@ -167,6 +136,45 @@ local function spawn_cracking_sound_text(surface, position)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function schedule_collapse(args)
|
||||||
|
local surface = args.surface
|
||||||
|
local surface_index = surface.index
|
||||||
|
local collapse_positions_map = collapse_positions_storage[surface_index]
|
||||||
|
local position = args.position
|
||||||
|
|
||||||
|
mask_disc_blur(
|
||||||
|
position.x, position.y,
|
||||||
|
config.collapse_threshold_total_strength,
|
||||||
|
function(x, y, value) --optimize this!
|
||||||
|
stress_map_check_stress_in_threshold(
|
||||||
|
surface,
|
||||||
|
{x = x, y = y},
|
||||||
|
value,
|
||||||
|
function(_, position)
|
||||||
|
local x = position.x
|
||||||
|
local y = position.y
|
||||||
|
if collapse_positions_map[x] then
|
||||||
|
collapse_positions_map[x][y] = true
|
||||||
|
else
|
||||||
|
collapse_positions_map[x] = {[y] = true}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local on_collapse_timeout_finished = Token.register(schedule_collapse)
|
||||||
|
|
||||||
|
local function execute_collapses()
|
||||||
|
for surface_index, map in pairs(collapse_positions_storage) do
|
||||||
|
local surface = game.surfaces[surface_index]
|
||||||
|
local tiles, entities = create_collapse_template(map, surface)
|
||||||
|
collapse_positions_storage[surface_index] = {}
|
||||||
|
Template.insert(surface, tiles, entities)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function on_collapse_triggered(event)
|
local function on_collapse_triggered(event)
|
||||||
local surface = event.surface
|
local surface = event.surface
|
||||||
local position = event.position
|
local position = event.position
|
||||||
@ -316,6 +324,8 @@ function DiggyCaveCollapse.register(cfg)
|
|||||||
Event.add(Template.events.on_void_added, on_void_added)
|
Event.add(Template.events.on_void_added, on_void_added)
|
||||||
Event.add(defines.events.on_surface_created, on_surface_created)
|
Event.add(defines.events.on_surface_created, on_surface_created)
|
||||||
|
|
||||||
|
Event.on_nth_tick(80, execute_collapses)
|
||||||
|
|
||||||
Event.add(defines.events.on_marked_for_deconstruction, function (event)
|
Event.add(defines.events.on_marked_for_deconstruction, function (event)
|
||||||
if (nil ~= support_beam_entities[event.entity.name]) then
|
if (nil ~= support_beam_entities[event.entity.name]) then
|
||||||
event.entity.cancel_deconstruction(game.players[event.player_index].force)
|
event.entity.cancel_deconstruction(game.players[event.player_index].force)
|
||||||
@ -362,7 +372,7 @@ end
|
|||||||
|
|
||||||
@return number sum of old fraction + new fraction
|
@return number sum of old fraction + new fraction
|
||||||
]]
|
]]
|
||||||
function add_fraction(stress_map, x, y, fraction)
|
local function add_fraction(stress_map, x, y, fraction, no_collapse)
|
||||||
local quadrant = 1
|
local quadrant = 1
|
||||||
if x < 0 then
|
if x < 0 then
|
||||||
quadrant = quadrant + 1
|
quadrant = quadrant + 1
|
||||||
@ -397,11 +407,13 @@ function add_fraction(stress_map, x, y, fraction)
|
|||||||
if quadrant % 2 == 0 then
|
if quadrant % 2 == 0 then
|
||||||
x = -x
|
x = -x
|
||||||
end
|
end
|
||||||
|
if not no_collapse then
|
||||||
script.raise_event(
|
script.raise_event(
|
||||||
DiggyCaveCollapse.events.on_collapse_triggered,
|
DiggyCaveCollapse.events.on_collapse_triggered,
|
||||||
{surface = game.surfaces[stress_map.surface_index], position = {x = x, y = y}}
|
{surface = game.surfaces[stress_map.surface_index], position = {x = x, y = y}}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
if (enable_stress_grid) then
|
if (enable_stress_grid) then
|
||||||
local surface = game.surfaces[stress_map.surface_index]
|
local surface = game.surfaces[stress_map.surface_index]
|
||||||
if quadrant > 2 then
|
if quadrant > 2 then
|
||||||
@ -415,7 +427,7 @@ function add_fraction(stress_map, x, y, fraction)
|
|||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
|
|
||||||
function add_fraction_by_quadrant(stress_map, x, y, fraction, quadrant)
|
local function add_fraction_by_quadrant(stress_map, x, y, fraction, quadrant)
|
||||||
local x_t = quadrant[x]
|
local x_t = quadrant[x]
|
||||||
if not x_t then
|
if not x_t then
|
||||||
x_t = {}
|
x_t = {}
|
||||||
@ -462,6 +474,8 @@ end
|
|||||||
on_surface_created = function(event)
|
on_surface_created = function(event)
|
||||||
stress_map_storage[event.surface_index] = {}
|
stress_map_storage[event.surface_index] = {}
|
||||||
|
|
||||||
|
collapse_positions_storage[event.surface_index] = {}
|
||||||
|
|
||||||
local map = stress_map_storage[event.surface_index]
|
local map = stress_map_storage[event.surface_index]
|
||||||
|
|
||||||
map['surface_index'] = event.surface_index
|
map['surface_index'] = event.surface_index
|
||||||
@ -480,7 +494,7 @@ end
|
|||||||
]]
|
]]
|
||||||
stress_map_check_stress_in_threshold = function(surface, position, threshold, callback)
|
stress_map_check_stress_in_threshold = function(surface, position, threshold, callback)
|
||||||
local stress_map = stress_map_storage[surface.index]
|
local stress_map = stress_map_storage[surface.index]
|
||||||
local value = add_fraction(stress_map, position.x, position.y, 0)
|
local value = add_fraction(stress_map, position.x, position.y, 0, true)
|
||||||
|
|
||||||
if (value >= stress_threshold_causing_collapse - threshold) then
|
if (value >= stress_threshold_causing_collapse - threshold) then
|
||||||
callback(surface, position)
|
callback(surface, position)
|
||||||
@ -488,6 +502,8 @@ stress_map_check_stress_in_threshold = function(surface, position, threshold, ca
|
|||||||
end
|
end
|
||||||
|
|
||||||
stress_map_blur_add = function(surface, position, factor)
|
stress_map_blur_add = function(surface, position, factor)
|
||||||
|
|
||||||
|
|
||||||
local x_start = floor(position.x)
|
local x_start = floor(position.x)
|
||||||
local y_start = floor(position.y)
|
local y_start = floor(position.y)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user