2022-09-19 02:16:22 +02:00
local wea = worldeditadditions
local wea_c = worldeditadditions_core
2021-03-01 22:26:33 +01:00
-- ███ ███ █████ ██ ██ ███████ ██████ ██████
-- ████ ████ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ████ ██ ███████ █████ █████ █████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██████
2021-03-16 22:39:12 +01: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 23:53:16 +02:00
function worldeditadditions . noise . make_2d ( size , start_pos , params )
2021-08-07 23:30:10 +02:00
params = worldeditadditions.noise . params_apply_default ( params )
2021-03-16 22:39:12 +01:00
local result = { }
2021-07-12 03:45:32 +02:00
for layer_i , layer in ipairs ( params ) do
2021-07-03 23:53:16 +02:00
local generator
if layer.algorithm == " perlin " then
2021-07-04 14:21:13 +02:00
generator = wea.noise . engines.Perlin . new ( )
2021-08-07 23:30:10 +02:00
elseif layer.algorithm == " perlinmt " then
generator = wea.noise . engines.PerlinMT . new ( )
2021-07-04 14:21:13 +02:00
elseif layer.algorithm == " sin " then
generator = wea.noise . engines.Sin . new ( )
2021-07-12 03:45:32 +02:00
elseif layer.algorithm == " white " then
generator = wea.noise . engines.White . new ( )
2021-07-13 01:15:23 +02:00
elseif layer.algorithm == " red " then
generator = wea.noise . engines.Red . new ( )
2021-07-13 01:54:52 +02:00
elseif layer.algorithm == " infrared " then
generator = wea.noise . engines.Infrared . new ( )
2021-07-03 23:53:16 +02:00
else -- We don't have any other generators just yet
2021-07-12 03:45:32 +02: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 23:53:16 +02:00
end
2021-07-12 03:46:47 +02:00
-- TODO: Optimise performance by making i count backwards in sequence
2021-07-12 03:45:32 +02:00
for x = 0 , size.x - 1 do
for y = 0 , size.z - 1 do
2021-07-03 23:53:16 +02: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 03:45:32 +02:00
local result_before = result [ i ]
2021-07-03 23:53:16 +02:00
result [ i ] = result [ i ] + ( noise_value ^ layer.exponent ) * layer.multiply + layer.add
2021-07-12 03:45:32 +02: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 22:39:12 +01:00
end
2021-07-03 23:53:16 +02:00
end
end
2021-07-12 03:45:32 +02:00
print ( " NOISE MAKE_2D \n " )
2022-09-19 02:16:22 +02:00
wea_c.format . array_2d ( result , size.x )
2021-07-12 03:45:32 +02:00
2021-07-04 14:21:13 +02:00
-- We don't round here, because otherwise when we apply it it'll be inaccurate
2021-07-03 23:53:16 +02:00
return true , result
2021-03-01 22:26:33 +01:00
end