Add linter-friendly WeightedRandom prototypes (#18729)

This commit is contained in:
Vordenburg
2023-08-05 22:31:25 -04:00
committed by GitHub
parent 9c84108672
commit cc8b642444
16 changed files with 88 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
using Content.Shared.Mining;
using Content.Shared.Mining;
using Content.Shared.Random;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
@@ -20,7 +20,7 @@ public sealed class OreVeinComponent : Component
/// <summary>
/// The weighted random prototype used for determining what ore will be dropped.
/// </summary>
[DataField("oreRarityPrototypeId", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>))]
[DataField("oreRarityPrototypeId", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomOrePrototype>))]
public string? OreRarityPrototypeId;
/// <summary>

View File

@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Content.Shared.Dataset;
using Content.Shared.FixedPoint;
using Robust.Shared.Random;
@@ -12,7 +12,7 @@ namespace Content.Shared.Random.Helpers
return random.Pick(prototype.Values);
}
public static string Pick(this WeightedRandomPrototype prototype, System.Random random)
public static string Pick(this IWeightedRandomPrototype prototype, System.Random random)
{
var picks = prototype.Weights;
var sum = picks.Values.Sum();
@@ -34,7 +34,7 @@ namespace Content.Shared.Random.Helpers
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
}
public static string Pick(this WeightedRandomPrototype prototype, IRobustRandom? random = null)
public static string Pick(this IWeightedRandomPrototype prototype, IRobustRandom? random = null)
{
IoCManager.Resolve(ref random);
var picks = prototype.Weights;

View File

@@ -0,0 +1,13 @@
using Robust.Shared.Prototypes;
namespace Content.Shared.Random;
/// <summary>
/// IWeightedRandomPrototype implements a dictionary of strings to float weights
/// to be used with <see cref="Helpers.SharedRandomExtensions.Pick(IWeightedRandomPrototype, Robust.Shared.Random.IRobustRandom)" />.
/// </summary>
public interface IWeightedRandomPrototype : IPrototype
{
[ViewVariables]
public Dictionary<string, float> Weights { get; }
}

View File

@@ -0,0 +1,17 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Shared.Random;
/// <summary>
/// Linter-friendly version of weightedRandom for Entity prototypes.
/// </summary>
[Prototype("weightedRandomEntity")]
public sealed class WeightedRandomEntityPrototype : IWeightedRandomPrototype
{
[IdDataField]
public string ID { get; } = default!;
[DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<float, EntityPrototype>))]
public Dictionary<string, float> Weights { get; } = new();
}

View File

@@ -0,0 +1,18 @@
using Content.Shared.Mining;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Shared.Random;
/// <summary>
/// Linter-friendly version of weightedRandom for Ore prototypes.
/// </summary>
[Prototype("weightedRandomOre")]
public sealed class WeightedRandomOrePrototype : IWeightedRandomPrototype
{
[IdDataField]
public string ID { get; } = default!;
[DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<float, OrePrototype>))]
public Dictionary<string, float> Weights { get; } = new();
}

View File

@@ -6,10 +6,11 @@ namespace Content.Shared.Random;
/// Generic random weighting dataset to use.
/// </summary>
[Prototype("weightedRandom")]
public sealed class WeightedRandomPrototype : IPrototype
public sealed class WeightedRandomPrototype : IWeightedRandomPrototype
{
[IdDataField] public string ID { get; } = default!;
[IdDataField]
public string ID { get; } = default!;
[DataField("weights")]
public Dictionary<string, float> Weights = new();
public Dictionary<string, float> Weights { get; } = new();
}

View File

@@ -0,0 +1,18 @@
using Content.Shared.Humanoid.Prototypes;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Shared.Random;
/// <summary>
/// Linter-friendly version of weightedRandom for Species prototypes.
/// </summary>
[Prototype("weightedRandomSpecies")]
public sealed class WeightedRandomSpeciesPrototype : IWeightedRandomPrototype
{
[IdDataField]
public string ID { get; } = default!;
[DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<float, SpeciesPrototype>))]
public Dictionary<string, float> Weights { get; } = new();
}

View File

@@ -179,7 +179,7 @@ public abstract class SharedSalvageSystem : EntitySystem
foreach (var id in ids)
{
// pick a random reward to give
var weights = _proto.Index<WeightedRandomPrototype>(id);
var weights = _proto.Index<WeightedRandomEntityPrototype>(id);
rewards.Add(weights.Pick(rand));
}
@@ -187,7 +187,7 @@ public abstract class SharedSalvageSystem : EntitySystem
}
/// <summary>
/// Get a list of WeightedRandomPrototype IDs with the rewards for a certain difficulty.
/// Get a list of WeightedRandomEntityPrototype IDs with the rewards for a certain difficulty.
/// </summary>
private string[] RewardsForDifficulty(DifficultyRating rating)
{