diff options
-rw-r--r-- | README.md | 44 | ||||
-rw-r--r-- | functions.lua | 97 | ||||
-rw-r--r-- | init.lua | 81 |
3 files changed, 213 insertions, 9 deletions
@@ -107,6 +107,30 @@ Stack the current WorldEdit region along the x/y/z axis <count> times. //stack y -1 //stack z +5 +### //transpose x/y/z x/y/z + +Transpose the current WorldEdit region along the x/y/z and x/y/z axes. + + //transpose x y + //transpose x z + //transpose y z + +### //flip x/y/z + +Flip the current WorldEdit region along the x/y/z axis. + + //flip x + //flip y + //flip z + +### //rotate + +Rotate the current WorldEdit region around the y axis by angle <angle> (90 degree increment). + + //rotate 90 + //rotate 180 + //rotate 270 + ### //dig Dig the current WorldEdit region. @@ -163,10 +187,28 @@ Returns the number of nodes moved. ### worldedit.stack(pos1, pos2, axis, count) -duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times. +Duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times. Returns the number of nodes stacked. +### worldedit.transpose(pos1, pos2, axis1, axis2) + +Transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes ("x" or "y" or "z"). + +Returns the number of nodes transposed. + +### worldedit.flip(pos1, pos2, axis) + +Flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z"). + +Returns the number of nodes flipped. + +### worldedit.rotate(pos1, pos2, angle) + +Rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around the y axis (supporting 90 degree increments only). + +Returns the number of nodes rotated. + ### worldedit.dig(pos1, pos2) Digs a region defined by positions `pos1` and `pos2`. diff --git a/functions.lua b/functions.lua index 1e727db..7912b9e 100644 --- a/functions.lua +++ b/functions.lua @@ -84,7 +84,7 @@ worldedit.copy = function(pos1, pos2, axis, amount) while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
- local node = env:get_node(pos, node)
+ local node = env:get_node(pos)
local value = pos[axis]
pos[axis] = value - amount
env:add_node(pos, node)
@@ -102,7 +102,7 @@ worldedit.copy = function(pos1, pos2, axis, amount) while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
- local node = minetest.env:get_node(pos, node)
+ local node = minetest.env:get_node(pos)
local value = pos[axis]
pos[axis] = value + amount
minetest.env:add_node(pos, node)
@@ -129,7 +129,7 @@ worldedit.move = function(pos1, pos2, axis, amount) while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
- local node = env:get_node(pos, node)
+ local node = env:get_node(pos)
env:remove_node(pos)
local value = pos[axis]
pos[axis] = value - amount
@@ -148,7 +148,7 @@ worldedit.move = function(pos1, pos2, axis, amount) while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
- local node = minetest.env:get_node(pos, node)
+ local node = minetest.env:get_node(pos)
env:remove_node(pos)
local value = pos[axis]
pos[axis] = value + amount
@@ -181,6 +181,95 @@ worldedit.stack = function(pos1, pos2, axis, count) return worldedit.volume(pos1, pos2)
end
+--transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes, returning the number of nodes transposed
+worldedit.transpose = function(pos1, pos2, axis1, axis2)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local env = minetest.env
+ 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 extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]
+ if extent1 < extent2 then
+ local node1 = env:get_node(pos)
+ local value1, value2 = pos[axis1], pos[axis2]
+ pos[axis1], pos[axis2] = pos1[axis1] + extent1, pos1[axis2] + extent2
+ local node2 = env:get_node(pos)
+ env:add_node(pos, node1)
+ pos[axis1], pos[axis2] = value1, value2
+ env:add_node(pos, node2)
+ end
+ pos.z = pos.z + 1
+ end
+ pos.y = pos.y + 1
+ end
+ pos.x = pos.x + 1
+ end
+ return worldedit.volume(pos1, pos2)
+end
+
+--flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z"), returning the number of nodes flipped
+worldedit.flip = function(pos1, pos2, axis)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local start = pos1[axis] + pos2[axis]
+ pos2[axis] = pos1[axis] + math.floor((pos2[axis] - pos1[axis]) / 2)
+ local env = minetest.env
+ 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 node1 = env:get_node(pos)
+ local value = pos[axis]
+ pos[axis] = start - value
+ local node2 = env:get_node(pos)
+ env:add_node(pos, node1)
+ pos[axis] = value
+ env:add_node(pos, node2)
+ pos.z = pos.z + 1
+ end
+ pos.y = pos.y + 1
+ end
+ pos.x = pos.x + 1
+ end
+ return worldedit.volume(pos1, pos2)
+end
+
+--rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around the y axis (supporting 90 degree increments only), returning the number of nodes rotated
+worldedit.rotate = function(pos1, pos2, angle)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ angle = angle % 360
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local newpos = {x=0, y=0, z=0}
+ local offsetx, offsetz
+ local env = minetest.env
+
+ if angle == 90 then
+ worldedit.transpose(pos1, pos2, "x", "z")
+ pos1.x, pos1.z = pos1.z, pos1.x
+ pos2.x, pos2.z = pos2.z, pos2.x
+ worldedit.flip(pos1, pos2, "z")
+ elseif angle == 180 then
+ worldedit.flip(pos1, pos2, "x")
+ worldedit.flip(pos1, pos2, "z")
+ elseif angle == 270 then
+ worldedit.transpose(pos1, pos2, "x", "z")
+ pos1.x, pos1.z = pos1.z, pos1.x
+ pos2.x, pos2.z = pos2.z, pos2.x
+ worldedit.flip(pos1, pos2, "x")
+ else
+ return 0
+ end
+ return worldedit.volume(pos1, pos2)
+end
+
--digs a region defined by positions `pos1` and `pos2`, returning the number of nodes dug
worldedit.dig = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
@@ -162,7 +162,7 @@ minetest.register_chatcommand("/replace", { return
end
- local found, _, searchnode, replacenode = param:find("([^%s]+)%s+([^%s]+)")
+ local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+([^%s]+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
@@ -192,7 +192,7 @@ minetest.register_chatcommand("/copy", { return
end
- local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")
+ local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
@@ -214,7 +214,7 @@ minetest.register_chatcommand("/move", { return
end
- local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")
+ local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
@@ -236,7 +236,7 @@ minetest.register_chatcommand("/stack", { return
end
- local found, _, axis, count = param:find("([xyz])%s+([+-]?%d+)")
+ local found, _, axis, count = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
@@ -247,6 +247,79 @@ minetest.register_chatcommand("/stack", { end,
})
+minetest.register_chatcommand("/transpose", {
+ params = "x/y/z x/y/z",
+ description = "Transpose the current WorldEdit region along the x/y/z and x/y/z axes",
+ 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 found, _, axis1, axis2 = param:find("^([xyz])%s+([xyz])$")
+ if found == nil then
+ minetest.chat_send_player(name, "Invalid usage: " .. param)
+ return
+ end
+ if axis1 == axis2 then
+ minetest.chat_send_player(name, "Invalid usage: axes are the same")
+ return
+ end
+
+ local count = worldedit.transpose(pos1, pos2, axis1, axis2)
+ minetest.chat_send_player(name, count .. " nodes transposed")
+ end,
+})
+
+minetest.register_chatcommand("/flip", {
+ params = "x/y/z",
+ description = "Flip the current WorldEdit region along the x/y/z axis",
+ 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 param ~= "x" and param ~= "y" and param ~= "z" then
+ minetest.chat_send_player(name, "Invalid usage: " .. param)
+ return
+ end
+
+ local count = worldedit.flip(pos1, pos2, param)
+ minetest.chat_send_player(name, count .. " nodes flipped")
+ end,
+})
+
+minetest.register_chatcommand("/rotate", {
+ params = "<angle>",
+ description = "Rotate the current WorldEdit region around the y axis by angle <angle> (90 degree increment)",
+ 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
+
+ angle = tonumber(param)
+ if angle == nil then
+ minetest.chat_send_player(name, "Invalid usage: " .. param)
+ return
+ end
+ if angle % 90 ~= 0 then
+ minetest.chat_send_player(name, "Invalid usage: angle must be multiple of 90")
+ return
+ end
+
+ local count = worldedit.rotate(pos1, pos2, angle)
+ minetest.chat_send_player(name, count .. " nodes rotated")
+ end,
+})
+
minetest.register_chatcommand("/dig", {
params = "",
description = "Dig the current WorldEdit region",
|