1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/shared/generate.lua

156 lines
3.9 KiB
Lua
Raw Normal View History

2018-05-16 12:38:51 +02:00
local Task = require 'utils.Task'
local Token = require 'utils.global_token'
local Event = require 'utils.event'
2017-08-12 02:20:08 +02:00
2018-05-09 00:46:49 +02:00
local shape
2018-05-16 12:38:51 +02:00
local regen_decoratives
local function do_row(row, data)
2018-05-10 21:42:38 +02:00
local function do_tile(tile, pos)
2018-03-27 17:16:37 +02:00
if not tile then
2018-05-16 12:38:51 +02:00
table.insert(data.tiles, {name = 'out-of-map', position = pos})
elseif type(tile) == 'string' then
2018-05-10 21:42:38 +02:00
table.insert(data.tiles, {name = tile, position = pos})
end
end
local y = data.top_y + row
local top_x = data.top_x
2018-05-10 21:42:38 +02:00
data.y = y
2018-05-08 18:47:34 +02:00
for x = top_x, top_x + 31 do
2018-05-08 18:47:34 +02:00
data.x = x
2018-05-10 21:42:38 +02:00
local pos = {data.x, data.y}
2018-05-08 18:47:34 +02:00
-- local coords need to be 'centered' to allow for correct rotation and scaling.
2018-05-09 00:46:49 +02:00
local tile = shape(x + 0.5, y + 0.5, data)
2018-05-16 12:38:51 +02:00
if type(tile) == 'table' then
--[[ local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
end
end ]]
2018-05-10 21:42:38 +02:00
do_tile(tile.tile, pos)
local entities = tile.entities
if entities then
for _, entity in ipairs(entities) do
2018-05-10 21:42:38 +02:00
if not entity.position then
entity.position = pos
end
table.insert(data.entities, entity)
end
end
else
2018-05-10 21:42:38 +02:00
do_tile(tile, pos)
end
end
end
local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true)
end
2018-05-10 21:42:38 +02:00
local decoratives = {
2018-05-16 12:38:51 +02:00
'brown-asterisk',
'brown-carpet-grass',
'brown-fluff',
'brown-fluff-dry',
'brown-hairy-grass',
'garballo',
'garballo-mini-dry',
'green-asterisk',
'green-bush-mini',
'green-carpet-grass',
'green-hairy-grass',
'green-pita',
'green-pita-mini',
'green-small-grass',
'red-asterisk'
2018-05-10 21:42:38 +02:00
}
local function do_place_decoratives(data)
2018-05-16 12:38:51 +02:00
if regen_decoratives then
data.surface.regenerate_decorative(decoratives, {{data.top_x / 32, data.top_y / 32}})
end
end
local function do_place_entities(data)
local surface = data.surface
for _, e in ipairs(data.entities) do
if surface.can_place_entity(e) then
2018-05-10 21:42:38 +02:00
local entity = surface.create_entity(e)
if e.callback then
local callback = Token.get(e.callback)
callback(entity)
end
end
end
end
local function run_chart_update(data)
local x = data.top_x / 32
local y = data.top_y / 32
2018-02-23 14:11:39 +02:00
if game.forces.player.is_chunk_charted(data.surface, {x, y}) then
-- Don't use full area, otherwise adjacent chunks get charted
game.forces.player.chart(
2018-02-23 14:11:39 +02:00
data.surface,
{
{data.top_x, data.top_y},
{data.top_x + 1, data.top_y + 1}
}
)
end
end
function map_gen_action(data)
local state = data.state
if state < 32 then
do_row(state, data)
data.state = state + 1
return true
elseif state == 32 then
do_place_tiles(data)
data.state = 33
return true
elseif state == 33 then
2018-05-10 21:42:38 +02:00
do_place_entities(data)
data.state = 34
return true
elseif state == 34 then
2018-05-10 21:42:38 +02:00
do_place_decoratives(data)
data.state = 35
return true
elseif state == 35 then
run_chart_update(data)
return false
end
end
2018-05-09 00:46:49 +02:00
local function on_chunk(event)
local area = event.area
local data = {
state = 0,
top_x = area.left_top.x,
top_y = area.left_top.y,
surface = event.surface,
tiles = {},
entities = {},
decoratives = {}
}
2018-05-16 12:38:51 +02:00
Task.queue_task('map_gen_action', data, 36)
end
2018-05-16 12:38:51 +02:00
local function init(args)
shape = args.shape
regen_decoratives = args.regen_decoratives or false
Event.add(defines.events.on_chunk_generated, on_chunk)
2018-05-09 00:46:49 +02:00
end
return init