2018-09-23 17:13:54 +02:00
|
|
|
--[[-- info
|
|
|
|
Provides the ability to spawn random ores all over the place.
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- dependencies
|
|
|
|
local Event = require 'utils.event'
|
2018-10-01 22:28:53 +02:00
|
|
|
local Debug = require 'map_gen.Diggy.Debug'
|
2018-09-23 17:13:54 +02:00
|
|
|
local Template = require 'map_gen.Diggy.Template'
|
|
|
|
|
|
|
|
-- this
|
|
|
|
local ScatteredResources = {}
|
|
|
|
|
|
|
|
local function get_name_by_random(collection)
|
|
|
|
local random = math.random()
|
|
|
|
local current = 0
|
|
|
|
|
|
|
|
for name, probability in pairs(collection) do
|
|
|
|
current = current + probability
|
|
|
|
if (current >= random) then
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-01 22:28:53 +02:00
|
|
|
Debug.print('Current \'' .. current .. '\' should be higher or equal to random \'' .. random .. '\'')
|
2018-09-23 17:13:54 +02:00
|
|
|
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)]
|
|
|
|
local amount = math.ceil(math.random(min_max[1], min_max[2]) * (1 + ((distance / config.distance_richness_modifier) / 100)))
|
2018-09-23 17:13:54 +02:00
|
|
|
|
|
|
|
if ('crude-oil' == resource_name) then
|
|
|
|
amount = amount * config.oil_value_modifier
|
|
|
|
end
|
|
|
|
|
2018-10-01 22:28:53 +02:00
|
|
|
local position = {x = x, y = y}
|
|
|
|
|
|
|
|
Template.resources(surface, {{name = resource_name, position = position, amount = amount}})
|
2018-09-23 17:13:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Registers all event handlers.
|
|
|
|
]]
|
2018-10-07 17:05:59 +02:00
|
|
|
function ScatteredResources.register(config)
|
2018-09-23 17:13:54 +02:00
|
|
|
function sum(t)
|
|
|
|
local sum = 0
|
|
|
|
for _, v in pairs(t) do
|
|
|
|
sum = sum + v
|
|
|
|
end
|
|
|
|
|
|
|
|
return sum
|
|
|
|
end
|
|
|
|
|
2018-10-01 22:28:53 +02:00
|
|
|
local resource_sum = sum(config.resource_chances)
|
2018-09-23 17:13:54 +02:00
|
|
|
if (1 ~= resource_sum) then
|
|
|
|
error('Expected a sum of 1.00, got \'' .. resource_sum .. '\' for config.feature.ScatteredResources.resource_chances.')
|
|
|
|
end
|
|
|
|
|
2018-10-01 22:28:53 +02:00
|
|
|
local richness_sum = sum(config.resource_richness_probability)
|
2018-09-23 17:13:54 +02:00
|
|
|
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.old_tile.position.x
|
|
|
|
local y = event.old_tile.position.y
|
|
|
|
|
|
|
|
local distance = math.floor(math.sqrt(x^2 + y^2))
|
2018-10-01 22:28:53 +02:00
|
|
|
local calculated_probability = config.resource_probability + ((distance / config.distance_probability_modifier) / 100)
|
2018-09-23 17:13:54 +02:00
|
|
|
local probability = 0.7
|
|
|
|
|
|
|
|
if (calculated_probability < probability) then
|
|
|
|
probability = calculated_probability
|
|
|
|
end
|
|
|
|
|
|
|
|
if (probability > math.random()) then
|
2018-10-01 22:28:53 +02:00
|
|
|
spawn_resource(config, event.surface, x, y, distance)
|
2018-09-23 17:13:54 +02:00
|
|
|
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
|
|
|
|
|
2018-09-23 17:13:54 +02:00
|
|
|
return ScatteredResources
|