From 67616ad330c5a90b9c0372892ea2eb85582eea54 Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 26 Nov 2018 22:07:53 +0100 Subject: [PATCH] Improved scanner performance --- map_gen/Diggy/Feature/DiggyHole.lua | 42 +++++++++++++++++++++-------- map_gen/Diggy/Scanner.lua | 42 ----------------------------- 2 files changed, 31 insertions(+), 53 deletions(-) delete mode 100644 map_gen/Diggy/Scanner.lua diff --git a/map_gen/Diggy/Feature/DiggyHole.lua b/map_gen/Diggy/Feature/DiggyHole.lua index fae20bc9..7d86c726 100644 --- a/map_gen/Diggy/Feature/DiggyHole.lua +++ b/map_gen/Diggy/Feature/DiggyHole.lua @@ -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 diff --git a/map_gen/Diggy/Scanner.lua b/map_gen/Diggy/Scanner.lua deleted file mode 100644 index 40fa2a93..00000000 --- a/map_gen/Diggy/Scanner.lua +++ /dev/null @@ -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