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
@ -78,10 +78,9 @@ end
|
|||||||
@param position Position {x, y}
|
@param position Position {x, y}
|
||||||
]]
|
]]
|
||||||
function Debug.print_grid_value(value, surface, position)
|
function Debug.print_grid_value(value, surface, position)
|
||||||
value = max(-1, min(1, value))
|
local r = max(1, value)
|
||||||
local r = value
|
|
||||||
local g = 1 - abs(value)
|
local g = 1 - abs(value)
|
||||||
local b = value
|
local b = min(1, value)
|
||||||
|
|
||||||
if (r > 0) then
|
if (r > 0) then
|
||||||
r = 0
|
r = 0
|
||||||
|
@ -75,9 +75,11 @@ local function create_collapse_template(positions, surface)
|
|||||||
local tiles = {}
|
local tiles = {}
|
||||||
local map = {}
|
local map = {}
|
||||||
for _, position in pairs(positions) do
|
for _, position in pairs(positions) do
|
||||||
map[position.x] = map[position.x] or {}
|
local x = position.x
|
||||||
map[position.x][position.y] = map[position.x][position.y] or true
|
local y = position.y
|
||||||
insert(tiles, {position = {x = position.x, y = position.y}, name = 'out-of-map'})
|
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
|
end
|
||||||
|
|
||||||
for x, y_tbl in pairs(map) do
|
for x, y_tbl in pairs(map) do
|
||||||
@ -97,13 +99,14 @@ local function create_collapse_template(positions, surface)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local find_entities_filtered = surface.find_entities_filtered
|
||||||
|
|
||||||
for _, new_spawn in pairs({entities, tiles}) do
|
for _, new_spawn in pairs({entities, tiles}) do
|
||||||
for _, tile in pairs(new_spawn) 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()
|
pcall(function()
|
||||||
local strength = support_beam_entities[entity.name]
|
local strength = support_beam_entities[entity.name]
|
||||||
local position = entity.position
|
local position = entity.position
|
||||||
local surface = entity.surface
|
|
||||||
|
|
||||||
entity.die()
|
entity.die()
|
||||||
if strength then
|
if strength then
|
||||||
@ -165,43 +168,46 @@ local function spawn_cracking_sound_text(surface, position)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function on_collapse_triggered(event)
|
local function on_collapse_triggered(event)
|
||||||
|
local surface = event.surface
|
||||||
local position = event.position
|
local position = event.position
|
||||||
local x = position.x
|
local x = position.x
|
||||||
local y = position.y
|
local y = position.y
|
||||||
|
|
||||||
local x_t = new_tile_map[x]
|
local x_t = new_tile_map[x]
|
||||||
if x_t and x_t[y] then
|
if x_t and x_t[y] then
|
||||||
Template.insert(event.surface, {}, {{position = position, name = 'sand-rock-big'}})
|
Template.insert(surface, {}, {{position = position, name = 'sand-rock-big'}})
|
||||||
else
|
return
|
||||||
spawn_cracking_sound_text(event.surface, position)
|
end
|
||||||
|
spawn_cracking_sound_text(surface, position)
|
||||||
Task.set_timeout(
|
Task.set_timeout(
|
||||||
config.collapse_delay,
|
config.collapse_delay,
|
||||||
on_collapse_timeout_finished,
|
on_collapse_timeout_finished,
|
||||||
{surface = event.surface, position = position}
|
{surface = surface, position = position}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
local function on_built_tile(surface, new_tile, tiles)
|
local function on_built_tile(surface, new_tile, tiles)
|
||||||
local new_tile_strength = support_beam_entities[new_tile.name]
|
local new_tile_strength = support_beam_entities[new_tile.name]
|
||||||
|
|
||||||
for _, tile in pairs(tiles) do
|
for _, tile in pairs(tiles) do
|
||||||
if new_tile_strength then
|
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
|
end
|
||||||
|
|
||||||
local old_tile_strength = support_beam_entities[tile.old_tile.name]
|
local old_tile_strength = support_beam_entities[tile.old_tile.name]
|
||||||
if (old_tile_strength) then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_robot_mined_tile(event)
|
local function on_robot_mined_tile(event)
|
||||||
|
local surface
|
||||||
for _, tile in pairs(event.tiles) do
|
for _, tile in pairs(event.tiles) do
|
||||||
local strength = support_beam_entities[tile.old_tile.name]
|
local strength = support_beam_entities[tile.old_tile.name]
|
||||||
if strength then
|
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
|
end
|
||||||
end
|
end
|
||||||
@ -218,23 +224,20 @@ local function on_player_mined_tile(event)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function on_mined_entity(event)
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_built_entity(event)
|
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
|
if strength then
|
||||||
stress_map_blur_add(
|
stress_map_blur_add(entity.surface, entity.position, -1 * strength)
|
||||||
event.created_entity.surface,
|
|
||||||
event.created_entity.position,
|
|
||||||
-1 * strength,
|
|
||||||
'on_built_entity'
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,15 +28,16 @@ local function diggy_hole(entity)
|
|||||||
|
|
||||||
local tiles = {}
|
local tiles = {}
|
||||||
local rocks = {}
|
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
|
for _, position in pairs(out_of_map_found) do
|
||||||
insert(tiles, {name = 'dirt-' .. random(1, 7), position = {x = position.x, y = position.y}})
|
insert(tiles, {name = 'dirt-' .. random(1, 7), position = position})
|
||||||
insert(rocks, {name = 'sand-rock-big', position = {x = position.x, y = position.y}})
|
insert(rocks, {name = 'sand-rock-big', position = position})
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.insert(entity.surface, tiles, rocks)
|
Template.insert(surface, tiles, rocks)
|
||||||
end
|
end
|
||||||
|
|
||||||
local artificial_tiles = {
|
local artificial_tiles = {
|
||||||
|
@ -14,24 +14,25 @@ local Scanner = {}
|
|||||||
]]
|
]]
|
||||||
function Scanner.scan_around_position(surface, position, tile_search)
|
function Scanner.scan_around_position(surface, position, tile_search)
|
||||||
local tile_found = {}
|
local tile_found = {}
|
||||||
|
local get_tile = surface.get_tile
|
||||||
|
|
||||||
-- north
|
-- north
|
||||||
if (tile_search == surface.get_tile(position.x, position.y - 1).name) then
|
if (tile_search == get_tile(position.x, position.y - 1).name) then
|
||||||
insert(tile_found, {x = position.x, y = position.y - 1})
|
insert(tile_found, {x = position.x, y = position.y - 1})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- east
|
-- east
|
||||||
if (tile_search == surface.get_tile(position.x + 1, position.y).name) then
|
if (tile_search == get_tile(position.x + 1, position.y).name) then
|
||||||
insert(tile_found, {x = position.x + 1, y = position.y})
|
insert(tile_found, {x = position.x + 1, y = position.y})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- south
|
-- south
|
||||||
if (tile_search == surface.get_tile(position.x, position.y + 1).name) then
|
if (tile_search == get_tile(position.x, position.y + 1).name) then
|
||||||
insert(tile_found, {x = position.x, y = position.y + 1})
|
insert(tile_found, {x = position.x, y = position.y + 1})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- west
|
-- west
|
||||||
if (tile_search == surface.get_tile(position.x - 1, position.y).name) then
|
if (tile_search == get_tile(position.x - 1, position.y).name) then
|
||||||
insert(tile_found, {x = position.x - 1, y = position.y})
|
insert(tile_found, {x = position.x - 1, y = position.y})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ local Debug = require 'map_gen.Diggy.Debug'
|
|||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
local min = math.min
|
local min = math.min
|
||||||
local ceil = math.ceil
|
local ceil = math.ceil
|
||||||
|
local raise_event = script.raise_event
|
||||||
|
|
||||||
-- this
|
-- this
|
||||||
local Template = {}
|
local Template = {}
|
||||||
@ -38,6 +39,7 @@ local function insert_next_tiles(data)
|
|||||||
local void_removed = {}
|
local void_removed = {}
|
||||||
local void_added = {}
|
local void_added = {}
|
||||||
local surface = data.surface
|
local surface = data.surface
|
||||||
|
local get_tile = surface.get_tile
|
||||||
local tiles = {}
|
local tiles = {}
|
||||||
|
|
||||||
pcall(
|
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
|
for i = data.tile_iterator, min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do
|
||||||
local new_tile = data.tiles[i]
|
local new_tile = data.tiles[i]
|
||||||
insert(tiles, new_tile)
|
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 current_is_void = current_tile.name == 'out-of-map'
|
||||||
local new_is_void = new_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)
|
surface.set_tiles(tiles)
|
||||||
|
|
||||||
for _, event in pairs(void_removed) do
|
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
|
end
|
||||||
|
|
||||||
for _, event in pairs(void_added) do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local function insert_next_entities(data)
|
local function insert_next_entities(data)
|
||||||
local created_entities = {}
|
local created_entities = {}
|
||||||
local surface = data.surface
|
local surface = data.surface
|
||||||
|
local create_entity = surface.create_entity
|
||||||
|
|
||||||
pcall(
|
pcall(
|
||||||
function()
|
function()
|
||||||
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
--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
|
for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do
|
||||||
local entity = data.entities[i]
|
local entity = data.entities[i]
|
||||||
local created_entity = surface.create_entity(entity)
|
local created_entity = create_entity(entity)
|
||||||
if (nil == created_entity) then
|
if (nil == created_entity) then
|
||||||
error('Failed creating entity ' .. entity.name .. ' on surface.')
|
error('Failed creating entity ' .. entity.name .. ' on surface.')
|
||||||
end
|
end
|
||||||
@ -102,7 +105,7 @@ local function insert_next_entities(data)
|
|||||||
data.entity_iterator = data.entity_iterator + entities_per_call
|
data.entity_iterator = data.entity_iterator + entities_per_call
|
||||||
|
|
||||||
for _, entity in pairs(created_entities) do
|
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
|
end
|
||||||
|
|
||||||
return data.entity_iterator <= data.entities_n
|
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
|
@param entities table of entities as required by create_entity
|
||||||
]]
|
]]
|
||||||
function Template.insert(surface, tiles, entities)
|
function Template.insert(surface, tiles, entities)
|
||||||
|
|
||||||
tiles = tiles or {}
|
tiles = tiles or {}
|
||||||
entities = entities or {}
|
entities = entities or {}
|
||||||
|
|
||||||
@ -154,6 +156,7 @@ function Template.insert(surface, tiles, entities)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if continue then
|
if continue then
|
||||||
Task.queue_task(insert_token, data, total_calls - 4)
|
Task.queue_task(insert_token, data, total_calls - 4)
|
||||||
end
|
end
|
||||||
@ -170,14 +173,17 @@ end
|
|||||||
]]
|
]]
|
||||||
function Template.units(surface, units, non_colliding_distance)
|
function Template.units(surface, units, non_colliding_distance)
|
||||||
non_colliding_distance = non_colliding_distance or 1
|
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
|
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
|
if (nil ~= position) then
|
||||||
entity.position = position
|
entity.position = position
|
||||||
surface.create_entity(entity)
|
create_entity(entity)
|
||||||
else
|
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
|
end
|
||||||
end
|
end
|
||||||
@ -191,8 +197,9 @@ end
|
|||||||
@param resources table of entities as required by create_entity
|
@param resources table of entities as required by create_entity
|
||||||
]]
|
]]
|
||||||
function Template.resources(surface, resources)
|
function Template.resources(surface, resources)
|
||||||
|
local create_entity = surface.create_entity
|
||||||
for _, entity in pairs(resources) do
|
for _, entity in pairs(resources) do
|
||||||
surface.create_entity(entity)
|
create_entity(entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -207,10 +214,11 @@ end
|
|||||||
]]
|
]]
|
||||||
function Template.market(surface, position, force, currency_item, market_inventory)
|
function Template.market(surface, position, force, currency_item, market_inventory)
|
||||||
local market = surface.create_entity({name = 'market', position = position})
|
local market = surface.create_entity({name = 'market', position = position})
|
||||||
|
local add_market_item = market.add_market_item
|
||||||
market.destructible = false
|
market.destructible = false
|
||||||
|
|
||||||
for _, item in ipairs(market_inventory) do
|
for _, item in ipairs(market_inventory) do
|
||||||
market.add_market_item(item)
|
add_market_item(item)
|
||||||
end
|
end
|
||||||
|
|
||||||
force.add_chart_tag(surface, {
|
force.add_chart_tag(surface, {
|
||||||
@ -219,7 +227,7 @@ function Template.market(surface, position, force, currency_item, market_invento
|
|||||||
position = position,
|
position = position,
|
||||||
})
|
})
|
||||||
|
|
||||||
script.raise_event(Template.events.on_placed_entity, {entity = market})
|
raise_event(Template.events.on_placed_entity, {entity = market})
|
||||||
end
|
end
|
||||||
|
|
||||||
return Template
|
return Template
|
||||||
|
Loading…
x
Reference in New Issue
Block a user