Add Spaceshrooms (#17092)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Dataset;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Shared.Random.Helpers
|
||||
@@ -75,5 +76,61 @@ namespace Content.Shared.Random.Helpers
|
||||
|
||||
throw new InvalidOperationException($"Invalid weighted pick");
|
||||
}
|
||||
|
||||
public static (string reagent, FixedPoint2 quantity) Pick(this WeightedRandomFillSolutionPrototype prototype, IRobustRandom? random = null)
|
||||
{
|
||||
var randomFill = prototype.PickRandomFill(random);
|
||||
|
||||
IoCManager.Resolve(ref random);
|
||||
|
||||
var sum = randomFill.Reagents.Count;
|
||||
var accumulated = 0f;
|
||||
|
||||
var rand = random.NextFloat() * sum;
|
||||
|
||||
foreach (var reagent in randomFill.Reagents)
|
||||
{
|
||||
accumulated += 1f;
|
||||
|
||||
if (accumulated >= rand)
|
||||
{
|
||||
return (reagent, randomFill.Quantity);
|
||||
}
|
||||
}
|
||||
|
||||
// Shouldn't happen
|
||||
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
|
||||
}
|
||||
|
||||
public static RandomFillSolution PickRandomFill(this WeightedRandomFillSolutionPrototype prototype, IRobustRandom? random = null)
|
||||
{
|
||||
IoCManager.Resolve(ref random);
|
||||
|
||||
var fills = prototype.Fills;
|
||||
Dictionary<RandomFillSolution, float> picks = new();
|
||||
|
||||
foreach (var fill in fills)
|
||||
{
|
||||
picks[fill] = fill.Weight;
|
||||
}
|
||||
|
||||
var sum = picks.Values.Sum();
|
||||
var accumulated = 0f;
|
||||
|
||||
var rand = random.NextFloat() * sum;
|
||||
|
||||
foreach (var (randSolution, weight) in picks)
|
||||
{
|
||||
accumulated += weight;
|
||||
|
||||
if (accumulated >= rand)
|
||||
{
|
||||
return randSolution;
|
||||
}
|
||||
}
|
||||
|
||||
// Shouldn't happen
|
||||
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user