1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/entities/harmonic_gen.lua

52 lines
1.5 KiB
Lua
Raw Normal View History

2018-05-23 23:34:47 +02:00
local perlin = require 'map_gen.shared.perlin_noise'
2017-11-10 14:03:46 +02:00
-- list of {x, y, ore_type, size, richness, rng_seed}
local ctrs = {
2018-05-23 23:34:47 +02:00
{1, -15, 'iron-ore', 0.3, 400, 113},
{15, 15, 'copper-ore', 0.3, 400, 80},
{4, 21, 'coal', 0.25, 640, 31},
{10, 0, 'stone', 0.5, 100, 17},
{-17, 7, 'uranium-ore', 0.6, 100, 203}
2017-11-10 14:03:46 +02:00
}
local function harmonic(x, y)
2018-05-23 23:34:47 +02:00
local max_idx = 0
local max = -1
local richness = 0
for i in ipairs(ctrs) do
2018-06-12 17:57:39 +02:00
local noise = perlin.noise(x / 32, y / 32, ctrs[i][6])
2018-05-23 23:34:47 +02:00
local h_coeff =
1 /
(1 +
.05 *
math.sqrt(
(x / 32 - ctrs[i][1]) * (x / 32 - ctrs[i][1]) + (y / 32 - ctrs[i][2]) * (y / 32 - ctrs[i][2])
))
if noise > max and noise > h_coeff * ctrs[i][4] + (1 - h_coeff) then
max = noise
max_idx = i
richness = (40 * (1 - h_coeff) + 0.5 * h_coeff) * ctrs[i][5]
end
end
return max, max_idx, richness
2017-11-10 14:03:46 +02:00
end
return function(_, _, world)
2018-05-23 23:34:47 +02:00
if math.abs(world.x / 32) < 3 and math.abs(world.y / 32) < 3 then
return
end
2018-05-10 21:42:24 +02:00
2018-05-23 23:34:47 +02:00
local entities = world.surface.find_entities_filtered {position = {world.x + 0.5, world.y + 0.5}, type = 'resource'}
for _, e in ipairs(entities) do
if e.name ~= 'crude-oil' then
e.destroy()
end
end
2018-05-10 21:42:24 +02:00
2018-05-23 23:34:47 +02:00
local max, max_idx, richness = harmonic(world.x, world.y)
2018-05-10 21:42:24 +02:00
2018-05-23 23:34:47 +02:00
if -1 ~= max then
return {name = ctrs[max_idx][3], amount = richness}
end
2017-11-10 14:03:46 +02:00
end