2018-09-08 18:29:27 +02:00
|
|
|
-- this
|
|
|
|
local Template = {}
|
|
|
|
|
|
|
|
Template.events = {
|
2018-09-11 22:15:02 +02:00
|
|
|
--[[--
|
|
|
|
When an entity is placed via the template function.
|
|
|
|
- event.entity LuaEntity
|
|
|
|
]]
|
|
|
|
on_placed_entity = script.generate_event_name(),
|
2018-09-13 20:42:12 +02:00
|
|
|
|
|
|
|
--[[--
|
2018-09-13 21:32:01 +02:00
|
|
|
Triggers when an 'out-of-map' tile is placed on something else.
|
2018-09-23 17:13:54 +02:00
|
|
|
|
|
|
|
{surface, old_tile={name, position={x, y}}}
|
2018-09-13 21:32:01 +02:00
|
|
|
]]
|
|
|
|
on_void_added = script.generate_event_name(),
|
2018-09-13 20:42:12 +02:00
|
|
|
|
2018-09-13 21:32:01 +02:00
|
|
|
--[[--
|
|
|
|
Triggers when an 'out-of-map' tile is replaced by something else.
|
2018-09-23 17:13:54 +02:00
|
|
|
|
|
|
|
{surface, old_tile={name, position={x, y}}}
|
2018-09-13 20:42:12 +02:00
|
|
|
]]
|
|
|
|
on_void_removed = script.generate_event_name(),
|
2018-09-08 18:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
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
|
|
|
|
]]
|
2018-09-13 21:32:01 +02:00
|
|
|
function Template.insert(surface, tiles, entities)
|
|
|
|
local void_removed = {}
|
|
|
|
local void_added = {}
|
|
|
|
|
|
|
|
for _, new_tile in pairs(tiles) do
|
|
|
|
local current_tile = surface.get_tile(new_tile.position.x, new_tile.position.y)
|
|
|
|
local current_is_void = current_tile.name == 'out-of-map'
|
|
|
|
local new_is_void = new_tile.name == 'out-of-map'
|
|
|
|
|
|
|
|
if (current_is_void and not new_is_void) then
|
|
|
|
table.insert(void_removed, {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}})
|
|
|
|
end
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-13 21:32:01 +02:00
|
|
|
if (new_is_void and not current_is_void) then
|
2018-09-13 21:34:39 +02:00
|
|
|
table.insert(void_added, {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}})
|
2018-09-13 20:42:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-13 21:32:01 +02:00
|
|
|
surface.set_tiles(tiles)
|
|
|
|
|
|
|
|
for _, event in pairs(void_removed) do
|
|
|
|
script.raise_event(Template.events.on_void_removed, event)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, event in pairs(void_added) do
|
|
|
|
script.raise_event(Template.events.on_void_added, event)
|
|
|
|
end
|
|
|
|
|
2018-09-12 23:13:54 +02:00
|
|
|
local created_entities = {}
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
for _, entity in pairs(entities) do
|
2018-09-12 23:13:54 +02:00
|
|
|
created_entity = surface.create_entity(entity)
|
|
|
|
if (nil == created_entity) then
|
2018-09-11 22:15:02 +02:00
|
|
|
error('Failed creating entity ' .. entity.name .. ' on surface.')
|
|
|
|
end
|
2018-09-12 23:13:54 +02:00
|
|
|
|
2018-09-14 21:42:58 +02:00
|
|
|
if ('sand-rock-big' == created_entity.name) then
|
|
|
|
created_entity.destructible = false
|
|
|
|
end
|
|
|
|
|
2018-09-12 23:13:54 +02:00
|
|
|
table.insert(created_entities, created_entity)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, entity in pairs(created_entities) do
|
2018-09-11 22:15:02 +02:00
|
|
|
script.raise_event(Template.events.on_placed_entity, {entity = entity})
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Template
|