dreadpiratepeter has asked for the wisdom of the Perl Monks concerning the following question:
I could use some help here as I am a bear of little brain. I am writing some rpg stuff and I am working on a method that generates random items a la Diablo. Things like a "Flaming silver +3 longsword of Slay Undead".
The items are of a form:
<prefix1>? <prefix2>{0,3) <plus> <weapon_type> <suffix>?
For purposes of discussion assume my list of each type of component looks like:
where the second parameter is a weight. The weapon type is just a simple unweighted choice and for argument the plus runs from 0-5 with the value also being the weight.@prefix1 = ( [flaming => 2], [frost => 2], [acid => 4], ... );
So, for example:
+2 mace = weight 2 Flaming longsword = weight 2 Acid +3 dagger = weight 7
Here is the challenging part, that I haven't solved. Given a weight range, generate an item within that range.
Note that you can add only 1 prefix and 1 suffix to an item, but you can add up to 3 prefix2.
I had a few thoughts as to an algorithm, but they all have problems:
- I could just randomly generate items till I got one in the range. Obviously, this is flawed because it could take a very long time
- Randomly choose a thing to add to the item (that is still available), keep a running weight and stop when I hit the right range or run out of available slots. I think this would bias the items toward high pluses and large numbers of prefix2 parts, because a low weight for prefix and suffix would leave a lot of weight left to fill with prefix2 items. Also it would seriously lower the chance of getting items the don't have all of the component types.
- Some sort of heuristic that would set an appropriate lower limit on weight to prevent the above bias from occuring. This would be good because higher weight items should not have a lot of lower weight compoents.
I hope it is clear what I am trying to do. I am not looking for code, just some ideas for an elegant solution.
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."