2021-03-01 21:26:33 +00:00
-- ███ ███ █████ ██ ██ ███████ ██████ ██████
-- ████ ████ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ████ ██ ███████ █████ █████ █████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██████
2021-07-04 12:21:13 +00:00
local wea = worldeditadditions
2021-03-16 21:39:12 +00:00
-- Generate a flat array of 2D noise.
-- Written with help from https://www.redblobgames.com/maps/terrain-from-noise/
-- @param size Vector An x/y vector representing the size of the noise area to generate.
-- @param params table|table<table> A table of noise params to use to generate the noise. Values that aren't specified are filled in automatically. If a table of tables is specified, it is interpreted as multiple octaves of noise to apply in sequence.
2021-07-03 21:53:16 +00:00
function worldeditadditions . noise . make_2d ( size , start_pos , params )
2021-08-07 21:30:10 +00:00
params = worldeditadditions.noise . params_apply_default ( params )
2021-03-16 21:39:12 +00:00
local result = { }
2021-07-12 01:45:32 +00:00
for layer_i , layer in ipairs ( params ) do
2021-07-03 21:53:16 +00:00
local generator
if layer.algorithm == " perlin " then
2021-07-04 12:21:13 +00:00
generator = wea.noise . engines.Perlin . new ( )
2021-08-07 21:30:10 +00:00
elseif layer.algorithm == " perlinmt " then
generator = wea.noise . engines.PerlinMT . new ( )
2021-07-04 12:21:13 +00:00
elseif layer.algorithm == " sin " then
generator = wea.noise . engines.Sin . new ( )
2021-07-12 01:45:32 +00:00
elseif layer.algorithm == " white " then
generator = wea.noise . engines.White . new ( )
2021-07-12 23:15:23 +00:00
elseif layer.algorithm == " red " then
generator = wea.noise . engines.Red . new ( )
2021-07-12 23:54:52 +00:00
elseif layer.algorithm == " infrared " then
generator = wea.noise . engines.Infrared . new ( )
2021-07-03 21:53:16 +00:00
else -- We don't have any other generators just yet
2021-07-12 01:45:32 +00:00
return false , " Error: Unknown noise algorithm ' " .. tostring ( layer.algorithm ) .. " ' in layer " .. layer_i .. " of " .. # params .. " (available algorithms: " .. table.concat ( wea.noise . engines.available , " , " ) .. " ). "
2021-07-03 21:53:16 +00:00
end
2021-07-12 01:46:47 +00:00
-- TODO: Optimise performance by making i count backwards in sequence
2021-07-12 01:45:32 +00:00
for x = 0 , size.x - 1 do
for y = 0 , size.z - 1 do
2021-07-03 21:53:16 +00:00
local i = y * size.x + x
local noise_x = ( x + 100000 + start_pos.x + layer.offset . x ) * layer.scale . x
local noise_y = ( y + 100000 + start_pos.z + layer.offset . z ) * layer.scale . z
local noise_value = generator : noise ( noise_x , noise_y , 0 )
if type ( result [ i ] ) ~= " number " then result [ i ] = 0 end
2021-07-12 01:45:32 +00:00
local result_before = result [ i ]
2021-07-03 21:53:16 +00:00
result [ i ] = result [ i ] + ( noise_value ^ layer.exponent ) * layer.multiply + layer.add
2021-07-12 01:45:32 +00:00
-- if layer_i == 1 and result[i] > 1 then
-- print("NOISE TOO BIG noise_value", noise_value, "noise_x", noise_x, "noise_y", noise_y, "i", i, "result[i]: BEFORE", result_before, "AFTER", result[i])
-- end
2021-03-16 21:39:12 +00:00
end
2021-07-03 21:53:16 +00:00
end
end
2021-07-12 01:45:32 +00:00
print ( " NOISE MAKE_2D \n " )
worldeditadditions.format . array_2d ( result , size.x )
2021-07-04 12:21:13 +00:00
-- We don't round here, because otherwise when we apply it it'll be inaccurate
2021-07-03 21:53:16 +00:00
return true , result
2021-03-01 21:26:33 +00:00
end