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
+\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 of `node_name` centered at `pos`.\r
-- @param pos Position to center sphere at.\r
-- @param radius Sphere radius.\r