2018-10-01 22:26:43 +02:00
|
|
|
-- dependencies
|
2018-09-30 17:14:45 +02:00
|
|
|
local Task = require 'utils.Task'
|
|
|
|
local Token = require 'utils.global_token'
|
2018-10-01 22:26:43 +02:00
|
|
|
local Debug = require 'map_gen.Diggy.Debug'
|
2018-10-11 19:43:04 +02:00
|
|
|
local insert = table.insert
|
|
|
|
local min = math.min
|
|
|
|
local ceil = math.ceil
|
2018-10-11 20:13:42 +02:00
|
|
|
local raise_event = script.raise_event
|
2018-09-30 17:14:45 +02:00
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
-- this
|
|
|
|
local Template = {}
|
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
local tiles_per_call = 5 --how many tiles are inserted with each call of insert_action
|
2018-10-07 13:50:25 +02:00
|
|
|
local entities_per_call = 5 --how many entities are inserted with each call of insert_action
|
2018-09-30 17:14:45 +02:00
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
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 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
|
|
|
}
|
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
local function insert_next_tiles(data)
|
2018-09-13 21:32:01 +02:00
|
|
|
local void_removed = {}
|
2018-09-30 17:14:45 +02:00
|
|
|
local surface = data.surface
|
2018-10-11 20:13:42 +02:00
|
|
|
local get_tile = surface.get_tile
|
2018-09-30 17:14:45 +02:00
|
|
|
local tiles = {}
|
|
|
|
|
2018-10-07 13:50:25 +02:00
|
|
|
pcall(
|
|
|
|
function()
|
|
|
|
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
2018-10-11 19:43:04 +02:00
|
|
|
for i = data.tile_iterator, min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do
|
2018-10-07 13:50:25 +02:00
|
|
|
local new_tile = data.tiles[i]
|
2018-10-11 19:43:04 +02:00
|
|
|
insert(tiles, new_tile)
|
2018-10-11 20:13:42 +02:00
|
|
|
local current_tile = get_tile(new_tile.position.x, new_tile.position.y)
|
2018-10-07 13:50:25 +02:00
|
|
|
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
|
2018-11-10 14:37:24 +02:00
|
|
|
insert(void_removed, {surface = surface, position = current_tile.position})
|
2018-10-07 13:50:25 +02:00
|
|
|
end
|
2018-09-30 17:14:45 +02:00
|
|
|
end
|
2018-09-13 21:32:01 +02:00
|
|
|
end
|
2018-10-07 13:50:25 +02:00
|
|
|
)
|
2018-09-08 18:29:27 +02:00
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
data.tile_iterator = data.tile_iterator + tiles_per_call
|
2018-09-13 20:42:12 +02:00
|
|
|
|
2018-09-13 21:32:01 +02:00
|
|
|
surface.set_tiles(tiles)
|
|
|
|
|
|
|
|
for _, event in pairs(void_removed) do
|
2018-10-11 20:13:42 +02:00
|
|
|
raise_event(Template.events.on_void_removed, event)
|
2018-09-13 21:32:01 +02:00
|
|
|
end
|
2018-09-30 17:14:45 +02:00
|
|
|
end
|
2018-09-13 21:32:01 +02:00
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
local function insert_next_entities(data)
|
2018-09-12 23:13:54 +02:00
|
|
|
local created_entities = {}
|
2018-09-30 17:14:45 +02:00
|
|
|
local surface = data.surface
|
2018-10-11 20:13:42 +02:00
|
|
|
local create_entity = surface.create_entity
|
2018-09-12 23:13:54 +02:00
|
|
|
|
2018-10-07 13:50:25 +02:00
|
|
|
pcall(
|
|
|
|
function()
|
|
|
|
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
|
2018-10-11 19:43:04 +02:00
|
|
|
for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do
|
2018-10-07 13:50:25 +02:00
|
|
|
local entity = data.entities[i]
|
2018-10-11 20:13:42 +02:00
|
|
|
local created_entity = create_entity(entity)
|
2018-10-07 13:50:25 +02:00
|
|
|
if (nil == created_entity) then
|
|
|
|
error('Failed creating entity ' .. entity.name .. ' on surface.')
|
|
|
|
end
|
|
|
|
|
2018-10-11 19:43:04 +02:00
|
|
|
insert(created_entities, created_entity)
|
2018-09-30 17:14:45 +02:00
|
|
|
end
|
2018-09-14 21:42:58 +02:00
|
|
|
end
|
2018-10-07 13:50:25 +02:00
|
|
|
)
|
2018-09-14 21:42:58 +02:00
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
data.entity_iterator = data.entity_iterator + entities_per_call
|
2018-09-12 23:13:54 +02:00
|
|
|
|
|
|
|
for _, entity in pairs(created_entities) do
|
2018-10-11 20:13:42 +02:00
|
|
|
raise_event(Template.events.on_placed_entity, {entity = entity})
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
2018-09-30 17:14:45 +02:00
|
|
|
|
|
|
|
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
|
2018-09-30 21:55:29 +02:00
|
|
|
|
|
|
|
return insert_next_entities(data)
|
2018-09-30 17:14:45 +02:00
|
|
|
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)
|
2018-09-30 21:55:29 +02:00
|
|
|
tiles = tiles or {}
|
|
|
|
entities = entities or {}
|
|
|
|
|
2018-09-30 17:14:45 +02:00
|
|
|
local tiles_n = #tiles
|
|
|
|
local entities_n = #entities
|
2018-10-11 19:43:04 +02:00
|
|
|
local total_calls = ceil(tiles_n / tiles_per_call) + (entities_n / entities_per_call)
|
2018-09-30 17:14:45 +02:00
|
|
|
local data = {
|
|
|
|
tiles_n = tiles_n,
|
|
|
|
tile_iterator = 1,
|
|
|
|
entities_n = entities_n,
|
|
|
|
entity_iterator = 1,
|
|
|
|
surface = surface,
|
|
|
|
tiles = tiles,
|
|
|
|
entities = entities
|
|
|
|
}
|
|
|
|
|
2018-09-30 18:46:08 +02:00
|
|
|
local continue = true
|
2018-10-07 13:50:25 +02:00
|
|
|
for i = 1, 4 do
|
|
|
|
continue = insert_action(data)
|
|
|
|
if not continue then
|
|
|
|
return
|
|
|
|
end
|
2018-09-30 18:46:08 +02:00
|
|
|
end
|
2018-10-11 20:13:42 +02:00
|
|
|
|
2018-09-30 18:46:08 +02:00
|
|
|
if continue then
|
|
|
|
Task.queue_task(insert_token, data, total_calls - 4)
|
|
|
|
end
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
|
2018-10-01 22:26:43 +02:00
|
|
|
--[[--
|
|
|
|
Designed to spawn aliens, uses find_non_colliding_position.
|
|
|
|
|
|
|
|
@see LuaSurface.entity
|
|
|
|
|
|
|
|
@param surface LuaSurface to put the tiles and entities on
|
|
|
|
@param units table of entities as required by create_entity
|
2018-10-03 22:11:24 +02:00
|
|
|
@param non_colliding_distance int amount of tiles to scan around original position in case it's already taken
|
2018-11-11 23:33:22 +02:00
|
|
|
@param generic_unit_name_for_spawn_size String allows setting a custom unit name for spawn size, will overwrite the actual
|
2018-10-01 22:26:43 +02:00
|
|
|
]]
|
2018-11-11 23:33:22 +02:00
|
|
|
function Template.units(surface, units, non_colliding_distance, generic_unit_name_for_spawn_size)
|
2018-10-03 22:11:24 +02:00
|
|
|
non_colliding_distance = non_colliding_distance or 1
|
2018-11-11 23:33:22 +02:00
|
|
|
generic_unit_name_for_spawn_size = generic_unit_name_for_spawn_size or 'player'
|
|
|
|
|
2018-10-11 20:13:42 +02:00
|
|
|
local create_entity = surface.create_entity
|
2018-11-11 23:33:22 +02:00
|
|
|
local position
|
2018-10-11 20:13:42 +02:00
|
|
|
|
2018-10-01 22:26:43 +02:00
|
|
|
for _, entity in pairs(units) do
|
2018-11-11 23:33:22 +02:00
|
|
|
position = position or surface.find_non_colliding_position(
|
|
|
|
generic_unit_name_for_spawn_size,
|
|
|
|
entity.position, non_colliding_distance,
|
|
|
|
0.5
|
|
|
|
)
|
2018-10-01 22:26:43 +02:00
|
|
|
|
|
|
|
if (nil ~= position) then
|
|
|
|
entity.position = position
|
2018-10-11 20:13:42 +02:00
|
|
|
create_entity(entity)
|
2018-11-11 23:33:22 +02:00
|
|
|
elseif (nil == create_entity(entity)) then
|
2018-11-11 16:52:51 +02:00
|
|
|
Debug.print_position(entity.position, "Failed to spawn '" .. entity.name .. "'")
|
2018-10-01 22:26:43 +02:00
|
|
|
end
|
|
|
|
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)
|
2018-10-11 20:13:42 +02:00
|
|
|
local create_entity = surface.create_entity
|
2018-10-01 22:26:43 +02:00
|
|
|
for _, entity in pairs(resources) do
|
2018-10-11 20:13:42 +02:00
|
|
|
create_entity(entity)
|
2018-10-01 22:26:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-06 15:53:28 +02:00
|
|
|
--[[--
|
|
|
|
Designed to spawn a market.
|
|
|
|
|
|
|
|
@param surface LuaSurface
|
|
|
|
@param position Position
|
|
|
|
@param force LuaForce
|
|
|
|
@param market_items Table
|
|
|
|
]]
|
2018-11-11 23:36:00 +02:00
|
|
|
function Template.market(surface, position, force, market_inventory)
|
2018-10-06 15:53:28 +02:00
|
|
|
local market = surface.create_entity({name = 'market', position = position})
|
2018-10-11 20:13:42 +02:00
|
|
|
local add_market_item = market.add_market_item
|
2018-10-06 15:53:28 +02:00
|
|
|
market.destructible = false
|
|
|
|
|
|
|
|
for _, item in ipairs(market_inventory) do
|
2018-10-11 20:13:42 +02:00
|
|
|
add_market_item(item)
|
2018-10-06 15:53:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
force.add_chart_tag(surface, {
|
2018-11-11 23:36:00 +02:00
|
|
|
text = 'Market',
|
2018-10-06 15:53:28 +02:00
|
|
|
position = position,
|
|
|
|
})
|
2018-10-07 19:40:30 +02:00
|
|
|
|
2018-10-11 20:13:42 +02:00
|
|
|
raise_event(Template.events.on_placed_entity, {entity = market})
|
2018-10-06 15:53:28 +02:00
|
|
|
end
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
return Template
|