//layers: fix

This commit is contained in:
Starbeamrainbowlabs 2021-08-04 12:17:39 +01:00
parent db830c6633
commit ef678e6a05
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 23 additions and 6 deletions

View File

@ -6,6 +6,15 @@
local wea = worldeditadditions
local function print_slopes(slopemap, width)
local copy = wea.table.shallowcopy(slopemap)
for key,value in pairs(copy) do
copy[key] = wea.round(math.deg(value), 2)
end
worldeditadditions.format.array_2d(copy, width)
end
--- Replaces the non-air nodes in each column with a list of nodes from top to bottom.
-- @param pos1 Vector Position 1 of the region to operate on
-- @param pos2 Vector Position 2 of the region to operate on
@ -13,7 +22,7 @@ local wea = worldeditadditions
-- @param min_slope number?
-- @param max_slope number?
function worldeditadditions.layers(pos1, pos2, node_weights, min_slope, max_slope)
pos1, pos2 = wea.vector3.sort(pos1, pos2)
pos1, pos2 = wea.Vector3.sort(pos1, pos2)
if not min_slope then min_slope = math.rad(0) end
if not max_slope then max_slope = math.rad(180) end
-- pos2 will always have the highest co-ordinates now
@ -31,9 +40,12 @@ function worldeditadditions.layers(pos1, pos2, node_weights, min_slope, max_slop
manip, area, data
)
local slopemap = wea.calculate_slopes(heightmap, heightmap_size)
worldeditadditions.format.array_2d(heightmap, heightmap_size.x)
print_slopes(slopemap, heightmap_size.x)
--luacheck:ignore 311
heightmap = nil -- Just in case Lua wants to garbage collect it
-- minetest.log("action", "pos1: " .. wea.vector.tostring(pos1))
-- minetest.log("action", "pos2: " .. wea.vector.tostring(pos2))
-- for i,v in ipairs(node_ids) do
@ -47,7 +59,9 @@ function worldeditadditions.layers(pos1, pos2, node_weights, min_slope, max_slop
local next_index = 1 -- We use table.insert() in make_weighted
local placed_node = false
local hi = z*heightmap_size.x + x
local hi = (z-pos1.z)*heightmap_size.x + (x-pos1.x)
-- print("DEBUG hi", hi, "x", x, "z", z, "slope", slopemap[hi], "as deg", math.deg(slopemap[hi]))
-- Again, Lua 5.1 doesn't have a continue statement :-/
if slopemap[hi] >= min_slope and slopemap[hi] <= max_slope then

View File

@ -73,7 +73,7 @@ function worldeditadditions.calculate_normals(heightmap, heightmap_size)
-- print("[normals] LEFT | index", z*heightmap_size.x + (x-1), "x", x, "x-1", x - 1, "left", left, "limit", 0)
-- print("[normals] RIGHT | index", z*heightmap_size.x + (x+1), "x", x, "x+1", x + 1, "right", right, "limit", heightmap_size.x-1)
result[hi] = wea.vector3.new(
result[hi] = wea.Vector3.new(
left - right, -- x
2, -- y - Z & Y are flipped
down - up -- z
@ -95,14 +95,15 @@ function worldeditadditions.calculate_slopes(heightmap, heightmap_size)
local normals = worldeditadditions.calculate_normals(heightmap, heightmap_size)
local slopes = { }
local up = wea.vector3(0, 1, 0) -- Z & Y are flipped
local up = wea.Vector3.new(0, 1, 0) -- Z & Y are flipped
for z = heightmap_size.z-1, 0, -1 do
for x = heightmap_size.x-1, 0, -1 do
local hi = z*heightmap_size.x + x
-- Ref https://stackoverflow.com/a/16669463/1460422
slopes[hi] = wea.vector3.dot_product(normals[hi], up)
-- slopes[hi] = wea.Vector3.dot_product(normals[hi], up)
slopes[hi] = math.acos(normals[hi].y)
end
end

View File

@ -49,7 +49,6 @@ worldedit.register_command("layers", {
if not min_slope then min_slope = 0 end
if not max_slope then max_slope = 180 end
local node_list
success, node_list = worldeditadditions.parse.weighted_nodes(
parts,
@ -69,6 +68,9 @@ worldedit.register_command("layers", {
)
local time_taken = worldeditadditions.get_ms_time() - start_time
print("DEBUG min_slope", min_slope, "max_slope", max_slope)
print("DEBUG min_slope", math.deg(min_slope), "max_slope", math.deg(max_slope))
minetest.log("action", name .. " used //layers at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. changes.replaced .. " nodes and skipping " .. changes.skipped_columns .. " columns ("..changes.skipped_columns_slope.." due to slope constraints) in " .. time_taken .. "s")
return true, changes.replaced .. " nodes replaced and " .. changes.skipped_columns .. " columns skipped ("..changes.skipped_columns_slope.." due to slope constraints) in " .. worldeditadditions.format.human_time(time_taken)
end