Finish initial //metaballs command implementation, but it's untested

This commit is contained in:
Starbeamrainbowlabs 2022-05-16 01:21:09 +01:00
parent 3311d80a2a
commit 722e62bae0
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
2 changed files with 75 additions and 8 deletions

@ -63,7 +63,7 @@ dofile(wea.modpath.."/lib/conv/conv.lua")
dofile(wea.modpath.."/lib/erode/erode.lua") dofile(wea.modpath.."/lib/erode/erode.lua")
dofile(wea.modpath.."/lib/noise/init.lua") dofile(wea.modpath.."/lib/noise/init.lua")
wea.sculpt = dofile(wea.modpath.."/lib/sculpt/init.lua") wea.sculpt = dofile(wea.modpath.."/lib/sculpt/init.lua")
wea.metaballs = dofile(wea.modpath.."/lib/metaballs.lua") wea.metaballs = dofile(wea.modpath.."/lib/metaballs/init.lua")
dofile(wea.modpath.."/lib/copy.lua") dofile(wea.modpath.."/lib/copy.lua")
dofile(wea.modpath.."/lib/move.lua") dofile(wea.modpath.."/lib/move.lua")

@ -7,7 +7,7 @@ local Vector3 = wea.Vector3
-- ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██
-- ██████ ██████ ██ ██ ███████ -- ██████ ██████ ██ ██ ███████
worldedit.register_command("metaballs", { worldedit.register_command("metaballs", {
params = "add <radius> | remove <index> | list | render <replace_node> | clear", params = "add <radius> | remove <index> | list | render <replace_node> [<threshold=1>] | clear",
description = "Defines and creates metaballs. After using the add subcommand to define 1 or more metaballs (uses pos1), the render subcommand can then be used to create the metaballs as nodes.", description = "Defines and creates metaballs. After using the add subcommand to define 1 or more metaballs (uses pos1), the render subcommand can then be used to create the metaballs as nodes.",
privs = { worldedit = true }, privs = { worldedit = true },
require_pos = 1, require_pos = 1,
@ -19,8 +19,18 @@ worldedit.register_command("metaballs", {
if #parts < 1 then if #parts < 1 then
return false, "Error: Not enough arguments (see /help /dome for usage information)." return false, "Error: Not enough arguments (see /help /dome for usage information)."
end end
local subcommand = parts[1] local subcommand = parts[1]
local subargs = {} local subargs = {}
if subcommand == "delete" then subcommand = "remove" end
if subcommand == "deleteall" then subcommand = "clear" end
if subcommand == "append" then subcommand = "add" end
if subcommand == "list" then subcommand = "list" end
if subcommand == "make" then subcommand = "render" end
if subcommand == "generate" then subcommand = "render" end
if subcommand == "create" then subcommand = "render" end
if subcommand == "add" then if subcommand == "add" then
local radius = tonumber(parts[2]) local radius = tonumber(parts[2])
if not radius then if not radius then
@ -41,10 +51,22 @@ worldedit.register_command("metaballs", {
table.insert(subargs, index) table.insert(subargs, index)
elseif subcommand == "render" then elseif subcommand == "render" then
local replace_node = worldedit.normalize_nodename(parts[2]) local replace_node = worldedit.normalize_nodename(parts[2])
local threshold = 1
if not replace_node then if not replace_node then
return false, "Error: Invalid replace_node '"..parts[2].."'." return false, "Error: Invalid replace_node '"..parts[2].."'."
end end
if #parts >= 3 then
threshold = tonumber(parts[3])
if not threshold then
return false, "Error: The threshold value must be a valid number (a floating-point number is ok)."
end
end
table.insert(subargs, replace_node) table.insert(subargs, replace_node)
table.insert(subargs, threshold)
elseif subcommand ~= "list" and subcommand ~= "clear" then
return false, "Error: Unknown subcommand '"..parts[1].."'."
end end
return true, subcommand, subargs return true, subcommand, subargs
@ -56,16 +78,61 @@ worldedit.register_command("metaballs", {
return 0 return 0
end end
end, end,
func = function(name, radius, replace_node, axes, hollow) func = function(name, subcommand, subargs)
local start_time = wea.get_ms_time() local start_time = wea.get_ms_time()
local message = ""
local append_time = true
if subcommand == "list" then
local success, list = wea.metaballs.list_pretty(name)
if not success then return success, list end
message = list
append_time = false
elseif subcommand == "clear" then
local success, metaballs_cleared = wea.metaballs.clear(name)
if not success then return success, metaballs_cleared end
message = tostring(metaballs_cleared).." cleared"
elseif subcommand == "remove" then
local index = subargs[1]
local success, metaballs_count = wea.metaballs.remove(name, index)
if not success then return success, metaballs_count end
message = "metaball at index "..tostring(index).." removed - "..metaballs_count.." metaballs remain"
elseif subcommand == "add" then
local pos = Vector3.clone(worldedit.pos1[name])
local radius = subargs[1]
local success, metaballs_count = wea.metaballs.add(name, pos, radius)
if not success then return success, metaballs_count end
message = "added metaball at "..pos.." with radius "..tostring(radius).." - "..metaballs_count.." metaballs are now defined"
elseif subcommand == "render" then
local replace_node = subargs[1]
local threshold = subargs[2]
local success, metaballs = wea.metaballs.list(name)
if not success then return success, metaballs end
if #metaballs < 2 then
return false, "Error: At least 2 metaballs must be defined to render them."
end
local success2, nodes_replaced = wea.metaballs.render(metaballs, replace_node, threshold)
if not success2 then return success2, nodes_replaced end
message = nodes_replaced.." nodes replaced using "..tostring(#metaballs).." metaballs"
end
local time_taken = wea.get_ms_time() - start_time local time_taken = wea.get_ms_time() - start_time
minetest.log("action", name.." used //dome+ at "..pos.." with a radius of "..tostring(radius)..", modifying "..nodes_replaced.." nodes in "..wea.format.human_time(time_taken)) if append_time then
return true, nodes_replaced.." nodes replaced "..wea.format.human_time(time_taken) message = message.." in "..wea.format.human_time(time_taken)
end
minetest.log("action", name.." used //metaballs "..subcommand.." in "..wea.format.human_time(time_taken))
return true, message
end end
}) })