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