2020-05-03 01:37:18 +02:00
-- ████████ ██████ ██████ ██ ██ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██████ ██ ██ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██████ ██ ██ ██████ ███████
local function parse_params_torus ( params_text )
2021-05-18 03:49:07 +02:00
local found , _ , major_radius , minor_radius , replace_node = params_text : find ( " ([0-9]+)%s+([0-9]+)%s+([a-z:_ \\ -]+) " )
2020-05-03 01:37:18 +02:00
2021-05-18 03:49:07 +02:00
if found == nil then
return nil , nil
2020-05-10 22:44:00 +02:00
end
2021-05-18 03:49:07 +02:00
major_radius = tonumber ( major_radius )
minor_radius = tonumber ( minor_radius )
replace_node = worldedit.normalize_nodename ( replace_node )
2021-05-15 03:00:40 +02:00
if not replace_node then
2021-05-18 03:49:07 +02:00
return false , " Error: Invalid replace_node. "
2021-05-15 03:00:40 +02:00
end
2021-05-18 03:49:07 +02:00
if not major_radius or major_radius < 1 then
return false , " Error: Invalid major radius (expected integer greater than 0) "
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-18 02:16:04 +02:00
local hollow = parts [ 5 ]
if hollow == " false " then hollow = false 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
2020-05-10 22:44:00 +02:00
worldedit.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 ) }
return unpack ( values )
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 )
2020-06-26 21:46:35 +02:00
local start_time = worldeditadditions.get_ms_time ( )
2021-05-18 02:16:04 +02:00
local replaced = worldeditadditions.torus ( worldedit.pos1 [ name ] , major_radius , minor_radius , target_node , axes , hollow )
2020-06-26 21:46:35 +02:00
local time_taken = worldeditadditions.get_ms_time ( ) - start_time
2020-05-03 01:37:18 +02:00
minetest.log ( " action " , name .. " used //torus at " .. worldeditadditions.vector . tostring ( worldedit.pos1 [ name ] ) .. " , replacing " .. replaced .. " nodes in " .. time_taken .. " s " )
2021-03-20 02:48:56 +01:00
return true , replaced .. " nodes replaced in " .. worldeditadditions.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?
2020-05-10 22:44:00 +02:00
worldedit.register_command ( " hollowtorus " , {
2021-05-18 03:49:07 +02:00
params = " <major_radius> <minor_radius> <replace_node> " ,
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>. " ,
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 ) }
return unpack ( values )
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 ,
func = function ( name , target_node , major_radius , minor_radius )
2020-06-26 21:46:35 +02:00
local start_time = worldeditadditions.get_ms_time ( )
2021-05-18 03:49:07 +02:00
local replaced = worldeditadditions.torus ( worldedit.pos1 [ name ] , major_radius , minor_radius , target_node , true )
2020-06-26 21:46:35 +02:00
local time_taken = worldeditadditions.get_ms_time ( ) - start_time
2020-05-03 01:37:18 +02:00
minetest.log ( " action " , name .. " used //hollowtorus at " .. worldeditadditions.vector . tostring ( worldedit.pos1 [ name ] ) .. " , replacing " .. replaced .. " nodes in " .. time_taken .. " s " )
2021-03-20 02:48:56 +01:00
return true , replaced .. " nodes replaced in " .. worldeditadditions.format . human_time ( time_taken )
2020-05-10 22:44:00 +02:00
end
2020-05-03 01:37:18 +02:00
} )