-worldedit = worldedit or {}\r
-local minetest = minetest --local copy of global\r
-\r
---adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.hollow_sphere = function(pos, radius, nodename)\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}\r
- local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
-\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
- local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
- local zstride, ystride = area.zstride, area.ystride\r
+--- Functions for creating primitive shapes.\r
+-- @module worldedit.primitives\r
+\r
+local mh = worldedit.manip_helpers\r
+\r
+\r
+--- Adds a cube\r
+-- @param pos Position of ground level center of cube\r
+-- @param width Cube width. (x)\r
+-- @param height Cube height. (y)\r
+-- @param length Cube length. (z)\r
+-- @param node_name Name of node to make cube of.\r
+-- @param hollow Whether the cube should be hollow.\r
+-- @return The number of nodes added.\r
+function worldedit.cube(pos, width, height, length, node_name, hollow)\r
+ -- Set up voxel manipulator\r
+ local basepos = vector.subtract(pos, {x=math.floor(width/2), y=0, z=math.floor(length/2)})\r
+ local manip, area = mh.init(basepos, vector.add(basepos, {x=width, y=height, z=length}))\r
+ local data = mh.get_empty_data(area)\r
+\r
+ -- Add cube\r
+ local node_id = minetest.get_content_id(node_name)\r
+ local stride = {x=1, y=area.ystride, z=area.zstride}\r
+ local offset = vector.subtract(basepos, area.MinEdge)\r
local count = 0\r
- for z = -radius, radius do\r
- local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed\r
- for y = -radius, radius do\r
- local newy = newz + (y + offsety) * ystride\r
- for x = -radius, radius do\r
- local squared = x * x + y * y + z * z\r
- if squared >= min_radius and squared <= max_radius then --position is on surface of sphere\r
- local i = newy + (x + offsetx)\r
- nodes[i] = node_id\r
- count = count + 1\r
- end\r
- end\r
- end\r
- end\r
-\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
-\r
- return count\r
-end\r
-\r
---adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.sphere = function(pos, radius, nodename)\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}\r
- local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
- local max_radius = radius * (radius + 1)\r
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
- local zstride, ystride = area.zstride, area.ystride\r
- local count = 0\r
- for z = -radius, radius do\r
- local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed\r
- for y = -radius, radius do\r
- local newy = newz + (y + offsety) * ystride\r
- for x = -radius, radius do\r
- if x * x + y * y + z * z <= max_radius then --position is inside sphere\r
- local i = newy + (x + offsetx)\r
- nodes[i] = node_id\r
+ for z = 0, length-1 do\r
+ local index_z = (offset.z + z) * stride.z + 1 -- +1 for 1-based indexing\r
+ for y = 0, height-1 do\r
+ local index_y = index_z + (offset.y + y) * stride.y\r
+ for x = 0, width-1 do\r
+ local is_wall = z == 0 or z == length-1\r
+ or y == 0 or y == height-1\r
+ or x == 0 or x == width-1\r
+ if not hollow or is_wall then\r
+ local i = index_y + (offset.x + x)\r
+ data[i] = node_id\r
count = count + 1\r
end\r
end\r
end\r
end\r
\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
-\r
+ mh.finish(manip, data)\r
return count\r
end\r
\r
---adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.hollow_dome = function(pos, radius, nodename)\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}\r
- local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
+--- Adds a sphere of `node_name` centered at `pos`.\r
+-- @param pos Position to center sphere at.\r
+-- @param radius Sphere radius.\r
+-- @param node_name Name of node to make shere of.\r
+-- @param hollow Whether the sphere should be hollow.\r
+-- @return The number of nodes added.\r
+function worldedit.sphere(pos, radius, node_name, hollow)\r
+ local manip, area = mh.init_radius(pos, radius)\r
\r
- local miny, maxy = 0, radius\r
- if radius < 0 then\r
- radius = -radius\r
- miny, maxy = -radius, 0\r
- end\r
+ local data = mh.get_empty_data(area)\r
\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
+ -- Fill selected area with node\r
+ local node_id = minetest.get_content_id(node_name)\r
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
- local zstride, ystride = area.zstride, area.ystride\r
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
+ local stride_z, stride_y = area.zstride, area.ystride\r
local count = 0\r
for z = -radius, radius do\r
- local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed\r
- for y = miny, maxy do\r
- local newy = newz + (y + offsety) * ystride\r
+ -- Offset contributed by z plus 1 to make it 1-indexed\r
+ local new_z = (z + offset_z) * stride_z + 1\r
+ for y = -radius, radius do\r
+ local new_y = new_z + (y + offset_y) * stride_y\r
for x = -radius, radius do\r
local squared = x * x + y * y + z * z\r
- if squared >= min_radius and squared <= max_radius then --position is on surface of sphere\r
- local i = newy + (x + offsetx)\r
- nodes[i] = node_id\r
+ if squared <= max_radius and (not hollow or squared >= min_radius) then\r
+ -- Position is on surface of sphere\r
+ local i = new_y + (x + offset_x)\r
+ data[i] = node_id\r
count = count + 1\r
end\r
end\r
end\r
end\r
\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
+ mh.finish(manip, data)\r
\r
return count\r
end\r
\r
---adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.dome = function(pos, radius, nodename)\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}\r
- local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
\r
- local miny, maxy = 0, radius\r
+--- Adds a dome.\r
+-- @param pos Position to center dome at.\r
+-- @param radius Dome radius. Negative for concave domes.\r
+-- @param node_name Name of node to make dome of.\r
+-- @param hollow Whether the dome should be hollow.\r
+-- @return The number of nodes added.\r
+-- TODO: Add axis option.\r
+function worldedit.dome(pos, radius, node_name, hollow)\r
+ local min_y, max_y = 0, radius\r
if radius < 0 then\r
radius = -radius\r
- miny, maxy = -radius, 0\r
+ min_y, max_y = -radius, 0\r
end\r
\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
- local max_radius = radius * (radius + 1)\r
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
- local zstride, ystride = area.zstride, area.ystride\r
+ local manip, area = mh.init_axis_radius(pos, "y", radius)\r
+ local data = mh.get_empty_data(area)\r
+\r
+ -- Add dome\r
+ local node_id = minetest.get_content_id(node_name)\r
+ local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
+ local stride_z, stride_y = area.zstride, area.ystride\r
local count = 0\r
for z = -radius, radius do\r
- local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed\r
- for y = miny, maxy do\r
- local newy = newz + (y + offsety) * ystride\r
+ local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed\r
+ for y = min_y, max_y do\r
+ local new_y = new_z + (y + offset_y) * stride_y\r
for x = -radius, radius do\r
- if x * x + y * y + z * z <= max_radius then --position is inside sphere\r
- local i = newy + (x + offsetx)\r
- nodes[i] = node_id\r
+ local squared = x * x + y * y + z * z\r
+ if squared <= max_radius and (not hollow or squared >= min_radius) then\r
+ -- Position is in dome\r
+ local i = new_y + (x + offset_x)\r
+ data[i] = node_id\r
count = count + 1\r
end\r
end\r
end\r
end\r
\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
+ mh.finish(manip, data)\r
\r
return count\r
end\r
\r
---adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)\r
- local other1, other2\r
- if axis == "x" then\r
- other1, other2 = "y", "z"\r
- elseif axis == "y" then\r
- other1, other2 = "x", "z"\r
- else --axis == "z"\r
- other1, other2 = "x", "y"\r
+--- Adds a cylinder.\r
+-- @param pos Position to center base of cylinder at.\r
+-- @param axis Axis ("x", "y", or "z")\r
+-- @param length Cylinder length.\r
+-- @param radius1 Cylinder base radius.\r
+-- @param radius2 Cylinder top radius.\r
+-- @param node_name Name of node to make cylinder of.\r
+-- @param hollow Whether the cylinder should be hollow.\r
+-- @return The number of nodes added.\r
+function worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)\r
+ local other1, other2 = worldedit.get_axis_others(axis)\r
+\r
+ -- Backwards compatibility\r
+ if type(radius2) == "string" then\r
+ hollow = node_name\r
+ node_name = radius2\r
+ radius2 = radius1 -- straight cylinder\r
end\r
\r
- --handle negative lengths\r
- local currentpos = {x=pos.x, y=pos.y, z=pos.z}\r
+ -- Handle negative lengths\r
+ local current_pos = {x=pos.x, y=pos.y, z=pos.z}\r
if length < 0 then\r
length = -length\r
- currentpos[axis] = currentpos[axis] - length\r
+ current_pos[axis] = current_pos[axis] - length\r
+ radius1, radius2 = radius2, radius1\r
end\r
\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {\r
- [axis]=currentpos[axis],\r
- [other1]=currentpos[other1] - radius,\r
- [other2]=currentpos[other2] - radius\r
- }\r
- local pos2 = {\r
- [axis]=currentpos[axis] + length - 1,\r
- [other1]=currentpos[other1] + radius,\r
- [other2]=currentpos[other2] + radius\r
- }\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
+ -- Set up voxel manipulator\r
+ local manip, area = mh.init_axis_radius_length(current_pos, axis, math.max(radius1, radius2), length)\r
+ local data = mh.get_empty_data(area)\r
\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
- local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
+ -- Add desired shape (anything inbetween cylinder & cone)\r
+ local node_id = minetest.get_content_id(node_name)\r
local stride = {x=1, y=area.ystride, z=area.zstride}\r
- local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z}\r
- local min_slice, max_slice = offset[axis], offset[axis] + length - 1\r
- local count = 0\r
- for index2 = -radius, radius do\r
- local newindex2 = (index2 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed\r
- for index3 = -radius, radius do\r
- local newindex3 = newindex2 + (index3 + offset[other2]) * stride[other2]\r
- local squared = index2 * index2 + index3 * index3\r
- if squared >= min_radius and squared <= max_radius then --position is on surface of cylinder\r
- for index1 = min_slice, max_slice do --add column along axis\r
- local i = newindex3 + index1 * stride[axis]\r
- nodes[i] = node_id\r
- end\r
- count = count + length\r
- end\r
- end\r
- end\r
-\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
-\r
- return count\r
-end\r
-\r
---adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.cylinder = function(pos, axis, length, radius, nodename)\r
- local other1, other2\r
- if axis == "x" then\r
- other1, other2 = "y", "z"\r
- elseif axis == "y" then\r
- other1, other2 = "x", "z"\r
- else --axis == "z"\r
- other1, other2 = "x", "y"\r
- end\r
-\r
- --handle negative lengths\r
- local currentpos = {x=pos.x, y=pos.y, z=pos.z}\r
- if length < 0 then\r
- length = -length\r
- currentpos[axis] = currentpos[axis] - length\r
- end\r
-\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local pos1 = {\r
- [axis]=currentpos[axis],\r
- [other1]=currentpos[other1] - radius,\r
- [other2]=currentpos[other2] - radius\r
+ local offset = {\r
+ x = current_pos.x - area.MinEdge.x,\r
+ y = current_pos.y - area.MinEdge.y,\r
+ z = current_pos.z - area.MinEdge.z,\r
}\r
- local pos2 = {\r
- [axis]=currentpos[axis] + length - 1,\r
- [other1]=currentpos[other1] + radius,\r
- [other2]=currentpos[other2] + radius\r
- }\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
-\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
- local max_radius = radius * (radius + 1)\r
- local stride = {x=1, y=area.ystride, z=area.zstride}\r
- local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z}\r
- local min_slice, max_slice = offset[axis], offset[axis] + length - 1\r
local count = 0\r
- for index2 = -radius, radius do\r
- local newindex2 = (index2 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed\r
- for index3 = -radius, radius do\r
- local newindex3 = newindex2 + (index3 + offset[other2]) * stride[other2]\r
- if index2 * index2 + index3 * index3 <= max_radius then --position is within cylinder\r
- for index1 = min_slice, max_slice do --add column along axis\r
- local i = newindex3 + index1 * stride[axis]\r
- nodes[i] = node_id\r
+ for i = 0, length - 1 do\r
+ -- Calulate radius for this "height" in the cylinder\r
+ local radius = radius1 + (radius2 - radius1) * (i + 1) / length\r
+ radius = math.floor(radius + 0.5) -- round\r
+ local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
+\r
+ for index2 = -radius, radius do\r
+ -- Offset contributed by other axis 1 plus 1 to make it 1-indexed\r
+ local new_index2 = (index2 + offset[other1]) * stride[other1] + 1\r
+ for index3 = -radius, radius do\r
+ local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]\r
+ local squared = index2 * index2 + index3 * index3\r
+ if squared <= max_radius and (not hollow or squared >= min_radius) then\r
+ -- Position is in cylinder, add node here\r
+ local vi = new_index3 + (offset[axis] + i) * stride[axis]\r
+ data[vi] = node_id\r
+ count = count + 1\r
end\r
- count = count + length\r
end\r
end\r
end\r
\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
+ mh.finish(manip, data)\r
\r
return count\r
end\r
\r
---adds a pyramid centered at `pos` with height `height`, composed of `nodename`, returning the number of nodes added\r
-worldedit.pyramid = function(pos, axis, height, nodename)\r
- local other1, other2\r
- if axis == "x" then\r
- other1, other2 = "y", "z"\r
- elseif axis == "y" then\r
- other1, other2 = "x", "z"\r
- else --axis == "z"\r
- other1, other2 = "x", "y"\r
- end\r
\r
- local pos1 = {x=pos.x - height, y=pos.y - height, z=pos.z - height}\r
- local pos2 = {x=pos.x + height, y=pos.y + height, z=pos.z + height}\r
+--- Adds a pyramid.\r
+-- @param pos Position to center base of pyramid at.\r
+-- @param axis Axis ("x", "y", or "z")\r
+-- @param height Pyramid height.\r
+-- @param node_name Name of node to make pyramid of.\r
+-- @param hollow Whether the pyramid should be hollow.\r
+-- @return The number of nodes added.\r
+function worldedit.pyramid(pos, axis, height, node_name, hollow)\r
+ local other1, other2 = worldedit.get_axis_others(axis)\r
+\r
+ -- Set up voxel manipulator\r
+ local manip, area = mh.init_axis_radius(pos, axis,\r
+ height >= 0 and height or -height)\r
+ local data = mh.get_empty_data(area)\r
\r
- --handle inverted pyramids\r
- local startaxis, endaxis, step\r
+ -- Handle inverted pyramids\r
+ local start_axis, end_axis, step\r
if height > 0 then\r
height = height - 1\r
step = 1\r
- pos1[axis] = pos[axis] --upper half of box\r
else\r
height = height + 1\r
step = -1\r
- pos2[axis] = pos[axis] --lower half of box\r
end\r
\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
-\r
- --fill selected area with node\r
- local node_id = minetest.get_content_id(nodename)\r
+ -- Add pyramid\r
+ local node_id = minetest.get_content_id(node_name)\r
local stride = {x=1, y=area.ystride, z=area.zstride}\r
- local offset = {x=pos.x - emerged_pos1.x, y=pos.y - emerged_pos1.y, z=pos.z - emerged_pos1.z}\r
- local size = height * step\r
+ local offset = {\r
+ x = pos.x - area.MinEdge.x,\r
+ y = pos.y - area.MinEdge.y,\r
+ z = pos.z - area.MinEdge.z,\r
+ }\r
+ local size = math.abs(height * step)\r
local count = 0\r
- for index1 = 0, height, step do --go through each level of the pyramid\r
- local newindex1 = (index1 + offset[axis]) * stride[axis] + 1 --offset contributed by axis plus 1 to make it 1-indexed\r
+ -- For each level of the pyramid\r
+ for index1 = 0, height, step do\r
+ -- Offset contributed by axis plus 1 to make it 1-indexed\r
+ local new_index1 = (index1 + offset[axis]) * stride[axis] + 1\r
for index2 = -size, size do\r
- local newindex2 = newindex1 + (index2 + offset[other1]) * stride[other1]\r
+ local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]\r
for index3 = -size, size do\r
- local i = newindex2 + (index3 + offset[other2]) * stride[other2]\r
- nodes[i] = node_id\r
+ local i = new_index2 + (index3 + offset[other2]) * stride[other2]\r
+ if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then\r
+ data[i] = node_id\r
+ count = count + 1\r
+ end\r
end\r
end\r
- count = count + (size * 2 + 1) ^ 2\r
size = size - 1\r
end\r
\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
+ mh.finish(manip, data)\r
\r
return count\r
end\r
\r
---adds a spiral centered at `pos` with side length `length`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added\r
-worldedit.spiral = function(pos, length, height, spacer, nodename)\r
+--- Adds a spiral.\r
+-- @param pos Position to center spiral at.\r
+-- @param length Spral length.\r
+-- @param height Spiral height.\r
+-- @param spacer Space between walls.\r
+-- @param node_name Name of node to make spiral of.\r
+-- @return Number of nodes added.\r
+-- TODO: Add axis option.\r
+function worldedit.spiral(pos, length, height, spacer, node_name)\r
local extent = math.ceil(length / 2)\r
- local pos1 = {x=pos.x - extent, y=pos.y, z=pos.z - extent}\r
- local pos2 = {x=pos.x + extent, y=pos.y + height, z=pos.z + extent}\r
-\r
- --set up voxel manipulator\r
- local manip = minetest.get_voxel_manip()\r
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
- local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
-\r
- --fill emerged area with ignore\r
- local nodes = {}\r
- local ignore = minetest.get_content_id("ignore")\r
- for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
- nodes[i] = ignore\r
- end\r
\r
- --\r
- local node_id = minetest.get_content_id(nodename)\r
+ local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)\r
+ local data = mh.get_empty_data(area)\r
+\r
+ -- Set up variables\r
+ local node_id = minetest.get_content_id(node_name)\r
local stride = {x=1, y=area.ystride, z=area.zstride}\r
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
- local i = offsetz * stride.z + offsety * stride.y + offsetx + 1\r
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
+ local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1\r
\r
- --add first column\r
+ -- Add first column\r
+ local count = height\r
local column = i\r
for y = 1, height do\r
- nodes[column] = node_id\r
+ data[column] = node_id\r
column = column + stride.y\r
end\r
\r
- --add spiral segments\r
- local axis, other = "x", "z"\r
- local sign = 1\r
- local count = height\r
- for segment = 1, length / spacer - 1 do --go through each segment except the last\r
- for index = 1, segment * spacer do --fill segment\r
- i = i + stride[axis] * sign\r
+ -- Add spiral segments\r
+ local stride_axis, stride_other = stride.x, stride.z\r
+ local sign = -1\r
+ local segment_length = 0\r
+ spacer = spacer + 1\r
+ -- Go through each segment except the last\r
+ for segment = 1, math.floor(length / spacer) * 2 do\r
+ -- Change sign and length every other turn starting with the first\r
+ if segment % 2 == 1 then\r
+ sign = -sign\r
+ segment_length = segment_length + spacer\r
+ end\r
+ -- Fill segment\r
+ for index = 1, segment_length do\r
+ -- Move along the direction of the segment\r
+ i = i + stride_axis * sign\r
local column = i\r
- for y = 1, height do --add column\r
- nodes[column] = node_id\r
+ -- Add column\r
+ for y = 1, height do\r
+ data[column] = node_id\r
column = column + stride.y\r
end\r
- count = count + height\r
- end\r
- axis, other = other, axis --swap axes\r
- if segment % 2 == 1 then --change sign every other turn\r
- sign = -sign\r
end\r
+ count = count + segment_length * height\r
+ stride_axis, stride_other = stride_other, stride_axis -- Swap axes\r
end\r
\r
- --add shorter final segment\r
- for index = 1, (math.floor(length / spacer) - 2) * spacer do\r
- i = i + stride[axis] * sign\r
+ -- Add shorter final segment\r
+ sign = -sign\r
+ for index = 1, segment_length do\r
+ i = i + stride_axis * sign\r
local column = i\r
- for y = 1, height do --add column\r
- nodes[column] = node_id\r
+ -- Add column\r
+ for y = 1, height do\r
+ data[column] = node_id\r
column = column + stride.y\r
end\r
- count = count + height\r
end\r
-print(minetest.serialize(nodes))\r
- --update map nodes\r
- manip:set_data(nodes)\r
- manip:write_to_map()\r
- manip:update_map()\r
+ count = count + segment_length * height\r
+\r
+ mh.finish(manip, data)\r
\r
return count\r
-end
\ No newline at end of file
+end\r