Added getsign, fixed srect glitch

This commit is contained in:
VorTechnix 2021-02-25 20:22:39 -08:00
parent b62e4f706d
commit cb1d54c665
2 changed files with 43 additions and 24 deletions

View File

@ -1,7 +1,7 @@
-- From http://lua-users.org/wiki/SimpleRound
function worldeditadditions.round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function worldeditadditions.hypotenuse(x1, y1, x2, y2)
@ -11,17 +11,17 @@ function worldeditadditions.hypotenuse(x1, y1, x2, y2)
end
function worldeditadditions.sum(list)
if #list == 0 then return 0 end
if #list == 0 then return 0 end
local sum = 0
for i,value in ipairs(list) do
sum = sum + value
end
return sum
return sum
end
function worldeditadditions.average(list)
if #list == 0 then return 0 end
if #list == 0 then return 0 end
return worldeditadditions.sum(list) / #list
end
@ -32,11 +32,29 @@ function worldeditadditions.get_ms_time()
end
function worldeditadditions.eta(existing_times, done_count, total_count)
local max = 100
local average = worldeditadditions.average(
worldeditadditions.table_get_last(existing_times, max)
)
local times_left = total_count - done_count
if times_left == 0 then return 0 end
return average * times_left
local max = 100
local average = worldeditadditions.average(
worldeditadditions.table_get_last(existing_times, max)
)
local times_left = total_count - done_count
if times_left == 0 then return 0 end
return average * times_left
end
--- Returns the sign (+ or -) at the beginning of a string if present.
-- @param str string Input string.
-- @param type string The type of value to return. Valid values: "string" (default), "int"
-- @return string|int Returns the sign string or signed multiplier (1|-1).
function worldeditadditions.getsign(str, type)
if not type then type = "string" end
if not (type == "string" or type == "int") then
return false, "Error: Unknown type '"..type.."'."
end
if str:sub(1, 1) == "-" then
if type == "int" then return -1
else return "-" end
else
if type == "int" then return 1
else return "+" end
end
end

View File

@ -3,20 +3,22 @@
-- ███████ ██████ █████ ██ ██
-- ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ███████ ██████ ██
-- lua parse_params_srect("10")
-- local -- TODO: set this to local once development is finished
function parse_params_srect(params_text)
local find, _, sn1, ax1, sn2, ax2, len = params_text:find("([+-]?)([xyz]?)%s*([+-]?)([xyz]?)%s*(%d*)")
local wea = worldeditadditions
local find = wea.split(params_text, "%s", false)
local ax1, ax2, len = find[1], find[2], find[table.maxn(find)]
-- If ax1 is nil set to player facing dir
if ax1 == "" then ax1 = "get"
else ax1 = {tonumber(sn1..1),string.lower(ax1)}
-- If ax1 is bad set to player facing dir
if ax1 == len or not ax1:match('[xyz]') then ax1 = "get"
else ax1 = {wea.getsign(ax1, "int"),ax1:gsub('-?',''):sub(1,1)}
end
-- If ax2 is nil set to +y
if ax2 == "" then ax2 = "y" end
ax2 = {tonumber(sn2..1),string.lower(ax2)}
-- If ax2 is bad set to +y
if not ax2 or ax2 == len or not ax2:match('[xyz]') then ax2 = "y" end
ax2 = {wea.getsign(ax2, "int"),ax2:gsub('-?',''):sub(1,1)}
len = tonumber(len)
-- If len == nill cancel the operation
if len == nil then
return false, "No length specified."
end
@ -36,17 +38,16 @@ worldedit.register_command("srect", {
if axis1 == "get" then axis1 = worldeditadditions.player_axis2d(name) end
local pos1 = worldedit.pos1[name]
local p2 = {["x"] = pos1.x,["y"] = pos1.y,["z"] = pos1.z}
local p2 = vector.new(pos1)
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.mark_pos2(name)
worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(p2))
return true, "position 2 set to " .. minetest.pos_to_string(p2)
end,
})
-- Tests
-- params_text = "-x z 13"
-- params_text = "-x a 13"
-- /multi //fp set1 -63 19 -20 //srect 5