public ItemAmount[] NextHarvestDrop(double minChance = 0.0, double maxChance = 1.0) { // sanitize inputs if (minChance < 0.0 || minChance > 1.0) throw new ArgumentOutOfRangeException(nameof(minChance), minChance, "The minimal drop chance must be in the [0.0, 1.0] range."); if (maxChance < 0.0 || maxChance > 1.0) throw new ArgumentOutOfRangeException(nameof(maxChance), maxChance, "The maximal drop chance must be in the [0.0, 1.0] range."); if (minChance > maxChance) throw new ArgumentOutOfRangeException(nameof(minChance), minChance, "The minimal drop chance must be lower than the maximal one."); ItemAmount[] results = null; // if no table, return an empty array *but not null* if (HarvestTables.Count > 0) { //if (HarvestTables.Count == 1) results = HarvestTables[0].Results; if (minChance == 1.0) results = HarvestTables.Last().Results; else { double selected = Winecrash.Random.NextDouble(); // if needed move the selected chance range to the minimal chance one if (minChance != 0 || maxChance != 0) selected = WMath.Remap(selected, 0.0, 1.0, minChance, maxChance); // invert the range in order to get the least chance // this will take the one with the least chance to appear // (considering the tables are sorted by chance, cf HarvestTable's constructor) HarvestTable table = HarvestTables.FirstOrDefault(ht => ht.Chance >= selected); if (table.Block != null) results = table.Results; } } return results ?? Array.Empty<ItemAmount>(); }
public HarvestTable[] ParseHarvestTables(bool addToCache = true) { HarvestTable[] parsedTables = new HarvestTable[HarvestTables.Length]; Parallel.For(0, parsedTables.Length, i => { parsedTables[i] = JsonConvert.DeserializeObject <HarvestTable>(File.ReadAllText(HarvestTables[i])); }); if (addToCache) { HarvestTable.Tables.AddRange(parsedTables); } return(parsedTables); }