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
\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
--- /dev/null
+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
-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
\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
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
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