Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Source/Client/Patches/Determinism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,30 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}

// FastTileFinder.ComputeQueryJob uses Interlocked.Increment to race-fill a 50-slot result array
// across parallel Unity Job batches. Thread scheduling differs between machines, so clients get
// different candidate tile sets. Force single-batch execution in MP so tiles are processed in
// tileId order, making the first 50 valid tiles consistent across all clients.
[HarmonyPatch(typeof(FastTileFinder), nameof(FastTileFinder.Query))]
static class FastTileFinderQueryDeterminismPatch
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var getIdealBatchCount = AccessTools.Method(typeof(UnityData), nameof(UnityData.GetIdealBatchCount));
var getBatchCount = AccessTools.Method(typeof(FastTileFinderQueryDeterminismPatch), nameof(GetBatchCount));

foreach (var instr in instructions)
{
if (instr.Calls(getIdealBatchCount))
yield return new CodeInstruction(OpCodes.Call, getBatchCount);
else
yield return instr;
}
}

static int GetBatchCount(int length) =>
Multiplayer.Client != null ? length : UnityData.GetIdealBatchCount(length);
}


}
Loading