From bbde06c72e5d783b02646ba8d4b15e4402310d3d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 22 Jun 2018 21:23:12 +0100 Subject: [PATCH] [floodfill] Bugfix: Don't get caught in an infinite loop if the search & replace nodes are identical Also fix a crash in the //floodfill command logic if pos1 is nil --- worldeditadditions/floodfill.lua | 4 ++++ worldeditadditions_commands/init.lua | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/worldeditadditions/floodfill.lua b/worldeditadditions/floodfill.lua index 168f50f..608decf 100644 --- a/worldeditadditions/floodfill.lua +++ b/worldeditadditions/floodfill.lua @@ -62,6 +62,10 @@ function worldedit.floodfill(start_pos, radius, replace_node) local replace_id = minetest.get_content_id(replace_node) local radius_sq = radius*radius + if search_id == replace_id then + return false + end + local count = 0 local remaining_nodes = Queue.new() Queue.enqueue(remaining_nodes, start_pos_index) diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua index e46f6ba..1f50a97 100644 --- a/worldeditadditions_commands/init.lua +++ b/worldeditadditions_commands/init.lua @@ -43,14 +43,28 @@ minetest.register_chatcommand("/floodfill", { return false end + if not worldedit.pos1[name] then + worldedit.player_notify(name, "Error: No pos1 defined.") + return false + end + local start_time = os.clock() local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node) local time_taken = os.clock() - start_time + if nodes_replaced == false then + worldedit.player_notify(name, "Error: The search node is the same as the replace node.") + return false + end + worldedit.player_notify(name, nodes_replaced .. " nodes replaced in " .. time_taken .. "s") minetest.log("action", name .. " used //floodfill at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. nodes_replaced .. " nodes in " .. time_taken .. "s") end, function(name, params_text) local replace_node, radius = parse_params_floodfill(params_text) + + if not worldedit.pos1[name] then + return 0 -- The actual command will send the error message to the client + end -- Volume of a hemisphere return math.ceil(((4 * math.pi * (tonumber(radius) ^ 3)) / 3) / 2) end)