2022-09-19 16:27:59 +00:00
local wea_c = worldeditadditions_core
2022-09-18 23:18:03 +00:00
local Vector3 = wea_c.Vector3
2024-01-02 16:59:51 +00:00
---
-- @module worldeditadditions
2021-01-16 18:36:17 +00:00
-- ██ ██ ███ ██ ███████
-- ██ ██ ████ ██ ██
-- ██ ██ ██ ██ ██ █████
-- ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ████ ███████
2022-09-18 19:58:28 +00:00
2023-07-06 23:42:57 +00:00
--- Counts the nodes in a given area.
-- @param pos1 Vector3 The position to start drawing the line from.
-- @param pos2 Vector3 The position to draw the line to.
-- @param thickness number The thickness of the line to draw.
-- @param node_name string The (normalised) name of the node to draw the line with.
-- @returns bool,{replaced=number} 1. A bool indicating whether the operation was successful or not.
-- 2. A table containing statistics. At present the only key in this table is `replaced`, which indicates the number of nodes replaced when drawing the line.
2021-01-16 18:36:17 +00:00
function worldeditadditions . line ( pos1 , pos2 , thickness , node_name )
2022-09-18 23:18:03 +00:00
local pos1_sorted , pos2_sorted = Vector3.sort ( pos1 , pos2 )
2021-01-16 18:36:17 +00:00
-- pos2 will always have the highest co-ordinates now
2022-09-18 23:18:03 +00:00
pos1 = Vector3.clone ( pos1 )
pos2 = Vector3.clone ( pos2 )
2021-01-16 18:36:17 +00: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-18 23:18:03 +00: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 18:36:17 +00: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