]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit/primitives.lua
Change compatibility notices.
[worldedit.git] / worldedit / primitives.lua
index d11596a812056f31d67301c2d498eccb0576a0c7..508503d6e6b03baf786c1bf01ce2ee5d6a5e1325 100644 (file)
@@ -209,8 +209,22 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip:
                currentpos[axis] = currentpos[axis] - length\r
        end\r
 \r
+       --make area stay loaded\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
+       manip:read_from_map(pos1, pos2)\r
+\r
        --create schematic for single node column along the axis\r
-       local node = {name=nodename, param1=0, param2=0}\r
+       local node = {name=nodename, param1=255, param2=0}\r
        local nodes = {}\r
        for i = 1, length do\r
                nodes[i] = node\r
@@ -306,13 +320,13 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
        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 axis1 = -radius, radius do\r
-               local newaxis1 = (axis1 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed\r
-               for axis2 = -radius, radius do\r
-                       local newaxis2 = newaxis1 + (axis2 + offset[other2]) * stride[other2]\r
-                       if axis1 * axis1 + axis2 * axis2 <= max_radius then\r
-                               for slice = min_slice, max_slice do\r
-                                       local i = newaxis2 + slice * stride[axis] + 1\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\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
@@ -330,9 +344,30 @@ end
 \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 pos1 = {x=pos.x - height, y=pos.y, z=pos.z - height}\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
 \r
+       --handle inverted pyramids\r
+       local startaxis, endaxis, 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
@@ -345,31 +380,23 @@ worldedit.pyramid = function(pos, axis, height, nodename)
                nodes[i] = ignore\r
        end\r
 \r
-       --handle inverted pyramids\r
-       height = height - 1\r
-       local size = height\r
-       local step = -1\r
-       if height < 0 then\r
-               size = 0\r
-               step = 1\r
-       end\r
---wip: support arbitrary axes\r
        --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\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 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 count = 0\r
-       for y = 0, height do --go through each level of the pyramid\r
-               local newy = (y + offsety) * ystride + 1 --offset contributed by y plus 1 to make it 1-indexed\r
-               for z = -size, size do\r
-                       local newz = newy + (z + offsetz) * zstride\r
-                       for x = -size, size do\r
-                               local i = newz + (x + offsetx)\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 index2 = -size, size do\r
+                       local newindex2 = newindex1 + (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
                        end\r
                end\r
-               size = size + step\r
-               count = count + ((height - y) * 2 + 1) ^ 2\r
+               count = count + (size * 2 + 1) ^ 2\r
+               size = size - 1\r
        end\r
 \r
        --update map nodes\r
@@ -380,69 +407,72 @@ worldedit.pyramid = function(pos, axis, height, nodename)
        return count\r
 end\r
 \r
---adds a spiral centered 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, env) --wip: rewrite this whole thing, nobody can understand it anyways\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 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
+       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 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
+\r
+       --add first column\r
+       local column = i\r
+       for y = 1, height do\r
+               nodes[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 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
+                       local column = i\r
+                       for y = 1, height do --add column\r
+                               nodes[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
-               return ret\r
        end\r
-       if env == nil then env = minetest.env 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
-                                                       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
-                                                       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
-                                                       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
-                                                       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
+       for index = 1, (math.floor(length / spacer) - 2) * spacer 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
+                       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
+\r
        return count\r
-end\r
+end
\ No newline at end of file