]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit/primitives.lua
Change compatibility notices.
[worldedit.git] / worldedit / primitives.lua
index eb4624b5cdde33026817daeb7c01dbeff0fe5225..508503d6e6b03baf786c1bf01ce2ee5d6a5e1325 100644 (file)
@@ -224,7 +224,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip:
        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
@@ -326,7 +326,7 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
                        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] + 1\r
+                                       local i = newindex3 + index1 * stride[axis]\r
                                        nodes[i] = node_id\r
                                end\r
                                count = count + length\r
@@ -358,18 +358,14 @@ worldedit.pyramid = function(pos, axis, height, nodename)
 \r
        --handle inverted pyramids\r
        local startaxis, endaxis, step\r
-       local currentpos = {x=pos.x, y=pos.y, z=pos.z}\r
        if height > 0 then\r
                height = height - 1\r
-               startaxis, endaxis = 0, height\r
                step = 1\r
                pos1[axis] = pos[axis] --upper half of box\r
        else\r
-               height = -height - 1\r
-               startaxis, endaxis = height, 0\r
+               height = height + 1\r
                step = -1\r
-               pos2[axis] = pos[axis] + 1 --lower half of box\r
-               currentpos[axis] = pos[axis] - height --bottom of box\r
+               pos2[axis] = pos[axis] --lower half of box\r
        end\r
 \r
        --set up voxel manipulator\r
@@ -387,19 +383,20 @@ worldedit.pyramid = function(pos, axis, height, nodename)
        --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\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 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 index1 = startaxis, endaxis, step do --go through each level of the pyramid\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 = -height, height do\r
+               for index2 = -size, size do\r
                        local newindex2 = newindex1 + (index2 + offset[other1]) * stride[other1]\r
-                       for index3 = -height, height do\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
-               count = count + (height * 2 + 1) ^ 2\r
-               height = height - 1\r
+               count = count + (size * 2 + 1) ^ 2\r
+               size = size - 1\r
        end\r
 \r
        --update map nodes\r
@@ -410,70 +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
-       local abs = math.abs\r
-       local sign = 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 longest = math.max(abs(z), abs(x))\r
-               return (2*longest-1)^2 + 4*longest + 2*longest*sign(x+z) + sign(z^2-x^2)*(longest-(abs(z)==longest and sign(z)*x or sign(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
-       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
+       --\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
+\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