]> git.lizzy.rs Git - worldedit.git/commitdiff
Add //homogenize, //lua, and //luatransform commands, as well as their documentation.
authorAnthony Zhang <azhang9@gmail.com>
Sat, 12 Jan 2013 23:20:41 +0000 (18:20 -0500)
committerAnthony Zhang <azhang9@gmail.com>
Sat, 12 Jan 2013 23:20:41 +0000 (18:20 -0500)
Chat Commands.md
WorldEdit API.md
worldedit/code.lua [new file with mode: 0644]
worldedit/init.lua
worldedit_commands/init.lua

index bfca8bfd714ca3ad3e1963bd0d9657dadf12f20f..23920a4860bccc6423b3e5f3e4a43c7f4689c4a3 100644 (file)
@@ -238,4 +238,18 @@ Save the current WorldEdit region including metadata to "(world folder)/schems/<
 Load nodes and metadata from "(world folder)/schems/<file>.wem" with position 1 of the current WorldEdit region as the origin.\r
 \r
     //metaload some random filename\r
-    //metaload huge_base
\ No newline at end of file
+    //metaload huge_base\r
+\r
+### //lua <code>\r
+\r
+Executes <code> as a Lua chunk in the global namespace.\r
+\r
+    //lua worldedit.pos1["singleplayer"] = {x=0, y=0, z=0}\r
+    //lua worldedit.rotate(worldedit.pos1["singleplayer"], worldedit.pos2["singleplayer"], "y", 90)\r
+\r
+### //luatransform <code>\r
+\r
+Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region.\r
+\r
+    //luatransform minetest.env:add_node(pos, {name="default:stone"})\r
+    //luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"})
\ No newline at end of file
index a0a1a60666736cb099e2870f23004ff6493e8efe..0b383edfbb92661f48370822b3dca6522f287942 100644 (file)
@@ -182,4 +182,20 @@ Returns the number of nodes saved.
 \r
 Loads the nodes and meta from `file` to position `pos1`.\r
 \r
-Returns the number of nodes loaded.
\ No newline at end of file
+Returns the number of nodes loaded.\r
+\r
+Code\r
+----\r
+Contained in code.lua, this module allows arbitrary Lua code to be used with WorldEdit.\r
+\r
+### error = worldedit.lua(code)\r
+\r
+Executes `code` as a Lua chunk in the global namespace.\r
+\r
+Returns an error if the code fails or nil otherwise.\r
+\r
+### error = worldedit.luatransform(pos1, pos2, code)\r
+\r
+Executes `code` as a Lua chunk in the global namespace with the variable pos available, for each node in a region defined by positions `pos1` and `pos2`.\r
+\r
+Returns an error if the code fails or nil otherwise.
\ No newline at end of file
diff --git a/worldedit/code.lua b/worldedit/code.lua
new file mode 100644 (file)
index 0000000..ee2d340
--- /dev/null
@@ -0,0 +1,43 @@
+worldedit = worldedit or {}\r
+\r
+--executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise\r
+worldedit.lua = function(code)\r
+       local operation, message = loadstring(code)\r
+       if operation == nil then --code parsing failed\r
+               return message\r
+       end\r
+       local status, message = pcall(operation)\r
+       if status == nil then --operation failed\r
+               return message\r
+       end\r
+       return nil\r
+end\r
+\r
+--executes `code` as a Lua chunk in the global namespace with the variable pos available, for each node in a region defined by positions `pos1` and `pos2`, returning an error if the code fails or nil otherwise\r
+worldedit.luatransform = function(pos1, pos2, code)\r
+       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\r
+       local factory, message = loadstring("return function(pos) " .. code .. " end")\r
+       if factory == nil then --code parsing failed\r
+               return message\r
+       end\r
+       local operation = factory()\r
+\r
+       local pos = {x=pos1.x, y=0, z=0}\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 status, message = pcall(operation, pos)\r
+                               if status == nil then --operation failed\r
+                                       return message\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 nil\r
+end
\ No newline at end of file
index 6f841eb42fc52d13e75488f12e02ab15b369a70a..948d317604ff66e527e92899f701702eca563835 100644 (file)
@@ -1,4 +1,6 @@
-dofile(minetest.get_modpath("worldedit") .. "/manipulations.lua")\r
-dofile(minetest.get_modpath("worldedit") .. "/primitives.lua")\r
-dofile(minetest.get_modpath("worldedit") .. "/visualization.lua")\r
-dofile(minetest.get_modpath("worldedit") .. "/serialization.lua")
\ No newline at end of file
+local path = minetest.get_modpath("worldedit")\r
+dofile(path .. "/manipulations.lua")\r
+dofile(path .. "/primitives.lua")\r
+dofile(path .. "/visualization.lua")\r
+dofile(path .. "/serialization.lua")\r
+dofile(path .. "/code.lua")
\ No newline at end of file
index 15220c6fe971071ac8e8b74143c5d1c9e2d9da31..a2e6246a9fdb0c988ef89bc91526131241ad9312 100644 (file)
@@ -169,7 +169,7 @@ minetest.register_chatcommand("/set", {
 \r
 minetest.register_chatcommand("/replace", {\r
        params = "<search node> <replace node>",\r
-       description = "Replace all instances of <search node> with <place node> in the current WorldEdit region",\r
+       description = "Replace all instances of <search node> with <replace node> in the current WorldEdit region",\r
        privs = {worldedit=true},\r
        func = function(name, param)\r
                local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
@@ -197,6 +197,27 @@ minetest.register_chatcommand("/replace", {
        end,\r
 })\r
 \r
+minetest.register_chatcommand("/homogenize", {\r
+       params = "<node>",\r
+       description = "Replace all non-air nodes with <node> in the current WorldEdit region",\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 not worldedit.node_is_valid(param) then\r
+                       minetest.chat_send_player(name, "Invalid node name: " .. param)\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.homogenize(pos1, pos2, param)\r
+               minetest.chat_send_player(name, count .. " nodes homogenized")\r
+       end,\r
+})\r
+\r
 minetest.register_chatcommand("/hollowsphere", {\r
        params = "<radius> <node>",\r
        description = "Add hollow sphere at WorldEdit position 1 with radius <radius>, composed of <node>",\r
@@ -811,9 +832,43 @@ minetest.register_chatcommand("/metaload", {
                end\r
                local count, err = worldedit.metaload(pos1, param)\r
                if err then\r
-                       minetest.chat_send_player(name, "error loading file: " .. err)\r
+                       minetest.chat_send_player(name, "Error loading file: " .. err)\r
                else\r
                        minetest.chat_send_player(name, count .. " nodes loaded")\r
                end\r
        end,\r
 })\r
+\r
+minetest.register_chatcommand("/lua", {\r
+       params = "<code>",\r
+       description = "Executes <code> as a Lua chunk in the global namespace",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local err = worldedit.lua(param)\r
+               if err then\r
+                       minetest.chat_send_player(name, "Code error: " .. err)\r
+               else\r
+                       minetest.chat_send_player(name, "Code successfully executed")\r
+               end\r
+       end,\r
+})\r
+\r
+minetest.register_chatcommand("/luatransform", {\r
+       params = "<code>",\r
+       description = "Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region",\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 err = worldedit.luatransform(pos1, pos2, param)\r
+               if err then\r
+                       minetest.chat_send_player(name, "Code error: " .. err)\r
+               else\r
+                       minetest.chat_send_player(name, "Code successfully executed")\r
+               end\r
+       end,\r
+})
\ No newline at end of file