skywars/_chest_handler/treasures.lua

102 lines
2.8 KiB
Lua
Raw Normal View History

2020-09-07 16:37:12 -07:00
-- Select the treasures to put in the chests inventory
2020-07-18 14:18:57 -07:00
local mod = "skywars"
2020-09-07 16:37:12 -07:00
local function determine_count(treasure)
if(type(treasure.count)=="number") then
return treasure.count
else
local min,max,prob = treasure.count[1], treasure.count[2], treasure.count[3]
if(prob == nil) then
return(math.floor(min + math.random() * (max-min)))
else
return(math.floor(min + prob() * (max-min)))
end
end
end
2020-07-18 14:18:57 -07:00
2020-09-07 16:37:12 -07:00
local function treasure_to_itemstack(treasure)
local itemstack = {}
itemstack.name = treasure.name
itemstack.count = determine_count(treasure)
2020-07-18 14:18:57 -07:00
2020-09-07 16:37:12 -07:00
return ItemStack(itemstack)
end
2020-07-18 14:18:57 -07:00
function skywars.select_random_treasures(treasure_amount, min_preciousness, max_preciousness, arena)
2020-09-07 16:37:12 -07:00
if #arena.treasures == 0 and treasure_amount >= 1 then
minetest.log("info","[treasurer] I was asked to return "..treasure_amount.." treasure(s) but I cant return any because no treasure was registered to me.")
2020-07-18 14:18:57 -07:00
return {}
end
2020-09-07 16:37:12 -07:00
if treasure_amount == nil or treasure_amount == 0 then treasure_amount = 1 end
-- sorting the table from the rarest to the least rare treasure, so that if one of them has rarity one
-- and it's at index one it doesn't prevent other treasures from appearing
for j=#arena.treasures, 2, -1 do
for i=1, #arena.treasures-1 do
if arena.treasures[i].rarity < arena.treasures[i+1].rarity then
local temp = arena.treasures[i]
arena.treasures[i] = arena.treasures[i+ 1]
arena.treasures[i + 1] = temp
end
end
end
2020-07-18 14:18:57 -07:00
2020-09-07 16:37:12 -07:00
-- helper table
2020-07-18 14:18:57 -07:00
local p_treasures = {}
2020-09-07 16:37:12 -07:00
-- copying arena.treasures in p_treasures
for i=1, #arena.treasures do
table.insert(p_treasures, arena.treasures[i])
2020-07-18 14:18:57 -07:00
end
2020-09-07 16:37:12 -07:00
if(min_preciousness ~= nil) then
2020-07-18 14:18:57 -07:00
-- filter out too unprecious treasures
2020-09-07 16:37:12 -07:00
for t=#p_treasures, 1, -1 do
if(p_treasures[t].preciousness < min_preciousness) then
2020-07-18 14:18:57 -07:00
table.remove(p_treasures,t)
end
end
end
if(max_preciousness ~= nil) then
-- filter out too precious treasures
for t=#p_treasures,1,-1 do
if(p_treasures[t].preciousness > max_preciousness) then
table.remove(p_treasures,t)
end
end
end
local treasures = {}
2020-09-07 16:37:12 -07:00
-- while the generated treasures are less then the desired amount
while #treasures < treasure_amount do
for c=1,treasure_amount do
-- if there isn't a treasure
if treasures[c] == nil then
for t=1,#p_treasures do
local random = math.random(1, 20)
2020-09-07 16:37:12 -07:00
-- if the random number is a multiple of the item rarity then select it
if random % p_treasures[t].rarity == 0 then
table.insert(treasures, p_treasures[t])
end
end
2020-07-18 14:18:57 -07:00
end
end
end
local itemstacks = {}
for i=1,#treasures do
itemstacks[i] = treasure_to_itemstack(treasures[i])
end
2020-09-07 16:37:12 -07:00
if #itemstacks < treasure_amount then
minetest.log("info","[treasurer] I was asked to return "..treasure_amount.." treasure(s) but I could only return "..(#itemstacks)..".")
2020-07-18 14:18:57 -07:00
end
return itemstacks
end