2018-09-08 18:29:27 +02:00
|
|
|
--[[-- info
|
|
|
|
Provides the ability to make a simple room with contents
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- dependencies
|
2018-09-25 21:34:36 +02:00
|
|
|
local Template = require 'map_gen.Diggy.Template'
|
|
|
|
local Perlin = require 'map_gen.shared.perlin_noise'
|
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Debug = require'map_gen.Diggy.Debug'
|
2018-09-30 15:33:53 +02:00
|
|
|
local Task = require 'utils.Task'
|
|
|
|
local Token = require 'utils.global_token'
|
2018-09-08 18:29:27 +02:00
|
|
|
|
|
|
|
-- this
|
|
|
|
local SimpleRoomGenerator = {}
|
|
|
|
|
2018-09-30 15:33:53 +02:00
|
|
|
local do_spawn_tile = Token.register(function(params)
|
|
|
|
Template.insert(params.surface, {params.tile}, {})
|
|
|
|
end)
|
|
|
|
|
|
|
|
local do_mine = Token.register(function(params)
|
|
|
|
local sand_rocks = params.surface.find_entities_filtered({position = params.position, name = 'sand-rock-big'})
|
|
|
|
|
2018-09-30 21:55:29 +02:00
|
|
|
if (0 == #sand_rocks) then
|
|
|
|
Debug.printPosition(params.position, 'missing rock when trying to mine.')
|
2018-10-06 12:40:03 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, rock in pairs(sand_rocks) do
|
|
|
|
rock.die()
|
2018-09-30 21:55:29 +02:00
|
|
|
end
|
2018-09-30 15:33:53 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
local function handle_noise(name, surface, position)
|
|
|
|
Task.set_timeout_in_ticks(1, do_mine, {surface = surface, position = position})
|
|
|
|
|
|
|
|
if ('water' == name) then
|
2018-10-07 19:40:30 +02:00
|
|
|
-- water is slower because for some odd reason it doesn't always want to mine it properly
|
2018-10-07 19:59:37 +02:00
|
|
|
Task.set_timeout_in_ticks(4, do_spawn_tile, { surface = surface, tile = { name = 'deepwater-green', position = position}})
|
2018-09-30 15:33:53 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if ('dirt' == name) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
error('No noise handled for type \'' .. name .. '\'')
|
|
|
|
end
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
--[[--
|
|
|
|
Registers all event handlers.
|
|
|
|
]]
|
2018-10-07 17:05:59 +02:00
|
|
|
function SimpleRoomGenerator.register(config)
|
2018-10-11 19:43:04 +02:00
|
|
|
local room_noise_minimum_distance_sq = config.room_noise_minimum_distance * config.room_noise_minimum_distance
|
2018-10-03 00:59:05 +02:00
|
|
|
|
2018-11-07 20:14:34 +02:00
|
|
|
local seed
|
2018-09-25 21:34:36 +02:00
|
|
|
local function get_noise(surface, x, y)
|
2018-11-06 19:48:38 +02:00
|
|
|
seed = seed or surface.map_gen_settings.seed + surface.index + 100
|
2018-09-25 21:34:36 +02:00
|
|
|
return Perlin.noise(x * config.noise_variance, y * config.noise_variance, seed)
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(Template.events.on_void_removed, function (event)
|
2018-11-10 14:37:24 +02:00
|
|
|
local position = event.position
|
2018-10-08 20:40:32 +02:00
|
|
|
local x = position.x
|
|
|
|
local y = position.y
|
|
|
|
|
2018-11-06 19:48:38 +02:00
|
|
|
local distance_sq = x * x + y * y
|
2018-09-30 15:33:53 +02:00
|
|
|
|
2018-10-03 00:59:05 +02:00
|
|
|
if (distance_sq <= room_noise_minimum_distance_sq) then
|
2018-09-30 15:33:53 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-10-11 22:17:25 +02:00
|
|
|
local surface = event.surface
|
2018-10-15 23:45:14 +02:00
|
|
|
local noise = get_noise(surface, x, y)
|
2018-09-30 15:33:53 +02:00
|
|
|
|
|
|
|
for _, noise_range in pairs(config.room_noise_ranges) do
|
|
|
|
if (noise >= noise_range.min and noise <= noise_range.max) then
|
2018-10-11 22:17:25 +02:00
|
|
|
handle_noise(noise_range.name, surface, position)
|
2018-09-30 15:33:53 +02:00
|
|
|
end
|
|
|
|
end
|
2018-09-25 21:34:36 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
if (config.enable_noise_grid) then
|
|
|
|
Event.add(defines.events.on_chunk_generated, function (event)
|
2018-10-11 22:17:25 +02:00
|
|
|
local surface = event.surface
|
|
|
|
local area = event.area
|
|
|
|
|
|
|
|
for x = area.left_top.x, area.left_top.x + 31 do
|
|
|
|
for y = area.left_top.y, area.left_top.y + 31 do
|
|
|
|
Debug.print_grid_value(get_noise(surface, x, y), surface, {x = x, y = y})
|
2018-09-25 21:34:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2018-09-08 18:29:27 +02:00
|
|
|
end
|
|
|
|
|
2018-10-07 17:05:59 +02:00
|
|
|
function SimpleRoomGenerator.get_extra_map_info(config)
|
|
|
|
return 'Simple Room Generator, digging around might open rooms!'
|
|
|
|
end
|
|
|
|
|
2018-09-08 18:29:27 +02:00
|
|
|
return SimpleRoomGenerator
|