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.
This commit is contained in:
Starbeamrainbowlabs 2021-03-16 21:39:12 +00:00
parent 237c4839c8
commit a1911037d2
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 71 additions and 4 deletions

View file

@ -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

View file

@ -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<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

View file

@ -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")

View file

@ -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