-worldedit = worldedit or {}\r
+--- Functions for creating primitive shapes.\r
+-- @module worldedit.primitives\r
\r
---adds a hollow sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.hollow_sphere = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed\r
- local node = {name=nodename}\r
- local pos1 = {x=0, y=0, z=0}\r
- local full_radius = radius * radius + radius\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
- local env = minetest.env\r
- for x = -radius, radius do\r
- pos1.x = pos.x + x\r
- for y = -radius, radius do\r
- pos1.y = pos.y + y\r
- for z = -radius, radius do\r
- if x*x+y*y+z*z >= (radius-1) * (radius-1) + (radius-1) and x*x+y*y+z*z <= full_radius then\r
- pos1.z = pos.z + z\r
- env:add_node({x=pos.x+x,y=pos.y+y,z=pos.z+z}, node)\r
+\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
+ mh.finish(manip, data)\r
return count\r
end\r
\r
---adds a sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.sphere = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed\r
- local node = {name=nodename}\r
- local pos1 = {x=0, y=0, z=0}\r
- local full_radius = radius * radius + radius\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 data = mh.get_empty_data(area)\r
+\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 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
- local env = minetest.env\r
- for x = -radius, radius do\r
- pos1.x = pos.x + x\r
+ for z = -radius, radius do\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
- pos1.y = pos.y + y\r
- for z = -radius, radius do\r
- if x*x+y*y+z*z <= full_radius then\r
- pos1.z = pos.z + z\r
- env:add_node(pos1, node)\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 <= 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
+ 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
+\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
+ min_y, max_y = -radius, 0\r
end\r
\r
- local env = minetest.env\r
- local currentpos = {x=pos.x, y=pos.y, z=pos.z}\r
- local node = {name=nodename}\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
- local step = 1\r
- if length < 0 then\r
- length = -length\r
- step = -1\r
- end\r
- for i = 1, length do\r
- local offset1, offset2 = 0, radius\r
- local delta = -radius\r
- while offset1 <= offset2 do\r
- --add node at each octant\r
- local first1, first2 = pos[other1] + offset1, pos[other1] - offset1\r
- local second1, second2 = pos[other2] + offset2, pos[other2] - offset2\r
- currentpos[other1], currentpos[other2] = first1, second1\r
- env:add_node(currentpos, node) --octant 1\r
- currentpos[other1] = first2\r
- env:add_node(currentpos, node) --octant 4\r
- currentpos[other2] = second2\r
- env:add_node(currentpos, node) --octant 5\r
- currentpos[other1] = first1\r
- env:add_node(currentpos, node) --octant 8\r
- local first1, first2 = pos[other1] + offset2, pos[other1] - offset2\r
- local second1, second2 = pos[other2] + offset1, pos[other2] - offset1\r
- currentpos[other1], currentpos[other2] = first1, second1\r
- env:add_node(currentpos, node) --octant 2\r
- currentpos[other1] = first2\r
- env:add_node(currentpos, node) --octant 3\r
- currentpos[other2] = second2\r
- env:add_node(currentpos, node) --octant 6\r
- currentpos[other1] = first1\r
- env:add_node(currentpos, node) --octant 7\r
-\r
- count = count + 8 --wip: broken\r
-\r
- --move to next location\r
- delta = delta + (offset1 * 2) + 1\r
- if delta >= 0 then\r
- offset2 = offset2 - 1\r
- delta = delta - (offset2 * 2)\r
+ for z = -radius, radius do\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
+ 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
- offset1 = offset1 + 1\r
end\r
- currentpos[axis] = currentpos[axis] + step\r
end\r
+\r
+ mh.finish(manip, data)\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
+--- 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
- local env = minetest.env\r
- local currentpos = {x=pos.x, y=pos.y, z=pos.z}\r
- local node = {name=nodename}\r
- local count = 0\r
- local step = 1\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
- step = -1\r
+ current_pos[axis] = current_pos[axis] - length\r
+ radius1, radius2 = radius2, radius1\r
end\r
- for i = 1, length do\r
- local offset1, offset2 = 0, radius\r
- local delta = -radius\r
- while offset1 <= offset2 do\r
- --connect each pair of octants\r
- currentpos[other1] = pos[other1] - offset1\r
- local second1, second2 = pos[other2] + offset2, pos[other2] - offset2\r
- for i = 0, offset1 * 2 do\r
- currentpos[other2] = second1\r
- env:add_node(currentpos, node) --octant 1 to 4\r
- currentpos[other2] = second2\r
- env:add_node(currentpos, node) --octant 5 to 8\r
- currentpos[other1] = currentpos[other1] + 1\r
- end\r
- currentpos[other1] = pos[other1] - offset2\r
- local second1, second2 = pos[other2] + offset1, pos[other2] - offset1\r
- for i = 0, offset2 * 2 do\r
- currentpos[other2] = second1\r
- env:add_node(currentpos, node) --octant 2 to 3\r
- currentpos[other2] = second2\r
- env:add_node(currentpos, node) --octant 6 to 7\r
- currentpos[other1] = currentpos[other1] + 1\r
- end\r
\r
- count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken\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
+ -- 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 = {\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 count = 0\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
- --move to next location\r
- delta = delta + (offset1 * 2) + 1\r
- offset1 = offset1 + 1\r
- if delta >= 0 then\r
- offset2 = offset2 - 1\r
- delta = delta - (offset2 * 2)\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
end\r
end\r
- currentpos[axis] = currentpos[axis] + step\r
end\r
+\r
+ mh.finish(manip, data)\r
+\r
return count\r
end\r
\r
---adds a pyramid at `pos` with height `height`, composed of `nodename`, returning the number of nodes added\r
-worldedit.pyramid = function(pos, height, nodename)\r
- local pos1x, pos1y, pos1z = pos.x - height, pos.y, pos.z - height\r
- local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height\r
- local pos = {x=0, y=pos1y, z=0}\r
\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
+ -- FIXME: passing negative <radius> causes mis-sorted pos to be passed\r
+ -- into mh.init() which is technically not allowed but works\r
+ local manip, area = mh.init_axis_radius(pos, axis, height)\r
+ local data = mh.get_empty_data(area)\r
+\r
+ -- Handle inverted pyramids\r
+ local step\r
+ if height > 0 then\r
+ height = height - 1\r
+ step = 1\r
+ else\r
+ height = height + 1\r
+ step = -1\r
+ end\r
+\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 = {\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
- local node = {name=nodename}\r
- local env = minetest.env\r
- while pos.y <= pos2y do --each vertical level of the pyramid\r
- pos.x = pos1x\r
- while pos.x <= pos2x do\r
- pos.z = pos1z\r
- while pos.z <= pos2z do\r
- env:add_node(pos, node)\r
- pos.z = pos.z + 1\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 new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]\r
+ for index3 = -size, size do\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
- pos.x = pos.x + 1\r
end\r
- count = count + ((pos2y - pos.y) * 2 + 1) ^ 2\r
- pos.y = pos.y + 1\r
+ size = size - 1\r
+ end\r
\r
- pos1x, pos2x = pos1x + 1, pos2x - 1\r
- pos1z, pos2z = pos1z + 1, pos2z - 1\r
+ mh.finish(manip, data)\r
\r
- end\r
return count\r
end\r
\r
---adds a spiral at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added\r
-worldedit.spiral = function(pos, width, height, spacer, nodename) --wip: clean this up\r
- -- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua\r
- av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end\r
- local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards\r
- if z == -x and z >= x then return (2*z+1)^2 end\r
- local l = math.max(av(z), av(x))\r
- return (2*l-1)^2+4*l+2*l*sn(x+z)+sn(z^2-x^2)*(l-(av(z)==l and sn(z)*x or sn(x)*z)) -- OH GOD WHAT\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
+\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 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
+ local count = height\r
+ local column = i\r
+ for y = 1, height do\r
+ data[column] = node_id\r
+ column = column + stride.y\r
end\r
- local function spiralt(side)\r
- local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)\r
- for i = 1, side do\r
- for j = 1, side do\r
- local id = side^2 - sindex(stop - i + 1,start + j - 1)\r
- ret[id] = {x=i,z=j}\r
+\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
+ -- Add column\r
+ for y = 1, height do\r
+ data[column] = node_id\r
+ column = column + stride.y\r
end\r
end\r
- return ret\r
+ count = count + segment_length * height\r
+ stride_axis, stride_other = stride_other, stride_axis -- Swap axes\r
end\r
- -- connect the joined parts\r
- local spiral = spiralt(width)\r
- height = tonumber(height)\r
- if height < 1 then height = 1 end\r
- spacer = tonumber(spacer)-1\r
- if spacer < 1 then spacer = 1 end\r
- local count = 0\r
- local node = {name=nodename}\r
- local np,lp\r
- for y=0,height do\r
- lp = nil\r
- for _,v in ipairs(spiral) do\r
- np = {x=pos.x+v.x*spacer, y=pos.y+y, z=pos.z+v.z*spacer}\r
- if lp~=nil then\r
- if lp.x~=np.x then \r
- if lp.x<np.x then \r
- for i=lp.x+1,np.x do\r
- minetest.env:add_node({x=i, y=np.y, z=np.z}, node)\r
- count = count + 1\r
- end\r
- else\r
- for i=np.x,lp.x-1 do\r
- minetest.env:add_node({x=i, y=np.y, z=np.z}, node)\r
- count = count + 1\r
- end\r
- end\r
- end\r
- if lp.z~=np.z then \r
- if lp.z<np.z then \r
- for i=lp.z+1,np.z do\r
- minetest.env:add_node({x=np.x, y=np.y, z=i}, node)\r
- count = count + 1\r
- end\r
- else\r
- for i=np.z,lp.z-1 do\r
- minetest.env:add_node({x=np.x, y=np.y, z=i}, node)\r
- count = count + 1\r
- end\r
- end\r
- end\r
- end\r
- lp = np\r
+\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
+ -- Add column\r
+ for y = 1, height do\r
+ data[column] = node_id\r
+ column = column + stride.y\r
end\r
end\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