2020-05-11 03:02:02 +02:00
|
|
|
-- From http://lua-users.org/wiki/SimpleRound
|
2020-05-12 01:38:42 +02:00
|
|
|
function worldeditadditions.round(num, numDecimalPlaces)
|
2021-02-26 05:22:39 +01:00
|
|
|
local mult = 10^(numDecimalPlaces or 0)
|
|
|
|
return math.floor(num * mult + 0.5) / mult
|
2020-05-11 03:02:02 +02:00
|
|
|
end
|
2020-06-09 02:21:32 +02:00
|
|
|
|
|
|
|
function worldeditadditions.hypotenuse(x1, y1, x2, y2)
|
|
|
|
local xSquare = math.pow(x1 - x2, 2);
|
|
|
|
local ySquare = math.pow(y1 - y2, 2);
|
|
|
|
return math.sqrt(xSquare + ySquare);
|
|
|
|
end
|
2020-06-26 21:46:35 +02:00
|
|
|
|
2020-10-10 22:43:22 +02:00
|
|
|
function worldeditadditions.sum(list)
|
2021-02-26 05:22:39 +01:00
|
|
|
if #list == 0 then return 0 end
|
2020-06-26 21:46:35 +02:00
|
|
|
local sum = 0
|
|
|
|
for i,value in ipairs(list) do
|
|
|
|
sum = sum + value
|
|
|
|
end
|
2021-02-26 05:22:39 +01:00
|
|
|
return sum
|
2020-10-10 22:43:22 +02:00
|
|
|
end
|
|
|
|
|
2021-05-30 00:13:10 +02:00
|
|
|
--- Calculates the mean of all the numbers in the given list.
|
|
|
|
-- @param list number[] The list (table) of numbers to calculate the mean for.
|
|
|
|
-- @returns The mean of the numbers in the given table.
|
2020-10-10 22:43:22 +02:00
|
|
|
function worldeditadditions.average(list)
|
2021-02-26 05:22:39 +01:00
|
|
|
if #list == 0 then return 0 end
|
2020-10-10 22:43:22 +02:00
|
|
|
return worldeditadditions.sum(list) / #list
|
2020-06-26 21:46:35 +02:00
|
|
|
end
|
|
|
|
|
2021-05-30 00:13:10 +02:00
|
|
|
--- Finds the minimum value in the given list.
|
|
|
|
-- @param list number[] The list (table) of numbers to find the minimum value of.
|
|
|
|
-- @returns number The minimum value in the given list.
|
|
|
|
function worldeditadditions.min(list)
|
|
|
|
if #list == 0 then return nil end
|
|
|
|
local min = nil
|
|
|
|
for i,value in ipairs(list) do
|
|
|
|
if min == nil or min > value then
|
|
|
|
min = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return min
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Finds the maximum value in the given list.
|
|
|
|
-- @param list number[] The list (table) of numbers to find the maximum value of.
|
|
|
|
-- @returns number The maximum value in the given list.
|
|
|
|
function worldeditadditions.max(list)
|
|
|
|
if #list == 0 then return nil end
|
|
|
|
local max = nil
|
|
|
|
for i,value in ipairs(list) do
|
|
|
|
if max == nil or max < value then
|
|
|
|
max = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return max
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-06-26 21:46:35 +02:00
|
|
|
--- Returns the minetest.get_us_time() in ms
|
|
|
|
-- @return float
|
|
|
|
function worldeditadditions.get_ms_time()
|
|
|
|
return minetest.get_us_time() / 1000
|
|
|
|
end
|
2020-10-10 18:01:08 +02:00
|
|
|
|
2021-03-20 02:56:14 +01:00
|
|
|
--- Calculates the estimated time remaining from a list of times.
|
|
|
|
-- Intended to be used where one has a number of works units, and one has a
|
|
|
|
-- list of how long the most recent units have taken to run.
|
|
|
|
-- @param existing_times number[] A list of times - in ms - that the most recent work units have taken.
|
|
|
|
-- @param done_count number The number of work units completed so far.
|
|
|
|
-- @param total_count number The total number of work units to be completed.
|
2021-02-07 03:45:34 +01:00
|
|
|
function worldeditadditions.eta(existing_times, done_count, total_count)
|
2021-03-20 03:09:28 +01:00
|
|
|
local max = 100
|
|
|
|
local average = worldeditadditions.average(
|
2021-06-28 01:56:29 +02:00
|
|
|
worldeditadditions.table.get_last(existing_times, max)
|
2021-03-20 03:09:28 +01:00
|
|
|
)
|
|
|
|
local times_left = total_count - done_count
|
|
|
|
if times_left == 0 then return 0 end
|
|
|
|
return average * times_left
|
2021-02-26 05:22:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the sign (+ or -) at the beginning of a string if present.
|
2021-03-09 20:05:03 +01:00
|
|
|
-- @param src string|int Input string.
|
|
|
|
-- @return string|int Returns the signed multiplier (1|-1).
|
|
|
|
function worldeditadditions.getsign(src)
|
2021-08-05 19:40:36 +02:00
|
|
|
if type(src) == "number" then
|
|
|
|
if src < 0 then return -1 else return 1 end
|
2021-03-09 20:05:03 +01:00
|
|
|
elseif type(src) ~= "string" then return 1
|
2021-08-05 19:40:36 +02:00
|
|
|
else
|
|
|
|
if src:match('-') then return -1 else return 1 end
|
|
|
|
end
|
2020-10-10 18:01:08 +02:00
|
|
|
end
|
2021-02-28 18:59:09 +01:00
|
|
|
|
2021-07-04 14:21:13 +02:00
|
|
|
--- Clamp a number to ensure it falls within a given range.
|
|
|
|
-- @param value number The value to clamp.
|
|
|
|
-- @param min number The minimum allowed value.
|
|
|
|
-- @param max number The maximum allowed value.
|
|
|
|
-- @returns number The clamped number.
|
|
|
|
function worldeditadditions.clamp(value, min, max)
|
|
|
|
if value < min then return min end
|
|
|
|
if value > max then return max end
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
2021-02-28 18:59:09 +01:00
|
|
|
-- For Testing:
|
|
|
|
-- worldeditadditions = {}
|
|
|
|
-- print(worldeditadditions.getsign('-y'))
|