From 22fe5185a3c121f50c30afd470d55d969f7f838c Mon Sep 17 00:00:00 2001 From: Fildrance Date: Mon, 20 Oct 2025 00:29:31 +0300 Subject: [PATCH] refactor: new overload for SharedRandomExtensions.HashCodeCombine (#40990) * refactor: new overload for SharedRandomExtensions.HashCodeCombine * Update Content.Shared/Random/Helpers/SharedRandomExtensions.cs --------- Co-authored-by: pa.pecherskij Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> --- Content.Shared/Body/Systems/SharedBloodstreamSystem.cs | 2 +- Content.Shared/Clumsy/ClumsySystem.cs | 10 +++++----- .../EntityEffects/SharedEntityEffectsSystem.cs | 4 ++-- Content.Shared/Flash/SharedFlashSystem.cs | 2 +- Content.Shared/Kitchen/SharedKitchenSpikeSystem.cs | 2 +- .../EntitySystems/IngestionSystem.Utensils.cs | 4 ++-- .../Nutrition/EntitySystems/MessyDrinkerSystem.cs | 2 +- .../Random/Helpers/SharedRandomExtensions.cs | 10 ++++++++-- Content.Shared/Throwing/CatchableSystem.cs | 2 +- Content.Shared/Traits/Assorted/NarcolepsySystem.cs | 2 +- 10 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Content.Shared/Body/Systems/SharedBloodstreamSystem.cs b/Content.Shared/Body/Systems/SharedBloodstreamSystem.cs index ee85fe65b7..693eede7d8 100644 --- a/Content.Shared/Body/Systems/SharedBloodstreamSystem.cs +++ b/Content.Shared/Body/Systems/SharedBloodstreamSystem.cs @@ -217,7 +217,7 @@ public abstract class SharedBloodstreamSystem : EntitySystem // TODO: Replace with RandomPredicted once the engine PR is merged // Use both the receiver and the damage causing entity for the seed so that we have different results for multiple attacks in the same tick - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id, GetNetEntity(args.Origin)?.Id ?? 0 }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id, GetNetEntity(args.Origin)?.Id ?? 0 ); var rand = new System.Random(seed); var prob = Math.Clamp(totalFloat / 25, 0, 1); if (totalFloat > 0 && rand.Prob(prob)) diff --git a/Content.Shared/Clumsy/ClumsySystem.cs b/Content.Shared/Clumsy/ClumsySystem.cs index 2506359c25..d7b4019eb8 100644 --- a/Content.Shared/Clumsy/ClumsySystem.cs +++ b/Content.Shared/Clumsy/ClumsySystem.cs @@ -49,7 +49,7 @@ public sealed class ClumsySystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.ClumsyDefaultCheck)) return; @@ -68,7 +68,7 @@ public sealed class ClumsySystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.ClumsyDefaultCheck)) return; @@ -87,7 +87,7 @@ public sealed class ClumsySystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(args.Item).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(args.Item).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.ClumsyDefaultCheck)) return; @@ -121,7 +121,7 @@ public sealed class ClumsySystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(args.Gun).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(args.Gun).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.ClumsyDefaultCheck)) return; @@ -146,7 +146,7 @@ public sealed class ClumsySystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); if (!_cfg.GetCVar(CCVars.GameTableBonk) && !rand.Prob(ent.Comp.ClumsyDefaultCheck)) return; diff --git a/Content.Shared/EntityEffects/SharedEntityEffectsSystem.cs b/Content.Shared/EntityEffects/SharedEntityEffectsSystem.cs index 1122f75f93..91360b3a84 100644 --- a/Content.Shared/EntityEffects/SharedEntityEffectsSystem.cs +++ b/Content.Shared/EntityEffects/SharedEntityEffectsSystem.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using Content.Shared.Administration.Logs; using Content.Shared.Chemistry; using Content.Shared.Chemistry.Reaction; @@ -91,7 +91,7 @@ public sealed partial class SharedEntityEffectsSystem : EntitySystem, IEntityEff // TODO: Replace with proper random prediciton when it exists. if (effect.Probability <= 1f) { - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(target).Id, 0 }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(target).Id, 0); var rand = new System.Random(seed); if (!rand.Prob(effect.Probability)) return false; diff --git a/Content.Shared/Flash/SharedFlashSystem.cs b/Content.Shared/Flash/SharedFlashSystem.cs index a762a94e58..dad8e6d156 100644 --- a/Content.Shared/Flash/SharedFlashSystem.cs +++ b/Content.Shared/Flash/SharedFlashSystem.cs @@ -206,7 +206,7 @@ public abstract class SharedFlashSystem : EntitySystem foreach (var entity in _entSet) { // TODO: Use RandomPredicted https://github.com/space-wizards/RobustToolbox/pull/5849 - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(entity).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(entity).Id); var rand = new System.Random(seed); if (!rand.Prob(probability)) continue; diff --git a/Content.Shared/Kitchen/SharedKitchenSpikeSystem.cs b/Content.Shared/Kitchen/SharedKitchenSpikeSystem.cs index bae411f356..530dcb437b 100644 --- a/Content.Shared/Kitchen/SharedKitchenSpikeSystem.cs +++ b/Content.Shared/Kitchen/SharedKitchenSpikeSystem.cs @@ -292,7 +292,7 @@ public sealed class SharedKitchenSpikeSystem : EntitySystem // Get a random entry to spawn. // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_gameTiming.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_gameTiming.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); var index = rand.Next(butcherable.SpawnedEntities.Count); diff --git a/Content.Shared/Nutrition/EntitySystems/IngestionSystem.Utensils.cs b/Content.Shared/Nutrition/EntitySystems/IngestionSystem.Utensils.cs index a4ef8422bb..f9cb03cb35 100644 --- a/Content.Shared/Nutrition/EntitySystems/IngestionSystem.Utensils.cs +++ b/Content.Shared/Nutrition/EntitySystems/IngestionSystem.Utensils.cs @@ -1,4 +1,4 @@ -using Content.Shared.Containers.ItemSlots; +using Content.Shared.Containers.ItemSlots; using Content.Shared.Hands.Components; using Content.Shared.Interaction; using Content.Shared.Nutrition.Components; @@ -66,7 +66,7 @@ public sealed partial class IngestionSystem return; // TODO: Once we have predicted randomness delete this for something sane... - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(entity).Id, GetNetEntity(userUid).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(entity).Id, GetNetEntity(userUid).Id); var rand = new System.Random(seed); if (!rand.Prob(entity.Comp.BreakChance)) diff --git a/Content.Shared/Nutrition/EntitySystems/MessyDrinkerSystem.cs b/Content.Shared/Nutrition/EntitySystems/MessyDrinkerSystem.cs index 7bfb7a5633..f672edaab4 100644 --- a/Content.Shared/Nutrition/EntitySystems/MessyDrinkerSystem.cs +++ b/Content.Shared/Nutrition/EntitySystems/MessyDrinkerSystem.cs @@ -38,7 +38,7 @@ public sealed class MessyDrinkerSystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.SpillChance)) return; diff --git a/Content.Shared/Random/Helpers/SharedRandomExtensions.cs b/Content.Shared/Random/Helpers/SharedRandomExtensions.cs index 87e839b56f..ceb4786ebe 100644 --- a/Content.Shared/Random/Helpers/SharedRandomExtensions.cs +++ b/Content.Shared/Random/Helpers/SharedRandomExtensions.cs @@ -185,6 +185,12 @@ namespace Content.Shared.Random.Helpers throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!"); } + /// + public static int HashCodeCombine(params int[] values) + { + return HashCodeCombine((IReadOnlyCollection)values); + } + /// /// A very simple, deterministic djb2 hash function for generating a combined seed for the random number generator. /// We can't use HashCode.Combine because that is initialized with a random value, creating different results on the server and client. @@ -192,10 +198,10 @@ namespace Content.Shared.Random.Helpers /// /// Combine the current game tick with a NetEntity Id in order to not get the same random result if this is called multiple times in the same tick. /// - /// var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + /// var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); /// /// - public static int HashCodeCombine(List values) + public static int HashCodeCombine(IReadOnlyCollection values) { int hash = 5381; foreach (var value in values) diff --git a/Content.Shared/Throwing/CatchableSystem.cs b/Content.Shared/Throwing/CatchableSystem.cs index 586397f58b..92ca7062b1 100644 --- a/Content.Shared/Throwing/CatchableSystem.cs +++ b/Content.Shared/Throwing/CatchableSystem.cs @@ -56,7 +56,7 @@ public sealed partial class CatchableSystem : EntitySystem return; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id); var rand = new System.Random(seed); if (!rand.Prob(ent.Comp.CatchChance)) return; diff --git a/Content.Shared/Traits/Assorted/NarcolepsySystem.cs b/Content.Shared/Traits/Assorted/NarcolepsySystem.cs index 7bce80c703..f59a1cf281 100644 --- a/Content.Shared/Traits/Assorted/NarcolepsySystem.cs +++ b/Content.Shared/Traits/Assorted/NarcolepsySystem.cs @@ -53,7 +53,7 @@ public sealed class NarcolepsySystem : EntitySystem continue; // TODO: Replace with RandomPredicted once the engine PR is merged - var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(uid).Id }); + var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(uid).Id); var rand = new System.Random(seed); var duration = narcolepsy.MinDurationOfIncident + (narcolepsy.MaxDurationOfIncident - narcolepsy.MinDurationOfIncident) * rand.NextDouble(); -- 2.51.2