I'm reworking my loot scripts. I want to move away from cumbersone merchant stores and into a more easily configured and less 'weighty' option by scripting.
The downside to having a loot table as a merchant's inventory is that you have very weak configurability as to the probabilities of items, as well as each subtle version of a loot table requires a completely separate merchant (which leads to filesize increases - having 100 separate loot merchants with mostly overlapping inventory is not a good use of module resources). The pro of this seems to be the speed - it's easy to find the merchant, pick a random item in its inventory, and copy it to the loot container.
Creating loot is a frequent chore to accomplish, so I don't want to introduce a system that gives up the speed, but I do want to add more precise control of the item probabilities (gold vs. weapons vs. ammo vs. armor vs. etc...) and allows me to scale the number of unique loot tables to as many as I need.
Here's the process I've mapped out so far. I'd love input into how I can better structure it for speed and efficiency, as well as any thoughts on how this would compare to the speed of the merchant stores. Maybe there's a better way altogether.
1) There are 2 dimensions of my Loot Tables: Location and difficulty. Location defines the y-axis, while difficulty defines the x-axis.)
2) I've grouped loot into ~30 different item types (heal potions, other potions, scrolls, gold, melee weapons, ranged weapons, ammo, etc...)
Each loot table has unique probabilities for each of the 30 item types based upon its Location/Difficulty metric. Ultimately specific item selection will also be affected by this, but right now, I'm focused on determining the item type.
As an example, I have:
string 50003 (the loot table - location 50, difficulty 3) = ""001009 002000 003000 004000 005000 006000 007008 008000 009005 010015 011000 012005 013002 014000 015000 016002 017009 018000 019002 020000 021002 022005 023000 024033 025000 026004";
This string defines the probability for each item type. In the above, Item Type 001 has 9% probability. Item Type 002 has 0% probability. And so on.
I've written a while loop that 'should' parse the string and add up the sum of the probabilities against a random roll. When the sum of the probabilities exceeds that of the random roll, the loop stops and the category that was the last one checked becomes the item category from which to select the specific item.
Here's what I've written, but would like comments. I'm concerned about potentially 30 StringParse and StringRemovedParse commands each time there's a need to generate loot (every creature death, every opened chest, etc...).
int CreateRandomLootType (int LootTable)
{
string sTableProbabilities = GetLootTableProbabilities(IntToString(LootTable)); //this is the loot table string of probabilities
int iItemType;
int iItemProbability;
int iProbabilitySum;
string sParsed;
int iRoll = d100();
while (iProbabilitySum<iRoll)
{
sParsed = StringParse(sTableProbabilities); //should return the 6 string chars before the space
iItemProbability = StringToInt(GetStringRight(sParsed,3));
iProbabilitySum = iProbabilitySum + iItemProbability;
//only need to check the section if the probability exceeds roll
if (iProbabilitySum>=iRoll)
{
iItemType = StringToInt(GetStringLeft(sParsed,3));
break;
}
else
{
sTableProbabilities = StringRemoveParsed(sTableProbabilities, sParsed);
}
}
return iItemType;
}
Thanks!