Minetest-WorldEditAdditions/worldeditadditions_commands/commands/layers.lua

83 lines
3.8 KiB
Lua
Raw Normal View History

2022-09-18 19:58:28 +00:00
local wea_c = worldeditadditions_core
2022-09-18 20:05:03 +00:00
local Vector3 = wea_c.Vector3
2022-09-18 19:58:28 +00:00
local function parse_slope_range(text)
if string.match(text, "%.%.") then
-- It's in the form a..b
2022-09-18 20:05:03 +00:00
local parts = wea_c.split(text, "..", true)
if not parts then return nil end
if #parts ~= 2 then return false, "Error: Exactly 2 numbers may be separated by a double dot '..' (e.g. 10..45)" end
local min_slope = tonumber(parts[1])
local max_slope = tonumber(parts[2])
if not min_slope then return false, "Error: Failed to parse the specified min_slope '"..tostring(min_slope).."' value as a number." end
if not max_slope then return false, "Error: Failed to parse the specified max_slope '"..tostring(max_slope).."' value as a number." end
-- math.rad converts degrees to radians
return true, math.rad(min_slope), math.rad(max_slope)
else
-- It's a single value
local max_slope = tonumber(text)
if not max_slope then return nil end
return true, 0, math.rad(max_slope)
end
end
-- ██████ ██ ██ ███████ ██████ ██ █████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ █████ ██████ ██ ███████ ████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██████ ████ ███████ ██ ██ ███████ ██ ██ ██
worldeditadditions_core.register_command("layers", {
params = "[<max_slope|min_slope..max_slope>] [<node_name_1> [<layer_count_1>]] [<node_name_2> [<layer_count_2>]] ...",
description = "Replaces the topmost non-airlike nodes with layers of the given nodes from top to bottom. Like WorldEdit for MC's //naturalize command. Optionally takes a maximum or minimum and maximum slope value. If a column's slope value falls outside the defined range, then it's skipped. Default: dirt_with_grass dirt 3",
privs = { worldedit = true },
require_pos = 2,
parse = function(params_text)
if not params_text or params_text == "" then
params_text = "dirt_with_grass dirt 3"
end
2022-09-18 20:05:03 +00:00
local parts = wea_c.split_shell(params_text)
local success, min_slope, max_slope
if #parts > 0 then
success, min_slope, max_slope = parse_slope_range(parts[1])
if success then
table.remove(parts, 1) -- Automatically shifts other values down
end
end
if not min_slope then min_slope = 0 end
if not max_slope then max_slope = 180 end
local node_list
2022-09-18 20:05:03 +00:00
success, node_list = wea_c.parse.weighted_nodes(
parts,
true
)
return success, node_list, min_slope, max_slope
end,
nodes_needed = function(name)
return worldedit.volume(worldedit.pos1[name], worldedit.pos2[name])
end,
func = function(name, node_list, min_slope, max_slope)
2022-09-18 20:05:03 +00:00
local start_time = wea_c.get_ms_time()
local pos1, pos2 = Vector3.sort(worldedit.pos1[name], worldedit.pos2[name])
local changes = worldeditadditions.layers(
2022-09-18 20:05:03 +00:00
pos1, pos2,
node_list,
min_slope, max_slope
)
2022-09-18 20:05:03 +00:00
local time_taken = wea_c.get_ms_time() - start_time
2021-12-28 18:37:54 +00:00
-- print("DEBUG min_slope", min_slope, "max_slope", max_slope)
-- print("DEBUG min_slope", math.deg(min_slope), "max_slope", math.deg(max_slope))
2021-08-04 11:17:39 +00:00
2022-09-18 20:05:03 +00:00
minetest.log("action", name.." used //layers at "..pos1.." - "..pos2..", replacing "..changes.replaced.." nodes and skipping "..changes.skipped_columns.." columns ("..changes.skipped_columns_slope.." due to slope constraints) in "..wea_c.format.human_time(time_taken))
return true, changes.replaced.." nodes replaced and "..changes.skipped_columns.." columns skipped ("..changes.skipped_columns_slope.." due to slope constraints) in "..wea_c.format.human_time(time_taken)
end
})