]> git.lizzy.rs Git - worldedit.git/blobdiff - functions.lua
Merge pull request #5 from khonkhortisan/master
[worldedit.git] / functions.lua
index 425a6ac2dfa6a69741cdc023ad348707e95ccab3..b2f644387814d9305bc21d5ccdf285198c6fdaec 100644 (file)
@@ -1,4 +1,3 @@
-dofile(minetest.get_modpath("worldedit") .. "/spirals.lua")\r
 --modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions\r
 worldedit.sort_pos = function(pos1, pos2)\r
        pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
@@ -185,24 +184,77 @@ end
 \r
 --adds a spiral at `pos` with size `size`, returning the number of nodes changed\r
 worldedit.spiral = function(pos, size, nodename)\r
-    local shift_x,shift_y\r
+       local shift_x, shift_y\r
        sa = spiralt(size)\r
-    shift_y = #sa -- "Height" of the Array\r
-    local fe = sa[1]\r
-    shift_x = #fe -- "Width" of the Array\r
-    fe = nil\r
-    \r
-    local count = 0\r
-    for x,v in ipairs(sa) do\r
-        for y, z in ipairs(v) do\r
-            minetest.env:add_node({x=pos.x-shift_x+x,y=pos.y-shift_y+y,z=pos.z+z}, {name=nodename})\r
-            count = count + 1\r
-        end\r
-    end\r
-       \r
+       shift_y = #sa -- "Height" of the Array\r
+       local fe = sa[1]\r
+       shift_x = #fe -- "Width" of the Array\r
+       fe = nil\r
+\r
+       local count = 0\r
+       local node = {name=nodename}\r
+       for x, v in ipairs(sa) do\r
+               for y, z in ipairs(v) do\r
+                       minetest.env:add_node({x=pos.x - shift_x + x,y=pos.y - shift_y + y,z=pos.z + z}, node)\r
+                       count = count + 1\r
+               end\r
+       end\r
        return count\r
 end\r
 \r
+--wip: \r
+sign = function(s)\r
+       if s > 0 then\r
+               return 1\r
+       end\r
+       if s < 0 then\r
+               return -1\r
+       end\r
+       return 0\r
+end\r
+\r
+--wip: needs to be faster\r
+function spiral_index(y, x) -- returns the value at (x, y) in a spiral that starts at 1 and goes outwards\r
+       if y == -x and y >= x then\r
+               return (2 * y + 1) ^ 2\r
+       end\r
+       local l = math.max(math.abs(y), math.abs(x))\r
+       local value\r
+       if math.abs(y) == l then\r
+               value = x\r
+               if y < 0 then\r
+                       value = -value\r
+               end\r
+       else\r
+               value = y\r
+               if x < 0 then\r
+                       value = -value\r
+               end\r
+       end\r
+       t1 = l * 2\r
+       if x + y < 0 then\r
+               t1 = -t1\r
+       end\r
+       t2 = y ^ 2 - x ^ 2\r
+       if t2 < 0 then\r
+               t2 = -t2\r
+       end\r
+       return ((2 * l - 1) ^ 2) + (l * 4) + t1 + (t2 * (l - value))\r
+end\r
+\r
+--wip: needs to be faster\r
+function spiralt(side)\r
+       local spiral = {}\r
+       local start, stop = math.floor((-side+1)/2), math.floor((side-1)/2)\r
+       for i = 1, side do\r
+               spiral[i] = {}\r
+               for j = 1, side do\r
+                       spiral[i][j] = side ^ 2 - spiral_index(stop - i + 1,start + j - 1) --moves the coordinates so (0,0) is at the center of the spiral\r
+               end\r
+       end\r
+       return spiral\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
@@ -340,13 +392,13 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2)
                                        local node1 = env:get_node(pos)\r
                                        local meta1a = 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] = pos1[axis1] + extent2, pos1[axis2] + extent1\r
                                        local node2 = env:get_node(pos)\r
                                        local meta2a = env:get_meta(pos):to_table()\r
                                        env:add_node(pos, node1)\r
                                        local meta1b = env:get_meta(pos)\r
                                        meta1b:from_table(meta1a)\r
-                                       pos[axis1], pos[axis2] = value1, value2\r
+                                       pos[axis1], pos[axis2] = pos1[axis1] + extent1, pos1[axis2] + extent2\r
                                        env:add_node(pos, node2)\r
                                        local meta2b = env:get_meta(pos)\r
                                        meta2b:from_table(meta2a)\r
@@ -395,10 +447,17 @@ 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 (if you are looking in the negative direction) around the `axis` (supporting 90 degree increments only), 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
+       if axis == 'x' then\r
+               axes = {'z', 'y'}\r
+       elseif axis == 'y' then\r
+               axes = {'x', 'z'}\r
+       else--if axis == 'z' then\r
+               axes = {'y', 'x'}\r
+       end\r
        angle = angle % 360\r
 \r
        local pos = {x=pos1.x, y=0, z=0}\r
@@ -407,18 +466,14 @@ worldedit.rotate = function(pos1, pos2, angle)
        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, axes[1], axes[2])\r
+               worldedit.flip(pos1, pos2, axes[2])\r
        elseif angle == 180 then\r
-               worldedit.flip(pos1, pos2, "x")\r
-               worldedit.flip(pos1, pos2, "z")\r
+               worldedit.flip(pos1, pos2, axes[1])\r
+               worldedit.flip(pos1, pos2, axes[2])\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
+               worldedit.transpose(pos1, pos2, axes[1], axes[2])\r
+               worldedit.flip(pos1, pos2, axes[1])\r
        else\r
                return 0\r
        end\r