2014-10-30 03:47:08 +01:00
|
|
|
--- Functions for creating primitive shapes.
|
|
|
|
-- @module worldedit.primitives
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local mh = worldedit.manip_helpers
|
2013-07-12 20:22:30 +02:00
|
|
|
|
|
|
|
|
2017-08-31 19:07:51 +02:00
|
|
|
--- Adds a cube
|
|
|
|
-- @param pos Position of ground level center of cube
|
|
|
|
-- @param width Cube width. (x)
|
|
|
|
-- @param height Cube height. (y)
|
|
|
|
-- @param length Cube length. (z)
|
|
|
|
-- @param node_name Name of node to make cube of.
|
|
|
|
-- @param hollow Whether the cube should be hollow.
|
|
|
|
-- @return The number of nodes added.
|
|
|
|
function worldedit.cube(pos, width, height, length, node_name, hollow)
|
|
|
|
-- Set up voxel manipulator
|
2020-02-12 19:35:51 +01:00
|
|
|
local basepos = vector.subtract(pos,
|
|
|
|
{x = math.floor(width / 2), y = 0, z = math.floor(length / 2)})
|
|
|
|
local endpos = vector.add(basepos, {x=width, y=height, z=length})
|
|
|
|
local manip, area = mh.init(basepos, endpos)
|
2017-08-31 19:07:51 +02:00
|
|
|
local data = mh.get_empty_data(area)
|
|
|
|
|
|
|
|
-- Add cube
|
|
|
|
local node_id = minetest.get_content_id(node_name)
|
|
|
|
local count = 0
|
2020-02-12 19:35:51 +01:00
|
|
|
local iterfunc
|
|
|
|
if hollow then
|
|
|
|
iterfunc = mh.iterp_hollowcuboid(area, basepos, endpos)
|
|
|
|
else
|
|
|
|
iterfunc = area:iter(basepos, endpos)
|
|
|
|
end
|
2017-08-31 19:07:51 +02:00
|
|
|
|
2020-02-12 19:35:51 +01:00
|
|
|
for vi in iterfunc do
|
|
|
|
data[vi] = node_id
|
|
|
|
count = count+1
|
2017-08-31 19:07:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
mh.finish(manip, data)
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Adds a sphere of `node_name` centered at `pos`.
|
|
|
|
-- @param pos Position to center sphere at.
|
|
|
|
-- @param radius Sphere radius.
|
|
|
|
-- @param node_name Name of node to make shere of.
|
|
|
|
-- @param hollow Whether the sphere should be hollow.
|
|
|
|
-- @return The number of nodes added.
|
|
|
|
function worldedit.sphere(pos, radius, node_name, hollow)
|
|
|
|
local manip, area = mh.init_radius(pos, radius)
|
2012-10-14 03:45:50 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local data = mh.get_empty_data(area)
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Fill selected area with node
|
|
|
|
local node_id = minetest.get_content_id(node_name)
|
|
|
|
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
|
|
|
local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
|
|
|
|
local stride_z, stride_y = area.zstride, area.ystride
|
2013-06-25 00:21:48 +02:00
|
|
|
local count = 0
|
2013-07-28 03:34:42 +02:00
|
|
|
for z = -radius, radius do
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Offset contributed by z plus 1 to make it 1-indexed
|
|
|
|
local new_z = (z + offset_z) * stride_z + 1
|
2013-07-28 03:34:42 +02:00
|
|
|
for y = -radius, radius do
|
2014-10-30 03:47:08 +01:00
|
|
|
local new_y = new_z + (y + offset_y) * stride_y
|
2013-07-28 03:34:42 +02:00
|
|
|
for x = -radius, radius do
|
2014-10-30 03:47:08 +01:00
|
|
|
local squared = x * x + y * y + z * z
|
|
|
|
if squared <= max_radius and (not hollow or squared >= min_radius) then
|
|
|
|
-- Position is on surface of sphere
|
|
|
|
local i = new_y + (x + offset_x)
|
|
|
|
data[i] = node_id
|
2013-07-28 03:34:42 +02:00
|
|
|
count = count + 1
|
|
|
|
end
|
2013-04-28 00:28:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-12 20:22:30 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-07-12 20:22:30 +02:00
|
|
|
|
2013-06-25 00:21:48 +02:00
|
|
|
return count
|
2013-04-28 00:28:20 +02:00
|
|
|
end
|
|
|
|
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Adds a dome.
|
|
|
|
-- @param pos Position to center dome at.
|
|
|
|
-- @param radius Dome radius. Negative for concave domes.
|
|
|
|
-- @param node_name Name of node to make dome of.
|
|
|
|
-- @param hollow Whether the dome should be hollow.
|
|
|
|
-- @return The number of nodes added.
|
|
|
|
-- TODO: Add axis option.
|
|
|
|
function worldedit.dome(pos, radius, node_name, hollow)
|
|
|
|
local min_y, max_y = 0, radius
|
2013-07-29 18:43:24 +02:00
|
|
|
if radius < 0 then
|
|
|
|
radius = -radius
|
2014-10-30 03:47:08 +01:00
|
|
|
min_y, max_y = -radius, 0
|
2013-07-29 18:43:24 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local manip, area = mh.init_axis_radius(pos, "y", radius)
|
|
|
|
local data = mh.get_empty_data(area)
|
|
|
|
|
|
|
|
-- Add dome
|
|
|
|
local node_id = minetest.get_content_id(node_name)
|
2013-07-12 20:22:30 +02:00
|
|
|
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
2014-10-30 03:47:08 +01:00
|
|
|
local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
|
|
|
|
local stride_z, stride_y = area.zstride, area.ystride
|
2013-06-25 00:21:48 +02:00
|
|
|
local count = 0
|
2013-07-28 03:34:42 +02:00
|
|
|
for z = -radius, radius do
|
2014-10-30 03:47:08 +01:00
|
|
|
local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed
|
|
|
|
for y = min_y, max_y do
|
|
|
|
local new_y = new_z + (y + offset_y) * stride_y
|
2013-07-28 03:34:42 +02:00
|
|
|
for x = -radius, radius do
|
|
|
|
local squared = x * x + y * y + z * z
|
2014-10-30 03:47:08 +01:00
|
|
|
if squared <= max_radius and (not hollow or squared >= min_radius) then
|
|
|
|
-- Position is in dome
|
|
|
|
local i = new_y + (x + offset_x)
|
|
|
|
data[i] = node_id
|
2013-07-28 03:34:42 +02:00
|
|
|
count = count + 1
|
|
|
|
end
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-12 20:22:30 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-07-12 20:22:30 +02:00
|
|
|
|
2013-06-25 00:21:48 +02:00
|
|
|
return count
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Adds a cylinder.
|
|
|
|
-- @param pos Position to center base of cylinder at.
|
|
|
|
-- @param axis Axis ("x", "y", or "z")
|
|
|
|
-- @param length Cylinder length.
|
2012-10-19 03:10:30 +02:00
|
|
|
-- @param radius1 Cylinder base radius.
|
|
|
|
-- @param radius2 Cylinder top radius.
|
2014-10-30 03:47:08 +01:00
|
|
|
-- @param node_name Name of node to make cylinder of.
|
|
|
|
-- @param hollow Whether the cylinder should be hollow.
|
|
|
|
-- @return The number of nodes added.
|
2012-10-19 03:10:30 +02:00
|
|
|
function worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)
|
2014-10-30 03:47:08 +01:00
|
|
|
local other1, other2 = worldedit.get_axis_others(axis)
|
|
|
|
|
2012-10-19 03:10:30 +02:00
|
|
|
-- Backwards compatibility
|
|
|
|
if type(radius2) == "string" then
|
|
|
|
hollow = node_name
|
|
|
|
node_name = radius2
|
|
|
|
radius2 = radius1 -- straight cylinder
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Handle negative lengths
|
2023-06-09 14:28:29 +02:00
|
|
|
local current_pos = vector.new(pos)
|
2012-10-14 03:45:50 +02:00
|
|
|
if length < 0 then
|
|
|
|
length = -length
|
2014-10-30 03:47:08 +01:00
|
|
|
current_pos[axis] = current_pos[axis] - length
|
2012-10-19 03:10:30 +02:00
|
|
|
radius1, radius2 = radius2, radius1
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
2013-06-23 18:46:59 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Set up voxel manipulator
|
2012-10-19 03:10:30 +02:00
|
|
|
local manip, area = mh.init_axis_radius_length(current_pos, axis, math.max(radius1, radius2), length)
|
2014-10-30 03:47:08 +01:00
|
|
|
local data = mh.get_empty_data(area)
|
2012-10-14 03:45:50 +02:00
|
|
|
|
2012-10-19 03:10:30 +02:00
|
|
|
-- Add desired shape (anything inbetween cylinder & cone)
|
2014-10-30 03:47:08 +01:00
|
|
|
local node_id = minetest.get_content_id(node_name)
|
2023-06-09 14:28:29 +02:00
|
|
|
local stride = vector.new(1, area.ystride, area.zstride)
|
2014-10-30 03:47:08 +01:00
|
|
|
local offset = {
|
|
|
|
x = current_pos.x - area.MinEdge.x,
|
|
|
|
y = current_pos.y - area.MinEdge.y,
|
|
|
|
z = current_pos.z - area.MinEdge.z,
|
|
|
|
}
|
2013-06-23 18:46:59 +02:00
|
|
|
local count = 0
|
2012-10-19 03:10:30 +02:00
|
|
|
for i = 0, length - 1 do
|
|
|
|
-- Calulate radius for this "height" in the cylinder
|
2017-09-03 20:11:31 +02:00
|
|
|
local radius = radius1 + (radius2 - radius1) * (i + 1) / length
|
2012-10-19 03:10:30 +02:00
|
|
|
radius = math.floor(radius + 0.5) -- round
|
|
|
|
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
|
|
|
|
|
|
|
for index2 = -radius, radius do
|
|
|
|
-- Offset contributed by other axis 1 plus 1 to make it 1-indexed
|
|
|
|
local new_index2 = (index2 + offset[other1]) * stride[other1] + 1
|
|
|
|
for index3 = -radius, radius do
|
|
|
|
local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]
|
|
|
|
local squared = index2 * index2 + index3 * index3
|
|
|
|
if squared <= max_radius and (not hollow or squared >= min_radius) then
|
|
|
|
-- Position is in cylinder, add node here
|
|
|
|
local vi = new_index3 + (offset[axis] + i) * stride[axis]
|
2014-10-30 03:47:08 +01:00
|
|
|
data[vi] = node_id
|
2012-10-19 03:10:30 +02:00
|
|
|
count = count + 1
|
2013-12-15 22:33:31 +01:00
|
|
|
end
|
|
|
|
end
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-15 22:33:31 +01:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-12-15 22:33:31 +01:00
|
|
|
|
2012-10-14 03:45:50 +02:00
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2013-07-12 20:22:30 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Adds a pyramid.
|
|
|
|
-- @param pos Position to center base of pyramid at.
|
|
|
|
-- @param axis Axis ("x", "y", or "z")
|
|
|
|
-- @param height Pyramid height.
|
|
|
|
-- @param node_name Name of node to make pyramid of.
|
2015-09-13 11:31:47 +02:00
|
|
|
-- @param hollow Whether the pyramid should be hollow.
|
2014-10-30 03:47:08 +01:00
|
|
|
-- @return The number of nodes added.
|
2015-09-13 11:31:47 +02:00
|
|
|
function worldedit.pyramid(pos, axis, height, node_name, hollow)
|
2014-10-30 03:47:08 +01:00
|
|
|
local other1, other2 = worldedit.get_axis_others(axis)
|
2012-10-14 03:45:50 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Set up voxel manipulator
|
2018-01-04 01:03:51 +01:00
|
|
|
-- FIXME: passing negative <radius> causes mis-sorted pos to be passed
|
|
|
|
-- into mh.init() which is technically not allowed but works
|
|
|
|
local manip, area = mh.init_axis_radius(pos, axis, height)
|
2015-11-01 22:50:36 +01:00
|
|
|
local data = mh.get_empty_data(area)
|
2013-07-28 03:34:42 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Handle inverted pyramids
|
2018-01-04 01:03:51 +01:00
|
|
|
local step
|
2013-07-31 06:02:37 +02:00
|
|
|
if height > 0 then
|
|
|
|
height = height - 1
|
|
|
|
step = 1
|
|
|
|
else
|
2013-08-01 04:15:08 +02:00
|
|
|
height = height + 1
|
2013-07-31 06:02:37 +02:00
|
|
|
step = -1
|
2013-07-28 03:34:42 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add pyramid
|
|
|
|
local node_id = minetest.get_content_id(node_name)
|
2023-06-09 14:28:29 +02:00
|
|
|
local stride = vector.new(1, area.ystride, area.zstride)
|
2014-10-30 03:47:08 +01:00
|
|
|
local offset = {
|
|
|
|
x = pos.x - area.MinEdge.x,
|
|
|
|
y = pos.y - area.MinEdge.y,
|
|
|
|
z = pos.z - area.MinEdge.z,
|
|
|
|
}
|
2015-09-14 22:09:36 +02:00
|
|
|
local size = math.abs(height * step)
|
2013-07-28 23:08:12 +02:00
|
|
|
local count = 0
|
2014-10-30 03:47:08 +01:00
|
|
|
-- For each level of the pyramid
|
|
|
|
for index1 = 0, height, step do
|
|
|
|
-- Offset contributed by axis plus 1 to make it 1-indexed
|
|
|
|
local new_index1 = (index1 + offset[axis]) * stride[axis] + 1
|
2013-08-01 04:15:08 +02:00
|
|
|
for index2 = -size, size do
|
2014-10-30 03:47:08 +01:00
|
|
|
local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]
|
2013-08-01 04:15:08 +02:00
|
|
|
for index3 = -size, size do
|
2014-10-30 03:47:08 +01:00
|
|
|
local i = new_index2 + (index3 + offset[other2]) * stride[other2]
|
2015-09-13 11:31:47 +02:00
|
|
|
if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then
|
2021-01-17 14:40:37 +01:00
|
|
|
data[i] = node_id
|
|
|
|
count = count + 1
|
2015-09-13 11:31:47 +02:00
|
|
|
end
|
2013-07-28 23:08:12 +02:00
|
|
|
end
|
|
|
|
end
|
2013-08-01 04:15:08 +02:00
|
|
|
size = size - 1
|
2013-07-28 03:34:42 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-07-28 03:34:42 +02:00
|
|
|
|
2012-10-14 03:45:50 +02:00
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Adds a spiral.
|
|
|
|
-- @param pos Position to center spiral at.
|
|
|
|
-- @param length Spral length.
|
|
|
|
-- @param height Spiral height.
|
|
|
|
-- @param spacer Space between walls.
|
|
|
|
-- @param node_name Name of node to make spiral of.
|
|
|
|
-- @return Number of nodes added.
|
|
|
|
-- TODO: Add axis option.
|
|
|
|
function worldedit.spiral(pos, length, height, spacer, node_name)
|
2013-08-01 04:15:08 +02:00
|
|
|
local extent = math.ceil(length / 2)
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
|
|
|
|
local data = mh.get_empty_data(area)
|
|
|
|
|
|
|
|
-- Set up variables
|
|
|
|
local node_id = minetest.get_content_id(node_name)
|
2023-06-09 14:28:29 +02:00
|
|
|
local stride = vector.new(1, area.ystride, area.zstride)
|
2014-10-30 03:47:08 +01:00
|
|
|
local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
|
|
|
|
local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add first column
|
2013-12-17 00:55:56 +01:00
|
|
|
local count = height
|
2013-08-01 04:15:08 +02:00
|
|
|
local column = i
|
|
|
|
for y = 1, height do
|
2014-10-30 03:47:08 +01:00
|
|
|
data[column] = node_id
|
2013-08-01 04:15:08 +02:00
|
|
|
column = column + stride.y
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add spiral segments
|
|
|
|
local stride_axis, stride_other = stride.x, stride.z
|
2013-12-17 00:55:56 +01:00
|
|
|
local sign = -1
|
|
|
|
local segment_length = 0
|
|
|
|
spacer = spacer + 1
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Go through each segment except the last
|
|
|
|
for segment = 1, math.floor(length / spacer) * 2 do
|
|
|
|
-- Change sign and length every other turn starting with the first
|
|
|
|
if segment % 2 == 1 then
|
2013-12-17 00:55:56 +01:00
|
|
|
sign = -sign
|
|
|
|
segment_length = segment_length + spacer
|
|
|
|
end
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Fill segment
|
|
|
|
for index = 1, segment_length do
|
|
|
|
-- Move along the direction of the segment
|
|
|
|
i = i + stride_axis * sign
|
2013-08-01 04:15:08 +02:00
|
|
|
local column = i
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add column
|
|
|
|
for y = 1, height do
|
|
|
|
data[column] = node_id
|
2013-08-01 04:15:08 +02:00
|
|
|
column = column + stride.y
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-17 00:55:56 +01:00
|
|
|
count = count + segment_length * height
|
2014-10-30 03:47:08 +01:00
|
|
|
stride_axis, stride_other = stride_other, stride_axis -- Swap axes
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add shorter final segment
|
2013-12-17 00:55:56 +01:00
|
|
|
sign = -sign
|
|
|
|
for index = 1, segment_length do
|
2014-10-30 03:47:08 +01:00
|
|
|
i = i + stride_axis * sign
|
2013-08-01 04:15:08 +02:00
|
|
|
local column = i
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Add column
|
|
|
|
for y = 1, height do
|
|
|
|
data[column] = node_id
|
2013-08-01 04:15:08 +02:00
|
|
|
column = column + stride.y
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-17 00:55:56 +01:00
|
|
|
count = count + segment_length * height
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2012-10-14 03:45:50 +02:00
|
|
|
return count
|
2014-10-30 03:47:08 +01:00
|
|
|
end
|