From 9e087ff995e7fccde690832a390741a43a824109 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 21:45:25 -0600 Subject: [PATCH 01/20] 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 c93a760..30e6f9a 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1182,3 +1182,5 @@ minetest.register_chatcommand("/clearobjects", { worldedit.player_notify(name, count .. " objects cleared") end), }) + +dofile(minetest.get_modpath("worldedit_commands") .. "/area.lua") From d475682d8f647dc30744ac8ba9809b8fe6722b7d Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 22:29:37 -0600 Subject: [PATCH 02/20] 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 f49663902c4ac393e599ff0cc82ea712c1beaa80 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Fri, 2 Jan 2015 23:40:56 -0600 Subject: [PATCH 03/20] 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 900b2f25aae79b1f16c7d1bb4ae6a4d20f7bc8ac Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 01:23:19 -0600 Subject: [PATCH 04/20] 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 0b97a7c74090492937a6a572babd0dcb9592aeed Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:04:02 -0600 Subject: [PATCH 05/20] 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 6cf8b924345e69f705ce7cc437e34d137bd773a6 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:29:28 -0600 Subject: [PATCH 06/20] 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 ae29a9f0641360855bc9c36c24e437d17fcc34dd Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 02:51:59 -0600 Subject: [PATCH 07/20] 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 8d213d32a092ec03dca17ada9217a87656bd2b18 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 19:46:59 -0600 Subject: [PATCH 08/20] 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 30e6f9a..21a6af3 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1183,4 +1183,4 @@ minetest.register_chatcommand("/clearobjects", { end), }) -dofile(minetest.get_modpath("worldedit_commands") .. "/area.lua") +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua") From 240380ff16bfb8dedb18b991315f0bacd937caf5 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 20:20:27 -0600 Subject: [PATCH 09/20] 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 859c6bd12a45a8c9c5ad25675b2916f39937e261 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 3 Jan 2015 20:56:27 -0600 Subject: [PATCH 10/20] 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 04d20de4cd9bc55fc37889ee53fd827e5ebcf9a0 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Tue, 6 Jan 2015 13:20:58 +0000 Subject: [PATCH 11/20] 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 21a6af3..eedc372 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -10,6 +10,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") local safe_region, check_region = dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua") @@ -1181,6 +1182,4 @@ minetest.register_chatcommand("/clearobjects", { local count = worldedit.clear_objects(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 40cee997006a976acdf89dc610d8ac15232c3645 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sun, 11 Jan 2015 13:12:37 +0000 Subject: [PATCH 12/20] 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 f1fe0c6bba883f1f547fb08b0e944878ce4293bf Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:28:50 +0000 Subject: [PATCH 13/20] 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 fad021d0ba40f3336e0770587b40811894cfb4a7 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:33:11 +0000 Subject: [PATCH 14/20] 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 d5e004be67447b5b20185c3428e8930cea025fc6 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 02:47:21 +0000 Subject: [PATCH 15/20] 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 f8f1f3b9cc28f78e60ce53d4966d6b288ee44156 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 03:46:45 +0000 Subject: [PATCH 16/20] 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 48f3f59fc3b03e0114ef8070f59fd58b918cc253 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Mon, 16 Feb 2015 04:15:52 +0000 Subject: [PATCH 17/20] 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 From b922097e7af7e3ad938741fd659210159e91cc4c Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Wed, 18 Feb 2015 17:27:32 -0600 Subject: [PATCH 18/20] Update command documentation --- ChatCommands.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ChatCommands.md b/ChatCommands.md index b11aaf0..1760e81 100644 --- a/ChatCommands.md +++ b/ChatCommands.md @@ -386,3 +386,38 @@ This mode can be left with `//mtschemprob finish`. `//mtschemprob get` will disp Clears all objects within the WorldEdit region. //clearobjects + +### `//shift x/y/z/?/up/down/left/right/front/back [+|-]` + +Shifts the selection area by [+|-] without touching its contents. The shifting axis can be absolute (x/y/z) or +relative (up/down/left/right/front/back). + + //shift left 5 + +### `//expand [+|-]x/y/z/?/up/down/left/right/front/back [reverse-amount]` + +Expands the selection by in the selected absolute or relative axis. If specified, the selection can be expanded in the +opposite direction iver the same axis by [reverse-amount] + + //expand right 7 5 + +### `//contract [+|-]x/y/z/?/up/down/left/right/front/back [reverse-amount]` + +Contracts the selection by in the selected absolute or relative axis. If specified, the selection can be contracted in the +opposite direction iver the same axis by [reverse-amount] + + //expand right 7 5 + +### `//outset [h|v] ` + +Expands the selection in all directions by . If specified, the selection can be expanded horizontally in the x and z axes [h] +or vertically in the y axis [v]. + + //outset v 5 + +### `//inset [h|v] ` + +Contracts the selection in all directions by . If specified, the selection can be contracted horizontally in the x and z axes [h] +or vertically in the y axis [v]. + + //outset v 5 \ No newline at end of file From 78890dde07e7006a9e4c2b853720d7e544760947 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Thu, 19 Feb 2015 14:01:45 -0600 Subject: [PATCH 19/20] Clean some documentation issues --- ChatCommands.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ChatCommands.md b/ChatCommands.md index 1760e81..6dc4c72 100644 --- a/ChatCommands.md +++ b/ChatCommands.md @@ -389,35 +389,35 @@ Clears all objects within the WorldEdit region. ### `//shift x/y/z/?/up/down/left/right/front/back [+|-]` -Shifts the selection area by [+|-] without touching its contents. The shifting axis can be absolute (x/y/z) or -relative (up/down/left/right/front/back). +Shifts the selection area by `[+|-]` without touching its contents. The shifting axis can be absolute (`x/y/z`) or +relative (`up/down/left/right/front/back`). //shift left 5 ### `//expand [+|-]x/y/z/?/up/down/left/right/front/back [reverse-amount]` -Expands the selection by in the selected absolute or relative axis. If specified, the selection can be expanded in the -opposite direction iver the same axis by [reverse-amount] +Expands the selection by `` in the selected absolute or relative axis. If specified, the selection can be expanded in the +opposite direction over the same axis by `[reverse-amount]`. //expand right 7 5 ### `//contract [+|-]x/y/z/?/up/down/left/right/front/back [reverse-amount]` -Contracts the selection by in the selected absolute or relative axis. If specified, the selection can be contracted in the -opposite direction iver the same axis by [reverse-amount] +Contracts the selection by `` in the selected absolute or relative axis. If specified, the selection can be contracted in the +opposite direction over the same axis by `[reverse-amount]`. //expand right 7 5 ### `//outset [h|v] ` -Expands the selection in all directions by . If specified, the selection can be expanded horizontally in the x and z axes [h] -or vertically in the y axis [v]. +Expands the selection in all directions by ``. If specified, the selection can be expanded horizontally in the x and z axes `[h]` +or vertically in the y axis `[v]`. //outset v 5 ### `//inset [h|v] ` -Contracts the selection in all directions by . If specified, the selection can be contracted horizontally in the x and z axes [h] -or vertically in the y axis [v]. +Contracts the selection in all directions by ``. If specified, the selection can be contracted horizontally in the x and z axes `[h]` +or vertically in the y axis `[v]`. //outset v 5 \ No newline at end of file From e18525d8c983eb78c8f7f2fa2425cede21fc8289 Mon Sep 17 00:00:00 2001 From: Daniel Sosa Date: Sat, 21 Feb 2015 19:07:14 +0000 Subject: [PATCH 20/20] Modify outset and inset to accept both hv and vh combinations. Update documentation accordingly. --- ChatCommands.md | 4 ++-- worldedit_commands/cuboid.lua | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ChatCommands.md b/ChatCommands.md index 6dc4c72..b26e894 100644 --- a/ChatCommands.md +++ b/ChatCommands.md @@ -408,14 +408,14 @@ opposite direction over the same axis by `[reverse-amount]`. //expand right 7 5 -### `//outset [h|v] ` +### `//outset [hv] ` Expands the selection in all directions by ``. If specified, the selection can be expanded horizontally in the x and z axes `[h]` or vertically in the y axis `[v]`. //outset v 5 -### `//inset [h|v] ` +### `//inset [hv] ` Contracts the selection in all directions by ``. If specified, the selection can be contracted horizontally in the x and z axes `[h]` or vertically in the y axis `[v]`. diff --git a/worldedit_commands/cuboid.lua b/worldedit_commands/cuboid.lua index 003b358..24dad28 100644 --- a/worldedit_commands/cuboid.lua +++ b/worldedit_commands/cuboid.lua @@ -6,7 +6,7 @@ minetest.register_chatcommand("/outset", { description = "outset the selection", privs = {worldedit=true}, func = function(name, param) - local find, _, dir, amount = param:find("([hv]?)%s*([+-]?%d+)") + local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)") if find == nil then return false, "invalid usage: " .. param @@ -20,7 +20,13 @@ minetest.register_chatcommand("/outset", { "Undefined region. Region must be defined beforehand." end - if dir == "" then + local hv_test = dir:find("[^hv]+") + + if hv_test ~= nil then + return false, "Invalid direction." + end + + if dir == "" or dir == "hv" or dir == "vh" then assert(worldedit.cuboid_volumetric_expand(name, amount)) elseif dir == "h" then assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount)) @@ -31,7 +37,7 @@ minetest.register_chatcommand("/outset", { assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount)) assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount)) else - return false, "Unknown error" + return false, "Invalid number of arguments" end worldedit.marker_update(name) @@ -46,7 +52,7 @@ minetest.register_chatcommand("/inset", { description = "inset the selection", privs = {worldedit=true}, func = function(name, param) - local find, _, dir, amount = param:find("([hv]?)%s*([+-]?%d+)") + local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)") if find == nil then return false, "invalid usage: " .. param @@ -60,7 +66,13 @@ minetest.register_chatcommand("/inset", { "Undefined region. Region must be defined beforehand." end - if dir == "" then + local hv_test = dir:find("[^hv]+") + + if hv_test ~= nil then + return false, "Invalid direction." + end + + if dir == "" or dir == "vh" or dir == "hv" then assert(worldedit.cuboid_volumetric_expand(name, -amount)) elseif dir == "h" then assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount)) @@ -71,7 +83,7 @@ minetest.register_chatcommand("/inset", { assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount)) assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount)) else - return false, "Unknown error" + return false, "Invalid number of arguments" end worldedit.marker_update(name)