Minetest-WorldEditAdditions/worldeditadditions/lib/line.lua
2022-09-19 17:27:59 +01:00

51 lines
1.6 KiB
Lua

local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
--- Counts the nodes in a given area.
-- @module worldeditadditions.count
-- ██ ██ ███ ██ ███████
-- ██ ██ ████ ██ ██
-- ██ ██ ██ ██ ██ █████
-- ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ████ ███████
function worldeditadditions.line(pos1, pos2, thickness, node_name)
local pos1_sorted, pos2_sorted = Vector3.sort(pos1, pos2)
-- pos2 will always have the highest co-ordinates now
pos1 = Vector3.clone(pos1)
pos2 = Vector3.clone(pos2)
local node_id_replace = minetest.get_content_id(node_name)
-- Fetch the nodes in the specified area
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
-- z y x is the preferred loop order (because CPU cache I'd guess, since then we're iterating linearly through the data array)
local counts = { replaced = 0 }
for z = pos2_sorted.z, pos1_sorted.z, -1 do
for x = pos2_sorted.x, pos1_sorted.x, -1 do
for y = pos2_sorted.y, pos1_sorted.y, -1 do
local here = Vector3.new(x, y, z)
local D = (pos2 - pos1):normalise()
local d = Vector3.dot(here - pos1, D)
local closest_on_line = pos1 + (D * d)
local distance = (here - closest_on_line):length()
if distance < thickness then
data[area:index(x, y, z)] = node_id_replace
counts.replaced = counts.replaced + 1
end
end
end
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return true, counts
end