Minetest-WorldEditAdditions/worldeditadditions/lib/ellipsoid2.lua

73 lines
2.3 KiB
Lua
Raw Normal View History

2022-09-19 01:18:03 +02:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2021-10-14 02:50:27 +02:00
---
-- @module worldeditadditions
2021-10-14 02:50:27 +02:00
-- ███████ ██ ██ ██ ██████ ███████ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██
-- █████ ██ ██ ██ ██████ ███████ █████
-- ██ ██ ██ ██ ██ ██ ██
-- ███████ ███████ ███████ ██ ██ ███████ ███████
function worldeditadditions.ellipsoid2(pos1, pos2, target_node, hollow)
2022-09-19 01:18:03 +02:00
pos1, pos2 = Vector3.sort(pos1, pos2)
local volume = pos2 - pos1
local volume_half = volume / 2
2021-10-14 02:50:27 +02:00
2022-09-19 01:18:03 +02:00
local radius = (pos2 - pos1) / 2
2021-10-14 02:50:27 +02:00
2022-09-25 02:57:41 +02:00
-- print("DEBUG:ellipsoid2 | pos1: "..pos1..", pos2: "..pos2..", target_node: "..target_node)
-- print("DEBUG:ellipsoid2 radius", radius)
2021-10-14 02:50:27 +02:00
-- position = { x, y, z }
2022-09-19 01:18:03 +02:00
local hollow_inner_radius = radius - 1
2021-10-14 02:50:27 +02: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-19 01:18:03 +02:00
local here = Vector3.new(x, y, z)
local pos_relative = (here - pos1) - volume_half
2021-10-14 02:50:27 +02:00
2022-09-25 02:57:41 +02:00
-- print("DEBUG pos1", pos1, "pos2", pos2, "volume_half", volume_half, "pos_relative", pos_relative)
2021-10-14 02:50:27 +02:00
-- If we're inside the ellipse, then fill it in
2022-09-19 01:18:03 +02:00
local comp = pos_relative / radius
2021-10-14 02:50:27 +02: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-19 01:18:03 +02:00
local h_comp = here / hollow_inner_radius
2021-10-14 02:50:27 +02:00
-- It's only ok to place it if it's outside our inner ellipse
2022-09-19 01:18:03 +02:00
place_ok = Vector3.dot(h_comp, h_comp) >= 1
2021-10-14 02:50:27 +02: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