I'm working on a Minecraft spigot hobbyp roject and decided to add openable chests and boss drops. Generating elements by weight is something I did before so it wasn't a hard task for me and I already finished it, but looking to improve.
While I was doing this I looked into some articles about generating elements by weight and found some nice and much faster methods but the problem with all of them is they only work (or only work that fast) when you have a fixed list of elements and fixed weights.
Because I'm doing boss drops and chest loot tables I want to generate multiple items, and while doing so remove them temporarily from the element list because I don't want to get the same item multiple times from one generation. What I also implemented is a luck system which gives a multiplier for certain items, which creates the problem of items not having a fixed weight.
For example let's say a boss mob has the following loot table:
- helmet: 40
- chestplate: 40
- leggings: 40
- boots: 40
- common sword: 30
- rare sword: 10
This is only the default loot table but with the luck system certain items gain weight. Let's say the player has a luck modifier of 5 and the rare sword's weight increases by 4 for every luck level so the table now looks like this:
- helmet: 40
- chestplate: 40
- leggings: 40
- boots: 40
- common sword: 30
- rare sword: 30
The luck level is actually a double and it can be anything from 0 to 20 usually but in some cases it can be even larger, which means I can't just pre-generate x number of loot tables, I have to compute their actual weight when the method is called. And on top of that I don't want to generate anything twice. Let's say I want to generate 3 items from this table; right now I generate the item and temporarily remove it so it can't be generated again, and at the end of the method I put them back in the main list.
Here is what I have now: https://github.com/Xeonmeister/ItemGenerator
I'm looking to improve the speed of my 'WeightedGenerator'. I've seen methods which generate items from a list using binary search or the alias method which generates by weight in O(1) but they only work if the weight never changes and/or the list of items doesn't change. Is there some method that can generate items with dynamic weights and without repeating items and runs faster than O(n) ?