1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

Improved scanner performance

This commit is contained in:
Lynn 2018-11-26 22:07:53 +01:00
parent fa4560fdcb
commit 67616ad330
2 changed files with 31 additions and 53 deletions

View File

@ -6,12 +6,10 @@
-- dependencies
local Event = require 'utils.event'
local Global = require 'utils.global'
local Scanner = require 'map_gen.Diggy.Scanner'
local Template = require 'map_gen.Diggy.Template'
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
local Debug = require 'map_gen.Diggy.Debug'
local CreateParticles = require 'features.create_particles'
local insert = table.insert
local random = math.random
local raise_event = script.raise_event
@ -56,8 +54,30 @@ local function diggy_hole(entity)
local rocks = {}
local surface = entity.surface
local position = entity.position
local x = position.x
local y = position.y
local get_tile = surface.get_tile
local out_of_map_found = {}
local count = 0
local out_of_map_found = Scanner.scan_around_position(surface, position, 'out-of-map');
if (get_tile(x, y - 1).name == 'out-of-map') then
count = count + 1
out_of_map_found[count] = {x = x, y = y - 1}
end
if (get_tile(x + 1, y).name == 'out-of-map') then
count = count + 1
out_of_map_found[count] = {x = x + 1, y = y}
end
if (get_tile(x, y + 1).name == 'out-of-map') then
count = count + 1
out_of_map_found[count] = {x = x, y = y + 1}
end
if (get_tile(x - 1, y).name == 'out-of-map') then
out_of_map_found[count + 1] = {x = x - 1, y = y}
end
for i = #out_of_map_found, 1, -1 do
local void_position = out_of_map_found[i]
@ -85,10 +105,11 @@ local artificial_tiles = {
local function on_mined_tile(surface, tiles)
local new_tiles = {}
local count = 0
for _, tile in pairs(tiles) do
if (artificial_tiles[tile.old_tile.name]) then
insert(new_tiles, { name = 'dirt-' .. random(1, 7), position = tile.position})
count = count + 1
new_tiles[count] = {name = 'dirt-' .. random(1, 7), position = tile.position}
end
end
@ -219,17 +240,16 @@ function DiggyHole.register(config)
local height = tonumber(params[4])
local surface_index = params[5]
local tiles = {}
local entities = {}
local count = 0
for x = 0, width do
for y = 0, height do
insert(tiles, {name = 'dirt-' .. random(1, 7), position = {x = x + left_top_x, y = y + left_top_y}})
count = count + 1
tiles[count] = {name = 'dirt-' .. random(1, 7), position = {x = x + left_top_x, y = y + left_top_y}}
end
end
Template.insert(game.surfaces[surface_index], tiles, entities)
end
)
Template.insert(game.surfaces[surface_index], tiles, {})
end)
end
end

View File

@ -1,42 +0,0 @@
-- dependencies
local insert = table.insert
-- this
local Scanner = {}
--[[--
returns a list with all direct positions that contain tile_search.
@param surface LuaSurface
@param position Position
@param tile_search string name of the tile to search for
@return table with 0~4 directions of which have the tile searched for adjacent
]]
function Scanner.scan_around_position(surface, position, tile_search)
local tile_found = {}
local get_tile = surface.get_tile
-- north
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 == get_tile(position.x + 1, position.y).name) then
insert(tile_found, {x = position.x + 1, y = position.y})
end
-- south
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 == get_tile(position.x - 1, position.y).name) then
insert(tile_found, {x = position.x - 1, y = position.y})
end
return tile_found;
end
return Scanner