2021-07-04 14:21:13 +02:00
local wea = worldeditadditions
2022-09-19 02:16:22 +02:00
local wea_c = worldeditadditions_core
2021-07-04 14:21:13 +02:00
2021-07-03 02:44:36 +02:00
--- Applies the given noise field to the given heightmap.
-- Mutates the given heightmap.
2022-09-19 02:16:22 +02:00
-- @param heightmap number[] A table of ZERO indexed numbers representing the heghtmap - see wea_c.terrain.make_heightmap().
2021-07-03 02:44:36 +02:00
-- @param noise number[] An table identical in structure to the heightmap containing the noise values to apply.
-- @param heightmap_size {x:number,z:number} A 2d vector representing the size of the heightmap.
-- @param region_height number The height of the defined region.
-- @param apply_mode string The apply mode to use to apply the noise to the heightmap.
-- @returns bool[,string] A boolean value representing whether the application was successful or not. If false, then an error message as a string is also returned describing the error that occurred.
2021-12-31 03:42:32 +01:00
function wea . noise . apply_2d ( heightmap , noise , heightmap_size , pos1 , pos2 , apply_mode )
2021-07-04 14:21:13 +02:00
if type ( apply_mode ) ~= " string " and type ( apply_mode ) ~= " number " then
return false , " Error: Expected value of type string or number for apply_mode, but received value of type " .. type ( apply_mode )
2021-07-03 02:44:36 +02:00
end
2021-07-04 14:21:13 +02:00
local region_height = pos2.y - pos1.y
2021-07-12 03:45:32 +02:00
2021-12-31 03:42:32 +01:00
-- print("NOISE APPLY_2D\n")
2022-09-19 02:16:22 +02:00
wea_c.format . array_2d ( noise , heightmap_size.x )
2021-07-04 14:21:13 +02:00
local height = tonumber ( apply_mode )
2021-12-31 03:42:32 +01:00
-- print("DEBUG apply_mode", apply_mode, "as height", height)
2021-07-04 14:21:13 +02:00
2021-07-03 02:44:36 +02:00
for z = heightmap_size.z - 1 , 0 , - 1 do
for x = heightmap_size.x - 1 , 0 , - 1 do
local i = ( z * heightmap_size.x ) + x
if apply_mode == " add " then
2022-09-19 02:16:22 +02:00
heightmap [ i ] = wea_c.round ( heightmap [ i ] + noise [ i ] )
2021-07-03 02:44:36 +02:00
elseif apply_mode == " multiply " then
2022-09-19 02:16:22 +02:00
heightmap [ i ] = wea_c.round ( heightmap [ i ] * noise [ i ] )
2021-07-04 14:21:13 +02:00
elseif height then
2021-07-03 02:44:36 +02:00
-- Rescale from 0 - 1 to -1 - +1
local rescaled = ( noise [ i ] * 2 ) - 1
2021-07-04 14:21:13 +02:00
-- Rescale to match the height specified
rescaled = rescaled * height
2022-09-19 02:16:22 +02:00
rescaled = math.floor ( wea_c.clamp (
2021-07-04 14:21:13 +02:00
heightmap [ i ] + rescaled ,
0 , region_height
) )
2021-07-03 02:44:36 +02:00
heightmap [ i ] = rescaled
else
2021-07-03 23:53:16 +02:00
return false , " Error: Unknown apply mode ' " .. apply_mode .. " ' "
2021-07-03 02:44:36 +02:00
end
end
end
2021-07-12 03:45:32 +02:00
-- for z = heightmap_size.z - 1, 0, -1 do
-- x = 0
-- heightmap[(z * heightmap_size.x) + x] = z
-- end
2021-12-31 03:42:32 +01:00
-- print("HEIGHTMAP\n")
2022-09-19 02:16:22 +02:00
-- wea_c.format.array_2d(heightmap, heightmap_size.x)
2021-07-12 03:45:32 +02:00
2021-07-03 02:44:36 +02:00
return true
end