2022-05-16 00:01:01 +00:00
local wea = worldeditadditions
2022-09-18 19:58:28 +00:00
local wea_c = worldeditadditions_core
2023-07-11 17:55:34 +00:00
local Vector3 = wea_c.Vector3
2022-05-16 00:01:01 +00:00
-- ██████ ██████ ███ ███ ███████
-- ██ ██ ██ ██ ████ ████ ██
-- ██ ██ ██ ██ ██ ████ ██ █████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- ██████ ██████ ██ ██ ███████
2022-05-19 21:10:09 +00:00
worldeditadditions_core.register_command ( " metaball " , {
2022-05-16 19:14:31 +00:00
params = " add <radius> | remove <index> | render <replace_node> [<threshold=1>] | list | clear | volume " ,
2022-05-16 00:01:01 +00:00
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 } ,
require_pos = 1 ,
parse = function ( params_text )
if not params_text then params_text = " " end
2022-09-18 19:58:28 +00:00
local parts = wea_c.split_shell ( params_text )
2022-05-16 00:01:01 +00:00
if # parts < 1 then
2023-07-11 18:14:59 +00:00
return false , " Error: Not enough arguments (see /help /metaball for usage information). "
2022-05-16 00:01:01 +00:00
end
2022-05-16 00:21:09 +00:00
2022-05-16 00:01:01 +00:00
local subcommand = parts [ 1 ]
local subargs = { }
2022-05-16 00:21:09 +00:00
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
2022-05-16 00:01:01 +00:00
if subcommand == " add " then
2022-05-16 19:14:31 +00:00
if # parts < 2 then
return false , " Error: Not enough arguments. "
end
2022-05-16 00:01:01 +00:00
local radius = tonumber ( parts [ 2 ] )
if not radius then
2022-05-16 19:14:31 +00:00
return false , " Error: Invalid radius ' " .. parts [ 2 ] .. " '. The radius must be a positive integer. "
2022-05-16 00:01:01 +00:00
end
if radius < 1 then
return false , " Error: The minimum radius size is 1, but you entered " .. tostring ( radius ) .. " . "
end
table.insert ( subargs , radius )
elseif subcommand == " remove " then
2022-05-16 19:14:31 +00:00
if # parts < 2 then
return false , " Error: Not enough arguments. "
end
2022-05-16 00:01:01 +00:00
local index = tonumber ( parts [ 2 ] )
if not index then
2022-05-16 19:14:31 +00:00
return false , " Error: Invalid index ' " .. parts [ 2 ] .. " '. The index to remove must be a positive integer. "
2022-05-16 00:01:01 +00:00
end
if index < 1 then
return false , " Error: The minimum index size is 1, but you entered " .. tostring ( index ) .. " . "
end
table.insert ( subargs , index )
elseif subcommand == " render " then
2022-05-16 19:14:31 +00:00
if # parts < 2 then
return false , " Error: Not enough arguments. "
end
2022-05-16 00:01:01 +00:00
local replace_node = worldedit.normalize_nodename ( parts [ 2 ] )
2022-05-16 00:21:09 +00:00
local threshold = 1
2022-05-16 00:01:01 +00:00
if not replace_node then
return false , " Error: Invalid replace_node ' " .. parts [ 2 ] .. " '. "
end
2022-05-16 00:21:09 +00:00
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
2022-05-16 00:01:01 +00:00
table.insert ( subargs , replace_node )
2022-05-16 00:21:09 +00:00
table.insert ( subargs , threshold )
2022-05-16 19:14:31 +00:00
elseif subcommand ~= " list " and subcommand ~= " clear " and subcommand ~= " volume " then
2022-05-16 00:21:09 +00:00
return false , " Error: Unknown subcommand ' " .. parts [ 1 ] .. " '. "
2022-05-16 00:01:01 +00:00
end
return true , subcommand , subargs
end ,
nodes_needed = function ( name , subcommand )
if subcommand == " render " then
2023-07-11 18:14:59 +00:00
local success , value = wea.metaballs . volume ( name )
if not success then
2024-10-16 23:27:33 +00:00
return false , value
2023-07-11 18:14:59 +00:00
end
return value
2022-05-16 00:01:01 +00:00
else
return 0
end
end ,
2022-05-16 00:21:09 +00:00
func = function ( name , subcommand , subargs )
2022-09-18 19:58:28 +00:00
local start_time = wea_c.get_ms_time ( )
2022-05-16 00:21:09 +00:00
local message = " "
2022-05-16 19:14:31 +00:00
local append_time = false
2022-05-16 00:21:09 +00:00
if subcommand == " list " then
local success , list = wea.metaballs . list_pretty ( name )
if not success then return success , list end
message = list
2022-05-16 19:14:31 +00:00
elseif subcommand == " volume " then
local success , metaballs_list = wea.metaballs . list ( name )
if not success then return success , metaballs_list end
local success2 , volume = wea.metaballs . volume ( name )
if not success2 then return success2 , volume end
2023-07-11 18:14:59 +00:00
message = # metaballs_list .. " metaballs will take up to " .. tostring ( volume ) .. " nodes of space "
2022-05-16 00:21:09 +00:00
elseif subcommand == " clear " then
2023-07-11 18:14:59 +00:00
local metaballs_cleared = wea.metaballs . clear ( name )
2023-07-11 18:30:43 +00:00
message = tostring ( metaballs_cleared ) .. " metaballs cleared "
2022-05-16 00:21:09 +00:00
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
2023-07-11 18:14:59 +00:00
message = " metaball at index " .. tostring ( index ) .. " removed - " .. tostring ( metaballs_count ) .. " metaballs remain "
2022-05-16 00:21:09 +00:00
elseif subcommand == " add " then
2023-07-11 18:14:59 +00:00
local pos = Vector3.clone ( worldedit.pos1 [ name ] ) : round ( )
2022-05-16 00:21:09 +00:00
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 "
2022-05-16 19:14:31 +00:00
append_time = false
2022-05-16 00:21:09 +00:00
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 "
2022-05-16 19:14:31 +00:00
append_time = true
2022-05-16 00:21:09 +00:00
end
2022-05-16 00:01:01 +00:00
2022-09-18 19:58:28 +00:00
local time_taken = wea_c.get_ms_time ( ) - start_time
2022-05-16 00:01:01 +00:00
2022-05-16 00:21:09 +00:00
if append_time then
2022-09-18 19:58:28 +00:00
message = message .. " in " .. wea_c.format . human_time ( time_taken )
2022-05-16 00:21:09 +00:00
end
2022-09-18 19:58:28 +00:00
minetest.log ( " action " , name .. " used //metaballs " .. subcommand .. " in " .. wea_c.format . human_time ( time_taken ) )
2022-05-16 00:21:09 +00:00
return true , message
2022-05-16 00:01:01 +00:00
end
} )