Merge pull request #42 from VorTechnix/master

//scube, //scol, vectors and vector population, and bulletproofing
This commit is contained in:
Starbeamrainbowlabs 2021-03-11 21:01:55 +00:00 committed by GitHub
commit 008a559854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 173 additions and 50 deletions

View file

@ -4,7 +4,7 @@ It's about time I started a changelog! This will serve from now on as the main c
Note to self: See the bottom of this file for the release template text. Note to self: See the bottom of this file for the release template text.
## v1.12 (unreleased) ## v1.12 (unreleased)
- Add `//srect` (_select rectangle_) - thanks, @VorTechnix! - Add `//srect` (_select rectangle_), `//scol` (_select column_), `//scube` (_select cube_) - thanks, @VorTechnix!
- Add `//spush`, `//spop`, and `//sstack` - Add `//spush`, `//spop`, and `//sstack`
- `//overlay`: Don't place nodes above water - `//overlay`: Don't place nodes above water
- `//multi`: Improve resilience by handling some edge cases - `//multi`: Improve resilience by handling some edge cases

View file

@ -482,6 +482,14 @@ Executes the given command, and then clips the result to the largest ellipsoid t
//ellipsoidapply layers desert_sand sand 2 desert_sandstone 4 sandstone 10 //ellipsoidapply layers desert_sand sand 2 desert_sandstone 4 sandstone 10
``` ```
## `//scol [<axis1> ] <length>`
Short for _select column_. Sets the pos2 at a set distance along 1 axis from pos1. If the axis isn't specified, defaults the direction you are facing. Implementation thanks to @VorTechnix.
```
//scol 10
//scol x 3
```
## `//srect [<axis1> [<axis2>]] <length>` ## `//srect [<axis1> [<axis2>]] <length>`
Short for _select rectangle_. Sets the pos2 at a set distance along 2 axes from pos1. If the axes aren't specified, defaults to positive y and the direction you are facing. Implementation thanks to @VorTechnix. Short for _select rectangle_. Sets the pos2 at a set distance along 2 axes from pos1. If the axes aren't specified, defaults to positive y and the direction you are facing. Implementation thanks to @VorTechnix.
@ -491,6 +499,16 @@ Short for _select rectangle_. Sets the pos2 at a set distance along 2 axes from
//srect -z y 25 //srect -z y 25
``` ```
## `//scube [<axis1> [<axis2> [<axis3>]]] <length>`
Short for _select cube_. Sets the pos2 at a set distance along 3 axes from pos1. If the axes aren't specified, defaults to positive y, the direction you are facing and the axis to the left of facing. Implementation thanks to @VorTechnix.
```
//scube 5
//scube z a y 12
//scube x z 3
//scube -z 12
```
## `//sstack` ## `//sstack`
Displays the contents of your per-user selection stack. This stack can be pushed to and popped from rather like a stack of plates. See also `//spush` (for pushing to the selection stack) and `//spop` (for popping from the selection stack). Displays the contents of your per-user selection stack. This stack can be pushed to and popped from rather like a stack of plates. See also `//spush` (for pushing to the selection stack) and `//spop` (for popping from the selection stack).

View file

@ -42,21 +42,12 @@ function worldeditadditions.eta(existing_times, done_count, total_count)
end end
--- Returns the sign (+ or -) at the beginning of a string if present. --- Returns the sign (+ or -) at the beginning of a string if present.
-- @param str string Input string. -- @param src string|int Input string.
-- @param type string The type of value to return. Valid values: "string" (default), "int" -- @return string|int Returns the signed multiplier (1|-1).
-- @return string|int Returns the sign string or signed multiplier (1|-1). function worldeditadditions.getsign(src)
function worldeditadditions.getsign(str, type) if type(src) == "number" then return src < 0 and -1 or 1
if not type then type = "string" end elseif type(src) ~= "string" then return 1
if not (type == "string" or type == "int") then else return src:match('-') and -1 or 1 end
return false, "Error: Unknown type '"..type.."'."
end
if str:sub(1, 1) == "-" then
if type == "int" then return true, -1
else return true, "-" end
else
if type == "int" then return true, 1
else return true, "+" end
end
end end
-- For Testing: -- For Testing:

View file

@ -1,11 +1,21 @@
-- Returns the player's facing direction on the horizontal axes only. -- Returns the player's facing direction on the horizontal axes only.
-- @param name string The name of the player to return facing direction of. -- @param name string The name of the player to return facing direction of.
-- @return table Returns axis name and sign multiplyer. -- @return Returns axis name and sign multiplier.
function worldeditadditions.player_axis2d(name) function worldeditadditions.player_axis2d(name)
-- minetest.get_player_by_name("singleplayer"): -- minetest.get_player_by_name("singleplayer"):
local dir = math.floor(minetest.get_player_by_name(name):get_look_horizontal() / math.pi * 2 + 0.5) % 4 local dir = minetest.get_player_by_name(name):get_look_dir()
local crdnl = { {1,"z"},{-1,"x"},{-1,"z"},{1,"x"} } local x, z= math.abs(dir.x), math.abs(dir.z)
return crdnl[dir+1] if x > z then return "x", dir.x > 0 and 1 or -1
else return "z", dir.z > 0 and 1 or -1 end
end
-- Returns the axis and sign of the axis to the left of the input axis.
-- @param axis string x or z.
-- @param sign int Sign multiplier.
-- @return Returns axis name and sign multiplier.
function worldeditadditions.axis_left(axis,sign)
if not axis:match("[xz]") then return false, "Error: Not a horizontal axis!"
elseif axis == "x" then return true, "z", sign
else return true, "x", -sign end
end end
-- Tests -- Tests

View file

@ -0,0 +1,46 @@
-- ███████ ██████ ██████ ██
-- ██ ██ ██ ██ ██
-- ███████ ██ ██ ██ ██
-- ██ ██ ██ ██ ██
-- ███████ ██████ ██████ ███████
local wea = worldeditadditions
worldedit.register_command("scol", {
params = "[<axis1>] <length>",
description = "Set WorldEdit region position 2 at a set distance along 1 axis.",
privs = {worldedit=true},
require_pos = 1,
parse = function(params_text)
local vec, tmp = vector.new(0, 0, 0), {}
local find = wea.split(params_text, "%s", false)
local ax1, sn1, len = (tostring(find[1]):match('[xyz]') or "g"):sub(1,1), wea.getsign(find[1]), find[table.maxn(find)]
tmp.len = tonumber(len)
-- If len == nil cancel the operation
if not tmp.len then return false, "No length specified." end
-- If ax1 is bad send "get" order
if ax1 == "g" then tmp.get = true
else vec[ax1] = sn1 * tmp.len end
return true, vec, tmp
-- tmp carries:
-- The length (len) arguement to the main function for use if "get" is invoked there
-- The bool value "get" to tell the main function if it needs to populate missing information in vec
end,
func = function(name, vec, tmp)
if tmp.get then
local ax, dir = wea.player_axis2d(name)
vec[ax] = tmp.len * dir
end
local p2 = vector.add(vec,worldedit.pos1[name])
worldedit.pos2[name] = p2
worldedit.mark_pos2(name)
return true, "position 2 set to " .. minetest.pos_to_string(p2)
end,
})
-- Tests
-- /multi //fp set1 -63 19 -20 //scol 5
-- lua print(worldedit.player_axis(myname))
-- tonumber(('y1'):gsub('[xyz]',''):sub(1,2))

View file

@ -0,0 +1,60 @@
-- ███████ ██████ ██ ██ ██████ ███████
-- ██ ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ██ ██████ █████
-- ██ ██ ██ ██ ██ ██ ██
-- ███████ ██████ ██████ ██████ ███████
local wea = worldeditadditions
worldedit.register_command("scube", {
params = "[<axis1> [<axis2> [<axis3>]]] <length>",
description = "Set WorldEdit region position 2 at a set distance along 3 axes.",
privs = { worldedit = true },
require_pos = 1,
parse = function(params_text)
local vec, tmp = vector.new(0, 0, 0), {}
local find = wea.split(params_text, "%s", false)
local ax1, ax2, ax3 = (tostring(find[1]):match('[xyz]') or "g"):sub(1,1), (tostring(find[2]):match('[xyz]') or "g"):sub(1,1),
(tostring(find[3]):match('[xyz]') or "g"):sub(1,1)
local sn1, sn2, sn3, len = wea.getsign(find[1]), wea.getsign(find[2]), wea.getsign(find[3]), find[table.maxn(find)]
tmp.len = tonumber(len)
-- If len is nill cancel the operation
if not tmp.len then return false, "No length specified." end
-- If axis is bad send "get" order
if ax1 == "g" then tmp.get = true
else vec[ax1] = sn1 * tmp.len end
if ax2 == "g" then tmp.get = true
else vec[ax2] = sn2 * tmp.len end
if ax3 == "g" then tmp.get = true
else vec[ax3] = sn3 * tmp.len end
tmp.axes = ax1..","..ax2..","..ax3
return true, vec, tmp
-- tmp carries:
-- The length (len) arguement to the main function for use if "get" is invoked there
-- The bool value "get" to tell the main function if it needs to populate missing information in vec
-- The string "axes" to tell the main function what axes are and/or need to be populated if "get" is invoked
end,
func = function(name, vec, tmp)
if tmp.get then
local ax, dir = wea.player_axis2d(name)
local _, left, sn = wea.axis_left(ax,dir)
if not tmp.axes:find("x") then vec.x = tmp.len * (ax == "x" and dir or sn) end
if not tmp.axes:find("z") then vec.z = tmp.len * (ax == "z" and dir or sn) end
if not tmp.axes:find("y") then vec.y = tmp.len end
end
local p2 = vector.add(vec,worldedit.pos1[name])
worldedit.pos2[name] = p2
worldedit.mark_pos2(name)
return true, "position 2 set to " .. minetest.pos_to_string(p2)
end,
})
-- Tests
-- /multi //fp set1 -63 19 -20 //scube 5
-- /multi //fp set1 -63 19 -20 //scube z 5
-- /multi //fp set1 -63 19 -20 //scube a z 5
-- /multi //fp set1 -63 19 -20 //scube z a y 5
-- /multi //fp set1 -63 19 -20 //scube -z y a 5
-- /multi //fp set1 -63 19 -20 //scube z z 5
-- /lua print()

View file

@ -3,45 +3,42 @@
-- ███████ ██████ █████ ██ ██ -- ███████ ██████ █████ ██ ██
-- ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ███████ ██████ ██ -- ███████ ██ ██ ███████ ██████ ██
local wea = worldeditadditions
worldedit.register_command("srect", { worldedit.register_command("srect", {
params = "[<axis1> [<axis2>]] <length>", params = "[<axis1> [<axis2>]] <length>",
description = "Set WorldEdit region position 2 at a set distance along 2 axes.", description = "Set WorldEdit region position 2 at a set distance along 2 axes.",
privs = { worldedit = true }, privs = { worldedit = true },
require_pos = 1, require_pos = 1,
parse = function(params_text) parse = function(params_text)
local wea = worldeditadditions local vec, tmp = vector.new(0, 0, 0), {}
local find = wea.split(params_text, "%s", false) local find = wea.split(params_text, "%s", false)
local ax1, ax2, len = find[1], find[2], find[table.maxn(find)] local ax1, ax2 = (tostring(find[1]):match('[xyz]') or "g"):sub(1,1), (tostring(find[2]):match('[xyz]') or "g"):sub(1,1)
local sn1, sn2, len = wea.getsign(find[1]), wea.getsign(find[2]), find[table.maxn(find)]
-- If ax1 is bad set to player facing dir tmp.len = tonumber(len)
if ax1 == len or not ax1:match('[xyz]') then ax1 = "get"
else
local success, value = wea.getsign(ax1, "int")
if not success then return success, value
else ax1 = { value, ax1:gsub('[^xyz]', ''):sub(1, 1) } end
end
-- If ax2 is bad set to +y
if not ax2 or ax2 == len or not ax2:match('[xyz]') then ax2 = "y" end
local success, value = wea.getsign(ax2, "int")
if not success then return success, value end
ax2 = { value, ax2:gsub('[^xyz]', ''):sub(1, 1) }
len = tonumber(len)
-- If len == nill cancel the operation -- If len == nill cancel the operation
if len == nil then if not tmp.len then return false, "No length specified." end
return false, "No length specified." -- If axis is bad send "get" order
if ax1 == "g" then tmp.get = true
else vec[ax1] = sn1 * tmp.len end
if ax2 == "g" then tmp.get = true
else vec[ax2] = sn2 * tmp.len end
tmp.axes = ax1..","..ax2
return true, vec, tmp
-- tmp carries:
-- The length (len) arguement to the main function for use if "get" is invoked there
-- The bool value "get" to tell the main function if it needs to populate missing information in vec
-- The string "axes" to tell the main function what axes are and/or need to be populated if "get" is invoked
end,
func = function(name, vec, tmp)
if tmp.get then
local ax, dir = wea.player_axis2d(name)
if not tmp.axes:find("[xz]") then vec[ax] = tmp.len * dir end
if not tmp.axes:find("y") then vec.y = tmp.len end
end end
return true, ax1, ax2, len local p2 = vector.add(vec,worldedit.pos1[name])
end,
func = function(name, axis1, axis2, len)
if axis1 == "get" then axis1 = worldeditadditions.player_axis2d(name) end
local p2 = vector.new(worldedit.pos1[name])
p2[axis1[2]] = p2[axis1[2]] + tonumber(len) * axis1[1]
p2[axis2[2]] = p2[axis2[2]] + tonumber(len) * axis2[1]
worldedit.pos2[name] = p2 worldedit.pos2[name] = p2
worldedit.mark_pos2(name) worldedit.mark_pos2(name)
return true, "position 2 set to " .. minetest.pos_to_string(p2) return true, "position 2 set to " .. minetest.pos_to_string(p2)
@ -56,3 +53,4 @@ worldedit.register_command("srect", {
-- /multi //fp set1 -63 19 -20 //srect -z 5 -- /multi //fp set1 -63 19 -20 //srect -z 5
-- /multi //fp set1 -63 19 -20 //srect a -x 5 -- /multi //fp set1 -63 19 -20 //srect a -x 5
-- /multi //fp set1 -63 19 -20 //srect -x -a 5 -- /multi //fp set1 -63 19 -20 //srect -x -a 5
-- lua vec = vector.new(15,-12,17); vec["len"] = 5; vec.get = true; vec2 = vector.add(vector.new(1,1,1),vec) print(vec2.x,vec2.y,vec2.z,vec2.len)

View file

@ -41,9 +41,9 @@ dofile(we_c.modpath.."/commands/meta/many.lua")
dofile(we_c.modpath.."/commands/meta/subdivide.lua") dofile(we_c.modpath.."/commands/meta/subdivide.lua")
dofile(we_c.modpath.."/commands/meta/ellipsoidapply.lua") dofile(we_c.modpath.."/commands/meta/ellipsoidapply.lua")
-- dofile(we_c.modpath.."/commands/selectors/scol.lua") dofile(we_c.modpath.."/commands/selectors/scol.lua")
dofile(we_c.modpath.."/commands/selectors/srect.lua") dofile(we_c.modpath.."/commands/selectors/srect.lua")
-- dofile(we_c.modpath.."/commands/selectors/scube.lua") dofile(we_c.modpath.."/commands/selectors/scube.lua")
dofile(we_c.modpath.."/commands/selectors/sstack.lua") dofile(we_c.modpath.."/commands/selectors/sstack.lua")
dofile(we_c.modpath.."/commands/selectors/spush.lua") dofile(we_c.modpath.."/commands/selectors/spush.lua")
dofile(we_c.modpath.."/commands/selectors/spop.lua") dofile(we_c.modpath.."/commands/selectors/spop.lua")