From cc8b642444bf3de187f82de042912c072c388304 Mon Sep 17 00:00:00 2001 From: Vordenburg <114301317+Vordenburg@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:31:25 -0400 Subject: [PATCH] Add linter-friendly WeightedRandom prototypes (#18729) --- Content.Server/Mining/MiningSystem.cs | 4 ++-- .../Salvage/SalvageMagnetComponent.cs | 2 +- Content.Server/Salvage/SalvageSystem.cs | 2 +- .../Station/Systems/StationSpawningSystem.cs | 2 +- .../Mining/Components/OreVeinComponent.cs | 4 ++-- .../Random/Helpers/SharedRandomExtensions.cs | 6 +++--- Content.Shared/Random/IWeightedRandom.cs | 13 +++++++++++++ .../Random/WeightedRandomEntityPrototype.cs | 17 +++++++++++++++++ .../Random/WeightedRandomOrePrototype.cs | 18 ++++++++++++++++++ .../Random/WeightedRandomPrototype.cs | 7 ++++--- .../Random/WeightedRandomSpeciesPrototype.cs | 18 ++++++++++++++++++ Content.Shared/Salvage/SharedSalvageSystem.cs | 4 ++-- .../Entities/Structures/Machines/salvage.yml | 2 +- .../Prototypes/Procedural/salvage_rewards.yml | 6 +++--- .../Prototypes/Species/species_weights.yml | 2 +- Resources/Prototypes/ore.yml | 2 +- 16 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 Content.Shared/Random/IWeightedRandom.cs create mode 100644 Content.Shared/Random/WeightedRandomEntityPrototype.cs create mode 100644 Content.Shared/Random/WeightedRandomOrePrototype.cs create mode 100644 Content.Shared/Random/WeightedRandomSpeciesPrototype.cs diff --git a/Content.Server/Mining/MiningSystem.cs b/Content.Server/Mining/MiningSystem.cs index edf23a50e8..a673cda946 100644 --- a/Content.Server/Mining/MiningSystem.cs +++ b/Content.Server/Mining/MiningSystem.cs @@ -1,4 +1,4 @@ -using Content.Server.Mining.Components; +using Content.Server.Mining.Components; using Content.Shared.Destructible; using Content.Shared.Mining; using Content.Shared.Random; @@ -47,6 +47,6 @@ public sealed class MiningSystem : EntitySystem if (component.CurrentOre != null || component.OreRarityPrototypeId == null || !_random.Prob(component.OreChance)) return; - component.CurrentOre = _proto.Index(component.OreRarityPrototypeId).Pick(_random); + component.CurrentOre = _proto.Index(component.OreRarityPrototypeId).Pick(_random); } } diff --git a/Content.Server/Salvage/SalvageMagnetComponent.cs b/Content.Server/Salvage/SalvageMagnetComponent.cs index 38c993d404..d99e52fc7f 100644 --- a/Content.Server/Salvage/SalvageMagnetComponent.cs +++ b/Content.Server/Salvage/SalvageMagnetComponent.cs @@ -108,7 +108,7 @@ namespace Content.Server.Salvage /// A weighted random prototype corresponding to /// what asteroid entities will be generated. /// - [DataField("asteroidPool", customTypeSerializer: typeof(PrototypeIdSerializer)), ViewVariables(VVAccess.ReadWrite)] + [DataField("asteroidPool", customTypeSerializer: typeof(PrototypeIdSerializer)), ViewVariables(VVAccess.ReadWrite)] public string AsteroidPool = "RandomAsteroidPool"; } diff --git a/Content.Server/Salvage/SalvageSystem.cs b/Content.Server/Salvage/SalvageSystem.cs index 678a36c2c5..ece652ad07 100644 --- a/Content.Server/Salvage/SalvageSystem.cs +++ b/Content.Server/Salvage/SalvageSystem.cs @@ -347,7 +347,7 @@ namespace Content.Server.Salvage EntityUid? salvageEnt; if (_random.Prob(component.AsteroidChance)) { - var asteroidProto = _prototypeManager.Index(component.AsteroidPool).Pick(_random); + var asteroidProto = _prototypeManager.Index(component.AsteroidPool).Pick(_random); salvageEnt = Spawn(asteroidProto, new MapCoordinates(0, 0, salvMap)); } else diff --git a/Content.Server/Station/Systems/StationSpawningSystem.cs b/Content.Server/Station/Systems/StationSpawningSystem.cs index 8885b5b786..fa54778b26 100644 --- a/Content.Server/Station/Systems/StationSpawningSystem.cs +++ b/Content.Server/Station/Systems/StationSpawningSystem.cs @@ -115,7 +115,7 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem if (_randomizeCharacters) { var weightId = _configurationManager.GetCVar(CCVars.ICRandomSpeciesWeights); - var weights = _prototypeManager.Index(weightId); + var weights = _prototypeManager.Index(weightId); speciesId = weights.Pick(_random); } else if (profile != null) diff --git a/Content.Shared/Mining/Components/OreVeinComponent.cs b/Content.Shared/Mining/Components/OreVeinComponent.cs index 8acf18f20c..e4316a9d9f 100644 --- a/Content.Shared/Mining/Components/OreVeinComponent.cs +++ b/Content.Shared/Mining/Components/OreVeinComponent.cs @@ -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 /// /// The weighted random prototype used for determining what ore will be dropped. /// - [DataField("oreRarityPrototypeId", customTypeSerializer: typeof(PrototypeIdSerializer))] + [DataField("oreRarityPrototypeId", customTypeSerializer: typeof(PrototypeIdSerializer))] public string? OreRarityPrototypeId; /// diff --git a/Content.Shared/Random/Helpers/SharedRandomExtensions.cs b/Content.Shared/Random/Helpers/SharedRandomExtensions.cs index d68fac2509..dcdc9d61d9 100644 --- a/Content.Shared/Random/Helpers/SharedRandomExtensions.cs +++ b/Content.Shared/Random/Helpers/SharedRandomExtensions.cs @@ -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; diff --git a/Content.Shared/Random/IWeightedRandom.cs b/Content.Shared/Random/IWeightedRandom.cs new file mode 100644 index 0000000000..56be7db6aa --- /dev/null +++ b/Content.Shared/Random/IWeightedRandom.cs @@ -0,0 +1,13 @@ +using Robust.Shared.Prototypes; + +namespace Content.Shared.Random; + +/// +/// IWeightedRandomPrototype implements a dictionary of strings to float weights +/// to be used with . +/// +public interface IWeightedRandomPrototype : IPrototype +{ + [ViewVariables] + public Dictionary Weights { get; } +} diff --git a/Content.Shared/Random/WeightedRandomEntityPrototype.cs b/Content.Shared/Random/WeightedRandomEntityPrototype.cs new file mode 100644 index 0000000000..c0cf8f1165 --- /dev/null +++ b/Content.Shared/Random/WeightedRandomEntityPrototype.cs @@ -0,0 +1,17 @@ +using Robust.Shared.Prototypes; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary; + +namespace Content.Shared.Random; + +/// +/// Linter-friendly version of weightedRandom for Entity prototypes. +/// +[Prototype("weightedRandomEntity")] +public sealed class WeightedRandomEntityPrototype : IWeightedRandomPrototype +{ + [IdDataField] + public string ID { get; } = default!; + + [DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer))] + public Dictionary Weights { get; } = new(); +} diff --git a/Content.Shared/Random/WeightedRandomOrePrototype.cs b/Content.Shared/Random/WeightedRandomOrePrototype.cs new file mode 100644 index 0000000000..bc1f52ce2a --- /dev/null +++ b/Content.Shared/Random/WeightedRandomOrePrototype.cs @@ -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; + +/// +/// Linter-friendly version of weightedRandom for Ore prototypes. +/// +[Prototype("weightedRandomOre")] +public sealed class WeightedRandomOrePrototype : IWeightedRandomPrototype +{ + [IdDataField] + public string ID { get; } = default!; + + [DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer))] + public Dictionary Weights { get; } = new(); +} diff --git a/Content.Shared/Random/WeightedRandomPrototype.cs b/Content.Shared/Random/WeightedRandomPrototype.cs index ef2aa5b664..7c44dbdf77 100644 --- a/Content.Shared/Random/WeightedRandomPrototype.cs +++ b/Content.Shared/Random/WeightedRandomPrototype.cs @@ -6,10 +6,11 @@ namespace Content.Shared.Random; /// Generic random weighting dataset to use. /// [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 Weights = new(); + public Dictionary Weights { get; } = new(); } diff --git a/Content.Shared/Random/WeightedRandomSpeciesPrototype.cs b/Content.Shared/Random/WeightedRandomSpeciesPrototype.cs new file mode 100644 index 0000000000..2b867eed98 --- /dev/null +++ b/Content.Shared/Random/WeightedRandomSpeciesPrototype.cs @@ -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; + +/// +/// Linter-friendly version of weightedRandom for Species prototypes. +/// +[Prototype("weightedRandomSpecies")] +public sealed class WeightedRandomSpeciesPrototype : IWeightedRandomPrototype +{ + [IdDataField] + public string ID { get; } = default!; + + [DataField("weights", customTypeSerializer: typeof(PrototypeIdDictionarySerializer))] + public Dictionary Weights { get; } = new(); +} diff --git a/Content.Shared/Salvage/SharedSalvageSystem.cs b/Content.Shared/Salvage/SharedSalvageSystem.cs index 5aa0fc4e61..5c1bf20f21 100644 --- a/Content.Shared/Salvage/SharedSalvageSystem.cs +++ b/Content.Shared/Salvage/SharedSalvageSystem.cs @@ -179,7 +179,7 @@ public abstract class SharedSalvageSystem : EntitySystem foreach (var id in ids) { // pick a random reward to give - var weights = _proto.Index(id); + var weights = _proto.Index(id); rewards.Add(weights.Pick(rand)); } @@ -187,7 +187,7 @@ public abstract class SharedSalvageSystem : EntitySystem } /// - /// 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. /// private string[] RewardsForDifficulty(DifficultyRating rating) { diff --git a/Resources/Prototypes/Entities/Structures/Machines/salvage.yml b/Resources/Prototypes/Entities/Structures/Machines/salvage.yml index e029a27051..1673f980ff 100644 --- a/Resources/Prototypes/Entities/Structures/Machines/salvage.yml +++ b/Resources/Prototypes/Entities/Structures/Machines/salvage.yml @@ -75,7 +75,7 @@ - type: ApcPowerReceiver powerLoad: 1000 -- type: weightedRandom +- type: weightedRandomEntity id: RandomAsteroidPool weights: AsteroidSalvageSmall: 3 diff --git a/Resources/Prototypes/Procedural/salvage_rewards.yml b/Resources/Prototypes/Procedural/salvage_rewards.yml index 92a393a6a7..763ae8bd4e 100644 --- a/Resources/Prototypes/Procedural/salvage_rewards.yml +++ b/Resources/Prototypes/Procedural/salvage_rewards.yml @@ -1,4 +1,4 @@ -- type: weightedRandom +- type: weightedRandomEntity id: SalvageRewardCommon weights: # basic materials @@ -20,7 +20,7 @@ SpaceCash500: 0.5 SpaceCash1000: 0.25 -- type: weightedRandom +- type: weightedRandomEntity id: SalvageRewardRare weights: # rare materials @@ -52,7 +52,7 @@ SpaceCash1000: 0.75 SpaceCash2500: 0.5 -- type: weightedRandom +- type: weightedRandomEntity id: SalvageRewardEpic weights: # rare machinery diff --git a/Resources/Prototypes/Species/species_weights.yml b/Resources/Prototypes/Species/species_weights.yml index 2e82a0b9db..63c196b95d 100644 --- a/Resources/Prototypes/Species/species_weights.yml +++ b/Resources/Prototypes/Species/species_weights.yml @@ -1,5 +1,5 @@ #default species weights used for randomly selected species -- type: weightedRandom +- type: weightedRandomSpecies id: SpeciesWeights weights: Human: 5 diff --git a/Resources/Prototypes/ore.yml b/Resources/Prototypes/ore.yml index b17e0839a2..67c3d0c5e2 100644 --- a/Resources/Prototypes/ore.yml +++ b/Resources/Prototypes/ore.yml @@ -42,7 +42,7 @@ minOreYield: 2 maxOreYield: 4 -- type: weightedRandom +- type: weightedRandomOre id: RandomOreDistributionStandard weights: OreSteel: 10