2018-11-18 18:12:00 +02:00
|
|
|
-- dependencies
|
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-11-18 18:12:00 +02:00
|
|
|
local min = math.min
|
|
|
|
local ceil = math.ceil
|
|
|
|
local raise_event = script.raise_event
|
2018-12-13 21:59:00 +02:00
|
|
|
local queue_task = Task.queue_task
|
|
|
|
local pairs = pairs
|
|
|
|
local pcall = pcall
|
2018-11-18 18:12:00 +02:00
|
|
|
|
|
|
|
-- this
|
|
|
|
local Template = {}
|
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
local tiles_per_call = 8 --how many tiles are inserted with each call of insert_action
|
2018-11-25 21:09:46 +02:00
|
|
|
local entities_per_call = 8 --how many entities are inserted with each call of insert_action
|
2018-11-18 18:12:00 +02:00
|
|
|
|
|
|
|
Template.events = {
|
|
|
|
--[[--
|
|
|
|
When an entity is placed via the template function.
|
|
|
|
- event.entity LuaEntity
|
|
|
|
]]
|
|
|
|
on_placed_entity = script.generate_event_name(),
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Triggers when an 'out-of-map' tile is replaced by something else.
|
|
|
|
|
|
|
|
{surface, old_tile={name, position={x, y}}}
|
|
|
|
]]
|
|
|
|
on_void_removed = script.generate_event_name(),
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
local on_void_removed = Template.events.on_void_removed
|
|
|
|
local on_placed_entity = Template.events.on_placed_entity
|
|
|
|
|
2018-11-18 18:12:00 +02:00
|
|
|
local function insert_next_tiles(data)
|
|
|
|
local void_removed = {}
|
2018-12-13 21:59:00 +02:00
|
|
|
local void_removed_count = 0
|
2018-11-18 18:12:00 +02:00
|
|
|
local surface = data.surface
|
|
|
|
local get_tile = surface.get_tile
|
|
|
|
local tiles = {}
|
2018-12-13 21:59:00 +02:00
|
|
|
local tile_count = 0
|
|
|
|
local tile_iterator = data.tile_iterator
|
2018-11-18 18:12:00 +02:00
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
pcall(function()
|
|
|
|
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
|
|
|
for i = tile_iterator, min(tile_iterator + tiles_per_call - 1, data.tiles_n) do
|
|
|
|
local new_tile = data.tiles[i]
|
|
|
|
tile_count = tile_count + 1
|
|
|
|
tiles[tile_count] = new_tile
|
2018-11-18 18:12:00 +02:00
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
if new_tile.name ~= 'out-of-map' then
|
|
|
|
local current_tile = get_tile(new_tile.position.x, new_tile.position.y)
|
|
|
|
if current_tile.name == 'out-of-map' then
|
|
|
|
void_removed_count = void_removed_count + 1
|
|
|
|
void_removed[void_removed_count] = {surface = surface, position = current_tile.position}
|
2018-11-18 18:12:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 21:59:00 +02:00
|
|
|
end)
|
2018-11-18 18:12:00 +02:00
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
data.tile_iterator = tile_iterator + tiles_per_call
|
2018-11-18 18:12:00 +02:00
|
|
|
|
|
|
|
surface.set_tiles(tiles)
|
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
for i = 1, void_removed_count do
|
|
|
|
raise_event(on_void_removed, void_removed[i])
|
2018-11-18 18:12:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function insert_next_entities(data)
|
|
|
|
local created_entities = {}
|
2018-12-13 21:59:00 +02:00
|
|
|
local created_entities_count = 0
|
2018-11-18 18:12:00 +02:00
|
|
|
local surface = data.surface
|
|
|
|
local create_entity = surface.create_entity
|
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
pcall(function()
|
|
|
|
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
|
|
|
for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do
|
|
|
|
created_entities_count = created_entities_count + 1
|
|
|
|
created_entities[created_entities_count] = create_entity(data.entities[i])
|
2018-11-18 18:12:00 +02:00
|
|
|
end
|
2018-12-13 21:59:00 +02:00
|
|
|
end)
|
2018-11-18 18:12:00 +02:00
|
|
|
|
|
|
|
data.entity_iterator = data.entity_iterator + entities_per_call
|
|
|
|
|
2018-12-13 21:59:00 +02:00
|
|
|
for i = 1, created_entities_count do
|
|
|
|
raise_event(on_placed_entity, {entity = created_entities[i]})
|
2018-11-18 18:12:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return data.entity_iterator <= data.entities_n
|
|
|
|
end
|
|
|
|
|
|
|
|
local function insert_action(data)
|
|
|
|
if data.tile_iterator <= data.tiles_n then
|
|
|
|
insert_next_tiles(data)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return insert_next_entities(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
local insert_token = Token.register(insert_action)
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Inserts a batch of tiles and then entities.
|
|
|
|
|
|
|
|
@see LuaSurface.set_tiles
|
|
|
|
@see LuaSurface.entity
|
|
|
|
|
|
|
|
@param surface LuaSurface to put the tiles and entities on
|
|
|
|
@param tiles table of tiles as required by set_tiles
|
|
|
|
@param entities table of entities as required by create_entity
|
|
|
|
]]
|
|
|
|
function Template.insert(surface, tiles, entities)
|
|
|
|
tiles = tiles or {}
|
|
|
|
entities = entities or {}
|
|
|
|
|
|
|
|
local tiles_n = #tiles
|
|
|
|
local entities_n = #entities
|
|
|
|
local total_calls = ceil(tiles_n / tiles_per_call) + (entities_n / entities_per_call)
|
|
|
|
local data = {
|
|
|
|
tiles_n = tiles_n,
|
|
|
|
tile_iterator = 1,
|
|
|
|
entities_n = entities_n,
|
|
|
|
entity_iterator = 1,
|
|
|
|
surface = surface,
|
|
|
|
tiles = tiles,
|
|
|
|
entities = entities
|
|
|
|
}
|
|
|
|
|
|
|
|
local continue = true
|
2018-11-25 21:09:46 +02:00
|
|
|
for _ = 1, 4 do
|
2018-11-18 18:12:00 +02:00
|
|
|
continue = insert_action(data)
|
|
|
|
if not continue then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if continue then
|
2018-12-13 21:59:00 +02:00
|
|
|
queue_task(insert_token, data, total_calls - 4)
|
2018-11-18 18:12:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Designed to spawn resources.
|
|
|
|
|
|
|
|
@see LuaSurface.entity
|
|
|
|
|
|
|
|
@param surface LuaSurface to put the tiles and entities on
|
|
|
|
@param resources table of entities as required by create_entity
|
|
|
|
]]
|
|
|
|
function Template.resources(surface, resources)
|
|
|
|
local create_entity = surface.create_entity
|
|
|
|
for _, entity in pairs(resources) do
|
|
|
|
create_entity(entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-01 20:00:42 +02:00
|
|
|
Template.diggy_rocks = {'sand-rock-big', 'rock-big', 'rock-huge'}
|
2018-12-01 18:58:22 +02:00
|
|
|
|
|
|
|
---Returns true if the entity name is that of a diggy rock.
|
|
|
|
---@param entity_name string
|
|
|
|
function Template.is_diggy_rock(entity_name)
|
|
|
|
return entity_name == 'sand-rock-big' or entity_name == 'rock-big' or entity_name == 'rock-huge'
|
|
|
|
end
|
|
|
|
|
2018-11-18 18:12:00 +02:00
|
|
|
return Template
|