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 <pa.pecherskij@interfax.ru>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
Fildrance
2025-10-20 00:29:31 +03:00
committed by GitHub
parent 9c44c1707e
commit 22fe5185a3
10 changed files with 23 additions and 17 deletions

View File

@@ -185,6 +185,12 @@ namespace Content.Shared.Random.Helpers
throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
}
/// <inheritdoc cref="HashCodeCombine(IReadOnlyCollection{int})"/>
public static int HashCodeCombine(params int[] values)
{
return HashCodeCombine((IReadOnlyCollection<int>)values);
}
/// <summary>
/// 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
/// <example>
/// 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.
/// <code>
/// var seed = SharedRandomExtensions.HashCodeCombine(new() { (int)_timing.CurTick.Value, GetNetEntity(ent).Id });
/// var seed = SharedRandomExtensions.HashCodeCombine((int)_timing.CurTick.Value, GetNetEntity(ent).Id);
/// </code>
/// </example>
public static int HashCodeCombine(List<int> values)
public static int HashCodeCombine(IReadOnlyCollection<int> values)
{
int hash = 5381;
foreach (var value in values)