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

Changes to generate to allow controlling the chunk event

This commit is contained in:
grilledham 2019-01-16 15:39:42 +00:00
parent e7538bf797
commit 6a2e7ed6d5

View File

@ -10,6 +10,8 @@ local surfaces
local total_calls
local Public = {}
local function do_tile(y, x, data, shape)
local function do_tile_inner(tile, pos)
if not tile then
@ -53,6 +55,58 @@ local function do_tile(y, x, data, shape)
end
end
local function do_row(row, data, shape)
local function do_tile(tile, pos)
if not tile then
insert(data.tiles, {name = 'out-of-map', position = pos})
elseif type(tile) == 'string' then
insert(data.tiles, {name = tile, position = pos})
end
end
local y = data.top_y + row
local top_x = data.top_x
data.y = y
for x = top_x, top_x + 31 do
data.x = x
local pos = {data.x, data.y}
-- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile = shape(x + 0.5, y + 0.5, data)
if type(tile) == 'table' then
do_tile(tile.tile, pos)
local hidden_tile = tile.hidden_tile
if hidden_tile then
insert(data.hidden_tiles, {tile = hidden_tile, position = pos})
end
local entities = tile.entities
if entities then
for _, entity in ipairs(entities) do
if not entity.position then
entity.position = pos
end
insert(data.entities, entity)
end
end
local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
insert(data.decoratives, decorative)
end
end
else
do_tile(tile, pos)
end
end
end
local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true)
end
@ -182,7 +236,7 @@ end
local map_gen_action_token = Token.register(map_gen_action)
local function on_chunk(event)
function Public.schedule_chunk(event)
local surface = event.surface
local shape = surfaces[surface.name]
@ -208,14 +262,66 @@ local function on_chunk(event)
Task.queue_task(map_gen_action_token, data, total_calls)
end
local function init(args)
function Public.do_chunk(event)
local surface = event.surface
local shape = surfaces[surface.name]
if not shape then
return
end
local area = event.area
local data = {
area = area,
top_x = area.left_top.x,
top_y = area.left_top.y,
surface = surface,
tiles = {},
hidden_tiles = {},
entities = {},
decoratives = {}
}
for row = 0, 31 do
do_row(row, data, shape)
end
do_place_tiles(data)
do_place_hidden_tiles(data)
do_place_entities(data)
do_place_decoratives(data)
end
function Public.init(args)
tiles_per_tick = args.tiles_per_tick or 32
regen_decoratives = args.regen_decoratives or false
surfaces = args.surfaces or {}
total_calls = math.ceil(1024 / tiles_per_tick) + 5
end
local do_chunk = Public.do_chunk
local schedule_chunk = Public.schedule_chunk
local function on_chunk(event)
if event.tick == 0 then
do_chunk(event)
else
schedule_chunk(event)
end
end
function Public.register()
Event.add(defines.events.on_chunk_generated, on_chunk)
end
return init
function Public.register_debug()
Event.add(defines.events.on_chunk_generated, do_chunk)
end
function Public.get_surfaces()
return surfaces
end
return Public