1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-16 10:19:27 +02:00
RedMew/map_gen/Diggy/Feature/ScatteredResources.lua

133 lines
4.3 KiB
Lua
Raw Normal View History

--[[-- info
Provides the ability to spawn random ores all over the place.
]]
-- dependencies
local Event = require 'utils.event'
local Debug = require 'map_gen.Diggy.Debug'
local Template = require 'map_gen.Diggy.Template'
2018-11-05 20:07:17 +02:00
local Perlin = require 'map_gen.shared.perlin_noise'
local random = math.random
local sqrt = math.sqrt
local ceil = math.ceil
local floor = math.floor
-- this
local ScatteredResources = {}
local function get_name_by_random(collection)
local pre_calculated = random()
local current = 0
for name, probability in pairs(collection) do
current = current + probability
if (current >= pre_calculated) then
return name
end
end
Debug.print('Current \'' .. current .. '\' should be higher or equal to random \'' .. pre_calculated .. '\'')
end
local function spawn_resource(config, surface, x, y, distance)
local resource_name = get_name_by_random(config.resource_chances)
if (config.minimum_resource_distance[resource_name] > distance) then
return
end
2018-10-06 15:53:28 +02:00
local min_max = config.resource_richness_values[get_name_by_random(config.resource_richness_probability)]
2018-11-05 20:07:17 +02:00
local amount = ceil(random(min_max[1], min_max[2]) * (1 + ((distance / config.distance_richness_modifier) * 0.01)))
if ('crude-oil' == resource_name) then
amount = amount * config.oil_value_modifier
end
2018-11-05 20:07:17 +02:00
if (config.cluster_mode) then
amount = amount * config.cluster_yield_multiplier
end
local position = {x = x, y = y}
Template.resources(surface, {{name = resource_name, position = position, amount = amount}})
end
--[[--
Registers all event handlers.
]]
2018-10-07 17:05:59 +02:00
function ScatteredResources.register(config)
function sum(t)
local sum = 0
for _, v in pairs(t) do
sum = sum + v
end
return sum
end
2018-11-07 20:14:34 +02:00
local seed
2018-11-05 20:07:17 +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 + 200
2018-11-05 20:07:17 +02:00
return Perlin.noise(x * config.noise_variance, y * config.noise_variance, seed)
end
local resource_sum = sum(config.resource_chances)
if (1 ~= resource_sum) then
error('Expected a sum of 1.00, got \'' .. resource_sum .. '\' for config.feature.ScatteredResources.resource_chances.')
end
local richness_sum = sum(config.resource_richness_probability)
if (1 ~= richness_sum) then
error('Expected a sum of 1.00, got \'' .. richness_sum .. '\' for config.feature.ScatteredResources.resource_richness_probability.')
end
Event.add(Template.events.on_void_removed, function(event)
local x = event.position.x
local y = event.position.y
2018-11-05 20:07:17 +02:00
local surface = event.surface
local distance = floor(sqrt(x * x + y * y))
if (config.cluster_mode and get_noise(surface, x, y) > config.noise_resource_threshold) then
spawn_resource(config, surface, x, y, distance)
2018-11-05 20:07:17 +02:00
return
end
2018-11-05 20:07:17 +02:00
local calculated_probability = config.resource_probability + ((distance / config.distance_probability_modifier) * 0.01)
local probability = config.max_resource_probability
if (calculated_probability < probability) then
probability = calculated_probability
end
2018-11-05 20:07:17 +02:00
-- cluster mode reduces the max probability to reduce max spread
if (config.cluster_mode) then
probability = probability * 0.5
end
if (probability > random()) then
spawn_resource(config, surface, x, y, distance)
end
end)
2018-11-05 20:07:17 +02:00
if (config.enable_noise_grid) then
Event.add(defines.events.on_chunk_generated, function (event)
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})
end
end
end)
end
end
2018-10-07 17:05:59 +02:00
function ScatteredResources.get_extra_map_info(config)
return [[Scattered Resources, resources are everywhere!
Scans of the mine have shown greater amounts of resources to be deeper in the mine]]
end
return ScatteredResources