Minetest-WorldEditAdditions/worldeditadditions/utils/parse/chance.lua

17 lines
909 B
Lua
Raw Normal View History

--- Parses a chance value, and returns the 1-in-N value thereof.
2021-05-11 21:12:04 +00:00
-- @param str string The string to parse.
-- @param invert_percent string The operation mode. Valid modes: "1-in-n" (default), "weight". "1-in-n" refers to a 1-in-N chance of something happening (lower numbers mean greater likelihood). "weight", on the other hand, is instead a weighting that something will happen (higher numbers mean a greater likelihood).
-- @returns number|nil The 1-in-N chance if parsing was successful, otherwise nil.
2021-05-11 21:12:04 +00:00
function worldeditadditions.parse.chance(str, mode)
if not mode then mode = "1-in-n" end
if tonumber(str) ~= nil then return tonumber(str) end
if str:sub(#str) == "%" then
local result = tonumber(str:sub(1, #str-1))
2021-05-11 20:59:43 +00:00
if not result then return nil end
2021-05-11 21:12:04 +00:00
if mode == "weight" then result = 100 - result end
return 1 / (result / 100) -- Convert percentage to 1-in-N chance
end
return nil
end