using System.Collections.Generic; using System.Linq; using UnityEngine; namespace DunGen { public class ChanceTable { [SerializeField] public List> Weights = new List>(); public void Add(T value, float weight) { Weights.Add(new Chance(value, weight)); } public void Remove(T value) { for (int i = 0; i < Weights.Count; i++) { if (Weights[i].Value.Equals(value)) { Weights.RemoveAt(i); } } } public T GetRandom(RandomStream random) { float num = Weights.Select((Chance x) => x.Weight).Sum(); float num2 = (float)(random.NextDouble() * (double)num); foreach (Chance weight in Weights) { if (num2 < weight.Weight) { return weight.Value; } num2 -= weight.Weight; } return default(T); } public static TVal GetCombinedRandom(RandomStream random, params ChanceTable[] tables) { float num = tables.SelectMany((ChanceTable x) => x.Weights.Select((Chance y) => y.Weight)).Sum(); float num2 = (float)(random.NextDouble() * (double)num); foreach (Chance item in tables.SelectMany((ChanceTable x) => x.Weights)) { if (num2 < item.Weight) { return item.Value; } num2 -= item.Weight; } return default(TVal); } } }