From 5dbe77ecba210806e6e75a8b299b54d3456a07b4 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Sat, 28 May 2022 23:03:21 +1200 Subject: [PATCH] Attempt to reduce audio helper resolves (#8493) --- Content.Shared/Audio/AudioHelpers.cs | 24 ++++++++++++++++++------ Content.Shared/Sound/SoundSpecifier.cs | 11 +++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Content.Shared/Audio/AudioHelpers.cs b/Content.Shared/Audio/AudioHelpers.cs index 9c32ea8c2d..f6fc4f9cfe 100644 --- a/Content.Shared/Audio/AudioHelpers.cs +++ b/Content.Shared/Audio/AudioHelpers.cs @@ -8,9 +8,19 @@ namespace Content.Shared.Audio /// /// Returns a random pitch. /// + [Obsolete("Use variant that takes in IRobustRandom instead.")] public static AudioParams WithVariation(float amplitude) { - var scale = (float)(IoCManager.Resolve().NextGaussian(1, amplitude)); + return WithVariation(amplitude, null); + } + + /// + /// Returns a random pitch. + /// + public static AudioParams WithVariation(float amplitude, IRobustRandom? rand) + { + IoCManager.Resolve(ref rand); + var scale = (float) rand.NextGaussian(1, amplitude); return AudioParams.Default.WithPitchScale(scale); } @@ -43,16 +53,18 @@ namespace Content.Shared.Audio /// Returns a pitch multiplier shifted by a random number of semitones within variation. /// /// Max number of semitones to shift in either direction. Values above 12 have no effect. - public static AudioParams WithSemitoneVariation(int variation) + public static AudioParams WithSemitoneVariation(int variation, IRobustRandom? rand) { + IoCManager.Resolve(ref rand); variation = Math.Clamp(variation, 0, 12); - return ShiftSemitone(IoCManager.Resolve().Next(-variation, variation)); + return ShiftSemitone(rand.Next(-variation, variation)); } - public static string GetRandomFileFromSoundCollection(string name) + public static string GetRandomFileFromSoundCollection(string name, IRobustRandom? rand, IPrototypeManager? proto) { - var soundCollection = IoCManager.Resolve().Index(name); - return IoCManager.Resolve().Pick(soundCollection.PickFiles).ToString(); + IoCManager.Resolve(ref rand, ref proto); + var soundCollection = proto.Index(name); + return rand.Pick(soundCollection.PickFiles).ToString(); } } } diff --git a/Content.Shared/Sound/SoundSpecifier.cs b/Content.Shared/Sound/SoundSpecifier.cs index f5962ecd03..adea9d2f57 100644 --- a/Content.Shared/Sound/SoundSpecifier.cs +++ b/Content.Shared/Sound/SoundSpecifier.cs @@ -1,6 +1,8 @@ using Content.Shared.Audio; using JetBrains.Annotations; using Robust.Shared.Audio; +using Robust.Shared.Prototypes; +using Robust.Shared.Random; using Robust.Shared.Serialization.TypeSerializers.Implementations; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Utility; @@ -13,7 +15,8 @@ namespace Content.Shared.Sound [ViewVariables(VVAccess.ReadWrite), DataField("params")] public AudioParams Params = AudioParams.Default; - public abstract string GetSound(); + // TODO: remove most uses of this function, and just make the audio-system take in a SoundSpecifier + public abstract string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null); } public sealed class SoundPathSpecifier : SoundSpecifier @@ -38,7 +41,7 @@ namespace Content.Shared.Sound Path = path; } - public override string GetSound() + public override string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null) { return Path == null ? string.Empty : Path.ToString(); } @@ -61,9 +64,9 @@ namespace Content.Shared.Sound Collection = collection; } - public override string GetSound() + public override string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null) { - return Collection == null ? string.Empty : AudioHelpers.GetRandomFileFromSoundCollection(Collection); + return Collection == null ? string.Empty : AudioHelpers.GetRandomFileFromSoundCollection(Collection, rand, proto); } } }