+--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 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
+ end\r
+ end\r
+ end\r
+ end\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
+ 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 <= full_radius then\r
+ pos1.z = pos.z + z\r
+ env:add_node(pos1, node)\r
+ count = count + 1\r
+ end\r
+ end\r
+ end\r
+ end\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
+ 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
+ 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
+ end\r
+ offset1 = offset1 + 1\r
+ end\r
+ currentpos[axis] = currentpos[axis] + step\r
+ end\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
+ 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
+ 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
+ --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
+\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
+ end\r
+ end\r
+ currentpos[axis] = currentpos[axis] + step\r
+ end\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
+ 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
+ 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
+\r
+ pos1x, pos2x = pos1x + 1, pos2x - 1\r
+ pos1z, pos2z = pos1z + 1, pos2z - 1\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
+ 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
+ end\r
+ end\r
+ return ret\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
+ end\r
+ end\r
+ return count\r
+end\r
+\r