mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-10 01:13:45 +01:00
14 lines
492 B
Lua
14 lines
492 B
Lua
|
|
--- Parses a chance value, and returns the 1-in-N value thereof.
|
|
-- @param str string The string to parse.
|
|
-- @returns number|nil The 1-in-N chance if parsing was successful, otherwise nil.
|
|
function worldeditadditions.parse.chance(str)
|
|
if tonumber(str) ~= nil then return tonumber(str) end
|
|
if str:sub(#str) == "%" then
|
|
local result = tonumber(str:sub(1, #str-1))
|
|
if not result then return nil end
|
|
return 1 / (result / 100) -- Convert percentage to 1-in-N chance
|
|
end
|
|
return nil
|
|
end
|