-- This creates the main table; all functions of this mod are stored in this table
treasurer={}
-- Table which stores all the treasures
treasurer.treasures={}
-- This table stores the treasures again, but this time sorted by groups
treasurer.groups={}
-- Groups defined by the Treasurer API
treasurer.groups.treasurer={}
treasurer.groups.treasurer.default={}
-- Groups defined by the Minetest API
treasurer.groups.minetest={}
--[[
formatoftreasuretable:
treasure={
name,-- treasure name, e.g. mymod:item
rarity,-- relative rarity on a scale from 0 to 1 (inclusive).
-- a rare treasure must not neccessarily be a precious treasure
count,-- count (see below)
preciousness,-- preciousness or “worth” of the treasure.
-- ranges from 0 (“scorched stuff”) to 10 (“diamond block”)
wear,-- wear (see below)
metadata,-- unused at the moment
}
treasurescanbenodesoritems
formatofcounttype:
count=number-- it’s always number times
count={min,max}-- it’s pseudorandomly between min and max times, math.random() will be used to chose the value
count={min,max,prob_func}-- it’s between min and max times, and the value is given by prob_func (which is not neccessarily random [in the strictly mathematical sense])
formatofweartype:
completelyanalogoustocounttype
formatofprob_funcfunction:
prob_func=function()
--> returns a random or pseudorandom number between 0 (inclusive) and 1 (exclusive)
--[[ We don’t trust our input, so we first check if the parameters
havethecorrecttypesandrefusetoaddthetreasureifa
parameterismalformed.
Whatfollowsisabunchofparameterchecks.
]]
-- check wheather name is a string
iftype(name)~="string"then
minetest.log("error","[treasure] I rejected a treasure because the name was of type \""..type(name).."\" instead of \"string\".")
returnfalse
end
-- first check if rarity is even a number
iftype(rarity)=="number"then
-- then check wheather the rarity lies in the allowed range
ifrarity<0orrarity>1then
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it’s rarity value is out of bounds. (it was "..tostring(rarity)..".)")
returnfalse
end
else
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of rarity. Given type was \""..type(rarity).."\".")
returnfalse
end
-- check if preciousness is even a number
iftype(preciousness)=="number"then
-- then check wheather the preciousness lies in the allowed range
ifpreciousness<0orpreciousness>10then
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it’s preciousness value is out of bounds. (it was "..tostring(preciousness)..".)")
returnfalse
end
else
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of preciousness. Given type was \""..type(preciousness).."\".")
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of “count”. Given type was \""..type(count).."\".")
returnfalse
end
-- if count’s a table, check if it’s format is correct
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of “wear”. Given type was \""..type(wear).."\".")
returnfalse
end
-- if wear’s a table, check if it’s format is correct
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because the treasure_group parameter is of type "..tosting(type(treasurer_groups)).." (expected: nil, string or table).")
returnfalse
end
--[[ End of checks. If we reached this point of the code, all checks have been passed
andwefinallyregisterthetreasure.]]
-- default count is 1
ifcount==nilthencount=1end
-- default wear is 0
ifwear==nilthenwear=0end
localtreasure={
name=name,
rarity=rarity,
count=count,
wear=wear,
preciousness=preciousness,
metadata="",
}
table.insert(treasurer.treasures,treasure)
--[[ Assign treasure to Treasurer group(s) or default if not provided ]]
minetest.log("info","[treasurer] I was asked to return "..count.." treasure(s) but I can’t return any because no treasure which fits to the given Treasurer group “"..treasurer_groups.."”.")