]> git.lizzy.rs Git - worldedit.git/blobdiff - functions.lua
Fix worldedit.spiral and the correspondign chat command, //spiral.
[worldedit.git] / functions.lua
index 7912b9eda6762bf9aec51fa8ee9a4fa72e102566..2d949c65bf2fdcb43f6f6e061010941bbbcffaca 100644 (file)
@@ -47,9 +47,12 @@ worldedit.replace = function(pos1, pos2, searchnode, replacenode)
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local env = minetest.env\r
 \r
-       if searchnode:find(":") == nil then\r
+       if minetest.registered_nodes[searchnode] == nil then\r
                searchnode = "default:" .. searchnode\r
        end\r
+       if minetest.registered_nodes[replacenode] == nil then\r
+               replacenode = "default:" .. replacenode\r
+       end\r
 \r
        local pos = {x=pos1.x, y=0, z=0}\r
        local node = {name=replacenode}\r
@@ -72,6 +75,264 @@ worldedit.replace = function(pos1, pos2, searchnode, replacenode)
        return count\r
 end\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 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
 --copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes copied\r
 worldedit.copy = function(pos1, pos2, axis, amount)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
@@ -84,11 +345,13 @@ worldedit.copy = function(pos1, pos2, axis, amount)
                        while pos.y <= pos2.y do\r
                                pos.z = pos1.z\r
                                while pos.z <= pos2.z do\r
-                                       local node = env:get_node(pos)\r
-                                       local value = pos[axis]\r
-                                       pos[axis] = value - amount\r
-                                       env:add_node(pos, node)\r
-                                       pos[axis] = value\r
+                                       local node = env:get_node(pos) --obtain current node\r
+                                       local meta = env:get_meta(pos):to_table() --get meta of current node\r
+                                       local value = pos[axis] --store current position\r
+                                       pos[axis] = value + amount --move along axis\r
+                                       env:add_node(pos, node) --copy node to new position\r
+                                       env:get_meta(pos):from_table(meta) --set metadata of new node\r
+                                       pos[axis] = value --restore old position\r
                                        pos.z = pos.z + 1\r
                                end\r
                                pos.y = pos.y + 1\r
@@ -102,11 +365,13 @@ worldedit.copy = function(pos1, pos2, axis, amount)
                        while pos.y >= pos1.y do\r
                                pos.z = pos2.z\r
                                while pos.z >= pos1.z do\r
-                                       local node = minetest.env:get_node(pos)\r
-                                       local value = pos[axis]\r
-                                       pos[axis] = value + amount\r
-                                       minetest.env:add_node(pos, node)\r
-                                       pos[axis] = value\r
+                                       local node = minetest.env:get_node(pos) --obtain current node\r
+                                       local meta = env:get_meta(pos):to_table() --get meta of current node\r
+                                       local value = pos[axis] --store current position\r
+                                       pos[axis] = value + amount --move along axis\r
+                                       minetest.env:add_node(pos, node) --copy node to new position\r
+                                       env:get_meta(pos):from_table(meta) --set metadata of new node\r
+                                       pos[axis] = value --restore old position\r
                                        pos.z = pos.z - 1\r
                                end\r
                                pos.y = pos.y - 1\r
@@ -129,12 +394,14 @@ worldedit.move = function(pos1, pos2, axis, amount)
                        while pos.y <= pos2.y do\r
                                pos.z = pos1.z\r
                                while pos.z <= pos2.z do\r
-                                       local node = env:get_node(pos)\r
+                                       local node = env:get_node(pos) --obtain current node\r
+                                       local meta = env:get_meta(pos):to_table() --get metadata of current node\r
                                        env:remove_node(pos)\r
-                                       local value = pos[axis]\r
-                                       pos[axis] = value - amount\r
-                                       env:add_node(pos, node)\r
-                                       pos[axis] = value\r
+                                       local value = pos[axis] --store current position\r
+                                       pos[axis] = value + amount --move along axis\r
+                                       env:add_node(pos, node) --move node to new position\r
+                                       env:get_meta(pos):from_table(meta) --set metadata of new node\r
+                                       pos[axis] = value --restore old position\r
                                        pos.z = pos.z + 1\r
                                end\r
                                pos.y = pos.y + 1\r
@@ -148,12 +415,14 @@ worldedit.move = function(pos1, pos2, axis, amount)
                        while pos.y >= pos1.y do\r
                                pos.z = pos2.z\r
                                while pos.z >= pos1.z do\r
-                                       local node = minetest.env:get_node(pos)\r
+                                       local node = env:get_node(pos) --obtain current node\r
+                                       local meta = env:get_meta(pos):to_table() --get metadata of current node\r
                                        env:remove_node(pos)\r
-                                       local value = pos[axis]\r
-                                       pos[axis] = value + amount\r
-                                       minetest.env:add_node(pos, node)\r
-                                       pos[axis] = value\r
+                                       local value = pos[axis] --store current position\r
+                                       pos[axis] = value + amount --move along axis\r
+                                       env:add_node(pos, node) --move node to new position\r
+                                       env:get_meta(pos):from_table(meta) --set metadata of new node\r
+                                       pos[axis] = value --restore old position\r
                                        pos.z = pos.z - 1\r
                                end\r
                                pos.y = pos.y - 1\r
@@ -168,12 +437,12 @@ end
 worldedit.stack = function(pos1, pos2, axis, count)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local length = pos2[axis] - pos1[axis] + 1\r
-       local amount = 0\r
-       local copy = worldedit.copy\r
        if count < 0 then\r
                count = -count\r
                length = -length\r
        end\r
+       local amount = 0\r
+       local copy = worldedit.copy\r
        for i = 1, count do\r
                amount = amount + length\r
                copy(pos1, pos2, axis, amount)\r
@@ -195,12 +464,16 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2)
                                local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]\r
                                if extent1 < extent2 then\r
                                        local node1 = env:get_node(pos)\r
+                                       local meta1 = env:get_meta(pos):to_table()\r
                                        local value1, value2 = pos[axis1], pos[axis2]\r
-                                       pos[axis1], pos[axis2] = pos1[axis1] + extent1, pos1[axis2] + extent2\r
+                                       pos[axis1], pos[axis2] = value1 + extent2, value2 + extent1\r
                                        local node2 = env:get_node(pos)\r
+                                       local meta2 = env:get_meta(pos):to_table()\r
                                        env:add_node(pos, node1)\r
+                                       env:get_meta(pos):from_table(meta1)\r
                                        pos[axis1], pos[axis2] = value1, value2\r
                                        env:add_node(pos, node2)\r
+                                       env:get_meta(pos):from_table(meta2)\r
                                end\r
                                pos.z = pos.z + 1\r
                        end\r
@@ -225,12 +498,16 @@ worldedit.flip = function(pos1, pos2, axis)
                        pos.z = pos1.z\r
                        while pos.z <= pos2.z do\r
                                local node1 = env:get_node(pos)\r
+                               local meta1 = env:get_meta(pos):to_table()\r
                                local value = pos[axis]\r
                                pos[axis] = start - value\r
                                local node2 = env:get_node(pos)\r
+                               local meta2 = env:get_meta(pos):to_table()\r
                                env:add_node(pos, node1)\r
+                               env:get_meta(pos):from_table(meta1)\r
                                pos[axis] = value\r
                                env:add_node(pos, node2)\r
+                               env:get_meta(pos):from_table(meta2)\r
                                pos.z = pos.z + 1\r
                        end\r
                        pos.y = pos.y + 1\r
@@ -240,32 +517,29 @@ worldedit.flip = function(pos1, pos2, axis)
        return worldedit.volume(pos1, pos2)\r
 end\r
 \r
---rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around the y axis (supporting 90 degree increments only), returning the number of nodes rotated\r
-worldedit.rotate = function(pos1, pos2, angle)\r
+--rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around axis `axis` (90 degree increment), returning the number of nodes rotated\r
+worldedit.rotate = function(pos1, pos2, axis, angle)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
 \r
+       local axis1, axis2\r
+       if axis == "x" then\r
+               axis1, axis2 = "z", "y"\r
+       elseif axis == "y" then\r
+               axis1, axis2 = "x", "z"\r
+       else --axis == "z"\r
+               axis1, axis2 = "y", "x"\r
+       end\r
        angle = angle % 360\r
 \r
-       local pos = {x=pos1.x, y=0, z=0}\r
-       local newpos = {x=0, y=0, z=0}\r
-       local offsetx, offsetz\r
-       local env = minetest.env\r
-\r
        if angle == 90 then\r
-               worldedit.transpose(pos1, pos2, "x", "z")\r
-               pos1.x, pos1.z = pos1.z, pos1.x\r
-               pos2.x, pos2.z = pos2.z, pos2.x\r
-               worldedit.flip(pos1, pos2, "z")\r
+               worldedit.transpose(pos1, pos2, axis1, axis2)\r
+               worldedit.flip(pos1, pos2, axis2)\r
        elseif angle == 180 then\r
-               worldedit.flip(pos1, pos2, "x")\r
-               worldedit.flip(pos1, pos2, "z")\r
+               worldedit.flip(pos1, pos2, axis1)\r
+               worldedit.flip(pos1, pos2, axis2)\r
        elseif angle == 270 then\r
-               worldedit.transpose(pos1, pos2, "x", "z")\r
-               pos1.x, pos1.z = pos1.z, pos1.x\r
-               pos2.x, pos2.z = pos2.z, pos2.x\r
-               worldedit.flip(pos1, pos2, "x")\r
-       else\r
-               return 0\r
+               worldedit.transpose(pos1, pos2, axis1, axis2)\r
+               worldedit.flip(pos1, pos2, axis1)\r
        end\r
        return worldedit.volume(pos1, pos2)\r
 end\r
@@ -366,4 +640,68 @@ worldedit.deserialize_old = function(originpos, value)
                count = count + 1\r
        end\r
        return count\r
+end\r
+\r
+--saves the nodes and meta defined by positions `pos1` and `pos2` into a file, returning the number of nodes saved\r
+worldedit.metasave = function(pos1, pos2, file) --wip: simply work with strings instead of doing IO\r
+       local path = minetest.get_worldpath() .. "/schems"\r
+       local filename = path .. "/" .. file .. ".wem"\r
+       os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist\r
+       local rows = {}\r
+       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+       local pos = {x=pos1.x, y=0, z=0}\r
+       local count = 0\r
+       local result = {}\r
+       local env = minetest.env\r
+       while pos.x <= pos2.x do\r
+               pos.y = pos1.y\r
+               while pos.y <= pos2.y do\r
+                       pos.z = pos1.z\r
+                       while pos.z <= pos2.z do\r
+                               local node = env:get_node(pos)\r
+                               if node.name ~= "air" and node.name ~= "ignore" then\r
+                                       count = count + 1\r
+                                       local row = {\r
+                                               x = pos.x-pos1.x,\r
+                                               y = pos.y-pos1.y,\r
+                                               z = pos.z-pos1.z,\r
+                                               name = node.name,\r
+                                               param1 = node.param1,\r
+                                               param2 = node.param2,\r
+                                               meta = env:get_meta(pos):to_table(),\r
+                                       }\r
+                                       table.insert(rows, row)\r
+                               end\r
+                               pos.z = pos.z + 1\r
+                       end\r
+                       pos.y = pos.y + 1\r
+               end\r
+               pos.x = pos.x + 1\r
+       end\r
+       local err = table.save(rows,filename)\r
+       if err then return _,err end\r
+       return count\r
+end\r
+\r
+--loads the nodes and meta from `file` to position `pos1`, returning the number of nodes loaded\r
+worldedit.metaload = function(pos1, file) --wip: simply work with strings instead of doing IO\r
+       local filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"\r
+       local rows, err = table.load(filename)\r
+       if err then return _,err end\r
+       local pos = {x=0, y=0, z=0}\r
+       local node = {name="", param1=0, param2=0}\r
+       local count = 0\r
+       local env = minetest.env\r
+       for i,row in pairs(rows) do\r
+               pos.x = pos1.x + tonumber(row.x)\r
+               pos.y = pos1.y + tonumber(row.y)\r
+               pos.z = pos1.z + tonumber(row.z)\r
+               node.name = row.name\r
+               node.param1 = row.param1\r
+               node.param2 = row.param2\r
+               env:add_node(pos, node)\r
+               env:get_meta(pos):from_table(row.meta)\r
+               count = count + 1\r
+       end\r
+       return count\r
 end
\ No newline at end of file