worldeditadditions.parse.chance: add API function

This commit is contained in:
Starbeamrainbowlabs 2021-05-11 21:55:44 +01:00
parent efd8a8682e
commit 4ec04c03b3
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
2 changed files with 15 additions and 1 deletions

@ -0,0 +1,13 @@
--- 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 result == nil then return nil end
return 1 / (result / 100) -- Convert percentage to 1-in-N chance
end
return nil
end

@ -1,5 +1,6 @@
worldeditadditions.parse = {}
dofile(worldeditadditions.modpath.."/utils/parse/weighted_nodes.lua")
dofile(worldeditadditions.modpath.."/utils/parse/chance.lua")
dofile(worldeditadditions.modpath.."/utils/parse/map.lua")
dofile(worldeditadditions.modpath.."/utils/parse/seed.lua")
dofile(worldeditadditions.modpath.."/utils/parse/weighted_nodes.lua")