mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-02-07 13:31:54 +02:00
Improved scanner performance
This commit is contained in:
parent
fa4560fdcb
commit
67616ad330
@ -6,12 +6,10 @@
|
|||||||
-- dependencies
|
-- dependencies
|
||||||
local Event = require 'utils.event'
|
local Event = require 'utils.event'
|
||||||
local Global = require 'utils.global'
|
local Global = require 'utils.global'
|
||||||
local Scanner = require 'map_gen.Diggy.Scanner'
|
|
||||||
local Template = require 'map_gen.Diggy.Template'
|
local Template = require 'map_gen.Diggy.Template'
|
||||||
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
|
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
|
||||||
local Debug = require 'map_gen.Diggy.Debug'
|
local Debug = require 'map_gen.Diggy.Debug'
|
||||||
local CreateParticles = require 'features.create_particles'
|
local CreateParticles = require 'features.create_particles'
|
||||||
local insert = table.insert
|
|
||||||
local random = math.random
|
local random = math.random
|
||||||
local raise_event = script.raise_event
|
local raise_event = script.raise_event
|
||||||
|
|
||||||
@ -56,8 +54,30 @@ local function diggy_hole(entity)
|
|||||||
local rocks = {}
|
local rocks = {}
|
||||||
local surface = entity.surface
|
local surface = entity.surface
|
||||||
local position = entity.position
|
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
|
for i = #out_of_map_found, 1, -1 do
|
||||||
local void_position = out_of_map_found[i]
|
local void_position = out_of_map_found[i]
|
||||||
@ -85,10 +105,11 @@ local artificial_tiles = {
|
|||||||
|
|
||||||
local function on_mined_tile(surface, tiles)
|
local function on_mined_tile(surface, tiles)
|
||||||
local new_tiles = {}
|
local new_tiles = {}
|
||||||
|
local count = 0
|
||||||
for _, tile in pairs(tiles) do
|
for _, tile in pairs(tiles) do
|
||||||
if (artificial_tiles[tile.old_tile.name]) then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -219,17 +240,16 @@ function DiggyHole.register(config)
|
|||||||
local height = tonumber(params[4])
|
local height = tonumber(params[4])
|
||||||
local surface_index = params[5]
|
local surface_index = params[5]
|
||||||
local tiles = {}
|
local tiles = {}
|
||||||
local entities = {}
|
local count = 0
|
||||||
|
|
||||||
for x = 0, width do
|
for x = 0, width do
|
||||||
for y = 0, height 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
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.insert(game.surfaces[surface_index], tiles, entities)
|
Template.insert(game.surfaces[surface_index], tiles, {})
|
||||||
end
|
end)
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user