1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/Diggy/Template.lua
SimonFlapse 4258568c90
Catching up (#2) (#3)
* Add creep spread mechanic

* Added bot ore islands, use bool flag to turn on.

* Added a noise based chest spawning system for artefacts

* Added coin loot from biters and mining

* Track artefacts launched into space

* Add Creepy map preset

* Update donators.lua

* Add scenario info for crashsite (#291)

* Split colors resources from colors code & catchup on donators (#288)

* Split colors resources from colors code

* Switch back to colors

* Added jail button

Moved parts of the jail command to report.lua

Made jailing possible from a users report.

closes #215

* Added sound for new reports

Admins now get a sound when a new report is created. Useful for admins that doesn't have Factorio as active window.

Currently set the sound_path as utility/tutorial_notice

* Split chat triggers from control

* Move cheat tracking outside of control.lua

* Remove player-specific code

* Split donator/on_join messages out of control

* Move features into features folder

* Indentation fix

I aimed to fix the troublesome indentation.

* Added a single indent

* Make sure biter modifiers are accessible for mods

* Consistency change regarding table access

* Added NightTime.lua

Split the time assignment into a new file NightTime.lua allowing for it to be disabled. (Restore normal day/night cycle)

Also added a popup when a player places a solar panel informing that they are purely cosmetic

Removed the recipe for portable solar panels because they are useless, but the technology is needed.

* Fixed debug print grid value to show non-rounded value

* added support for hidden tiles

* Add newline to eof

* Fixed no newline, indentation and scope of research

* crash_site outposts set hidden tile to 'grass-1'

* Log items spawned by crafting in cheat mode

* Newline to eof

* Put responses into table

* Use trigger as table key

* Change name of lattice to diagonal lattice (#297)

* Add Diagonal Ribbon to map presets (#294)

* Reorganize control (#312)

* Check for config before disabling fish market (#311)

* Force diggy biters to spawn, even if there's no space (#308)

* Fixed some small things from feedback and issues (#313)

* Update fractal_balls.lua

* Add ALo's message and color (#314)

* Add join message for ALo

* Add ALo's color

* Typo in donators.lua

* fixed tile corruption issus closes #310 (#317)

* Update player_list.lua

* Update diagonal_ribbon.lua (#322)

* Regulars: remove duplicates and sort alphabetically (#320)
2018-11-12 20:23:25 +01:00

217 lines
6.6 KiB
Lua

-- dependencies
local Task = require 'utils.Task'
local Token = require 'utils.global_token'
local Debug = require 'map_gen.Diggy.Debug'
local insert = table.insert
local min = math.min
local ceil = math.ceil
local raise_event = script.raise_event
-- this
local Template = {}
local tiles_per_call = 5 --how many tiles are inserted with each call of insert_action
local entities_per_call = 5 --how many entities are inserted with each call of insert_action
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(),
}
local function insert_next_tiles(data)
local void_removed = {}
local surface = data.surface
local get_tile = surface.get_tile
local tiles = {}
pcall(
function()
--use pcall to assure tile_iterator is always incremented, to avoid endless loops
for i = data.tile_iterator, min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do
local new_tile = data.tiles[i]
insert(tiles, new_tile)
local current_tile = 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
insert(void_removed, {surface = surface, position = current_tile.position})
end
end
end
)
data.tile_iterator = data.tile_iterator + tiles_per_call
surface.set_tiles(tiles)
for _, event in pairs(void_removed) do
raise_event(Template.events.on_void_removed, event)
end
end
local function insert_next_entities(data)
local created_entities = {}
local surface = data.surface
local create_entity = surface.create_entity
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
local entity = data.entities[i]
local created_entity = create_entity(entity)
if (nil == created_entity) then
error('Failed creating entity ' .. entity.name .. ' on surface.')
end
insert(created_entities, created_entity)
end
end
)
data.entity_iterator = data.entity_iterator + entities_per_call
for _, entity in pairs(created_entities) do
raise_event(Template.events.on_placed_entity, {entity = entity})
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
for i = 1, 4 do
continue = insert_action(data)
if not continue then
return
end
end
if continue then
Task.queue_task(insert_token, data, total_calls - 4)
end
end
--[[--
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
@param non_colliding_distance int amount of tiles to scan around original position in case it's already taken
@param generic_unit_name_for_spawn_size String allows setting a custom unit name for spawn size, will overwrite the actual
]]
function Template.units(surface, units, non_colliding_distance, generic_unit_name_for_spawn_size)
non_colliding_distance = non_colliding_distance or 1
generic_unit_name_for_spawn_size = generic_unit_name_for_spawn_size or 'player'
local create_entity = surface.create_entity
local position
for _, entity in pairs(units) do
position = position or surface.find_non_colliding_position(
generic_unit_name_for_spawn_size,
entity.position, non_colliding_distance,
0.5
)
if (nil ~= position) then
entity.position = position
create_entity(entity)
elseif (nil == create_entity(entity)) then
Debug.print_position(entity.position, "Failed to spawn '" .. entity.name .. "'")
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)
local create_entity = surface.create_entity
for _, entity in pairs(resources) do
create_entity(entity)
end
end
--[[--
Designed to spawn a market.
@param surface LuaSurface
@param position Position
@param force LuaForce
@param market_items Table
]]
function Template.market(surface, position, force, market_inventory)
local market = surface.create_entity({name = 'market', position = position})
local add_market_item = market.add_market_item
market.destructible = false
for _, item in ipairs(market_inventory) do
add_market_item(item)
end
force.add_chart_tag(surface, {
text = 'Market',
position = position,
})
raise_event(Template.events.on_placed_entity, {entity = market})
end
return Template