2019-01-04 22:02:55 +02:00
|
|
|
local Task = require 'utils.task'
|
2018-11-26 03:07:03 +02:00
|
|
|
local Token = require 'utils.token'
|
2018-05-16 12:38:51 +02:00
|
|
|
local Event = require 'utils.event'
|
2017-08-12 02:20:08 +02:00
|
|
|
|
2018-11-11 17:08:35 +02:00
|
|
|
local insert = table.insert
|
|
|
|
|
2018-05-24 15:29:24 +02:00
|
|
|
local tiles_per_tick
|
2018-05-16 12:38:51 +02:00
|
|
|
local regen_decoratives
|
2018-06-07 14:09:28 +02:00
|
|
|
local surfaces
|
2018-01-15 19:25:29 +02:00
|
|
|
|
2018-05-24 15:29:24 +02:00
|
|
|
local total_calls
|
|
|
|
|
2018-06-07 14:09:28 +02:00
|
|
|
local function do_tile(y, x, data, shape)
|
2018-05-24 15:29:24 +02:00
|
|
|
local function do_tile_inner(tile, pos)
|
|
|
|
if not tile then
|
2018-11-11 17:08:35 +02:00
|
|
|
insert(data.tiles, {name = 'out-of-map', position = pos})
|
2018-05-24 15:29:24 +02:00
|
|
|
elseif type(tile) == 'string' then
|
2018-11-11 17:08:35 +02:00
|
|
|
insert(data.tiles, {name = tile, position = pos})
|
2018-05-24 15:29:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local pos = {x, 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_inner(tile.tile, pos)
|
|
|
|
|
2018-11-11 17:08:35 +02:00
|
|
|
local hidden_tile = tile.hidden_tile
|
|
|
|
if hidden_tile then
|
|
|
|
insert(data.hidden_tiles, {tile = hidden_tile, position = pos})
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:29:24 +02:00
|
|
|
local entities = tile.entities
|
|
|
|
if entities then
|
|
|
|
for _, entity in ipairs(entities) do
|
|
|
|
if not entity.position then
|
|
|
|
entity.position = pos
|
|
|
|
end
|
2018-11-11 17:08:35 +02:00
|
|
|
insert(data.entities, entity)
|
2018-05-24 15:29:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local decoratives = tile.decoratives
|
|
|
|
if decoratives then
|
|
|
|
for _, decorative in ipairs(decoratives) do
|
2018-11-11 17:08:35 +02:00
|
|
|
insert(data.decoratives, decorative)
|
2018-05-24 15:29:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
do_tile_inner(tile, pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-23 13:40:26 +02:00
|
|
|
local function do_place_tiles(data)
|
|
|
|
data.surface.set_tiles(data.tiles, true)
|
2017-11-13 01:56:26 +02:00
|
|
|
end
|
2017-11-12 23:30:17 +02:00
|
|
|
|
2018-11-11 17:08:35 +02:00
|
|
|
local function do_place_hidden_tiles(data)
|
|
|
|
local surface = data.surface
|
|
|
|
for _, t in ipairs(data.hidden_tiles) do
|
|
|
|
surface.set_hidden_tile(t.position, t.tile)
|
|
|
|
end
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-15 19:25:29 +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}})
|
2018-02-23 13:40:26 +02:00
|
|
|
end
|
2018-05-24 00:04:28 +02:00
|
|
|
|
|
|
|
local dec = data.decoratives
|
2018-05-24 20:05:32 +02:00
|
|
|
if #dec > 0 then
|
2018-05-24 00:04:28 +02:00
|
|
|
data.surface.create_decoratives({check_collision = true, decoratives = dec})
|
|
|
|
end
|
2018-01-15 19:25:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function do_place_entities(data)
|
2018-02-23 13:40:26 +02:00
|
|
|
local surface = data.surface
|
|
|
|
for _, e in ipairs(data.entities) do
|
2018-06-07 17:42:02 +02:00
|
|
|
if e.always_place or surface.can_place_entity(e) then
|
2018-05-10 21:42:38 +02:00
|
|
|
local entity = surface.create_entity(e)
|
2018-06-07 17:42:02 +02:00
|
|
|
if entity and e.callback then
|
2018-05-10 21:42:38 +02:00
|
|
|
local callback = Token.get(e.callback)
|
2018-06-16 13:57:11 +02:00
|
|
|
callback(entity, e.data)
|
2018-05-10 21:42:38 +02:00
|
|
|
end
|
2018-02-23 13:40:26 +02:00
|
|
|
end
|
2018-01-15 19:25:29 +02:00
|
|
|
end
|
2017-11-13 01:56:26 +02:00
|
|
|
end
|
2017-11-12 23:30:17 +02:00
|
|
|
|
2018-05-07 13:18:18 +02:00
|
|
|
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
|
2018-02-23 13:40:26 +02:00
|
|
|
-- Don't use full area, otherwise adjacent chunks get charted
|
|
|
|
game.forces.player.chart(
|
2018-02-23 14:11:39 +02:00
|
|
|
data.surface,
|
2018-02-23 13:40:26 +02:00
|
|
|
{
|
|
|
|
{data.top_x, data.top_y},
|
|
|
|
{data.top_x + 1, data.top_y + 1}
|
|
|
|
}
|
2018-05-07 13:18:18 +02:00
|
|
|
)
|
2018-02-23 13:40:26 +02:00
|
|
|
end
|
2017-11-13 01:56:26 +02:00
|
|
|
end
|
2017-11-12 23:30:17 +02:00
|
|
|
|
2018-05-24 16:16:38 +02:00
|
|
|
local function map_gen_action(data)
|
|
|
|
local state = data.y
|
2018-01-15 19:25:29 +02:00
|
|
|
|
2018-05-24 16:16:38 +02:00
|
|
|
if state < 32 then
|
2018-06-07 14:09:28 +02:00
|
|
|
local shape = surfaces[data.surface.name]
|
|
|
|
if shape == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:29:24 +02:00
|
|
|
local count = tiles_per_tick
|
|
|
|
|
2018-05-24 16:16:38 +02:00
|
|
|
local y = state + data.top_y
|
|
|
|
local x = data.x
|
|
|
|
|
|
|
|
local max_x = data.top_x + 32
|
2018-05-24 15:29:24 +02:00
|
|
|
|
|
|
|
data.y = y
|
|
|
|
|
|
|
|
repeat
|
|
|
|
count = count - 1
|
2018-06-07 14:09:28 +02:00
|
|
|
do_tile(y, x, data, shape)
|
2018-05-24 15:29:24 +02:00
|
|
|
|
|
|
|
x = x + 1
|
2018-05-24 16:16:38 +02:00
|
|
|
if x == max_x then
|
2018-05-24 15:29:24 +02:00
|
|
|
y = y + 1
|
|
|
|
if y == data.top_y + 32 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
x = data.top_x
|
|
|
|
data.y = y
|
|
|
|
end
|
|
|
|
|
|
|
|
data.x = x
|
|
|
|
until count == 0
|
|
|
|
|
|
|
|
data.y = y - data.top_y
|
|
|
|
return true
|
2018-05-24 16:16:38 +02:00
|
|
|
elseif state == 32 then
|
2018-05-24 15:29:24 +02:00
|
|
|
do_place_tiles(data)
|
|
|
|
data.y = 33
|
|
|
|
return true
|
2018-05-24 16:16:38 +02:00
|
|
|
elseif state == 33 then
|
2018-11-11 17:08:35 +02:00
|
|
|
do_place_hidden_tiles(data)
|
2018-05-24 15:29:24 +02:00
|
|
|
data.y = 34
|
|
|
|
return true
|
2018-05-24 16:16:38 +02:00
|
|
|
elseif state == 34 then
|
2018-11-11 17:08:35 +02:00
|
|
|
do_place_entities(data)
|
2018-05-24 15:29:24 +02:00
|
|
|
data.y = 35
|
|
|
|
return true
|
2018-05-24 16:16:38 +02:00
|
|
|
elseif state == 35 then
|
2018-11-11 17:08:35 +02:00
|
|
|
do_place_decoratives(data)
|
|
|
|
data.y = 36
|
|
|
|
return true
|
|
|
|
elseif state == 36 then
|
2018-05-24 15:29:24 +02:00
|
|
|
run_chart_update(data)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-24 16:16:38 +02:00
|
|
|
local map_gen_action_token = Token.register(map_gen_action)
|
|
|
|
|
2018-05-09 00:46:49 +02:00
|
|
|
local function on_chunk(event)
|
2018-06-07 14:09:28 +02:00
|
|
|
local surface = event.surface
|
|
|
|
local shape = surfaces[surface.name]
|
|
|
|
|
|
|
|
if not shape then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-02-23 13:40:26 +02:00
|
|
|
local area = event.area
|
2018-05-07 13:18:18 +02:00
|
|
|
|
2018-02-23 13:40:26 +02:00
|
|
|
local data = {
|
2018-05-24 15:29:24 +02:00
|
|
|
y = 0,
|
2018-05-24 16:16:38 +02:00
|
|
|
x = area.left_top.x,
|
2018-05-24 15:29:24 +02:00
|
|
|
area = area,
|
2018-02-23 13:40:26 +02:00
|
|
|
top_x = area.left_top.x,
|
|
|
|
top_y = area.left_top.y,
|
2018-06-07 14:09:28 +02:00
|
|
|
surface = surface,
|
2018-02-23 13:40:26 +02:00
|
|
|
tiles = {},
|
2018-11-11 17:08:35 +02:00
|
|
|
hidden_tiles = {},
|
2018-02-23 13:40:26 +02:00
|
|
|
entities = {},
|
|
|
|
decoratives = {}
|
|
|
|
}
|
2018-05-24 16:16:38 +02:00
|
|
|
|
|
|
|
Task.queue_task(map_gen_action_token, data, total_calls)
|
2017-11-12 23:30:17 +02:00
|
|
|
end
|
|
|
|
|
2018-05-16 12:38:51 +02:00
|
|
|
local function init(args)
|
2018-05-24 15:29:24 +02:00
|
|
|
tiles_per_tick = args.tiles_per_tick or 32
|
2018-05-16 12:38:51 +02:00
|
|
|
regen_decoratives = args.regen_decoratives or false
|
2018-06-07 14:09:28 +02:00
|
|
|
surfaces = args.surfaces or {}
|
2018-05-16 12:38:51 +02:00
|
|
|
|
2018-11-11 17:08:35 +02:00
|
|
|
total_calls = math.ceil(1024 / tiles_per_tick) + 5
|
2018-05-24 15:29:24 +02:00
|
|
|
|
2018-05-16 12:38:51 +02:00
|
|
|
Event.add(defines.events.on_chunk_generated, on_chunk)
|
2018-05-09 00:46:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return init
|