From a1911037d2543a7baab9df82591410be26690e6a Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 16 Mar 2021 21:39:12 +0000 Subject: [PATCH] Fill out noise.make_2d, but it's untested Next, we need to do the make_3d version, followed by the plumbing to worldeditadditions.noise(), followed by the chat command ....there's a lot to do. --- .../lib/noise/engines/perlin.lua | 3 +- worldeditadditions/lib/noise/make_2d.lua | 31 +++++++++++++++- worldeditadditions/lib/noise/noise.lua | 4 +- worldeditadditions/lib/noise/noise_params.lua | 37 +++++++++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 worldeditadditions/lib/noise/noise_params.lua diff --git a/worldeditadditions/lib/noise/engines/perlin.lua b/worldeditadditions/lib/noise/engines/perlin.lua index 88196e4..29fa779 100644 --- a/worldeditadditions/lib/noise/engines/perlin.lua +++ b/worldeditadditions/lib/noise/engines/perlin.lua @@ -57,9 +57,10 @@ wea.noise.perlin.randMax = 256 --- Creates a new perlin instance. -- @return perlin A new perlin instance. -function wea.noice.perlin.new() +function wea.noise.perlin.new() local instance = {} setmetatable(instance, wea.noise.perlin) + instance:load() return instance end diff --git a/worldeditadditions/lib/noise/make_2d.lua b/worldeditadditions/lib/noise/make_2d.lua index 9810d44..7f8a7bb 100644 --- a/worldeditadditions/lib/noise/make_2d.lua +++ b/worldeditadditions/lib/noise/make_2d.lua @@ -4,6 +4,33 @@ -- ██ ████ ██ ███████ █████ █████ █████ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██████ -function worldeditadditions.noise.make_2d(size, noise_func) - -- TODO: Follow https://www.redblobgames.com/maps/terrain-from-noise/ + + +-- 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 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. +function worldeditadditions.noise.make_2d(size, params) + params = worldeditadditions.noise.params_apply_default(params) + + local result = {} + + local generator; + if params.algorithm == "perlin" then + generator = worldeditadditions.noise.perlin.new() + else -- We don't have any other generators just yet + return false, "Error: Unknown noise algorithm '"..params.."'." + end + + for x=params.offset.x, params.offset.x+size.x do + for y=params.offset.y, params.offset.y+size.y do + local result = 0 + for i,params_octave in ipairs(params) do + result = result + (generator:noise(x * scale.x, y * scale.y, 0) ^ params.exponent) * params.multiply + params.add + end + result[y*size.x + x] = result + end + end + + return result end diff --git a/worldeditadditions/lib/noise/noise.lua b/worldeditadditions/lib/noise/noise.lua index 30b2549..e7720d7 100644 --- a/worldeditadditions/lib/noise/noise.lua +++ b/worldeditadditions/lib/noise/noise.lua @@ -1,3 +1,5 @@ worldeditadditions.noise = {} -dofile(worldeditadditions.modpath.."/lib/noise/alg_perlin.lua") +dofile(worldeditadditions.modpath.."/lib/noise/noise_params.lua") +dofile(worldeditadditions.modpath.."/lib/noise/make_2d.lua") +dofile(worldeditadditions.modpath.."/lib/noise/engines/perlin.lua") diff --git a/worldeditadditions/lib/noise/noise_params.lua b/worldeditadditions/lib/noise/noise_params.lua new file mode 100644 index 0000000..e03546d --- /dev/null +++ b/worldeditadditions/lib/noise/noise_params.lua @@ -0,0 +1,37 @@ +--- Makes sure that the default settings are all present in the given params table. +-- This way not all params have to be specified by the user. +-- @param params table The params table generated from the user's input. +-- @return table A NEW table with all the missing properties filled in with the default values. +function worldeditadditions.noise.params_apply_default(params) + local params_default = { + algorithm = "perlin", + -- Zooms in and out + scale = vector.new(1, 1, 1), + -- Offset the generated noise by this vector. + offset = vector.new(0, 0, 0), + -- Apply this exponent to the resulting noise value + exponent = 1, + -- Multiply the resulting noise value by this number. Changes the "strength" of the noise + multiply = 1, + -- Add this number to the resulting noise value + add = 0 + -- The seed to generate the noise with. nil = randomly chosen + -- The perlin generator isn't currently seedable :-/ + -- seed = nil + } + + if not params[1] then params = { params } end + + -- If params[1] is thing, this is a list of params + -- This might be a thing if we're dealingw ith multiple octaves + for i,params_el in ipairs(params) do + local default_copy = worldeditadditions.shallowcopy(params_default) + worldeditadditions.table_apply( + params_el, + default_copy + ) + params[i] = default_copy + end + + return params +end