Minetest-WorldEditAdditions/worldeditadditions/lib/ellipsoid2.lua

70 lines
2.2 KiB
Lua
Raw Normal View History

2022-09-18 23:18:03 +00:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2021-10-14 00:50:27 +00:00
-- ███████ ██ ██ ██ ██████ ███████ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- █████ ██ ██ ██ ██████ ███████ █████
-- ██ ██ ██ ██ ██ ██ ██
-- ███████ ███████ ███████ ██ ██ ███████ ███████
function worldeditadditions.ellipsoid2(pos1, pos2, target_node, hollow)
2022-09-18 23:18:03 +00:00
pos1, pos2 = Vector3.sort(pos1, pos2)
local volume = pos2 - pos1
local volume_half = volume / 2
2021-10-14 00:50:27 +00:00
2022-09-18 23:18:03 +00:00
local radius = (pos2 - pos1) / 2
2021-10-14 00:50:27 +00:00
2022-09-25 00:57:41 +00:00
-- print("DEBUG:ellipsoid2 | pos1: "..pos1..", pos2: "..pos2..", target_node: "..target_node)
-- print("DEBUG:ellipsoid2 radius", radius)
2021-10-14 00:50:27 +00:00
-- position = { x, y, z }
2022-09-18 23:18:03 +00:00
local hollow_inner_radius = radius - 1
2021-10-14 00:50:27 +00:00
-- Fetch the nodes in the specified area
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
local node_id = minetest.get_content_id(target_node)
local count = 0 -- The number of nodes replaced
for z = pos2.z, pos1.z, -1 do
for y = pos2.y, pos1.y, -1 do
for x = pos2.x, pos1.x, -1 do
2022-09-18 23:18:03 +00:00
local here = Vector3.new(x, y, z)
local pos_relative = (here - pos1) - volume_half
2021-10-14 00:50:27 +00:00
2022-09-25 00:57:41 +00:00
-- print("DEBUG pos1", pos1, "pos2", pos2, "volume_half", volume_half, "pos_relative", pos_relative)
2021-10-14 00:50:27 +00:00
-- If we're inside the ellipse, then fill it in
2022-09-18 23:18:03 +00:00
local comp = pos_relative / radius
2021-10-14 00:50:27 +00:00
local ellipsoid_dist = comp:length_squared()
if ellipsoid_dist <= 1 then
local place_ok = not hollow;
if not place_ok then
-- It must be hollow! Do some additional calculations.
2022-09-18 23:18:03 +00:00
local h_comp = here / hollow_inner_radius
2021-10-14 00:50:27 +00:00
-- It's only ok to place it if it's outside our inner ellipse
2022-09-18 23:18:03 +00:00
place_ok = Vector3.dot(h_comp, h_comp) >= 1
2021-10-14 00:50:27 +00:00
end
if place_ok then
data[area:index(x, y, z)] = node_id
count = count + 1
end
end
end
end
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return count
end