]> git.lizzy.rs Git - worldedit.git/commitdiff
Add the //transpose, //flip, and //rotate commands as well as their documentation...
authorAnthony Zhang <azhang9@gmail.com>
Sun, 22 Jul 2012 22:12:29 +0000 (18:12 -0400)
committerAnthony Zhang <azhang9@gmail.com>
Sun, 22 Jul 2012 22:12:29 +0000 (18:12 -0400)
README.md
functions.lua
init.lua

index 2d69c92bfb7ee89849ef9818ef96240057c0d0a9..99b06ff82ad9d35159ffb72ea2c4b98ba17e4a72 100644 (file)
--- a/README.md
+++ b/README.md
@@ -107,6 +107,30 @@ Stack the current WorldEdit region along the x/y/z axis <count> times.
     //stack y -1
     //stack z +5
 
+### //transpose x/y/z x/y/z
+
+Transpose the current WorldEdit region along the x/y/z and x/y/z axes.
+
+    //transpose x y
+    //transpose x z
+    //transpose y z
+
+### //flip x/y/z
+
+Flip the current WorldEdit region along the x/y/z axis.
+
+   //flip x
+   //flip y
+   //flip z
+
+### //rotate
+
+Rotate the current WorldEdit region around the y axis by angle <angle> (90 degree increment).
+
+    //rotate 90
+    //rotate 180
+    //rotate 270
+
 ### //dig
 
 Dig the current WorldEdit region.
@@ -163,10 +187,28 @@ Returns the number of nodes moved.
 
 ### worldedit.stack(pos1, pos2, axis, count)
 
-duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times.
+Duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times.
 
 Returns the number of nodes stacked.
 
+### worldedit.transpose(pos1, pos2, axis1, axis2)
+
+Transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes ("x" or "y" or "z").
+
+Returns the number of nodes transposed.
+
+### worldedit.flip(pos1, pos2, axis)
+
+Flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z").
+
+Returns the number of nodes flipped.
+
+### worldedit.rotate(pos1, pos2, angle)
+
+Rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around the y axis (supporting 90 degree increments only).
+
+Returns the number of nodes rotated.
+
 ### worldedit.dig(pos1, pos2)
 
 Digs a region defined by positions `pos1` and `pos2`.
index 1e727db3fa2c11033906845985800d0b61f325cf..7912b9eda6762bf9aec51fa8ee9a4fa72e102566 100644 (file)
@@ -84,7 +84,7 @@ 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, node)\r
+                                       local node = env:get_node(pos)\r
                                        local value = pos[axis]\r
                                        pos[axis] = value - amount\r
                                        env:add_node(pos, node)\r
@@ -102,7 +102,7 @@ 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, node)\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
@@ -129,7 +129,7 @@ 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, node)\r
+                                       local node = env:get_node(pos)\r
                                        env:remove_node(pos)\r
                                        local value = pos[axis]\r
                                        pos[axis] = value - amount\r
@@ -148,7 +148,7 @@ 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, node)\r
+                                       local node = minetest.env:get_node(pos)\r
                                        env:remove_node(pos)\r
                                        local value = pos[axis]\r
                                        pos[axis] = value + amount\r
@@ -181,6 +181,95 @@ worldedit.stack = function(pos1, pos2, axis, count)
        return worldedit.volume(pos1, pos2)\r
 end\r
 \r
+--transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes, returning the number of nodes transposed\r
+worldedit.transpose = function(pos1, pos2, axis1, axis2)\r
+       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\r
+       local pos = {x=pos1.x, y=0, z=0}\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 extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]\r
+                               if extent1 < extent2 then\r
+                                       local node1 = env:get_node(pos)\r
+                                       local value1, value2 = pos[axis1], pos[axis2]\r
+                                       pos[axis1], pos[axis2] = pos1[axis1] + extent1, pos1[axis2] + extent2\r
+                                       local node2 = env:get_node(pos)\r
+                                       env:add_node(pos, node1)\r
+                                       pos[axis1], pos[axis2] = value1, value2\r
+                                       env:add_node(pos, node2)\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
+       return worldedit.volume(pos1, pos2)\r
+end\r
+\r
+--flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z"), returning the number of nodes flipped\r
+worldedit.flip = function(pos1, pos2, axis)\r
+       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\r
+       local pos = {x=pos1.x, y=0, z=0}\r
+       local start = pos1[axis] + pos2[axis]\r
+       pos2[axis] = pos1[axis] + math.floor((pos2[axis] - pos1[axis]) / 2)\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 node1 = env:get_node(pos)\r
+                               local value = pos[axis]\r
+                               pos[axis] = start - value\r
+                               local node2 = env:get_node(pos)\r
+                               env:add_node(pos, node1)\r
+                               pos[axis] = value\r
+                               env:add_node(pos, node2)\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
+       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
+       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\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
+       elseif angle == 180 then\r
+               worldedit.flip(pos1, pos2, "x")\r
+               worldedit.flip(pos1, pos2, "z")\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
+       end\r
+       return worldedit.volume(pos1, pos2)\r
+end\r
+\r
 --digs a region defined by positions `pos1` and `pos2`, returning the number of nodes dug\r
 worldedit.dig = function(pos1, pos2)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
index d5c4eb527497e4bc1c245c13c04de86e85a6d081..5c37567138723f654da5ee6bdd273364ac9661a8 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -162,7 +162,7 @@ minetest.register_chatcommand("/replace", {
                        return\r
                end\r
 \r
-               local found, _, searchnode, replacenode = param:find("([^%s]+)%s+([^%s]+)")\r
+               local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+([^%s]+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
@@ -192,7 +192,7 @@ minetest.register_chatcommand("/copy", {
                        return\r
                end\r
 \r
-               local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")\r
+               local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
@@ -214,7 +214,7 @@ minetest.register_chatcommand("/move", {
                        return\r
                end\r
 \r
-               local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")\r
+               local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
@@ -236,7 +236,7 @@ minetest.register_chatcommand("/stack", {
                        return\r
                end\r
 \r
-               local found, _, axis, count = param:find("([xyz])%s+([+-]?%d+)")\r
+               local found, _, axis, count = param:find("^([xyz])%s+([+-]?%d+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
@@ -247,6 +247,79 @@ minetest.register_chatcommand("/stack", {
        end,\r
 })\r
 \r
+minetest.register_chatcommand("/transpose", {\r
+       params = "x/y/z x/y/z",\r
+       description = "Transpose the current WorldEdit region along the x/y/z and x/y/z axes",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
+               if pos1 == nil or pos2 == nil then\r
+                       minetest.chat_send_player(name, "No WorldEdit region selected")\r
+                       return\r
+               end\r
+\r
+               local found, _, axis1, axis2 = param:find("^([xyz])%s+([xyz])$")\r
+               if found == nil then\r
+                       minetest.chat_send_player(name, "Invalid usage: " .. param)\r
+                       return\r
+               end\r
+               if axis1 == axis2 then\r
+                       minetest.chat_send_player(name, "Invalid usage: axes are the same")\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.transpose(pos1, pos2, axis1, axis2)\r
+               minetest.chat_send_player(name, count .. " nodes transposed")\r
+       end,\r
+})\r
+\r
+minetest.register_chatcommand("/flip", {\r
+       params = "x/y/z",\r
+       description = "Flip the current WorldEdit region along the x/y/z axis",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
+               if pos1 == nil or pos2 == nil then\r
+                       minetest.chat_send_player(name, "No WorldEdit region selected")\r
+                       return\r
+               end\r
+\r
+               if param ~= "x" and param ~= "y" and param ~= "z" then\r
+                       minetest.chat_send_player(name, "Invalid usage: " .. param)\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.flip(pos1, pos2, param)\r
+               minetest.chat_send_player(name, count .. " nodes flipped")\r
+       end,\r
+})\r
+\r
+minetest.register_chatcommand("/rotate", {\r
+       params = "<angle>",\r
+       description = "Rotate the current WorldEdit region around the y axis by angle <angle> (90 degree increment)",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
+               if pos1 == nil or pos2 == nil then\r
+                       minetest.chat_send_player(name, "No WorldEdit region selected")\r
+                       return\r
+               end\r
+\r
+               angle = tonumber(param)\r
+               if angle == nil then\r
+                       minetest.chat_send_player(name, "Invalid usage: " .. param)\r
+                       return\r
+               end\r
+               if angle % 90 ~= 0 then\r
+                       minetest.chat_send_player(name, "Invalid usage: angle must be multiple of 90")\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.rotate(pos1, pos2, angle)\r
+               minetest.chat_send_player(name, count .. " nodes rotated")\r
+       end,\r
+})\r
+\r
 minetest.register_chatcommand("/dig", {\r
        params = "",\r
        description = "Dig the current WorldEdit region",\r