Minetest-WorldEditAdditions/worldeditadditions/lib/floodfill.lua

86 lines
3.4 KiB
Lua
Raw Normal View History

--- Flood-fill command for complex lakes etc.
-- @module worldeditadditions.floodfill
local wea = worldeditadditions
2020-05-11 00:29:49 +02:00
function worldeditadditions.floodfill(start_pos, radius, replace_node)
-- Calculate the area we want to modify
local pos1 = vector.add(start_pos, { x = radius, y = 0, z = radius })
local pos2 = vector.subtract(start_pos, { x = radius, y = radius, z = radius })
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
-- pos2 will always have the highest co-ordinates now
-- Fetch the nodes in the specified area
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
-- Setup for the floodfill operation itself
2018-05-20 15:19:43 +02:00
local start_pos_index = area:index(start_pos.x, start_pos.y, start_pos.z)
local search_id = data[start_pos_index]
local replace_id = minetest.get_content_id(replace_node)
2018-05-20 15:19:43 +02:00
local radius_sq = radius*radius
if search_id == replace_id then
return false
end
local count = 0
2021-08-07 03:31:21 +02:00
local remaining_nodes = wea.Queue.new() remaining_nodes:enqueue(start_pos_index)
-- Do the floodfill
while remaining_nodes:is_empty() == false do
local cur = remaining_nodes:dequeue()
-- Replace this node
data[cur] = replace_id
count = count + 1
2018-05-20 14:05:03 +02:00
-- Check all the nearby nodes
-- We don't need to go upwards here, since we're filling in lake-style
2018-05-20 14:05:03 +02:00
local xplus = cur + 1 -- +X
2018-05-20 15:19:43 +02:00
if data[xplus] == search_id and
worldeditadditions.vector.lengthsquared(vector.subtract(area:position(xplus), start_pos)) < radius_sq and
not remaining_nodes:contains(xplus) then
2018-05-20 14:05:03 +02:00
-- minetest.log("action", "[floodfill] [+X] index " .. xplus .. " is a " .. data[xplus] .. ", searching for a " .. search_id)
remaining_nodes:enqueue(xplus)
end
2018-05-20 14:05:03 +02:00
local xminus = cur - 1 -- -X
2018-05-20 15:19:43 +02:00
if data[xminus] == search_id and
worldeditadditions.vector.lengthsquared(vector.subtract(area:position(xminus), start_pos)) < radius_sq and
not remaining_nodes:contains(xminus) then
2018-05-20 14:05:03 +02:00
-- minetest.log("action", "[floodfill] [-X] index " .. xminus .. " is a " .. data[xminus] .. ", searching for a " .. search_id)
remaining_nodes:enqueue(xminus)
end
2018-05-20 14:05:03 +02:00
local zplus = cur + area.zstride -- +Z
2018-05-20 15:19:43 +02:00
if data[zplus] == search_id and
worldeditadditions.vector.lengthsquared(vector.subtract(area:position(zplus), start_pos)) < radius_sq and
not remaining_nodes:contains(zplus) then
2018-05-20 14:05:03 +02:00
-- minetest.log("action", "[floodfill] [+Z] index " .. zplus .. " is a " .. data[zplus] .. ", searching for a " .. search_id)
remaining_nodes:enqueue(zplus)
end
2018-05-20 14:05:03 +02:00
local zminus = cur - area.zstride -- -Z
2018-05-20 15:19:43 +02:00
if data[zminus] == search_id and
worldeditadditions.vector.lengthsquared(vector.subtract(area:position(zminus), start_pos)) < radius_sq and
not remaining_nodes:contains(zminus) then
2018-05-20 14:05:03 +02:00
-- minetest.log("action", "[floodfill] [-Z] index " .. zminus .. " is a " .. data[zminus] .. ", searching for a " .. search_id)
remaining_nodes:enqueue(zminus)
end
2018-05-20 14:05:03 +02:00
local yminus = cur - area.ystride -- -Y
2018-05-20 15:19:43 +02:00
if data[yminus] == search_id and
worldeditadditions.vector.lengthsquared(vector.subtract(area:position(yminus), start_pos)) < radius_sq and
not remaining_nodes:contains(yminus) then
2018-05-20 14:05:03 +02:00
-- minetest.log("action", "[floodfill] [-Y] index " .. yminus .. " is a " .. data[yminus] .. ", searching for a " .. search_id)
remaining_nodes:enqueue(yminus)
end
2018-05-20 14:05:03 +02:00
count = count + 1
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return count
end