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}!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
33
Content.Shared/Random/RandomFillSolution.cs
Normal file
33
Content.Shared/Random/RandomFillSolution.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
||||
|
||||
namespace Content.Shared.Random;
|
||||
|
||||
/// <summary>
|
||||
/// Data that specifies reagents that share the same weight and quantity for use with WeightedRandomSolution.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
[DataDefinition]
|
||||
public sealed class RandomFillSolution
|
||||
{
|
||||
/// <summary>
|
||||
/// Quantity of listed reagents.
|
||||
/// </summary>
|
||||
[DataField("quantity")]
|
||||
public FixedPoint2 Quantity = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Random weight of listed reagents.
|
||||
/// </summary>
|
||||
[DataField("weight")]
|
||||
public float Weight = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Listed reagents that the weight and quantity apply to.
|
||||
/// </summary>
|
||||
[DataField("reagents", required: true, customTypeSerializer: typeof(PrototypeIdListSerializer<ReagentPrototype>))]
|
||||
public List<string> Reagents = new();
|
||||
}
|
||||
18
Content.Shared/Random/WeightedRandomFillSolutionPrototype.cs
Normal file
18
Content.Shared/Random/WeightedRandomFillSolutionPrototype.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Random;
|
||||
|
||||
/// <summary>
|
||||
/// Random weighting dataset for solutions, able to specify reagents quantity.
|
||||
/// </summary>
|
||||
[Prototype("weightedRandomFillSolution")]
|
||||
public sealed class WeightedRandomFillSolutionPrototype : IPrototype
|
||||
{
|
||||
[IdDataField] public string ID { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// List of RandomFills that can be picked from.
|
||||
/// </summary>
|
||||
[DataField("fills", required: true)]
|
||||
public List<RandomFillSolution> Fills = new();
|
||||
}
|
||||
Reference in New Issue
Block a user