]> git.lizzy.rs Git - worldedit.git/blobdiff - init.lua
Remove unused Code
[worldedit.git] / init.lua
index 9c72ac1b372ad3e252d66d85c59171f73c973704..7bd79beaeeafb2a83f2cbd722662c60f4083eb6f 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -1,8 +1,5 @@
 minetest.register_privilege("worldedit", "Can use WorldEdit commands")\r
 \r
---wip: check to make sure player positions are set before doing editing\r
---wip; fix meseconedit to export to new WorldEdit format\r
-\r
 worldedit = {}\r
 \r
 worldedit.set_pos = {}\r
@@ -15,14 +12,20 @@ dofile(minetest.get_modpath("worldedit") .. "/mark.lua")
 \r
 --determines whether `nodename` is a valid node name, returning a boolean\r
 worldedit.node_is_valid = function(temp_pos, nodename)\r
-       local originalnode = minetest.env:get_node(temp_pos) --save the original node to restore later\r
-       minetest.env:add_node(temp_pos, {name=nodename}) --attempt to add the node\r
-       local value = minetest.env:get_node(temp_pos).name --obtain the name of the newly added node\r
-       if value == nodename or value == "default:" .. nodename then --successfully added node\r
-               minetest.env:add_node(temp_pos, originalnode) --restore the original node\r
-               return true --node is valid\r
+       return minetest.registered_nodes[nodename] ~= nil\r
+       or minetest.registered_nodes["default:" .. nodename] ~= nil\r
+end\r
+\r
+worldedit.player_axis = function(name)\r
+       local dir = minetest.env:get_player_by_name(name):get_look_dir()\r
+       if dir.x > dir.y then\r
+               if dir.x > dir.z then\r
+                       return "x"\r
+               end\r
+       elseif dir.y > dir.z then\r
+               return "y"\r
        end\r
-       return false --node is not valid\r
+       return "z"\r
 end\r
 \r
 minetest.register_chatcommand("/reset", {\r
@@ -38,18 +41,27 @@ minetest.register_chatcommand("/reset", {
        end,\r
 })\r
 \r
+minetest.register_chatcommand("/mark", {\r
+       params = "",\r
+       description = "Show markers at the region positions",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               worldedit.mark_pos1(name)\r
+               worldedit.mark_pos2(name)\r
+               minetest.chat_send_player(name, "WorldEdit region marked")\r
+       end,\r
+})\r
+\r
 minetest.register_chatcommand("/pos1", {\r
        params = "",\r
        description = "Set WorldEdit region position 1 to the player's location",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos = minetest.env:get_player_by_name(name):getpos()\r
-               pos.x = math.floor(pos.x)\r
-               pos.y = math.floor(pos.y)\r
-               pos.z = math.floor(pos.z)\r
+               pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)\r
                worldedit.pos1[name] = pos\r
                worldedit.mark_pos1(name)\r
-               minetest.chat_send_player(name, "WorldEdit position 1 set to (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
+               minetest.chat_send_player(name, "WorldEdit position 1 set to " .. minetest.pos_to_string(pos))\r
        end,\r
 })\r
 \r
@@ -59,12 +71,10 @@ minetest.register_chatcommand("/pos2", {
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos = minetest.env:get_player_by_name(name):getpos()\r
-               pos.x = math.floor(pos.x)\r
-               pos.y = math.floor(pos.y)\r
-               pos.z = math.floor(pos.z)\r
+               pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)\r
                worldedit.pos2[name] = pos\r
                worldedit.mark_pos2(name)\r
-               minetest.chat_send_player(name, "WorldEdit position 2 set to (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
+               minetest.chat_send_player(name, "WorldEdit position 2 set to " .. minetest.pos_to_string(pos))\r
        end,\r
 })\r
 \r
@@ -77,10 +87,16 @@ minetest.register_chatcommand("/p", {
                        worldedit.set_pos[name] = 1\r
                        minetest.chat_send_player(name, "Select positions by punching two nodes")\r
                elseif param == "get" then --display current WorldEdit positions\r
-                       local pos = worldedit.pos1[name]\r
-                       minetest.chat_send_player(name, "WorldEdit position 1: (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
-                       local pos = worldedit.pos2[name]\r
-                       minetest.chat_send_player(name, "WorldEdit position 2: (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
+                       if worldedit.pos1[name] ~= nil then\r
+                               minetest.chat_send_player(name, "WorldEdit position 1: " .. minetest.pos_to_string(worldedit.pos1[name]))\r
+                       else\r
+                               minetest.chat_send_player(name, "WorldEdit position 1 not set")\r
+                       end\r
+                       if worldedit.pos2[name] ~= nil then\r
+                               minetest.chat_send_player(name, "WorldEdit position 2: " .. minetest.pos_to_string(worldedit.pos2[name]))\r
+                       else\r
+                               minetest.chat_send_player(name, "WorldEdit position 2 not set")\r
+                       end\r
                else\r
                        minetest.chat_send_player(name, "Unknown subcommand: " .. param)\r
                end\r
@@ -94,12 +110,12 @@ minetest.register_on_punchnode(function(pos, node, puncher)
                        worldedit.set_pos[name] = 2 --set position 2 on the next invocation\r
                        worldedit.pos1[name] = pos\r
                        worldedit.mark_pos1(name)\r
-                       minetest.chat_send_player(name, "WorldEdit region position 1 set to (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
+                       minetest.chat_send_player(name, "WorldEdit region position 1 set to " .. minetest.pos_to_string(pos))\r
                else --setting position 2\r
                        worldedit.set_pos[name] = nil --finished setting positions\r
                        worldedit.pos2[name] = pos\r
                        worldedit.mark_pos2(name)\r
-                       minetest.chat_send_player(name, "WorldEdit region position 2 set to (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")")\r
+                       minetest.chat_send_player(name, "WorldEdit region position 2 set to " .. minetest.pos_to_string(pos))\r
                end\r
        end\r
 end)\r
@@ -152,7 +168,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
@@ -171,9 +187,94 @@ minetest.register_chatcommand("/replace", {
        end,\r
 })\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
+       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, _, 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 = worldedit.player_axis(name)\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.hollow_cylinder(pos, axis, tonumber(length), tonumber(radius), nodename)\r
+               minetest.chat_send_player(name, count .. " nodes added")\r
+       end,\r
+})\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
+       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, _, 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 = worldedit.player_axis(name)\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.cylinder(pos, axis, tonumber(length), tonumber(radius), nodename)\r
+               minetest.chat_send_player(name, count .. " nodes added")\r
+       end,\r
+})\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
@@ -182,11 +283,14 @@ 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 = worldedit.player_axis(name)\r
+               end\r
 \r
                local count = worldedit.copy(pos1, pos2, axis, tonumber(amount))\r
                minetest.chat_send_player(name, count .. " nodes copied")\r
@@ -194,8 +298,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
@@ -204,11 +308,14 @@ 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 = worldedit.player_axis(name)\r
+               end\r
 \r
                local count = worldedit.move(pos1, pos2, axis, tonumber(amount))\r
                minetest.chat_send_player(name, count .. " nodes moved")\r
@@ -216,8 +323,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
@@ -226,17 +333,102 @@ 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 = worldedit.player_axis(name)\r
+               end\r
 \r
                local count = worldedit.stack(pos1, pos2, axis, tonumber(count))\r
                minetest.chat_send_player(name, count .. " nodes stacked")\r
        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 == "?" 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
+               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 == "?" 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
+               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
@@ -312,8 +504,13 @@ minetest.register_chatcommand("/load", {
                local value = file:read("*a")\r
                file:close()\r
 \r
-               local count = worldedit.deserialize(pos1, value)\r
+               local count\r
+               if value:find("{") then --old WorldEdit format\r
+                       count = worldedit.deserialize_old(pos1, value)\r
+               else --new WorldEdit format\r
+                       count = worldedit.deserialize(pos1, value)\r
+               end\r
 \r
                minetest.chat_send_player(name, count .. " nodes loaded")\r
        end,\r
-})
\ No newline at end of file
+})\r