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

136 lines
3.3 KiB
Lua
Raw Normal View History

2018-05-16 12:38:51 +02:00
local Token = require 'utils.global_token'
local Event = require 'utils.event'
2018-01-15 19:25:46 +02:00
2018-05-16 12:38:51 +02:00
local regen_decoratives
2018-06-07 14:09:28 +02:00
local surfaces
2018-05-09 00:46:49 +02:00
2018-06-07 14:09:28 +02:00
local function do_row(row, data, shape)
2018-05-10 21:42:38 +02:00
local function do_tile(tile, pos)
2018-05-08 18:47:34 +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})
2018-05-08 18:47:34 +02:00
end
end
local y = data.top_y + row
local top_x = data.top_x
2018-01-15 19:25:46 +02:00
2018-05-08 18:47:34 +02:00
data.y = y
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-16 12:38:51 +02:00
local tile = shape(x + 0.5, y + 0.5, data)
2018-01-15 19:25:46 +02:00
2018-05-16 12:38:51 +02:00
if type(tile) == 'table' then
2018-05-10 21:42:38 +02:00
do_tile(tile.tile, pos)
2018-01-15 19:25:46 +02:00
2018-05-08 18:47:34 +02:00
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
2018-05-08 18:47:34 +02:00
table.insert(data.entities, entity)
end
end
2018-05-24 00:04:28 +02:00
local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
end
end
2018-05-08 18:47:34 +02:00
else
2018-05-10 21:42:38 +02:00
do_tile(tile, pos)
end
end
2018-01-15 19:25:46 +02:00
end
local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true)
2018-01-15 19:25:46 +02:00
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:46 +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
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:46 +02:00
end
local function do_place_entities(data)
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)
callback(entity)
end
end
2018-01-15 19:25:46 +02:00
end
end
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-05-10 21:42:38 +02:00
local area = event.area
2018-01-15 19:25:46 +02:00
local data = {
2018-05-24 15:29:40 +02:00
area = area,
top_x = area.left_top.x,
top_y = area.left_top.y,
2018-06-07 14:09:28 +02:00
surface = surface,
tiles = {},
entities = {},
decoratives = {}
}
2018-01-15 19:25:46 +02:00
for row = 0, 31 do
2018-06-07 14:09:28 +02:00
do_row(row, data, shape)
end
2018-01-15 19:25:46 +02:00
do_place_tiles(data)
2018-05-10 21:42:38 +02:00
do_place_entities(data)
do_place_decoratives(data)
2018-01-15 19:25:46 +02:00
end
2018-05-09 00:46:49 +02:00
2018-05-16 12:38:51 +02:00
local function init(args)
regen_decoratives = args.regen_decoratives
2018-06-07 14:09:28 +02:00
surfaces = args.surfaces or {}
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