I'm trying to figure out the best way to implement a loot drop system for my roguelike. I need individual enemies to have lists of items they can drop, each with different odds of dropping. Here's how I'm doing it now:
public static Item checkDrop(List<LootChance> loot_list)
{
if (checkLootList(loot_list))
{
float roll = (float)rand.NextDouble() * 100;
float chance_count = 0;
foreach (LootChance lc in loot_list)
{
if (roll <= lc.PercentChance)
{
return lc.Loot;
}
chance_count += lc.PercentChance;
}
}
else
{
Log.add("Loot odds exceed 100%!");
}
return null;
}
This works for now, but it will stop me from ever having the total odds of all possible loot items to exceed 100. I could see there being other, possibly better ways of handling this. How is this kind of thing usually calculated?