feat(world-gen): add WormHash deterministic mixing function

This commit is contained in:
2026-06-01 15:20:09 +02:00
parent 4587f81071
commit 013d524544
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
using VoxelEngine.World.WorldGen;
using Xunit;
namespace VoxelEngine.Tests.WorldGen;
public class WormHashTests
{
[Fact]
public void Hash_IsDeterministic_ForSameInputs()
{
uint a = WormHash.Hash(3, -2, 7, seed: 1337);
uint b = WormHash.Hash(3, -2, 7, seed: 1337);
Assert.Equal(a, b);
}
[Fact]
public void Hash_DiffersForDifferentCells()
{
uint a = WormHash.Hash(3, -2, 7, seed: 1337);
uint b = WormHash.Hash(3, -2, 8, seed: 1337);
Assert.NotEqual(a, b);
}
[Fact]
public void Hash_DiffersForDifferentSeeds()
{
uint a = WormHash.Hash(3, -2, 7, seed: 1337);
uint b = WormHash.Hash(3, -2, 7, seed: 9999);
Assert.NotEqual(a, b);
}
[Fact]
public void Hash_HandlesNegativeCoords()
{
// Just shouldn't throw and shouldn't collide trivially with the positive case.
uint a = WormHash.Hash(-1, -1, -1, seed: 1);
uint b = WormHash.Hash(1, 1, 1, seed: 1);
Assert.NotEqual(a, b);
}
}

View File

@@ -22,4 +22,8 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="VoxelEngine.Tests" />
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,21 @@
namespace VoxelEngine.World.WorldGen;
// Deterministic 32-bit mixing hash for worm-cell coordinates.
// Cell coords can be negative; cast through unchecked to preserve bit pattern.
internal static class WormHash
{
public static uint Hash(int cellX, int cellY, int cellZ, int seed)
{
unchecked
{
uint h = (uint)seed;
h ^= (uint)cellX * 73856093u;
h ^= (uint)cellY * 19349663u;
h ^= (uint)cellZ * 83492791u;
h ^= h >> 13;
h *= 0x5bd1e995u;
h ^= h >> 15;
return h;
}
}
}