2022-09-18 18:59:57 +02:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2020-05-03 01:37:18 +02:00
-- ████████ ██████ ██████ ██ ██ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██████ ██ ██ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██████ ██ ██ ██████ ███████
local function parse_params_torus ( params_text )
2022-09-18 18:59:57 +02:00
local parts = wea_c.split_shell ( params_text )
2021-06-25 20:58:44 +02:00
2021-05-18 06:12:46 +02:00
if # parts < 1 then
return false , " Error: No replace_node specified. "
end
if # parts < 2 then
return false , " Error: No major radius specified (expected integer greater than 0). "
end
if # parts < 3 then
return false , " Error: No minor radius specified (expected integer greater than 0). "
end
2021-06-25 20:58:44 +02:00
2021-05-18 06:12:46 +02:00
local major_radius = tonumber ( parts [ 1 ] )
local minor_radius = tonumber ( parts [ 2 ] )
local replace_node = worldedit.normalize_nodename ( parts [ 3 ] )
local axes
if # parts > 3 then axes = parts [ 4 ] end
if not axes then axes = " xy " end
2021-06-25 20:58:44 +02:00
2021-05-18 06:12:46 +02:00
if major_radius < 1 then
return false , " Error: The major radius must be greater than 0. "
end
if minor_radius < 1 then
return false , " Error: The minor radius must be greater than 0. "
2020-05-10 22:44:00 +02:00
end
2021-05-15 03:00:40 +02:00
if not replace_node then
2021-05-18 06:12:46 +02:00
return false , " Error: Invalid node name. "
2021-05-15 03:00:40 +02:00
end
2021-05-18 06:12:46 +02:00
if axes : find ( " [^xyz] " ) then
return false , " Error: The axes may only contain the letters x, y, and z. "
2021-05-15 03:00:40 +02:00
end
2021-05-18 02:16:04 +02:00
if # axes > 2 then
return false , " Error: 2 or less axes must be specified. For example, xy is valid, but xzy is not. "
2021-05-15 03:00:40 +02:00
end
2021-05-17 18:13:43 +02:00
local hollow = false
if parts [ 5 ] == " hollow " or parts [ 5 ] == " h " then
hollow = true
end
2021-05-15 03:00:40 +02:00
-- Sort the axis names (this is important to ensure we can identify the direction properly)
if axes == " yx " then axes = " xy " end
if axes == " zx " then axes = " xz " end
if axes == " zy " then axes = " yz " end
2021-05-18 02:16:04 +02:00
return true , replace_node , major_radius , minor_radius , axes , hollow
2020-05-03 01:37:18 +02:00
end
2022-05-19 23:10:09 +02:00
worldeditadditions_core.register_command ( " torus " , {
2021-05-18 05:33:08 +02:00
params = " <major_radius> <minor_radius> <replace_node> [<axes=xy> [h[ollow]]] " ,
2021-05-15 03:00:40 +02:00
description = " Creates a 3D torus with a major radius of <major_radius> and a minor radius of <minor_radius> at pos1, filled with <replace_node>, on axes <axes> (i.e. 2 axis names: xz, zy, etc). " ,
2020-05-03 01:37:18 +02:00
privs = { worldedit = true } ,
2020-05-10 22:44:00 +02:00
require_pos = 1 ,
parse = function ( params_text )
local values = { parse_params_torus ( params_text ) }
2022-09-18 18:59:57 +02:00
return wea_c.table . unpack ( values )
2020-05-10 22:44:00 +02:00
end ,
nodes_needed = function ( name , target_node , major_radius , minor_radius )
return math.ceil ( 2 * math.pi * math.pi * major_radius * minor_radius * minor_radius )
end ,
2021-05-18 02:16:04 +02:00
func = function ( name , target_node , major_radius , minor_radius , axes , hollow )
2022-09-18 18:59:57 +02:00
local start_time = wea_c.get_ms_time ( )
local pos1 = Vector3.clone ( worldedit.pos1 [ name ] )
2021-05-17 18:13:43 +02:00
local replaced = worldeditadditions.torus (
2022-09-18 18:59:57 +02:00
pos1 ,
2021-05-17 18:13:43 +02:00
major_radius , minor_radius ,
target_node ,
axes ,
hollow
)
2022-09-18 18:59:57 +02:00
local time_taken = wea_c.get_ms_time ( ) - start_time
2020-05-03 01:37:18 +02:00
2022-09-18 18:59:57 +02:00
minetest.log ( " action " , name .. " used //torus at " .. pos1 .. " , replacing " .. replaced .. " nodes in " .. time_taken .. " s " )
return true , replaced .. " nodes replaced in " .. wea_c.format . human_time ( time_taken )
2020-05-10 22:44:00 +02:00
end
2020-05-03 01:37:18 +02:00
} )
-- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit?
2022-05-19 23:10:09 +02:00
worldeditadditions_core.register_command ( " hollowtorus " , {
2021-05-18 06:12:46 +02:00
params = " <major_radius> <minor_radius> <replace_node> [<axes=xy>] " ,
description = " Creates a 3D hollow torus with a major radius of <major_radius> and a minor radius of <minor_radius> at pos1, made out of <replace_node>, on axes <axes> (i.e. 2 axis names: xz, zy, etc). " ,
2020-05-03 01:37:18 +02:00
privs = { worldedit = true } ,
2020-05-10 22:44:00 +02:00
require_pos = 1 ,
parse = function ( params_text )
local values = { parse_params_torus ( params_text ) }
2022-09-19 18:34:53 +02:00
return wea_c.table . unpack ( values )
2020-05-10 22:44:00 +02:00
end ,
2021-05-18 03:49:07 +02:00
nodes_needed = function ( name , target_node , major_radius , minor_radius )
2020-05-10 22:44:00 +02:00
return math.ceil ( 2 * math.pi * math.pi * major_radius * minor_radius * minor_radius )
end ,
2021-05-17 18:13:43 +02:00
func = function ( name , target_node , major_radius , minor_radius , axes )
2022-09-19 18:34:53 +02:00
local start_time = wea_c.get_ms_time ( )
2022-09-19 18:33:02 +02:00
local pos1 = Vector3.clone ( worldedit.pos1 [ name ] )
2021-05-17 18:13:43 +02:00
local replaced = worldeditadditions.torus (
2022-09-19 18:33:02 +02:00
pos1 ,
2021-05-17 18:13:43 +02:00
major_radius , minor_radius ,
target_node ,
axes ,
true -- hollow
)
2022-09-19 18:34:53 +02:00
local time_taken = wea_c.get_ms_time ( ) - start_time
2020-05-03 01:37:18 +02:00
2022-09-19 18:33:02 +02:00
minetest.log ( " action " , name .. " used //hollowtorus at " .. pos1 .. " , replacing " .. replaced .. " nodes in " .. time_taken .. " s " )
2022-09-19 18:34:53 +02:00
return true , replaced .. " nodes replaced in " .. wea_c.format . human_time ( time_taken )
2020-05-10 22:44:00 +02:00
end
2020-05-03 01:37:18 +02:00
} )