Bugfix new //erode river implementation

next stop: docs!
This commit is contained in:
Starbeamrainbowlabs 2021-05-30 01:42:06 +01:00
parent dc7ccf49f3
commit ccf27c8629
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 74 additions and 43 deletions

View file

@ -1,6 +1,7 @@
worldeditadditions.erode = {} worldeditadditions.erode = {}
dofile(worldeditadditions.modpath.."/lib/erode/snowballs.lua") dofile(worldeditadditions.modpath.."/lib/erode/snowballs.lua")
dofile(worldeditadditions.modpath.."/lib/erode/river.lua")
function worldeditadditions.erode.run(pos1, pos2, algorithm, params) function worldeditadditions.erode.run(pos1, pos2, algorithm, params)

View file

@ -14,8 +14,10 @@ end
function worldeditadditions.erode.river(heightmap_initial, heightmap, heightmap_size, region_height, params_custom) function worldeditadditions.erode.river(heightmap_initial, heightmap, heightmap_size, region_height, params_custom)
local params = { local params = {
steps = 1, -- Number of rounds/passes of the algorithm to run steps = 1, -- Number of rounds/passes of the algorithm to run
remove_sides = "0,1", -- Cells with this many adjacent horizontal neighbours will be removed remove_sides = "4,3", -- Cells with this many adjacent horizontal neighbours that are lower than the current pixel will be removed
fill_sides = "4,3" -- Cells with this many adjaect horizontal neighbours will be filled in fill_sides = "4,3", -- Cells with this many adjacect horizontal neighbours that are higher than the current pixel will be filled in
doraise = true, -- Whether to do raise operations or not
dolower = true -- Whether to do lower operations or not
} }
-- Apply the custom settings -- Apply the custom settings
wea.table_apply(params_custom, params) wea.table_apply(params_custom, params)
@ -27,64 +29,92 @@ function worldeditadditions.erode.river(heightmap_initial, heightmap, heightmap_
local filled = 0 local filled = 0
local removed = 0 local removed = 0
for i=1,params.steps do for i=1,params.steps do
print("[DEBUG:river] step ", i)
wea.format.array_2d(heightmap, heightmap_size.x)
local time_start = wea.get_ms_time() local time_start = wea.get_ms_time()
-- Store up changes to make and make them at the end of the step
-- This is important, because decisions
local fill = { } -- Indexes to add 1 to
local remove = { } -- Indexes to take 1 away from
for z = heightmap_size.z - 1, 0, -1 do for z = heightmap_size.z - 1, 0, -1 do
for x = heightmap_size.x - 1, 0, -1 do for x = heightmap_size.x - 1, 0, -1 do
local hi = z*heightmap_size.x + x local hi = z*heightmap_size.x + x
local thisheight = heightmap[hi] local thisheight = heightmap[hi]
print("[DEBUG:river] z", z, "x", x, "thisheight", thisheight)
local sides = 0 local height_up = heightmap[hi]
local adjacent_heights = { } local height_down = heightmap[hi]
if x > 0 then local height_left = heightmap[hi]
table.insert(adjacent_heights, heightmap[z*heightmap_size.x + x-1]) local height_down = heightmap[hi]
if heightmap[z*heightmap_size.x + x-1] >= thisheight then
sides = sides + 1 if x > 0 then height_left = heightmap[z*heightmap_size.x + x-1] end
end if x < heightmap_size.x - 1 then height_right = heightmap[z*heightmap_size.x + x+1] end
end if z > 0 then height_up = heightmap[(z-1)*heightmap_size.x + x] end
if x < heightmap_size.x - 1 then if z < heightmap_size.z - 1 then height_down = heightmap[(z+1)*heightmap_size.x + x] end
table.insert(adjacent_heights, heightmap[z*heightmap_size.x + x+1])
if heightmap[z*heightmap_size.x + x+1] >= thisheight then -- Whether this pixel is on the edge
sides = sides + 1 local isedge = x <= 0
end or z <= 0
end or x >= heightmap_size.x - 1
if z > 0 then or z >= heightmap_size.z - 1
table.insert(adjacent_heights, heightmap[(z-1)*heightmap_size.x + x])
if heightmap[(z-1)*heightmap_size.x + x] >= thisheight then local sides_higher = 0 -- Number of sides higher than this pixel
sides = sides + 1 local sides_lower = 0 -- Number of sides lower than this pixel
end if not isedge then
end if height_down > thisheight then sides_higher = sides_higher + 1 end
if z < heightmap_size.z - 1 then if height_up > thisheight then sides_higher = sides_higher + 1 end
table.insert(adjacent_heights, heightmap[(z+1)*heightmap_size.x + x]) if height_left > thisheight then sides_higher = sides_higher + 1 end
if heightmap[(z+1)*heightmap_size.x + x] >= thisheight then if height_right > thisheight then sides_higher = sides_higher + 1 end
sides = sides + 1
end if height_down < thisheight then sides_lower = sides_lower + 1 end
if height_up < thisheight then sides_lower = sides_lower + 1 end
if height_left < thisheight then sides_lower = sides_lower + 1 end
if height_right < thisheight then sides_lower = sides_lower + 1 end
end end
-- Perform an action, but only if we're not on the edge
-- This is important, as we can't accurately tell how many
-- adjacent neighbours a pixel on the edge has.
local action = "none" local action = "none"
for i,sidecount in ipairs(params.fill_sides) do if not isedge then
if sidecount == sides then if sides_higher > sides_lower then
action = "fill" for i,sidecount in ipairs(params.fill_sides) do
break if sidecount == sides_higher then
end action = "fill"
end break
for i,sidecount in ipairs(params.remove_sides) do end
if sidecount == sides then end
action = "remove" else
break for i,sidecount in ipairs(params.remove_sides) do
if sidecount == sides_lower then
action = "remove"
break
end
end
end end
end end
if action == "fill" then print("[DEBUG:river] sides_higher", sides_higher, "sides_lower", sides_lower, "action", action)
heightmap[hi] = heightmap[hi] + 1 if action == "fill" and params.doraise then
table.insert(fill, hi)
filled = filled + 1 filled = filled + 1
elseif action == "remove" then elseif action == "remove" and params.dolower then
heightmap[hi] = heightmap[hi] - 1 table.insert(remove, hi)
removed = removed + 1 removed = removed + 1
end end
wea.format.array_2d(heightmap, heightmap_size.x)
end end
end end
for i,hi in ipairs(fill) do
heightmap[hi] = heightmap[hi] + 1
end
for i,hi in ipairs(remove) do
heightmap[hi] = heightmap[hi] - 1
end
table.insert(timings, wea.get_ms_time() - time_start) table.insert(timings, wea.get_ms_time() - time_start)
end end

View file

@ -3,7 +3,7 @@
-- @param tbl number[] The ZERO-indexed list of numbers -- @param tbl number[] The ZERO-indexed list of numbers
-- @param width number The width of 2D array. -- @param width number The width of 2D array.
function worldeditadditions.format.array_2d(tbl, width) function worldeditadditions.format.array_2d(tbl, width)
print("==== count: "..#tbl..", width:"..width.." ====") print("==== count: "..(#tbl+1)..", width:"..width.." ====")
local display_width = 1 local display_width = 1
for _i,value in pairs(tbl) do for _i,value in pairs(tbl) do
display_width = math.max(display_width, #tostring(value)) display_width = math.max(display_width, #tostring(value))

View file

@ -4,7 +4,7 @@
-- ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ██████ ██████ ███████ -- ███████ ██ ██ ██████ ██████ ███████
worldedit.register_command("erode", { worldedit.register_command("erode", {
params = "[<snowballs|...> [<key_1> [<value_1>]] [<key_2> [<value_2>]] ...]", params = "[<snowballs|river> [<key_1> [<value_1>]] [<key_2> [<value_2>]] ...]",
description = "**experimental** Runs the specified erosion algorithm over the given defined region. This may occur in 2d or 3d. Currently implemented algorithms: snowballs (default;2d hydraulic-like). Also optionally takes an arbitrary set of key - value pairs representing parameters to pass to the algorithm. See the full documentation for details.", description = "**experimental** Runs the specified erosion algorithm over the given defined region. This may occur in 2d or 3d. Currently implemented algorithms: snowballs (default;2d hydraulic-like). Also optionally takes an arbitrary set of key - value pairs representing parameters to pass to the algorithm. See the full documentation for details.",
privs = { worldedit = true }, privs = { worldedit = true },
require_pos = 2, require_pos = 2,