Minetest-WorldEditAdditions/worldeditadditions/lib/line.lua

51 lines
1.6 KiB
Lua
Raw Normal View History

local wea_c = worldeditadditions_core
2022-09-19 01:18:03 +02:00
local Vector3 = wea_c.Vector3
2021-01-16 19:36:17 +01:00
--- Counts the nodes in a given area.
-- @module worldeditadditions.count
-- ██ ██ ███ ██ ███████
-- ██ ██ ████ ██ ██
-- ██ ██ ██ ██ ██ █████
-- ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ████ ███████
2022-09-18 21:58:28 +02:00
2021-01-16 19:36:17 +01:00
function worldeditadditions.line(pos1, pos2, thickness, node_name)
2022-09-19 01:18:03 +02:00
local pos1_sorted, pos2_sorted = Vector3.sort(pos1, pos2)
2021-01-16 19:36:17 +01:00
-- pos2 will always have the highest co-ordinates now
2022-09-19 01:18:03 +02:00
pos1 = Vector3.clone(pos1)
pos2 = Vector3.clone(pos2)
2021-01-16 19:36:17 +01:00
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
2022-09-19 01:18:03 +02:00
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()
2021-01-16 19:36:17 +01:00
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