]> git.lizzy.rs Git - worldedit.git/blobdiff - init.lua
Fix player axis detection, make spirals code a bit easier to read.
[worldedit.git] / init.lua
index eae615b349532654492100cea2204cffd7632f2c..1c3d323cfa0793a4eff552607cd4f8f6483ee70e 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -16,6 +16,20 @@ worldedit.node_is_valid = function(temp_pos, nodename)
        or minetest.registered_nodes["default:" .. nodename] ~= nil\r
 end\r
 \r
+--determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)\r
+worldedit.player_axis = function(name)\r
+       local dir = minetest.env:get_player_by_name(name):get_look_dir()\r
+       local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z)\r
+       if x > y then\r
+               if x > z then\r
+                       return "x", dir.x > 0 and 1 or -1\r
+               end\r
+       elseif y > z then\r
+               return "y", dir.y > 0 and 1 or -1\r
+       end\r
+       return "z", dir.z > 0 and 1 or -1\r
+end\r
+\r
 minetest.register_chatcommand("/reset", {\r
        params = "",\r
        description = "Reset the region so that it is empty",\r
@@ -176,8 +190,8 @@ minetest.register_chatcommand("/replace", {
 })\r
 \r
 minetest.register_chatcommand("/hollowcylinder", {\r
-       params = "x/y/z <length> <radius> <node>",\r
-       description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z axis with length <length> and radius <radius>, composed of <node>",\r
+       params = "x/y/z/? <length> <radius> <node>",\r
+       description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos = worldedit.pos1[name]\r
@@ -186,11 +200,15 @@ minetest.register_chatcommand("/hollowcylinder", {
                        return\r
                end\r
 \r
-               local found, _, axis, length, radius, nodename = param:find("^([xyz])%s+([+-]?%d+)%s+(%d+)%s+([^%s]+)$")\r
+               local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+([^%s]+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
                end\r
+               if axis == "?" then\r
+                       axis, sign = worldedit.player_axis(name)\r
+                       length = length * sign\r
+               end\r
                if not worldedit.node_is_valid(pos, nodename) then\r
                        minetest.chat_send_player(name, "Invalid node name: " .. param)\r
                        return\r
@@ -201,9 +219,35 @@ minetest.register_chatcommand("/hollowcylinder", {
        end,\r
 })\r
 \r
+minetest.register_chatcommand("/spiral", {\r
+       params = "<size> <node>",\r
+       description = "Add spiral at WorldEdit position 1 with size <size>, composed of <node>",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local pos = worldedit.pos1[name]\r
+               if pos == nil then\r
+                       minetest.chat_send_player(name, "No WorldEdit region selected")\r
+                       return\r
+               end\r
+\r
+               local found, _, size, nodename = param:find("(%d+)%s+([^%s]+)$")\r
+               if found == nil then\r
+                       minetest.chat_send_player(name, "Invalid usage: " .. param)\r
+                       return\r
+               end\r
+               if not worldedit.node_is_valid(pos, nodename) then\r
+                       minetest.chat_send_player(name, "Invalid node name: " .. param)\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.spiral(pos, tonumber(size), nodename)\r
+               minetest.chat_send_player(name, count .. " nodes changed")\r
+       end,\r
+})\r
+\r
 minetest.register_chatcommand("/cylinder", {\r
-       params = "x/y/z <length> <radius> <node>",\r
-       description = "Add cylinder at WorldEdit position 1 along the x/y/z axis with length <length> and radius <radius>, composed of <node>",\r
+       params = "x/y/z/? <length> <radius> <node>",\r
+       description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos = worldedit.pos1[name]\r
@@ -212,11 +256,15 @@ minetest.register_chatcommand("/cylinder", {
                        return\r
                end\r
 \r
-               local found, _, axis, length, radius, nodename = param:find("^([xyz])%s+([+-]?%d+)%s+(%d+)%s+([^%s]+)$")\r
+               local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+([^%s]+)$")\r
                if found == nil then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
                end\r
+               if axis == "?" then\r
+                       axis, sign = worldedit.player_axis(name)\r
+                       length = length * sign\r
+               end\r
                if not worldedit.node_is_valid(pos, nodename) then\r
                        minetest.chat_send_player(name, "Invalid node name: " .. param)\r
                        return\r
@@ -228,8 +276,8 @@ minetest.register_chatcommand("/cylinder", {
 })\r
 \r
 minetest.register_chatcommand("/copy", {\r
-       params = "x/y/z <amount>",\r
-       description = "Copy the current WorldEdit region along the x/y/z axis by <amount> nodes",\r
+       params = "x/y/z/? <amount>",\r
+       description = "Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
@@ -238,11 +286,15 @@ 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
                end\r
+               if axis == "?" then\r
+                       axis, sign = worldedit.player_axis(name)\r
+                       amount = amount * sign\r
+               end\r
 \r
                local count = worldedit.copy(pos1, pos2, axis, tonumber(amount))\r
                minetest.chat_send_player(name, count .. " nodes copied")\r
@@ -250,8 +302,8 @@ minetest.register_chatcommand("/copy", {
 })\r
 \r
 minetest.register_chatcommand("/move", {\r
-       params = "x/y/z <amount>",\r
-       description = "Move the current WorldEdit region along the x/y/z axis by <amount> nodes",\r
+       params = "x/y/z/? <amount>",\r
+       description = "Move the current WorldEdit region along the x/y/z/? axis by <amount> nodes",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
@@ -260,11 +312,15 @@ 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
                end\r
+               if axis == "?" then\r
+                       axis, sign = worldedit.player_axis(name)\r
+                       amount = amount * sign\r
+               end\r
 \r
                local count = worldedit.move(pos1, pos2, axis, tonumber(amount))\r
                minetest.chat_send_player(name, count .. " nodes moved")\r
@@ -272,8 +328,8 @@ minetest.register_chatcommand("/move", {
 })\r
 \r
 minetest.register_chatcommand("/stack", {\r
-       params = "x/y/z <count>",\r
-       description = "Stack the current WorldEdit region along the x/y/z axis <count> times",\r
+       params = "x/y/z/? <count>",\r
+       description = "Stack the current WorldEdit region along the x/y/z/? axis <count> times",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
@@ -282,11 +338,15 @@ 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
                end\r
+               if axis == "?" then\r
+                       axis, sign = worldedit.player_axis(name)\r
+                       count = count * sign\r
+               end\r
 \r
                local count = worldedit.stack(pos1, pos2, axis, tonumber(count))\r
                minetest.chat_send_player(name, count .. " nodes stacked")\r
@@ -294,8 +354,8 @@ minetest.register_chatcommand("/stack", {
 })\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
+       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
@@ -304,11 +364,17 @@ minetest.register_chatcommand("/transpose", {
                        return\r
                end\r
 \r
-               local found, _, axis1, axis2 = param:find("^([xyz])%s+([xyz])$")\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 == "?" then\r
+                       axis1 = worldedit.player_axis(name)\r
+               end\r
+               if axis2 == "?" then\r
+                       axis2 = worldedit.player_axis(name)\r
+               end\r
                if axis1 == axis2 then\r
                        minetest.chat_send_player(name, "Invalid usage: axes are the same")\r
                        return\r
@@ -320,8 +386,8 @@ minetest.register_chatcommand("/transpose", {
 })\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
+       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
@@ -330,6 +396,9 @@ minetest.register_chatcommand("/flip", {
                        return\r
                end\r
 \r
+               if param == "?" then\r
+                       param = worldedit.player_axis(name)\r
+               end\r
                if param ~= "x" and param ~= "y" and param ~= "z" then\r
                        minetest.chat_send_player(name, "Invalid usage: " .. param)\r
                        return\r
@@ -450,4 +519,4 @@ minetest.register_chatcommand("/load", {
 \r
                minetest.chat_send_player(name, count .. " nodes loaded")\r
        end,\r
-})
\ No newline at end of file
+})\r