Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SomeClass {
- public class WeightedItem {
- public readonly int weight;
- public readonly Item drop;
- public WeightedItem(Item i, int w) {
- drop = i;
- weight = w;
- }
- public ItemStack GetStackFromDrop() {
- return new ItemStack(drop);
- }
- }
- private List<WeightedItem> bucket = new List<WeightedItem>();
- private totalWeight = 0;
- public void Add (Item item, int count) {
- if(count <= 0) throw new Exception("Invalid random weight");
- bucket.Add(new WeightedItem(item, count));
- totalWeight += count;
- }
- public void Add(WeightedItem item) {
- bucket.Add(item);
- totalWeight += item.weight;
- }
- public ItemStack GetRandom (Random rng) {
- int randomVal = rng.Next(0, totalWeight);
- foreach(WeightedItem item in bucket) {
- randomVal -= item.weight;
- if(randomVal <= 0) {
- return item.GetStackFromDrop();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment