From 35d8e64aab6d48520018e32c76386918260fe205 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 21:45:25 -0600 Subject: [PATCH 01/17] Add outset, inset, shift and initial draft of expand --- worldedit_commands/.gitignore | 1 + worldedit_commands/area.lua | 285 ++++++++++++++++++++++++++++++++++ worldedit_commands/init.lua | 2 + 3 files changed, 288 insertions(+) create mode 100644 worldedit_commands/.gitignore create mode 100644 worldedit_commands/area.lua diff --git a/worldedit_commands/.gitignore b/worldedit_commands/.gitignore new file mode 100644 index 0000000..e4e5f6c --- /dev/null +++ b/worldedit_commands/.gitignore @@ -0,0 +1 @@ +*~ \ No newline at end of file diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua new file mode 100644 index 0000000..c3a16e6 --- /dev/null +++ b/worldedit_commands/area.lua @@ -0,0 +1,285 @@ +minetest.register_chatcommand( + "/outset", + { + params = " [h|v]", + description = "expand the selection", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + local message + + if find == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + message = "Undefined region. Region must be defined beforehand." + else + amount = tonumber(amount) + local curpos1 = worldedit.pos1[name] + local curpos2 = worldedit.pos2[name] + + local dirs = worldedit.get_outset_directions(curpos1, curpos2) + + if dir == 'h' then + worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1) + worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1) + + worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2) + worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2) + + message = "area expanded by " .. amount .. " blocks horizontally" + elseif dir == 'v' then + worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1) + worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2) + + message = "area expanded by " .. amount .. " blocks vertically" + else + worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1) + worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1) + worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1) + + worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2) + worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2) + worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2) + + message = "area expanded by " .. amount .. " blocks in all axes" + end + + worldedit.mark_pos1(name) + worldedit.mark_pos2(name) + end + + worldedit.player_notify(name, message) + end, + } +) + +minetest.register_chatcommand( + "/inset", + { + params = " [h|v]", + description = "contract", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + local message = "" + + if find == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + message = "Undefined region. Region must be defined beforehand." + else + amount = tonumber(amount) + local curpos1 = worldedit.pos1[name] + local curpos2 = worldedit.pos2[name] + + local dirs = worldedit.get_outset_directions(curpos1, curpos2) + + if dir == 'h' then + worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1) + worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1) + + worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2) + worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2) + + message = "area contracted by " .. amount .. " blocks horizontally" + elseif dir == 'v' then + worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1) + worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2) + + message = "area contracted by " .. amount .. " blocks vertically" + else + worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1) + worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1) + worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1) + + worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2) + worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2) + worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2) + + message = "area contracted by " .. amount .. " blocks in all axes" + end + + worldedit.mark_pos1(name) + worldedit.mark_pos2(name) + end + + worldedit.player_notify(name, message) + end, + } +) + +worldedit.get_outset_directions = function(mark1, mark2) + if mark1 == nil or mark2 == nil then return + end + + local dirs = + { + x1 = 0, + x2 = 0, + y1 = 0, + y2 = 0, + z1 = 0, + z2 = 0 + } + + if mark1.x < mark2.x then + dirs.x1 = -1 + dirs.x2 = 1 + else + dirs.x1 = 1 + dirs.x2 = -1 + end + + if mark1.y < mark2.y then + dirs.y1 = -1 + dirs.y2 = 1 + else + dirs.y1 = 1 + dirs.y2 = -1 + end + + if mark1.z < mark2.z then + dirs.z1 = -1 + dirs.z2 = 1 + else + dirs.z1 = 1 + dirs.z2 = -1 + end + + return dirs +end + + +minetest.register_chatcommand( + "/shift", + { + params = "[+|-] [x|y|z]", + description = "Moves the selection region. Does not move contents.", + privs = {worldedit=true}, + func = function(name, param) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + local find, _, sign, amount, axis = param:find("^([+-]?)(%d+)[%s+]?([xyz]?)$") + + if find == nil then + minetest.debug("entering if") + worldedit.player_notify(name, "invalid usage: " .. param) + return + end + + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") + return + end + + amount = tonumber(amount) + + local direction = "" + + if sign ~= nil and sign == '-' then + amount = amount * -1 + end + + if axis == "" then + direction, _ = worldedit.player_axis(name) + amount = amount * _ + else + direction = axis + end + + if direction == 'x' then + worldedit.pos1[name].x = pos1.x + amount + worldedit.pos2[name].x = pos2.x + amount + elseif direction == 'y' then + worldedit.pos1[name].y = pos1.y + amount + worldedit.pos2[name].y = pos2.y + amount + elseif direction == 'z' then + worldedit.pos1[name].z = pos1.z + amount + worldedit.pos2[name].z = pos2.z + amount + else + worldedit.player_notify(name, "unexpected error. direction = " .. direction) + end + + worldedit.mark_pos1(name) + worldedit.mark_pos2(name) + + worldedit.player_notify(name, "Area shifted by " .. amount .. " in " .. direction .. " axis") + end, + } +) + +minetest.register_chatcommand( + "/expand", + { + params = " [reverse-amount] [direction]", + description = "expand the selection in one or two directions at once", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, arg2, arg3 = param:find("^(%d+)[%s+]?([%w+]?)[%s+]?([xyz]?)$") + + if find == nil then + worldedit.player_notify(name, "invalid use: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") + return + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + local axis, dir + + if arg2 == "" and arg3 == "" then + axis, dir = worldedit.player_axis(name) + if worldedit.get_closest_marker(name) == 1 then + if axis == 'x' then + worldedit.pos2[name].x = pos2.x + (amount * dir) + elseif axis == 'y' then + worldedit.pos2[name].y = pos2.y + (amount * dir) + elseif axis == 'z' then + worldedit.pos2[name].z = pos2.z + (amount * dir) + end + + worldedit.mark_pos2(name) + else + if axis == 'x' then + worldedit.pos1[name].x = pos1.x + (amount * dir) + elseif axis == 'y' then + worldedit.pos1[name].y = pos1.y + (amount * dir) + elseif axis == 'z' then + worldedit.pos1[name].z = pos1.z + (amount * dir) + end + + worldedit.mark_pos1(name) + end + elseif arg2 ~= "" and arg3 == "" then + -- TODO + elseif arg2 ~= "" and arg3 ~= "" then + -- TODO + end + + worldedit.player_notify(name, "Area expanded by " .. amount .. " on " .. axis) + end, + } +) + +worldedit.get_closest_marker = function(name) + local playerpos = minetest.get_player_by_name(name):getpos() + + local dist1 = vector.distance(playerpos, worldedit.pos1[name]) + local dist2 = vector.distance(playerpos, worldedit.pos2[name]) + + if dist1 < dist2 then + return 1 + else + return 2 + end +end diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 9daa809..2b3564f 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1118,3 +1118,5 @@ minetest.register_chatcommand("/clearobjects", { worldedit.player_notify(name, count .. " objects cleared") end), }) + +dofile(minetest.get_modpath("worldedit_commands") .. "/area.lua") From 9decac79d5272b699180718f99c0d845e2e6a41f Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 22:29:37 -0600 Subject: [PATCH 02/17] Add common functions to move and update the markers --- worldedit_commands/area.lua | 88 ++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index c3a16e6..fc5e1ff 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -233,39 +233,25 @@ minetest.register_chatcommand( return end - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - local axis, dir - + local axis, direction, closest_mark + if arg2 == "" and arg3 == "" then - axis, dir = worldedit.player_axis(name) - if worldedit.get_closest_marker(name) == 1 then - if axis == 'x' then - worldedit.pos2[name].x = pos2.x + (amount * dir) - elseif axis == 'y' then - worldedit.pos2[name].y = pos2.y + (amount * dir) - elseif axis == 'z' then - worldedit.pos2[name].z = pos2.z + (amount * dir) - end - - worldedit.mark_pos2(name) - else - if axis == 'x' then - worldedit.pos1[name].x = pos1.x + (amount * dir) - elseif axis == 'y' then - worldedit.pos1[name].y = pos1.y + (amount * dir) - elseif axis == 'z' then - worldedit.pos1[name].z = pos1.z + (amount * dir) - end - - worldedit.mark_pos1(name) - end - elseif arg2 ~= "" and arg3 == "" then - -- TODO - elseif arg2 ~= "" and arg3 ~= "" then - -- TODO + axis, direction = worldedit.player_axis(name) + else + worldedit.player_notify("WIP :)") + return end + closest_mark = worldedit.get_closest_marker(name) + + if closest_mark == 1 then + closest_mark = 2 + else + closest_mark = 1 + end + + worldedit.move_marker(name, closest_mark, axis, amount * direction) + worldedit.update_markers(name) worldedit.player_notify(name, "Area expanded by " .. amount .. " on " .. axis) end, } @@ -283,3 +269,45 @@ worldedit.get_closest_marker = function(name) return 2 end end + +worldedit.move_marker = function(name, marker, axis, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if marker == 1 then + if axis == 'x' then + worldedit.pos1[name].x = pos1.x + amount + elseif axis == 'y' then + worldedit.pos1[name].y = pos1.y + amount + elseif axis == 'z' then + worldedit.pos1[name].z = pos1.z + amount + else + minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis) + end + elseif marker == 2 then + if axis == 'x' then + worldedit.pos2[name].x = pos2.x + amount + elseif axis == 'y' then + worldedit.pos2[name].y = pos2.y + amount + elseif axis == 'z' then + worldedit.pos2[name].z = pos2.z + amount + else + minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis) + end + else + minetest.debug("Bad marker id at worldedit.move_marker") + end +end + +worldedit.update_markers = function(name, marker) + if marker == nil then + worldedit.mark_pos1(name) + worldedit.mark_pos2(name) + elseif marker == 1 then + worldedit.mark_pos1(name) + elseif marker == 2 then + worldedit.mark_pos2(name) + else + minetest.debug("worldedit: Invalid execution of function update_markers") + end +end From a1a518c8a83c43be99068f661adb526a586726c9 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 23:40:56 -0600 Subject: [PATCH 03/17] Major progress and simplification in /expand function --- worldedit_commands/area.lua | 85 ++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index fc5e1ff..576bf98 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -217,7 +217,7 @@ minetest.register_chatcommand( minetest.register_chatcommand( "/expand", { - params = " [reverse-amount] [direction]", + params = " [reverse-amount] [x|y|z]", description = "expand the selection in one or two directions at once", privs = {worldedit=true}, func = function(name, param) @@ -233,24 +233,40 @@ minetest.register_chatcommand( return end - local axis, direction, closest_mark - - if arg2 == "" and arg3 == "" then - axis, direction = worldedit.player_axis(name) - else - worldedit.player_notify("WIP :)") - return - end + local axis, direction, mark - closest_mark = worldedit.get_closest_marker(name) + axis, direction = worldedit.player_axis(name) + mark = worldedit.get_marker_in_axis(name, axis, direction) - if closest_mark == 1 then - closest_mark = 2 - else - closest_mark = 1 + if arg3 ~= "" then + axis = arg3 end - worldedit.move_marker(name, closest_mark, axis, amount * direction) + if arg2 ~= "" then + local tmp = tonumber(arg2) + + if tmp == nil then + if arg2:find("[xyz]") then + axis = arg2 + else + minetest.debug("worldedit: expand command. Something wrong.") + return false + end + else + local tmpmark + if mark == 1 then + tmpmark = 2 + else + tmpmark = 1 + end + + worldedit.move_marker(name, tmpmark, axis, tmp * direction * -1) + end + end + + worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) + worldedit.move_marker(name, mark, axis, amount * direction) + worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) worldedit.update_markers(name) worldedit.player_notify(name, "Area expanded by " .. amount .. " on " .. axis) end, @@ -270,6 +286,45 @@ worldedit.get_closest_marker = function(name) end end + +-- returns which marker is closest to the specified axis and direction +worldedit.get_marker_in_axis = function(name, axis, direction) + local pos1 = {x = 0, y = 0, z = 0} + local pos2 = {x = 0, y = 0, z = 0} + + if direction ~= 1 and direction ~= -1 then + return nil + end + + if axis == 'x' then + pos1.x = worldedit.pos1[name].x * direction + pos2.x = worldedit.pos2[name].x * direction + if pos1.x > pos2.x then + return 1 + else + return 2 + end + elseif axis == 'y' then + pos1.y = worldedit.pos1[name].y * direction + pos2.y = worldedit.pos2[name].y * direction + if pos1.y > pos2.y then + return 1 + else + return 2 + end + elseif axis == 'z' then + pos1.z = worldedit.pos1[name].z * direction + pos2.z = worldedit.pos2[name].z * direction + if pos1.z > pos2.z then + return 1 + else + return 2 + end + else + minetest.debug("worldedit.get_marker_in_axis: invalid axis. Value was: " .. axis) + end +end + worldedit.move_marker = function(name, marker, axis, amount) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] From f33d13948fa4670a9fb909017a7bcafef028f9cd Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 01:23:19 -0600 Subject: [PATCH 04/17] Fix faulty regex in expand. Add relative direction decoding. --- worldedit_commands/area.lua | 73 ++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index 576bf98..d9e07f1 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -217,11 +217,11 @@ minetest.register_chatcommand( minetest.register_chatcommand( "/expand", { - params = " [reverse-amount] [x|y|z]", + params = " [reverse-amount] [up|down|left|right|front|back]", description = "expand the selection in one or two directions at once", privs = {worldedit=true}, func = function(name, param) - local find, _, amount, arg2, arg3 = param:find("^(%d+)[%s+]?([%w+]?)[%s+]?([xyz]?)$") + local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") if find == nil then worldedit.player_notify(name, "invalid use: " .. param) @@ -239,19 +239,15 @@ minetest.register_chatcommand( mark = worldedit.get_marker_in_axis(name, axis, direction) if arg3 ~= "" then - axis = arg3 + axis, direction = worldedit.translate_directions(name, arg3) + worldedit.player_notify(name, "arg3: " .. arg3) end if arg2 ~= "" then local tmp = tonumber(arg2) if tmp == nil then - if arg2:find("[xyz]") then - axis = arg2 - else - minetest.debug("worldedit: expand command. Something wrong.") - return false - end + axis, direction = worldedit.translate_directions(name, arg2) else local tmpmark if mark == 1 then @@ -260,10 +256,18 @@ minetest.register_chatcommand( tmpmark = 1 end + if axis == nil or direction == nil then + return false, "Invalid use: " .. param + end + worldedit.move_marker(name, tmpmark, axis, tmp * direction * -1) end end + if axis == nil or direction == nil then + return false, "Invalid use: " .. param + end + worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) worldedit.move_marker(name, mark, axis, amount * direction) worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) @@ -273,6 +277,7 @@ minetest.register_chatcommand( } ) +-- Return the marker that is closest to the player worldedit.get_closest_marker = function(name) local playerpos = minetest.get_player_by_name(name):getpos() @@ -325,6 +330,7 @@ worldedit.get_marker_in_axis = function(name, axis, direction) end end +-- Moves the selected marker in a single axis by amount nodes worldedit.move_marker = function(name, marker, axis, amount) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] @@ -354,6 +360,7 @@ worldedit.move_marker = function(name, marker, axis, amount) end end +-- Updates the location ingame of the markers worldedit.update_markers = function(name, marker) if marker == nil then worldedit.mark_pos1(name) @@ -366,3 +373,51 @@ worldedit.update_markers = function(name, marker) minetest.debug("worldedit: Invalid execution of function update_markers") end end + + +-- Translates up, down, left, right, front, back to their corresponding axes and directions according to faced direction +worldedit.translate_directions = function(name, direction) + local axis, dir = worldedit.player_axis(name) + local resaxis, resdir + + if direction == "up" then + return 'y', 1 + end + + if direction == "down" then + return 'y', -1 + end + + if direction == "front" then + resaxis = axis + resdir = dir + end + + if direction == "back" then + resaxis = axis + resdir = -dir + end + + if direction == "left" then + if axis == 'x' then + resaxis = 'z' + elseif axis == 'z' then + resaxis = 'x' + end + + resdir = -dir + end + + if direction == "right" then + if axis == 'x' then + resaxis = 'z' + elseif axis == 'z' then + resaxis = 'x' + end + + resdir = dir + end + + return resaxis, resdir + +end From 0dd151befcc6673d31bc34435dd4439fd7882330 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:04:02 -0600 Subject: [PATCH 05/17] Finish /expand implementation. Add /contract command. --- worldedit_commands/area.lua | 84 ++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 11 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index d9e07f1..b88b2f5 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -240,7 +240,7 @@ minetest.register_chatcommand( if arg3 ~= "" then axis, direction = worldedit.translate_directions(name, arg3) - worldedit.player_notify(name, "arg3: " .. arg3) + mark = worldedit.get_marker_in_axis(name, axis, direction) end if arg2 ~= "" then @@ -248,6 +248,7 @@ minetest.register_chatcommand( if tmp == nil then axis, direction = worldedit.translate_directions(name, arg2) + mark = worldedit.get_marker_in_axis(name, axis, direction) else local tmpmark if mark == 1 then @@ -267,16 +268,77 @@ minetest.register_chatcommand( if axis == nil or direction == nil then return false, "Invalid use: " .. param end - - worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) - worldedit.move_marker(name, mark, axis, amount * direction) - worldedit.player_notify(name, "mark1 x:" .. worldedit.pos1[name].x .. "y:" .. worldedit.pos1[name].y .. "z:" .. worldedit.pos1[name].z) + + worldedit.move_marker(name, mark, axis, amount * direction) worldedit.update_markers(name) - worldedit.player_notify(name, "Area expanded by " .. amount .. " on " .. axis) + worldedit.player_notify(name, "Area expanded by " .. amount) end, } ) +minetest.register_chatcommand( + "/contract", + { + params = " [reverse-amount] [up|down|left|right|front|back]", + description = "contract the selection in one or two directions at once", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") + + if find == nil then + worldedit.player_notify(name, "invalid use: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") + return + end + + local axis, direction, mark + + axis, direction = worldedit.player_axis(name) + mark = worldedit.get_marker_in_axis(name, axis, direction) + + if arg3 ~= "" then + axis, direction = worldedit.translate_directions(name, arg3) + mark = worldedit.get_marker_in_axis(name, axis, direction) + end + + if arg2 ~= "" then + local tmp = tonumber(arg2) + + if tmp == nil then + axis, direction = worldedit.translate_directions(name, arg2) + mark = worldedit.get_marker_in_axis(name, axis, direction) + else + local tmpmark + if mark == 1 then + tmpmark = 2 + else + tmpmark = 1 + end + + if axis == nil or direction == nil then + return false, "Invalid use: " .. param + end + + worldedit.move_marker(name, tmpmark, axis, tmp * direction) + end + end + + if axis == nil or direction == nil then + return false, "Invalid use: " .. param + end + + worldedit.move_marker(name, mark, axis, amount * direction * -1) + worldedit.update_markers(name) + worldedit.player_notify(name, "Area contracted by " .. amount) + end, + } +) + + -- Return the marker that is closest to the player worldedit.get_closest_marker = function(name) local playerpos = minetest.get_player_by_name(name):getpos() @@ -326,7 +388,7 @@ worldedit.get_marker_in_axis = function(name, axis, direction) return 2 end else - minetest.debug("worldedit.get_marker_in_axis: invalid axis. Value was: " .. axis) + minetest.debug("worldedit.get_marker_in_axis: invalid axis.") end end @@ -401,21 +463,21 @@ worldedit.translate_directions = function(name, direction) if direction == "left" then if axis == 'x' then resaxis = 'z' + resdir = dir elseif axis == 'z' then resaxis = 'x' + resdir = -dir end - - resdir = -dir end if direction == "right" then if axis == 'x' then resaxis = 'z' + resdir = -dir elseif axis == 'z' then resaxis = 'x' + resdir = dir end - - resdir = dir end return resaxis, resdir From 969d0a0ace12fe7fcd31ae6102b8e9989879cb29 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:29:28 -0600 Subject: [PATCH 06/17] Reimplement /shift to make use of new marker functions --- worldedit_commands/area.lua | 54 +++++++++++++------------------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index b88b2f5..78b91d4 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -159,16 +159,15 @@ end minetest.register_chatcommand( "/shift", { - params = "[+|-] [x|y|z]", + params = " [up|down|left|right|front|back]", description = "Moves the selection region. Does not move contents.", privs = {worldedit=true}, func = function(name, param) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] - local find, _, sign, amount, axis = param:find("^([+-]?)(%d+)[%s+]?([xyz]?)$") + local find, _, amount, direction = param:find("(%d+)%s*(%l*)") if find == nil then - minetest.debug("entering if") worldedit.player_notify(name, "invalid usage: " .. param) return end @@ -178,36 +177,21 @@ minetest.register_chatcommand( return end - amount = tonumber(amount) + local axis, dirsign + + if direction ~= "" then + axis, dirsign = worldedit.translate_direction(name, direction) + else + axis, dirsign = worldedit.player_axis(name) + end - local direction = "" - - if sign ~= nil and sign == '-' then - amount = amount * -1 + if axis == nil or dirsign == nil then + return false, "Invalid usage: " .. param end - if axis == "" then - direction, _ = worldedit.player_axis(name) - amount = amount * _ - else - direction = axis - end - - if direction == 'x' then - worldedit.pos1[name].x = pos1.x + amount - worldedit.pos2[name].x = pos2.x + amount - elseif direction == 'y' then - worldedit.pos1[name].y = pos1.y + amount - worldedit.pos2[name].y = pos2.y + amount - elseif direction == 'z' then - worldedit.pos1[name].z = pos1.z + amount - worldedit.pos2[name].z = pos2.z + amount - else - worldedit.player_notify(name, "unexpected error. direction = " .. direction) - end - - worldedit.mark_pos1(name) - worldedit.mark_pos2(name) + worldedit.move_marker(name, 1, axis, amount * dirsign) + worldedit.move_marker(name, 2, axis, amount * dirsign) + worldedit.update_markers(name) worldedit.player_notify(name, "Area shifted by " .. amount .. " in " .. direction .. " axis") end, @@ -239,7 +223,7 @@ minetest.register_chatcommand( mark = worldedit.get_marker_in_axis(name, axis, direction) if arg3 ~= "" then - axis, direction = worldedit.translate_directions(name, arg3) + axis, direction = worldedit.translate_direction(name, arg3) mark = worldedit.get_marker_in_axis(name, axis, direction) end @@ -247,7 +231,7 @@ minetest.register_chatcommand( local tmp = tonumber(arg2) if tmp == nil then - axis, direction = worldedit.translate_directions(name, arg2) + axis, direction = worldedit.translate_direction(name, arg2) mark = worldedit.get_marker_in_axis(name, axis, direction) else local tmpmark @@ -301,7 +285,7 @@ minetest.register_chatcommand( mark = worldedit.get_marker_in_axis(name, axis, direction) if arg3 ~= "" then - axis, direction = worldedit.translate_directions(name, arg3) + axis, direction = worldedit.translate_direction(name, arg3) mark = worldedit.get_marker_in_axis(name, axis, direction) end @@ -309,7 +293,7 @@ minetest.register_chatcommand( local tmp = tonumber(arg2) if tmp == nil then - axis, direction = worldedit.translate_directions(name, arg2) + axis, direction = worldedit.translate_direction(name, arg2) mark = worldedit.get_marker_in_axis(name, axis, direction) else local tmpmark @@ -438,7 +422,7 @@ end -- Translates up, down, left, right, front, back to their corresponding axes and directions according to faced direction -worldedit.translate_directions = function(name, direction) +worldedit.translate_direction = function(name, direction) local axis, dir = worldedit.player_axis(name) local resaxis, resdir From 7cdaa9bed5bf9a368cf93270953a7f45c5ef6593 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:51:59 -0600 Subject: [PATCH 07/17] Reimplement inset/outset to make use of new marker functions --- worldedit_commands/area.lua | 153 ++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 84 deletions(-) diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua index 78b91d4..e9e5e73 100644 --- a/worldedit_commands/area.lua +++ b/worldedit_commands/area.lua @@ -2,56 +2,48 @@ minetest.register_chatcommand( "/outset", { params = " [h|v]", - description = "expand the selection", + description = "outset the selection", privs = {worldedit=true}, func = function(name, param) local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") - local message if find == nil then - worldedit.player_notify(name, "invalid usage: " .. param) - return + return false, "invalid usage: " .. param end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] - if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then - message = "Undefined region. Region must be defined beforehand." - else - amount = tonumber(amount) - local curpos1 = worldedit.pos1[name] - local curpos2 = worldedit.pos2[name] - - local dirs = worldedit.get_outset_directions(curpos1, curpos2) - - if dir == 'h' then - worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1) - worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1) - - worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2) - worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2) - - message = "area expanded by " .. amount .. " blocks horizontally" - elseif dir == 'v' then - worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1) - worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2) - - message = "area expanded by " .. amount .. " blocks vertically" - else - worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1) - worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1) - worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1) - - worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2) - worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2) - worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2) - - message = "area expanded by " .. amount .. " blocks in all axes" - end - - worldedit.mark_pos1(name) - worldedit.mark_pos2(name) + if pos1 == nil or pos2 == nil then + return false, "Undefined region. Region must be defined beforehand." end - worldedit.player_notify(name, message) + local dirs = worldedit.get_outset_directions(pos1, pos2) + + if dir == 'h' then + worldedit.move_marker(name, 1, 'x', amount * dirs.x1) + worldedit.move_marker(name, 1, 'z', amount * dirs.z1) + + worldedit.move_marker(name, 2, 'x', amount * dirs.x2) + worldedit.move_marker(name, 2, 'z', amount * dirs.z2) + message = "area outset by " .. amount .. " blocks horizontally" + elseif dir == 'v' then + worldedit.move_marker(name, 1, 'y', amount * dirs.y1) + worldedit.move_marker(name, 2, 'y', amount * dirs.y2) + message = "area outset by " .. amount .. " blocks vertically" + else + worldedit.move_marker(name, 1, 'x', amount * dirs.x1) + worldedit.move_marker(name, 1, 'y', amount * dirs.y1) + worldedit.move_marker(name, 1, 'z', amount * dirs.z1) + + worldedit.move_marker(name, 2, 'x', amount * dirs.x2) + worldedit.move_marker(name, 2, 'y', amount * dirs.y2) + worldedit.move_marker(name, 2, 'z', amount * dirs.z2) + message = "area outset by " .. amount .. " blocks in all axes" + end + + worldedit.update_markers(name) + return true, message end, } ) @@ -60,56 +52,49 @@ minetest.register_chatcommand( "/inset", { params = " [h|v]", - description = "contract", + description = "inset the selection", privs = {worldedit=true}, func = function(name, param) local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") - local message = "" if find == nil then - worldedit.player_notify(name, "invalid usage: " .. param) - return + return false, "invalid usage: " .. param end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] - if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then - message = "Undefined region. Region must be defined beforehand." - else - amount = tonumber(amount) - local curpos1 = worldedit.pos1[name] - local curpos2 = worldedit.pos2[name] - - local dirs = worldedit.get_outset_directions(curpos1, curpos2) - - if dir == 'h' then - worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1) - worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1) - - worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2) - worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2) - - message = "area contracted by " .. amount .. " blocks horizontally" - elseif dir == 'v' then - worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1) - worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2) - - message = "area contracted by " .. amount .. " blocks vertically" - else - worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1) - worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1) - worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1) - - worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2) - worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2) - worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2) - - message = "area contracted by " .. amount .. " blocks in all axes" - end - - worldedit.mark_pos1(name) - worldedit.mark_pos2(name) + if pos1 == nil or pos2 == nil then + return false, "Undefined region. Region must be defined beforehand." end - worldedit.player_notify(name, message) + local dirs = worldedit.get_outset_directions(pos1, pos2) + amount = -amount + + if dir == 'h' then + worldedit.move_marker(name, 1, 'x', amount * dirs.x1) + worldedit.move_marker(name, 1, 'z', amount * dirs.z1) + + worldedit.move_marker(name, 2, 'x', amount * dirs.x2) + worldedit.move_marker(name, 2, 'z', amount * dirs.z2) + message = "area inset by " .. amount .. " blocks horizontally" + elseif dir == 'v' then + worldedit.move_marker(name, 1, 'y', amount * dirs.y1) + worldedit.move_marker(name, 2, 'y', amount * dirs.y2) + message = "area inset by " .. amount .. " blocks vertically" + else + worldedit.move_marker(name, 1, 'x', amount * dirs.x1) + worldedit.move_marker(name, 1, 'y', amount * dirs.y1) + worldedit.move_marker(name, 1, 'z', amount * dirs.z1) + + worldedit.move_marker(name, 2, 'x', amount * dirs.x2) + worldedit.move_marker(name, 2, 'y', amount * dirs.y2) + worldedit.move_marker(name, 2, 'z', amount * dirs.z2) + message = "area inset by " .. amount .. " blocks in all axes" + end + + worldedit.update_markers(name) + return true, message end, } ) @@ -389,7 +374,7 @@ worldedit.move_marker = function(name, marker, axis, amount) elseif axis == 'z' then worldedit.pos1[name].z = pos1.z + amount else - minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis) + minetest.debug("worldedit: Invalid axis in move_marker.") end elseif marker == 2 then if axis == 'x' then @@ -399,7 +384,7 @@ worldedit.move_marker = function(name, marker, axis, amount) elseif axis == 'z' then worldedit.pos2[name].z = pos2.z + amount else - minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis) + minetest.debug("worldedit: Invalid axis in move_marker.") end else minetest.debug("Bad marker id at worldedit.move_marker") From fb9bf2f81171e26c6032bf78720824ed963ef724 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 19:46:59 -0600 Subject: [PATCH 08/17] refactor code for mantainability and reusability. Start of chat command refactor. Implement /outset. --- worldedit_commands/area.lua | 454 ------------------------------- worldedit_commands/cuboid.lua | 95 +++++++ worldedit_commands/cuboidapi.lua | 248 +++++++++++++++++ worldedit_commands/init.lua | 2 +- 4 files changed, 344 insertions(+), 455 deletions(-) delete mode 100644 worldedit_commands/area.lua create mode 100644 worldedit_commands/cuboid.lua create mode 100644 worldedit_commands/cuboidapi.lua diff --git a/worldedit_commands/area.lua b/worldedit_commands/area.lua deleted file mode 100644 index e9e5e73..0000000 --- a/worldedit_commands/area.lua +++ /dev/null @@ -1,454 +0,0 @@ -minetest.register_chatcommand( - "/outset", - { - params = " [h|v]", - description = "outset the selection", - privs = {worldedit=true}, - func = function(name, param) - local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") - - if find == nil then - return false, "invalid usage: " .. param - end - - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if pos1 == nil or pos2 == nil then - return false, "Undefined region. Region must be defined beforehand." - end - - local dirs = worldedit.get_outset_directions(pos1, pos2) - - if dir == 'h' then - worldedit.move_marker(name, 1, 'x', amount * dirs.x1) - worldedit.move_marker(name, 1, 'z', amount * dirs.z1) - - worldedit.move_marker(name, 2, 'x', amount * dirs.x2) - worldedit.move_marker(name, 2, 'z', amount * dirs.z2) - message = "area outset by " .. amount .. " blocks horizontally" - elseif dir == 'v' then - worldedit.move_marker(name, 1, 'y', amount * dirs.y1) - worldedit.move_marker(name, 2, 'y', amount * dirs.y2) - message = "area outset by " .. amount .. " blocks vertically" - else - worldedit.move_marker(name, 1, 'x', amount * dirs.x1) - worldedit.move_marker(name, 1, 'y', amount * dirs.y1) - worldedit.move_marker(name, 1, 'z', amount * dirs.z1) - - worldedit.move_marker(name, 2, 'x', amount * dirs.x2) - worldedit.move_marker(name, 2, 'y', amount * dirs.y2) - worldedit.move_marker(name, 2, 'z', amount * dirs.z2) - message = "area outset by " .. amount .. " blocks in all axes" - end - - worldedit.update_markers(name) - return true, message - end, - } -) - -minetest.register_chatcommand( - "/inset", - { - params = " [h|v]", - description = "inset the selection", - privs = {worldedit=true}, - func = function(name, param) - local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") - - if find == nil then - return false, "invalid usage: " .. param - end - - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if pos1 == nil or pos2 == nil then - return false, "Undefined region. Region must be defined beforehand." - end - - local dirs = worldedit.get_outset_directions(pos1, pos2) - amount = -amount - - if dir == 'h' then - worldedit.move_marker(name, 1, 'x', amount * dirs.x1) - worldedit.move_marker(name, 1, 'z', amount * dirs.z1) - - worldedit.move_marker(name, 2, 'x', amount * dirs.x2) - worldedit.move_marker(name, 2, 'z', amount * dirs.z2) - message = "area inset by " .. amount .. " blocks horizontally" - elseif dir == 'v' then - worldedit.move_marker(name, 1, 'y', amount * dirs.y1) - worldedit.move_marker(name, 2, 'y', amount * dirs.y2) - message = "area inset by " .. amount .. " blocks vertically" - else - worldedit.move_marker(name, 1, 'x', amount * dirs.x1) - worldedit.move_marker(name, 1, 'y', amount * dirs.y1) - worldedit.move_marker(name, 1, 'z', amount * dirs.z1) - - worldedit.move_marker(name, 2, 'x', amount * dirs.x2) - worldedit.move_marker(name, 2, 'y', amount * dirs.y2) - worldedit.move_marker(name, 2, 'z', amount * dirs.z2) - message = "area inset by " .. amount .. " blocks in all axes" - end - - worldedit.update_markers(name) - return true, message - end, - } -) - -worldedit.get_outset_directions = function(mark1, mark2) - if mark1 == nil or mark2 == nil then return - end - - local dirs = - { - x1 = 0, - x2 = 0, - y1 = 0, - y2 = 0, - z1 = 0, - z2 = 0 - } - - if mark1.x < mark2.x then - dirs.x1 = -1 - dirs.x2 = 1 - else - dirs.x1 = 1 - dirs.x2 = -1 - end - - if mark1.y < mark2.y then - dirs.y1 = -1 - dirs.y2 = 1 - else - dirs.y1 = 1 - dirs.y2 = -1 - end - - if mark1.z < mark2.z then - dirs.z1 = -1 - dirs.z2 = 1 - else - dirs.z1 = 1 - dirs.z2 = -1 - end - - return dirs -end - - -minetest.register_chatcommand( - "/shift", - { - params = " [up|down|left|right|front|back]", - description = "Moves the selection region. Does not move contents.", - privs = {worldedit=true}, - func = function(name, param) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - local find, _, amount, direction = param:find("(%d+)%s*(%l*)") - - if find == nil then - worldedit.player_notify(name, "invalid usage: " .. param) - return - end - - if pos1 == nil or pos2 == nil then - worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") - return - end - - local axis, dirsign - - if direction ~= "" then - axis, dirsign = worldedit.translate_direction(name, direction) - else - axis, dirsign = worldedit.player_axis(name) - end - - if axis == nil or dirsign == nil then - return false, "Invalid usage: " .. param - end - - worldedit.move_marker(name, 1, axis, amount * dirsign) - worldedit.move_marker(name, 2, axis, amount * dirsign) - worldedit.update_markers(name) - - worldedit.player_notify(name, "Area shifted by " .. amount .. " in " .. direction .. " axis") - end, - } -) - -minetest.register_chatcommand( - "/expand", - { - params = " [reverse-amount] [up|down|left|right|front|back]", - description = "expand the selection in one or two directions at once", - privs = {worldedit=true}, - func = function(name, param) - local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") - - if find == nil then - worldedit.player_notify(name, "invalid use: " .. param) - return - end - - if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then - worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") - return - end - - local axis, direction, mark - - axis, direction = worldedit.player_axis(name) - mark = worldedit.get_marker_in_axis(name, axis, direction) - - if arg3 ~= "" then - axis, direction = worldedit.translate_direction(name, arg3) - mark = worldedit.get_marker_in_axis(name, axis, direction) - end - - if arg2 ~= "" then - local tmp = tonumber(arg2) - - if tmp == nil then - axis, direction = worldedit.translate_direction(name, arg2) - mark = worldedit.get_marker_in_axis(name, axis, direction) - else - local tmpmark - if mark == 1 then - tmpmark = 2 - else - tmpmark = 1 - end - - if axis == nil or direction == nil then - return false, "Invalid use: " .. param - end - - worldedit.move_marker(name, tmpmark, axis, tmp * direction * -1) - end - end - - if axis == nil or direction == nil then - return false, "Invalid use: " .. param - end - - worldedit.move_marker(name, mark, axis, amount * direction) - worldedit.update_markers(name) - worldedit.player_notify(name, "Area expanded by " .. amount) - end, - } -) - -minetest.register_chatcommand( - "/contract", - { - params = " [reverse-amount] [up|down|left|right|front|back]", - description = "contract the selection in one or two directions at once", - privs = {worldedit=true}, - func = function(name, param) - local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") - - if find == nil then - worldedit.player_notify(name, "invalid use: " .. param) - return - end - - if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then - worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") - return - end - - local axis, direction, mark - - axis, direction = worldedit.player_axis(name) - mark = worldedit.get_marker_in_axis(name, axis, direction) - - if arg3 ~= "" then - axis, direction = worldedit.translate_direction(name, arg3) - mark = worldedit.get_marker_in_axis(name, axis, direction) - end - - if arg2 ~= "" then - local tmp = tonumber(arg2) - - if tmp == nil then - axis, direction = worldedit.translate_direction(name, arg2) - mark = worldedit.get_marker_in_axis(name, axis, direction) - else - local tmpmark - if mark == 1 then - tmpmark = 2 - else - tmpmark = 1 - end - - if axis == nil or direction == nil then - return false, "Invalid use: " .. param - end - - worldedit.move_marker(name, tmpmark, axis, tmp * direction) - end - end - - if axis == nil or direction == nil then - return false, "Invalid use: " .. param - end - - worldedit.move_marker(name, mark, axis, amount * direction * -1) - worldedit.update_markers(name) - worldedit.player_notify(name, "Area contracted by " .. amount) - end, - } -) - - --- Return the marker that is closest to the player -worldedit.get_closest_marker = function(name) - local playerpos = minetest.get_player_by_name(name):getpos() - - local dist1 = vector.distance(playerpos, worldedit.pos1[name]) - local dist2 = vector.distance(playerpos, worldedit.pos2[name]) - - if dist1 < dist2 then - return 1 - else - return 2 - end -end - - --- returns which marker is closest to the specified axis and direction -worldedit.get_marker_in_axis = function(name, axis, direction) - local pos1 = {x = 0, y = 0, z = 0} - local pos2 = {x = 0, y = 0, z = 0} - - if direction ~= 1 and direction ~= -1 then - return nil - end - - if axis == 'x' then - pos1.x = worldedit.pos1[name].x * direction - pos2.x = worldedit.pos2[name].x * direction - if pos1.x > pos2.x then - return 1 - else - return 2 - end - elseif axis == 'y' then - pos1.y = worldedit.pos1[name].y * direction - pos2.y = worldedit.pos2[name].y * direction - if pos1.y > pos2.y then - return 1 - else - return 2 - end - elseif axis == 'z' then - pos1.z = worldedit.pos1[name].z * direction - pos2.z = worldedit.pos2[name].z * direction - if pos1.z > pos2.z then - return 1 - else - return 2 - end - else - minetest.debug("worldedit.get_marker_in_axis: invalid axis.") - end -end - --- Moves the selected marker in a single axis by amount nodes -worldedit.move_marker = function(name, marker, axis, amount) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if marker == 1 then - if axis == 'x' then - worldedit.pos1[name].x = pos1.x + amount - elseif axis == 'y' then - worldedit.pos1[name].y = pos1.y + amount - elseif axis == 'z' then - worldedit.pos1[name].z = pos1.z + amount - else - minetest.debug("worldedit: Invalid axis in move_marker.") - end - elseif marker == 2 then - if axis == 'x' then - worldedit.pos2[name].x = pos2.x + amount - elseif axis == 'y' then - worldedit.pos2[name].y = pos2.y + amount - elseif axis == 'z' then - worldedit.pos2[name].z = pos2.z + amount - else - minetest.debug("worldedit: Invalid axis in move_marker.") - end - else - minetest.debug("Bad marker id at worldedit.move_marker") - end -end - --- Updates the location ingame of the markers -worldedit.update_markers = function(name, marker) - if marker == nil then - worldedit.mark_pos1(name) - worldedit.mark_pos2(name) - elseif marker == 1 then - worldedit.mark_pos1(name) - elseif marker == 2 then - worldedit.mark_pos2(name) - else - minetest.debug("worldedit: Invalid execution of function update_markers") - end -end - - --- Translates up, down, left, right, front, back to their corresponding axes and directions according to faced direction -worldedit.translate_direction = function(name, direction) - local axis, dir = worldedit.player_axis(name) - local resaxis, resdir - - if direction == "up" then - return 'y', 1 - end - - if direction == "down" then - return 'y', -1 - end - - if direction == "front" then - resaxis = axis - resdir = dir - end - - if direction == "back" then - resaxis = axis - resdir = -dir - end - - if direction == "left" then - if axis == 'x' then - resaxis = 'z' - resdir = dir - elseif axis == 'z' then - resaxis = 'x' - resdir = -dir - end - end - - if direction == "right" then - if axis == 'x' then - resaxis = 'z' - resdir = -dir - elseif axis == 'z' then - resaxis = 'x' - resdir = dir - end - end - - return resaxis, resdir - -end diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua new file mode 100644 index 0000000..1af5b3b --- /dev/null +++ b/worldedit_commands/cuboid.lua @@ -0,0 +1,95 @@ +minetest.register_chatcommand("/outset", { + params = " [h|v]", + description = "outset the selection", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + + if find == nil then + return false, "invalid usage: " .. param + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, + "Undefined region. Region must be defined beforehand." + end + + if dir == "" then + assert(worldedit.cuboid_volumetricexpand(name, amount)) + elseif dir == "h" then + assert(worldedit.cuboid_linealexpand(name, 'x', 1, amount)) + assert(worldedit.cuboid_linealexpand(name, 'x', -1, amount)) + assert(worldedit.cuboid_linealexpand(name, 'z', 1, amount)) + assert(worldedit.cuboid_linealexpand(name, 'z', -1, amount)) + elseif dir == "v" then + assert(worldedit.cuboid_linealexpand(name, 'y', 1, amount)) + assert(worldedit.cuboid_linealexpand(name, 'y', -1, amount)) + else + return false, "Unknown error" + end + + worldedit.marker_update(name) + return true, "Region outset by " .. amount .. " blocks" + end, + } +) + + +minetest.register_chatcommand( + "/shift", + { + params = " [up|down|left|right|front|back]", + description = "Moves the selection region. Does not move contents.", + privs = {worldedit=true}, + func = function(name, param) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + local find, _, amount, direction = param:find("(%d+)%s*(%l*)") + + if find == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return + end + + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, + "Undefined region. Region must be defined beforehand.") + return + end + + + end, + } +) + +minetest.register_chatcommand( + "/expand", + { + params = " [reverse-amount] [up|down|left|right|front|back]", + description = "expand the selection in one or two directions at once", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") + + if find == nil then + worldedit.player_notify(name, "invalid use: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + worldedit.player_notify(name, + "Undefined region. Region must be defined beforehand.") + return + end + + + end, + } +) + + +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboidapi.lua") + diff --git a/worldedit_commands/cuboidapi.lua b/worldedit_commands/cuboidapi.lua new file mode 100644 index 0000000..37194e4 --- /dev/null +++ b/worldedit_commands/cuboidapi.lua @@ -0,0 +1,248 @@ +-- Expands or contracts the cuboid in all axes by amount (positive or negative) +worldedit.cuboid_volumetricexpand = function(name, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "Undefined cuboid" + end + + local delta1 = vector.new() + local delta2 = vector.new() + local deltadir1 + local deltadir2 + + delta1 = vector.add(delta1, amount) + delta2 = vector.add(delta2, amount) + deltadir1, deltadir2 = worldedit.get_expansion_directions(pos1, pos2) + delta1 = vector.multiply(delta1, deltadir1) + delta2 = vector.multiply(delta2, deltadir2) + worldedit.pos1[name] = vector.add(pos1, delta1) + worldedit.pos2[name] = vector.add(pos2, delta2) + + return true +end + + +-- Expands or contracts the cuboid in a single axis by amount (positive or negative) +worldedit.cuboid_linealexpand = function(name, axis, direction, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "undefined cuboid" + end + + if direction ~= 1 and direction ~= -1 then + return false, "invalid marker" + end + + local marker = worldedit.marker_get_closest_to_axis(name, axis, direction) + local deltavect = vector.new() + + if axis == 'x' then + deltavect.x = amount * direction + elseif axis == 'y' then + deltavect.y = amount * direction + elseif axis == 'z' then + deltavect.z = amount * direction + else + return false, "invalid axis" + end + + worldedit.marker_move(name, marker, deltavect) + return true +end + + +-- Shifts the cuboid by '+-amount' in axis 'axis' +worldedit.cuboid_shift = function(name, axis, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "undefined cuboid" + end + + if axis == 'x' then + pos1.x = pos1.x + amount + pos2.x = pos2.x + amount + elseif axis == 'y' then + pos1.y = pos1.y + amount + pos2.y = pos2.y + amount + elseif axis == 'z' then + pos1.z = pos1.z + amount + pos2.z = pos2.z + amount + else + return false, "invalid axis" + end + + return true +end + + +-- Moves the location of a single marker by adding deltavector +worldedit.marker_move = function(name, marker, deltavector) + if marker ~= 1 and marker ~= 2 then + return false + end + + if marker == 1 then + local pos = worldedit.pos1[name] + worldedit.pos1[name] = vector.add(deltavector, pos) + else + local pos = worldedit.pos2[name] + worldedit.pos2[name] = vector.add(deltavector, pos) + end + + return true +end + +-- Updates the location ingame of the markers +worldedit.marker_update = function(name, marker) + if marker == nil then + worldedit.mark_pos1(name) + worldedit.mark_pos2(name) + elseif marker == 1 then + worldedit.mark_pos1(name) + elseif marker == 2 then + worldedit.mark_pos2(name) + else + minetest.debug( + "worldedit: Invalid execution of function update_markers") + end +end + + +-- Returns two vectors with the directions for volumetric expansion +worldedit.get_expansion_directions = function(mark1, mark2) + if mark1 == nil or mark2 == nil then + return + end + local dir1 = vector.new() + local dir2 = vector.new() + + if mark1.x < mark2.x then + dir1.x = -1 + dir2.x = 1 + else + dir1.x = 1 + dir2.x = -1 + end + if mark1.y < mark2.y then + dir1.y = -1 + dir2.y = 1 + else + dir1.y = 1 + dir2.y = -1 + end + if mark1.z < mark2.z then + dir1.z = -1 + dir2.z = 1 + else + dir1.z = 1 + dir2.z = -1 + end + return dir1, dir2 +end + + +-- Return the marker that is closest to the player +worldedit.marker_get_closest_to_player = function(name) + local playerpos = minetest.get_player_by_name(name):getpos() + local dist1 = vector.distance(playerpos, worldedit.pos1[name]) + local dist2 = vector.distance(playerpos, worldedit.pos2[name]) + + if dist1 < dist2 then + return 1 + else + return 2 + end +end + + +-- Returns the closest marker to the specified axis and direction +worldedit.marker_get_closest_to_axis = function(name, axis, direction) + local pos1 = vector.new() + local pos2 = vector.new() + + if direction ~= 1 and direction ~= -1 then + return nil + end + + if axis == 'x' then + pos1.x = worldedit.pos1[name].x * direction + pos2.x = worldedit.pos2[name].x * direction + if pos1.x > pos2.x then + return 1 + else + return 2 + end + elseif axis == 'y' then + pos1.y = worldedit.pos1[name].y * direction + pos2.y = worldedit.pos2[name].y * direction + if pos1.y > pos2.y then + return 1 + else + return 2 + end + elseif axis == 'z' then + pos1.z = worldedit.pos1[name].z * direction + pos2.z = worldedit.pos2[name].z * direction + if pos1.z > pos2.z then + return 1 + else + return 2 + end + else + return nil + end +end + + +-- Translates up, down, left, right, front, back to their corresponding axes and +-- directions according to faced direction +worldedit.translate_direction = function(name, direction) + local axis, dir = worldedit.player_axis(name) + local resaxis, resdir + + if direction == "up" then + return 'y', 1 + end + + if direction == "down" then + return 'y', -1 + end + + if direction == "front" then + resaxis = axis + resdir = dir + end + + if direction == "back" then + resaxis = axis + resdir = -dir + end + + if direction == "left" then + if axis == 'x' then + resaxis = 'z' + resdir = dir + elseif axis == 'z' then + resaxis = 'x' + resdir = -dir + end + end + + if direction == "right" then + if axis == 'x' then + resaxis = 'z' + resdir = -dir + elseif axis == 'z' then + resaxis = 'x' + resdir = dir + end + end + + return resaxis, resdir +end \ No newline at end of file diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 2b3564f..f4f2020 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1119,4 +1119,4 @@ minetest.register_chatcommand("/clearobjects", { end), }) -dofile(minetest.get_modpath("worldedit_commands") .. "/area.lua") +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua") From ac78575872b3bc0350a9819924490a6783c7a4a2 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 20:20:27 -0600 Subject: [PATCH 09/17] Implement /inset and /shift --- worldedit_commands/cuboid.lua | 92 ++++++++++++++++++++++++-------- worldedit_commands/cuboidapi.lua | 12 ++--- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 1af5b3b..2e59f16 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -37,30 +37,78 @@ minetest.register_chatcommand("/outset", { } ) +minetest.register_chatcommand("/inset", { + params = " [h|v]", + description = "inset the selection", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + + if find == nil then + return false, "invalid usage: " .. param + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, + "Undefined region. Region must be defined beforehand." + end + + if dir == "" then + assert(worldedit.cuboid_volumetricexpand(name, -amount)) + elseif dir == "h" then + assert(worldedit.cuboid_linealexpand(name, 'x', 1, -amount)) + assert(worldedit.cuboid_linealexpand(name, 'x', -1, -amount)) + assert(worldedit.cuboid_linealexpand(name, 'z', 1, -amount)) + assert(worldedit.cuboid_linealexpand(name, 'z', -1, -amount)) + elseif dir == "v" then + assert(worldedit.cuboid_linealexpand(name, 'y', 1, -amount)) + assert(worldedit.cuboid_linealexpand(name, 'y', -1, -amount)) + else + return false, "Unknown error" + end + + worldedit.marker_update(name) + return true, "Region inset by " .. amount .. " blocks" + end, + } +) -minetest.register_chatcommand( - "/shift", - { - params = " [up|down|left|right|front|back]", - description = "Moves the selection region. Does not move contents.", - privs = {worldedit=true}, - func = function(name, param) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - local find, _, amount, direction = param:find("(%d+)%s*(%l*)") - if find == nil then - worldedit.player_notify(name, "invalid usage: " .. param) - return - end - - if pos1 == nil or pos2 == nil then - worldedit.player_notify(name, - "Undefined region. Region must be defined beforehand.") - return - end - - +minetest.register_chatcommand("/shift", { + params = " [up|down|left|right|front|back]", + description = "Moves the selection region. Does not move contents.", + privs = {worldedit=true}, + func = function(name, param) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + local find, _, amount, direction = param:find("(%d+)%s*(%l*)") + + if find == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return + end + + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, + "Undefined region. Region must be defined beforehand.") + return + end + + local axis, dir + if direction ~= "" then + axis, dir = worldedit.translate_direction(name, direction) + else + axis, dir = worldedit.player_axis(name) + worldedit.player_notify(name, "entered player_axis") + end + + assert(worldedit.cuboid_shift(name, axis, amount * dir)) + worldedit.marker_update(name) + + return true, "region shifted by " .. amount .. " blocks" end, } ) diff --git a/worldedit_commands/cuboidapi.lua b/worldedit_commands/cuboidapi.lua index 37194e4..d473571 100644 --- a/worldedit_commands/cuboidapi.lua +++ b/worldedit_commands/cuboidapi.lua @@ -65,14 +65,14 @@ worldedit.cuboid_shift = function(name, axis, amount) end if axis == 'x' then - pos1.x = pos1.x + amount - pos2.x = pos2.x + amount + worldedit.pos1[name].x = pos1.x + amount + worldedit.pos2[name].x = pos2.x + amount elseif axis == 'y' then - pos1.y = pos1.y + amount - pos2.y = pos2.y + amount + worldedit.pos1[name].y = pos1.y + amount + worldedit.pos2[name].y = pos2.y + amount elseif axis == 'z' then - pos1.z = pos1.z + amount - pos2.z = pos2.z + amount + worldedit.pos1[name].z = pos1.z + amount + worldedit.pos2[name].z = pos2.z + amount else return false, "invalid axis" end From e90da67364e42adc666cd7c0938f086af1e5b195 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 20:56:27 -0600 Subject: [PATCH 10/17] Implement /expand and /contract --- worldedit_commands/cuboid.lua | 100 +++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 2e59f16..9d6d86a 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -113,27 +113,89 @@ minetest.register_chatcommand("/shift", { } ) -minetest.register_chatcommand( - "/expand", - { - params = " [reverse-amount] [up|down|left|right|front|back]", - description = "expand the selection in one or two directions at once", - privs = {worldedit=true}, - func = function(name, param) +minetest.register_chatcommand("/expand", { + params = " [reverse-amount] [up|down|left|right|front|back]", + description = "expand the selection in one or two directions at once", + privs = {worldedit=true}, + func = function(name, param) local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") - - if find == nil then - worldedit.player_notify(name, "invalid use: " .. param) - return - end - - if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then - worldedit.player_notify(name, - "Undefined region. Region must be defined beforehand.") - return - end - + if find == nil then + worldedit.player_notify(name, "invalid use: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + worldedit.player_notify(name, + "Undefined region. Region must be defined beforehand.") + return + end + + local tmp = tonumber(arg2) + local axis, dir + local reverseamount = 0 + + axis,dir = worldedit.player_axis(name) + + if arg2 ~= "" then + if tmp == nil then + axis, dir = worldedit.translate_direction(name, arg2) + else + reverseamount = tmp + end + end + + if arg3 ~= "" then + axis, dir = worldedit.translate_direction(name, arg3) + end + + worldedit.cuboid_linealexpand(name, axis, dir, amount) + worldedit.cuboid_linealexpand(name, axis, -dir, reverseamount) + worldedit.marker_update(name) + end, + } +) + + +minetest.register_chatcommand("/contract", { + params = " [reverse-amount] [up|down|left|right|front|back]", + description = "contract the selection in one or two directions at once", + privs = {worldedit=true}, + func = function(name, param) + local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") + + if find == nil then + worldedit.player_notify(name, "invalid use: " .. param) + return + end + + if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then + worldedit.player_notify(name, + "Undefined region. Region must be defined beforehand.") + return + end + + local tmp = tonumber(arg2) + local axis, dir + local reverseamount = 0 + + axis,dir = worldedit.player_axis(name) + + if arg2 ~= "" then + if tmp == nil then + axis, dir = worldedit.translate_direction(name, arg2) + else + reverseamount = tmp + end + end + + if arg3 ~= "" then + axis, dir = worldedit.translate_direction(name, arg3) + end + + worldedit.cuboid_linealexpand(name, axis, dir, -amount) + worldedit.cuboid_linealexpand(name, axis, -dir, -reverseamount) + worldedit.marker_update(name) end, } ) From e494f70b3e4f8252d985773828cf0163a1da2e7a Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Tue, 6 Jan 2015 13:20:58 +0000 Subject: [PATCH 11/17] Fix several typos --- worldedit_commands/cuboid.lua | 60 ++++++++++++++++---------------- worldedit_commands/cuboidapi.lua | 14 ++++---- worldedit_commands/init.lua | 5 ++- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 9d6d86a..35f2bfc 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -1,3 +1,6 @@ +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboidapi.lua") + + minetest.register_chatcommand("/outset", { params = " [h|v]", description = "outset the selection", @@ -18,15 +21,15 @@ minetest.register_chatcommand("/outset", { end if dir == "" then - assert(worldedit.cuboid_volumetricexpand(name, amount)) + assert(worldedit.cuboid_volumetric_expand(name, amount)) elseif dir == "h" then - assert(worldedit.cuboid_linealexpand(name, 'x', 1, amount)) - assert(worldedit.cuboid_linealexpand(name, 'x', -1, amount)) - assert(worldedit.cuboid_linealexpand(name, 'z', 1, amount)) - assert(worldedit.cuboid_linealexpand(name, 'z', -1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount)) elseif dir == "v" then - assert(worldedit.cuboid_linealexpand(name, 'y', 1, amount)) - assert(worldedit.cuboid_linealexpand(name, 'y', -1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount)) + assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount)) else return false, "Unknown error" end @@ -37,6 +40,7 @@ minetest.register_chatcommand("/outset", { } ) + minetest.register_chatcommand("/inset", { params = " [h|v]", description = "inset the selection", @@ -57,15 +61,15 @@ minetest.register_chatcommand("/inset", { end if dir == "" then - assert(worldedit.cuboid_volumetricexpand(name, -amount)) + assert(worldedit.cuboid_volumetric_expand(name, -amount)) elseif dir == "h" then - assert(worldedit.cuboid_linealexpand(name, 'x', 1, -amount)) - assert(worldedit.cuboid_linealexpand(name, 'x', -1, -amount)) - assert(worldedit.cuboid_linealexpand(name, 'z', 1, -amount)) - assert(worldedit.cuboid_linealexpand(name, 'z', -1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount)) elseif dir == "v" then - assert(worldedit.cuboid_linealexpand(name, 'y', 1, -amount)) - assert(worldedit.cuboid_linealexpand(name, 'y', -1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount)) + assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount)) else return false, "Unknown error" end @@ -93,7 +97,7 @@ minetest.register_chatcommand("/shift", { if pos1 == nil or pos2 == nil then worldedit.player_notify(name, - "Undefined region. Region must be defined beforehand.") + "Undefined region. Region must be defined beforehand.") return end @@ -102,17 +106,17 @@ minetest.register_chatcommand("/shift", { axis, dir = worldedit.translate_direction(name, direction) else axis, dir = worldedit.player_axis(name) - worldedit.player_notify(name, "entered player_axis") end assert(worldedit.cuboid_shift(name, axis, amount * dir)) worldedit.marker_update(name) - return true, "region shifted by " .. amount .. " blocks" + return true, "region shifted by " .. amount .. " nodes" end, } ) + minetest.register_chatcommand("/expand", { params = " [reverse-amount] [up|down|left|right|front|back]", description = "expand the selection in one or two directions at once", @@ -133,7 +137,7 @@ minetest.register_chatcommand("/expand", { local tmp = tonumber(arg2) local axis, dir - local reverseamount = 0 + local reverse_amount = 0 axis,dir = worldedit.player_axis(name) @@ -141,7 +145,7 @@ minetest.register_chatcommand("/expand", { if tmp == nil then axis, dir = worldedit.translate_direction(name, arg2) else - reverseamount = tmp + reverse_amount = tmp end end @@ -149,8 +153,8 @@ minetest.register_chatcommand("/expand", { axis, dir = worldedit.translate_direction(name, arg3) end - worldedit.cuboid_linealexpand(name, axis, dir, amount) - worldedit.cuboid_linealexpand(name, axis, -dir, reverseamount) + worldedit.cuboid_linear_expand(name, axis, dir, amount) + worldedit.cuboid_linear_expand(name, axis, -dir, reverse_amount) worldedit.marker_update(name) end, } @@ -177,7 +181,7 @@ minetest.register_chatcommand("/contract", { local tmp = tonumber(arg2) local axis, dir - local reverseamount = 0 + local reverse_amount = 0 axis,dir = worldedit.player_axis(name) @@ -185,7 +189,7 @@ minetest.register_chatcommand("/contract", { if tmp == nil then axis, dir = worldedit.translate_direction(name, arg2) else - reverseamount = tmp + reverse_amount = tmp end end @@ -193,13 +197,9 @@ minetest.register_chatcommand("/contract", { axis, dir = worldedit.translate_direction(name, arg3) end - worldedit.cuboid_linealexpand(name, axis, dir, -amount) - worldedit.cuboid_linealexpand(name, axis, -dir, -reverseamount) + worldedit.cuboid_linear_expand(name, axis, dir, -amount) + worldedit.cuboid_linear_expand(name, axis, -dir, -reverse_amount) worldedit.marker_update(name) end, } -) - - -dofile(minetest.get_modpath("worldedit_commands") .. "/cuboidapi.lua") - +) \ No newline at end of file diff --git a/worldedit_commands/cuboidapi.lua b/worldedit_commands/cuboidapi.lua index d473571..08b364a 100644 --- a/worldedit_commands/cuboidapi.lua +++ b/worldedit_commands/cuboidapi.lua @@ -1,5 +1,5 @@ -- Expands or contracts the cuboid in all axes by amount (positive or negative) -worldedit.cuboid_volumetricexpand = function(name, amount) +worldedit.cuboid_volumetric_expand = function(name, amount) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] @@ -9,14 +9,14 @@ worldedit.cuboid_volumetricexpand = function(name, amount) local delta1 = vector.new() local delta2 = vector.new() - local deltadir1 - local deltadir2 + local delta_dir1 + local delta_dir2 delta1 = vector.add(delta1, amount) delta2 = vector.add(delta2, amount) - deltadir1, deltadir2 = worldedit.get_expansion_directions(pos1, pos2) - delta1 = vector.multiply(delta1, deltadir1) - delta2 = vector.multiply(delta2, deltadir2) + delta_dir1, delta_dir2 = worldedit.get_expansion_directions(pos1, pos2) + delta1 = vector.multiply(delta1, delta_dir1) + delta2 = vector.multiply(delta2, delta_dir2) worldedit.pos1[name] = vector.add(pos1, delta1) worldedit.pos2[name] = vector.add(pos2, delta2) @@ -25,7 +25,7 @@ end -- Expands or contracts the cuboid in a single axis by amount (positive or negative) -worldedit.cuboid_linealexpand = function(name, axis, direction, amount) +worldedit.cuboid_linear_expand = function(name, axis, direction, amount) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index f4f2020..cf21780 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -12,6 +12,7 @@ if minetest.place_schematic then worldedit.prob_list = {} end +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua") dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua") dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua"); safe_region = safe_region or function(callback) return callback end @@ -1117,6 +1118,4 @@ minetest.register_chatcommand("/clearobjects", { local count = worldedit.clearobjects(worldedit.pos1[name], worldedit.pos2[name]) worldedit.player_notify(name, count .. " objects cleared") end), -}) - -dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua") +}) \ No newline at end of file From eaffe7e34791405c7ce2384822f8725ad0b3bec1 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sun, 11 Jan 2015 13:12:37 +0000 Subject: [PATCH 12/17] Fix a crash that happened when trying to shift the cuboid using relative direction while looking straight up or down --- worldedit_commands/cuboid.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 35f2bfc..9bc2822 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -108,6 +108,10 @@ minetest.register_chatcommand("/shift", { axis, dir = worldedit.player_axis(name) end + if axis == nil or dir == nil then + return false, "Invalid if looking up or down" + end + assert(worldedit.cuboid_shift(name, axis, amount * dir)) worldedit.marker_update(name) From 6186a37dc552abc4037c8b5d6431cf431dea4ae5 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:28:50 +0000 Subject: [PATCH 13/17] Fix /shift command to conform to worldedit command standard --- worldedit_commands/cuboid.lua | 12 ++++++++---- worldedit_commands/cuboidapi.lua | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 9bc2822..6ea1ffc 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -82,19 +82,23 @@ minetest.register_chatcommand("/inset", { minetest.register_chatcommand("/shift", { - params = " [up|down|left|right|front|back]", +-- params = " [up|down|left|right|front|back]", + params = "[x|y|z|?|up|down|left|right|front|back] [+|-]", description = "Moves the selection region. Does not move contents.", privs = {worldedit=true}, func = function(name, param) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] - local find, _, amount, direction = param:find("(%d+)%s*(%l*)") +-- local find, _, amount, direction = param:find("(%d+)%s*(%l*)") + local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)") if find == nil then worldedit.player_notify(name, "invalid usage: " .. param) return end + worldedit.player_notify(name, "direction = " .. direction .. " amount = " .. amount) + if pos1 == nil or pos2 == nil then worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") @@ -102,14 +106,14 @@ minetest.register_chatcommand("/shift", { end local axis, dir - if direction ~= "" then + if direction ~= "?" then axis, dir = worldedit.translate_direction(name, direction) else axis, dir = worldedit.player_axis(name) end if axis == nil or dir == nil then - return false, "Invalid if looking up or down" + return false, "Invalid" end assert(worldedit.cuboid_shift(name, axis, amount * dir)) diff --git a/worldedit_commands/cuboidapi.lua b/worldedit_commands/cuboidapi.lua index 08b364a..d91b071 100644 --- a/worldedit_commands/cuboidapi.lua +++ b/worldedit_commands/cuboidapi.lua @@ -205,7 +205,7 @@ end worldedit.translate_direction = function(name, direction) local axis, dir = worldedit.player_axis(name) local resaxis, resdir - + if direction == "up" then return 'y', 1 end From 1d372f7ed527c4b5019df315623b4ad07767eff8 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:33:11 +0000 Subject: [PATCH 14/17] Remove some debug code --- worldedit_commands/cuboid.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 6ea1ffc..4712659 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -82,14 +82,12 @@ minetest.register_chatcommand("/inset", { minetest.register_chatcommand("/shift", { --- params = " [up|down|left|right|front|back]", params = "[x|y|z|?|up|down|left|right|front|back] [+|-]", description = "Moves the selection region. Does not move contents.", privs = {worldedit=true}, func = function(name, param) local pos1 = worldedit.pos1[name] local pos2 = worldedit.pos2[name] --- local find, _, amount, direction = param:find("(%d+)%s*(%l*)") local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)") if find == nil then @@ -97,8 +95,6 @@ minetest.register_chatcommand("/shift", { return end - worldedit.player_notify(name, "direction = " .. direction .. " amount = " .. amount) - if pos1 == nil or pos2 == nil then worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.") From d4de7df26381acb4f4efc607db49d7286e178d78 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:47:21 +0000 Subject: [PATCH 15/17] Fix /outset and /inset to conform to WE standards --- worldedit_commands/cuboid.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 4712659..9efe919 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -2,11 +2,11 @@ dofile(minetest.get_modpath("worldedit_commands") .. "/cuboidapi.lua") minetest.register_chatcommand("/outset", { - params = " [h|v]", + params = "[h|v] ", description = "outset the selection", privs = {worldedit=true}, func = function(name, param) - local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + local find, _, dir, amount = param:find("([hv]?)%s*([+-]?%d+)") if find == nil then return false, "invalid usage: " .. param @@ -42,11 +42,11 @@ minetest.register_chatcommand("/outset", { minetest.register_chatcommand("/inset", { - params = " [h|v]", + params = "[h|v] ", description = "inset the selection", privs = {worldedit=true}, func = function(name, param) - local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$") + local find, _, dir, amount = param:find("([hv]?)%s*([+-]?%d+)") if find == nil then return false, "invalid usage: " .. param From bc2f4d0f9c0059edb75b1d20e03773afb584f478 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 03:46:45 +0000 Subject: [PATCH 16/17] Reimplement /expand and /contract to conform to WE standards --- worldedit_commands/cuboid.lua | 70 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 9efe919..2a5a9fb 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -122,11 +122,12 @@ minetest.register_chatcommand("/shift", { minetest.register_chatcommand("/expand", { - params = " [reverse-amount] [up|down|left|right|front|back]", + params = "[+|-] [reverse-amount]", description = "expand the selection in one or two directions at once", privs = {worldedit=true}, func = function(name, param) - local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") + local find, _, sign, direction, amount, + rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)") if find == nil then worldedit.player_notify(name, "invalid use: " .. param) @@ -139,26 +140,34 @@ minetest.register_chatcommand("/expand", { return end - local tmp = tonumber(arg2) - local axis, dir - local reverse_amount = 0 + local absolute = direction:find("[xyz?]") + local dir, axis - axis,dir = worldedit.player_axis(name) + if rev_amount == "" then + rev_amount = 0 + end - if arg2 ~= "" then - if tmp == nil then - axis, dir = worldedit.translate_direction(name, arg2) + if absolute == nil then + axis, dir = worldedit.translate_direction(name, direction) + + if axis == nil or dir == nil then + return false, "Invalid if looking straight up or down" + end + else + if direction == "?" then + axis, dir = worldedit.player_axis(name) else - reverse_amount = tmp + axis = direction + dir = 1 end end - if arg3 ~= "" then - axis, dir = worldedit.translate_direction(name, arg3) + if sign == "-" then + dir = -dir end worldedit.cuboid_linear_expand(name, axis, dir, amount) - worldedit.cuboid_linear_expand(name, axis, -dir, reverse_amount) + worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount) worldedit.marker_update(name) end, } @@ -166,11 +175,12 @@ minetest.register_chatcommand("/expand", { minetest.register_chatcommand("/contract", { - params = " [reverse-amount] [up|down|left|right|front|back]", + params = "[+|-] [reverse-amount]", description = "contract the selection in one or two directions at once", privs = {worldedit=true}, func = function(name, param) - local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)") + local find, _, sign, direction, amount, + rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)") if find == nil then worldedit.player_notify(name, "invalid use: " .. param) @@ -183,26 +193,34 @@ minetest.register_chatcommand("/contract", { return end - local tmp = tonumber(arg2) - local axis, dir - local reverse_amount = 0 + local absolute = direction:find("[xyz?]") + local dir, axis - axis,dir = worldedit.player_axis(name) + if rev_amount == "" then + rev_amount = 0 + end - if arg2 ~= "" then - if tmp == nil then - axis, dir = worldedit.translate_direction(name, arg2) + if absolute == nil then + axis, dir = worldedit.translate_direction(name, direction) + + if axis == nil or dir == nil then + return false, "Invalid if looking straight up or down" + end + else + if direction == "?" then + axis, dir = worldedit.player_axis(name) else - reverse_amount = tmp + axis = direction + dir = 1 end end - if arg3 ~= "" then - axis, dir = worldedit.translate_direction(name, arg3) + if sign == "-" then + dir = -dir end worldedit.cuboid_linear_expand(name, axis, dir, -amount) - worldedit.cuboid_linear_expand(name, axis, -dir, -reverse_amount) + worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount) worldedit.marker_update(name) end, } From a636a64413fd57a9aed157a9e0d900a41318c386 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 04:15:52 +0000 Subject: [PATCH 17/17] Implement some safety mechanisms --- worldedit_commands/cuboid.lua | 2 +- worldedit_commands/cuboidapi.lua | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 2a5a9fb..003b358 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -109,7 +109,7 @@ minetest.register_chatcommand("/shift", { end if axis == nil or dir == nil then - return false, "Invalid" + return false, "Invalid if looking straight up or down" end assert(worldedit.cuboid_shift(name, axis, amount * dir)) diff --git a/worldedit_commands/cuboidapi.lua b/worldedit_commands/cuboidapi.lua index d91b071..ce20761 100644 --- a/worldedit_commands/cuboidapi.lua +++ b/worldedit_commands/cuboidapi.lua @@ -215,13 +215,23 @@ worldedit.translate_direction = function(name, direction) end if direction == "front" then - resaxis = axis - resdir = dir + if axis == "y" then + resaxis = nil + resdir = nil + else + resaxis = axis + resdir = dir + end end if direction == "back" then - resaxis = axis - resdir = -dir + if axis == "y" then + resaxis = nil + resdir = nil + else + resaxis = axis + resdir = -dir + end end if direction == "left" then