mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-03-05 15:05:57 +02:00
Minor perf update
This commit is contained in:
parent
b165acdfcc
commit
440327b882
@ -43,7 +43,7 @@ local Config = {
|
||||
mask_size = 9,
|
||||
|
||||
--how much the mask will effect tiles in the different rings of the mask
|
||||
mask_relative_ring_weights = {2,3,4},
|
||||
mask_relative_ring_weights = {2, 3, 4},
|
||||
|
||||
-- delay in seconds before the cave collapses
|
||||
collapse_delay = 2.5,
|
||||
|
@ -78,10 +78,9 @@ end
|
||||
@param position Position {x, y}
|
||||
]]
|
||||
function Debug.print_grid_value(value, surface, position)
|
||||
value = max(-1, min(1, value))
|
||||
local r = value
|
||||
local r = max(1, value)
|
||||
local g = 1 - abs(value)
|
||||
local b = value
|
||||
local b = min(1, value)
|
||||
|
||||
if (r > 0) then
|
||||
r = 0
|
||||
|
@ -75,9 +75,11 @@ local function create_collapse_template(positions, surface)
|
||||
local tiles = {}
|
||||
local map = {}
|
||||
for _, position in pairs(positions) do
|
||||
map[position.x] = map[position.x] or {}
|
||||
map[position.x][position.y] = map[position.x][position.y] or true
|
||||
insert(tiles, {position = {x = position.x, y = position.y}, name = 'out-of-map'})
|
||||
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
|
||||
@ -97,13 +99,14 @@ local function create_collapse_template(positions, surface)
|
||||
end
|
||||
end
|
||||
|
||||
local find_entities_filtered = surface.find_entities_filtered
|
||||
|
||||
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
|
||||
for _, entity in pairs(find_entities_filtered({position = tile.position})) do
|
||||
pcall(function()
|
||||
local strength = support_beam_entities[entity.name]
|
||||
local position = entity.position
|
||||
local surface = entity.surface
|
||||
|
||||
entity.die()
|
||||
if strength then
|
||||
@ -165,21 +168,22 @@ local function spawn_cracking_sound_text(surface, position)
|
||||
end
|
||||
|
||||
local function on_collapse_triggered(event)
|
||||
local position = event.position
|
||||
local x = position.x
|
||||
local y = position.y
|
||||
local surface = event.surface
|
||||
local position = event.position
|
||||
local x = position.x
|
||||
local y = position.y
|
||||
|
||||
local x_t = new_tile_map[x]
|
||||
if x_t and x_t[y] then
|
||||
Template.insert(event.surface, {}, {{position = position, name = 'sand-rock-big'}})
|
||||
else
|
||||
spawn_cracking_sound_text(event.surface, position)
|
||||
Task.set_timeout(
|
||||
config.collapse_delay,
|
||||
on_collapse_timeout_finished,
|
||||
{surface = event.surface, position = position}
|
||||
)
|
||||
end
|
||||
local x_t = new_tile_map[x]
|
||||
if x_t and x_t[y] then
|
||||
Template.insert(surface, {}, {{position = position, name = 'sand-rock-big'}})
|
||||
return
|
||||
end
|
||||
spawn_cracking_sound_text(surface, position)
|
||||
Task.set_timeout(
|
||||
config.collapse_delay,
|
||||
on_collapse_timeout_finished,
|
||||
{surface = surface, position = position}
|
||||
)
|
||||
end
|
||||
|
||||
local function on_built_tile(surface, new_tile, tiles)
|
||||
@ -187,21 +191,23 @@ local function on_built_tile(surface, new_tile, tiles)
|
||||
|
||||
for _, tile in pairs(tiles) do
|
||||
if new_tile_strength then
|
||||
stress_map_blur_add(surface, tile.position, -1 * new_tile_strength, 'on_built_tile')
|
||||
stress_map_blur_add(surface, tile.position, -1 * new_tile_strength)
|
||||
end
|
||||
|
||||
local old_tile_strength = support_beam_entities[tile.old_tile.name]
|
||||
if (old_tile_strength) then
|
||||
stress_map_blur_add(surface, tile.position, old_tile_strength, 'on_built_tile')
|
||||
stress_map_blur_add(surface, tile.position, old_tile_strength)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function on_robot_mined_tile(event)
|
||||
local surface
|
||||
for _, tile in pairs(event.tiles) do
|
||||
local strength = support_beam_entities[tile.old_tile.name]
|
||||
if strength then
|
||||
stress_map_blur_add(event.robot.surface, tile.position, strength)
|
||||
surface = surface or event.robot.surface
|
||||
stress_map_blur_add(surface, tile.position, strength)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -218,23 +224,20 @@ local function on_player_mined_tile(event)
|
||||
end
|
||||
|
||||
local function on_mined_entity(event)
|
||||
local strength = support_beam_entities[event.entity.name]
|
||||
local entity = event.entity
|
||||
local strength = support_beam_entities[entity.name]
|
||||
|
||||
if strength then
|
||||
stress_map_blur_add(event.entity.surface, event.entity.position, strength)
|
||||
stress_map_blur_add(entity.surface, entity.position, strength)
|
||||
end
|
||||
end
|
||||
|
||||
local function on_built_entity(event)
|
||||
local strength = support_beam_entities[event.created_entity.name]
|
||||
local entity = event.created_entity
|
||||
local strength = support_beam_entities[entity.name]
|
||||
|
||||
if strength then
|
||||
stress_map_blur_add(
|
||||
event.created_entity.surface,
|
||||
event.created_entity.position,
|
||||
-1 * strength,
|
||||
'on_built_entity'
|
||||
)
|
||||
stress_map_blur_add(entity.surface, entity.position, -1 * strength)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,15 +28,16 @@ local function diggy_hole(entity)
|
||||
|
||||
local tiles = {}
|
||||
local rocks = {}
|
||||
local surface = entity.surface
|
||||
|
||||
local out_of_map_found = Scanner.scan_around_position(entity.surface, entity.position, 'out-of-map');
|
||||
local out_of_map_found = Scanner.scan_around_position(surface, entity.position, 'out-of-map');
|
||||
|
||||
for _, position in pairs(out_of_map_found) do
|
||||
insert(tiles, {name = 'dirt-' .. random(1, 7), position = {x = position.x, y = position.y}})
|
||||
insert(rocks, {name = 'sand-rock-big', position = {x = position.x, y = position.y}})
|
||||
insert(tiles, {name = 'dirt-' .. random(1, 7), position = position})
|
||||
insert(rocks, {name = 'sand-rock-big', position = position})
|
||||
end
|
||||
|
||||
Template.insert(entity.surface, tiles, rocks)
|
||||
Template.insert(surface, tiles, rocks)
|
||||
end
|
||||
|
||||
local artificial_tiles = {
|
||||
|
@ -14,25 +14,26 @@ local Scanner = {}
|
||||
]]
|
||||
function Scanner.scan_around_position(surface, position, tile_search)
|
||||
local tile_found = {}
|
||||
local get_tile = surface.get_tile
|
||||
|
||||
-- north
|
||||
if (tile_search == surface.get_tile(position.x, position.y - 1).name) then
|
||||
insert(tile_found, { x = position.x, y = position.y - 1})
|
||||
if (tile_search == get_tile(position.x, position.y - 1).name) then
|
||||
insert(tile_found, {x = position.x, y = position.y - 1})
|
||||
end
|
||||
|
||||
-- east
|
||||
if (tile_search == surface.get_tile(position.x + 1, position.y).name) then
|
||||
insert(tile_found, { x = position.x + 1, y = position.y})
|
||||
if (tile_search == get_tile(position.x + 1, position.y).name) then
|
||||
insert(tile_found, {x = position.x + 1, y = position.y})
|
||||
end
|
||||
|
||||
-- south
|
||||
if (tile_search == surface.get_tile(position.x, position.y + 1).name) then
|
||||
insert(tile_found, { x = position.x, y = position.y + 1})
|
||||
if (tile_search == get_tile(position.x, position.y + 1).name) then
|
||||
insert(tile_found, {x = position.x, y = position.y + 1})
|
||||
end
|
||||
|
||||
-- west
|
||||
if (tile_search == surface.get_tile(position.x - 1, position.y).name) then
|
||||
insert(tile_found, { x = position.x - 1, y = position.y})
|
||||
if (tile_search == get_tile(position.x - 1, position.y).name) then
|
||||
insert(tile_found, {x = position.x - 1, y = position.y})
|
||||
end
|
||||
|
||||
return tile_found;
|
||||
|
@ -5,6 +5,7 @@ local Debug = require 'map_gen.Diggy.Debug'
|
||||
local insert = table.insert
|
||||
local min = math.min
|
||||
local ceil = math.ceil
|
||||
local raise_event = script.raise_event
|
||||
|
||||
-- this
|
||||
local Template = {}
|
||||
@ -38,6 +39,7 @@ local function insert_next_tiles(data)
|
||||
local void_removed = {}
|
||||
local void_added = {}
|
||||
local surface = data.surface
|
||||
local get_tile = surface.get_tile
|
||||
local tiles = {}
|
||||
|
||||
pcall(
|
||||
@ -46,7 +48,7 @@ local function insert_next_tiles(data)
|
||||
for i = data.tile_iterator, min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do
|
||||
local new_tile = data.tiles[i]
|
||||
insert(tiles, new_tile)
|
||||
local current_tile = surface.get_tile(new_tile.position.x, new_tile.position.y)
|
||||
local current_tile = get_tile(new_tile.position.x, new_tile.position.y)
|
||||
local current_is_void = current_tile.name == 'out-of-map'
|
||||
local new_is_void = new_tile.name == 'out-of-map'
|
||||
|
||||
@ -72,24 +74,25 @@ local function insert_next_tiles(data)
|
||||
surface.set_tiles(tiles)
|
||||
|
||||
for _, event in pairs(void_removed) do
|
||||
script.raise_event(Template.events.on_void_removed, event)
|
||||
raise_event(Template.events.on_void_removed, event)
|
||||
end
|
||||
|
||||
for _, event in pairs(void_added) do
|
||||
script.raise_event(Template.events.on_void_added, event)
|
||||
raise_event(Template.events.on_void_added, event)
|
||||
end
|
||||
end
|
||||
|
||||
local function insert_next_entities(data)
|
||||
local created_entities = {}
|
||||
local surface = data.surface
|
||||
local create_entity = surface.create_entity
|
||||
|
||||
pcall(
|
||||
function()
|
||||
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
||||
for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do
|
||||
local entity = data.entities[i]
|
||||
local created_entity = surface.create_entity(entity)
|
||||
local created_entity = create_entity(entity)
|
||||
if (nil == created_entity) then
|
||||
error('Failed creating entity ' .. entity.name .. ' on surface.')
|
||||
end
|
||||
@ -102,7 +105,7 @@ local function insert_next_entities(data)
|
||||
data.entity_iterator = data.entity_iterator + entities_per_call
|
||||
|
||||
for _, entity in pairs(created_entities) do
|
||||
script.raise_event(Template.events.on_placed_entity, {entity = entity})
|
||||
raise_event(Template.events.on_placed_entity, {entity = entity})
|
||||
end
|
||||
|
||||
return data.entity_iterator <= data.entities_n
|
||||
@ -130,7 +133,6 @@ local insert_token = Token.register(insert_action)
|
||||
@param entities table of entities as required by create_entity
|
||||
]]
|
||||
function Template.insert(surface, tiles, entities)
|
||||
|
||||
tiles = tiles or {}
|
||||
entities = entities or {}
|
||||
|
||||
@ -154,6 +156,7 @@ function Template.insert(surface, tiles, entities)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if continue then
|
||||
Task.queue_task(insert_token, data, total_calls - 4)
|
||||
end
|
||||
@ -170,14 +173,17 @@ end
|
||||
]]
|
||||
function Template.units(surface, units, non_colliding_distance)
|
||||
non_colliding_distance = non_colliding_distance or 1
|
||||
local create_entity = surface.create_entity
|
||||
local find_non_colliding_position = surface.find_non_colliding_position
|
||||
|
||||
for _, entity in pairs(units) do
|
||||
local position = surface.find_non_colliding_position(entity.name, entity.position, non_colliding_distance, 1)
|
||||
local position = find_non_colliding_position(entity.name, entity.position, non_colliding_distance, 1)
|
||||
|
||||
if (nil ~= position) then
|
||||
entity.position = position
|
||||
surface.create_entity(entity)
|
||||
create_entity(entity)
|
||||
else
|
||||
Debug.print("Failed to spawn '" .. entity.name .. "' at '" .. serpent.line(entity.position) .. "'")
|
||||
Debug.printPosition(entity.position, "Failed to spawn '" .. entity.name .. "'")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -191,8 +197,9 @@ end
|
||||
@param resources table of entities as required by create_entity
|
||||
]]
|
||||
function Template.resources(surface, resources)
|
||||
local create_entity = surface.create_entity
|
||||
for _, entity in pairs(resources) do
|
||||
surface.create_entity(entity)
|
||||
create_entity(entity)
|
||||
end
|
||||
end
|
||||
|
||||
@ -207,10 +214,11 @@ end
|
||||
]]
|
||||
function Template.market(surface, position, force, currency_item, market_inventory)
|
||||
local market = surface.create_entity({name = 'market', position = position})
|
||||
local add_market_item = market.add_market_item
|
||||
market.destructible = false
|
||||
|
||||
for _, item in ipairs(market_inventory) do
|
||||
market.add_market_item(item)
|
||||
add_market_item(item)
|
||||
end
|
||||
|
||||
force.add_chart_tag(surface, {
|
||||
@ -219,7 +227,7 @@ function Template.market(surface, position, force, currency_item, market_invento
|
||||
position = position,
|
||||
})
|
||||
|
||||
script.raise_event(Template.events.on_placed_entity, {entity = market})
|
||||
raise_event(Template.events.on_placed_entity, {entity = market})
|
||||
end
|
||||
|
||||
return Template
|
||||
|
Loading…
x
Reference in New Issue
Block a user