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.Server.Mining.Components; using Content.Server.Mining.Components;
using Content.Shared.Destructible; using Content.Shared.Destructible;
using Content.Shared.Mining; using Content.Shared.Mining;
using Content.Shared.Random; using Content.Shared.Random;
@@ -47,6 +47,6 @@ public sealed class MiningSystem : EntitySystem
if (component.CurrentOre != null || component.OreRarityPrototypeId == null || !_random.Prob(component.OreChance)) if (component.CurrentOre != null || component.OreRarityPrototypeId == null || !_random.Prob(component.OreChance))
return; return;
component.CurrentOre = _proto.Index<WeightedRandomPrototype>(component.OreRarityPrototypeId).Pick(_random); component.CurrentOre = _proto.Index<WeightedRandomOrePrototype>(component.OreRarityPrototypeId).Pick(_random);
} }
} }

View File

@@ -108,7 +108,7 @@ namespace Content.Server.Salvage
/// A weighted random prototype corresponding to /// A weighted random prototype corresponding to
/// what asteroid entities will be generated. /// what asteroid entities will be generated.
/// </summary> /// </summary>
[DataField("asteroidPool", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>)), ViewVariables(VVAccess.ReadWrite)] [DataField("asteroidPool", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomEntityPrototype>)), ViewVariables(VVAccess.ReadWrite)]
public string AsteroidPool = "RandomAsteroidPool"; public string AsteroidPool = "RandomAsteroidPool";
} }

View File

@@ -347,7 +347,7 @@ namespace Content.Server.Salvage
EntityUid? salvageEnt; EntityUid? salvageEnt;
if (_random.Prob(component.AsteroidChance)) if (_random.Prob(component.AsteroidChance))
{ {
var asteroidProto = _prototypeManager.Index<WeightedRandomPrototype>(component.AsteroidPool).Pick(_random); var asteroidProto = _prototypeManager.Index<WeightedRandomEntityPrototype>(component.AsteroidPool).Pick(_random);
salvageEnt = Spawn(asteroidProto, new MapCoordinates(0, 0, salvMap)); salvageEnt = Spawn(asteroidProto, new MapCoordinates(0, 0, salvMap));
} }
else else

View File

@@ -115,7 +115,7 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem
if (_randomizeCharacters) if (_randomizeCharacters)
{ {
var weightId = _configurationManager.GetCVar(CCVars.ICRandomSpeciesWeights); var weightId = _configurationManager.GetCVar(CCVars.ICRandomSpeciesWeights);
var weights = _prototypeManager.Index<WeightedRandomPrototype>(weightId); var weights = _prototypeManager.Index<WeightedRandomSpeciesPrototype>(weightId);
speciesId = weights.Pick(_random); speciesId = weights.Pick(_random);
} }
else if (profile != null) else if (profile != null)

View File

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

View File

@@ -1,4 +1,4 @@
using System.Linq; using System.Linq;
using Content.Shared.Dataset; using Content.Shared.Dataset;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Robust.Shared.Random; using Robust.Shared.Random;
@@ -12,7 +12,7 @@ namespace Content.Shared.Random.Helpers
return random.Pick(prototype.Values); 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 picks = prototype.Weights;
var sum = picks.Values.Sum(); var sum = picks.Values.Sum();
@@ -34,7 +34,7 @@ namespace Content.Shared.Random.Helpers
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!"); 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); IoCManager.Resolve(ref random);
var picks = prototype.Weights; 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. /// Generic random weighting dataset to use.
/// </summary> /// </summary>
[Prototype("weightedRandom")] [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")] [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) foreach (var id in ids)
{ {
// pick a random reward to give // pick a random reward to give
var weights = _proto.Index<WeightedRandomPrototype>(id); var weights = _proto.Index<WeightedRandomEntityPrototype>(id);
rewards.Add(weights.Pick(rand)); rewards.Add(weights.Pick(rand));
} }
@@ -187,7 +187,7 @@ public abstract class SharedSalvageSystem : EntitySystem
} }
/// <summary> /// <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> /// </summary>
private string[] RewardsForDifficulty(DifficultyRating rating) private string[] RewardsForDifficulty(DifficultyRating rating)
{ {

View File

@@ -75,7 +75,7 @@
- type: ApcPowerReceiver - type: ApcPowerReceiver
powerLoad: 1000 powerLoad: 1000
- type: weightedRandom - type: weightedRandomEntity
id: RandomAsteroidPool id: RandomAsteroidPool
weights: weights:
AsteroidSalvageSmall: 3 AsteroidSalvageSmall: 3

View File

@@ -1,4 +1,4 @@
- type: weightedRandom - type: weightedRandomEntity
id: SalvageRewardCommon id: SalvageRewardCommon
weights: weights:
# basic materials # basic materials
@@ -20,7 +20,7 @@
SpaceCash500: 0.5 SpaceCash500: 0.5
SpaceCash1000: 0.25 SpaceCash1000: 0.25
- type: weightedRandom - type: weightedRandomEntity
id: SalvageRewardRare id: SalvageRewardRare
weights: weights:
# rare materials # rare materials
@@ -52,7 +52,7 @@
SpaceCash1000: 0.75 SpaceCash1000: 0.75
SpaceCash2500: 0.5 SpaceCash2500: 0.5
- type: weightedRandom - type: weightedRandomEntity
id: SalvageRewardEpic id: SalvageRewardEpic
weights: weights:
# rare machinery # rare machinery

View File

@@ -1,5 +1,5 @@
#default species weights used for randomly selected species #default species weights used for randomly selected species
- type: weightedRandom - type: weightedRandomSpecies
id: SpeciesWeights id: SpeciesWeights
weights: weights:
Human: 5 Human: 5

View File

@@ -42,7 +42,7 @@
minOreYield: 2 minOreYield: 2
maxOreYield: 4 maxOreYield: 4
- type: weightedRandom - type: weightedRandomOre
id: RandomOreDistributionStandard id: RandomOreDistributionStandard
weights: weights:
OreSteel: 10 OreSteel: 10