2020-05-03 01:37:18 +02:00
-- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████
2020-05-10 22:52:35 +02:00
worldedit.register_command ( " floodfill " , {
2020-05-03 01:37:18 +02:00
params = " [<replace_node> [<radius>]] " ,
description = " Floods all connected nodes of the same type starting at pos1 with <replace_node> (which defaults to `water_source`), in a sphere with a radius of <radius> (which defaults to 20). " ,
privs = { worldedit = true } ,
2020-05-10 22:52:35 +02:00
require_pos = 1 ,
parse = function ( params_text )
if not params_text then params_text = " " end
local found , _ , replace_node , radius = params_text : find ( " ([a-z:_ \\ -]+)%s+([0-9]+) " )
2020-05-03 01:37:18 +02:00
2020-05-10 22:52:35 +02:00
if found == nil then
found , _ , replace_node = params_text : find ( " ([a-z:_ \\ -]+) " )
radius = 20
2020-05-03 01:37:18 +02:00
end
2020-05-10 22:52:35 +02:00
if found == nil then
replace_node = " default:water_source "
end
radius = tonumber ( radius )
replace_node = worldedit.normalize_nodename ( replace_node )
2020-05-03 01:37:18 +02:00
2020-05-10 22:52:35 +02:00
if not replace_node then
2021-05-31 01:38:01 +02:00
return false , " Error: Invalid node name. "
2020-05-03 01:37:18 +02:00
end
2020-05-10 22:52:35 +02:00
return true , replace_node , radius
end ,
nodes_needed = function ( name , replace_node , radius )
-- Volume of a hemisphere
return math.ceil ( ( ( 4 * math.pi * ( tonumber ( radius ) ^ 3 ) ) / 3 ) / 2 )
end ,
func = function ( name , replace_node , radius )
2020-06-26 21:46:35 +02:00
local start_time = worldeditadditions.get_ms_time ( )
2020-05-11 00:29:49 +02:00
local nodes_replaced = worldeditadditions.floodfill ( worldedit.pos1 [ name ] , radius , replace_node )
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
if nodes_replaced == false then
2020-05-10 22:52:35 +02:00
return false , " Error: The search node is the same as the replace node. "
2020-05-03 01:37:18 +02:00
end
minetest.log ( " action " , name .. " used //floodfill at " .. worldeditadditions.vector . tostring ( worldedit.pos1 [ name ] ) .. " , replacing " .. nodes_replaced .. " nodes in " .. time_taken .. " s " )
2021-03-20 02:48:56 +01:00
return true , nodes_replaced .. " nodes replaced in " .. worldeditadditions.format . human_time ( time_taken )
2020-05-10 22:52:35 +02:00
end
2020-05-03 01:37:18 +02:00
} )