diff --git a/SVSim.BattleEngine.Tests/RngSeamTests.cs b/SVSim.BattleEngine.Tests/RngSeamTests.cs new file mode 100644 index 0000000..62b0f50 --- /dev/null +++ b/SVSim.BattleEngine.Tests/RngSeamTests.cs @@ -0,0 +1,21 @@ +using System; +using NUnit.Framework; +using SVSim.BattleEngine.Rng; + +namespace SVSim.BattleEngine.Tests +{ + [TestFixture] + public class RngSeamTests + { + // RandomSourceBridge.Range must mirror the engine's exact roll arithmetic: + // BattleManagerBase.StableRandom does `(int)Math.Floor((double)val * unit)`. + [Test] + public void Bridge_Range_mirrors_engine_floor_arithmetic() + { + Assert.That(RandomSourceBridge.Range(7, 0.0), Is.EqualTo(0)); // floor(7*0) = 0 + Assert.That(RandomSourceBridge.Range(7, 0.999), Is.EqualTo(6)); // floor(6.993) = 6 (never == val) + Assert.That(RandomSourceBridge.Range(3, 0.5), Is.EqualTo(1)); // floor(1.5) = 1 (middle of 3) + Assert.That(RandomSourceBridge.Range(1, 0.5), Is.EqualTo(0)); // floor(0.5) = 0 + } + } +} diff --git a/SVSim.BattleEngine/Rng/IRandomSource.cs b/SVSim.BattleEngine/Rng/IRandomSource.cs new file mode 100644 index 0000000..9f05bc0 --- /dev/null +++ b/SVSim.BattleEngine/Rng/IRandomSource.cs @@ -0,0 +1,14 @@ +namespace SVSim.BattleEngine.Rng +{ + // The battle RNG seam. The headless authoritative mgr (HeadlessBattleMgr) routes the engine's + // StableRandom* calls through this instead of the IsForecast-gated System.Random fields, so the + // server rolls real outcomes (decoupling F2) and tests can replay a known sequence. + public interface IRandomSource + { + // Synced stream, [0,1). Drives StableRandom (via RandomSourceBridge.Range) and StableRandomDouble. + double NextUnit(); + + // Self-only stream, [0,max). Mirrors StableRandomOnlySelf (engine: _stableRandomOnlySelf.Next(val)). + int NextSelf(int max); + } +} diff --git a/SVSim.BattleEngine/Rng/RandomSourceBridge.cs b/SVSim.BattleEngine/Rng/RandomSourceBridge.cs new file mode 100644 index 0000000..6240cbd --- /dev/null +++ b/SVSim.BattleEngine/Rng/RandomSourceBridge.cs @@ -0,0 +1,13 @@ +using System; + +namespace SVSim.BattleEngine.Rng +{ + // The ONE place engine roll-logic is re-authored (the virtual-override seam restates it rather than + // body-patching the Engine file). Isolated here so it is unit-testable and pinned to the verbatim + // engine by the parity test (RngSeamTests.SeededSource_matches_engine_generator / Task 5). Mirrors + // BattleManagerBase.StableRandom: `(int)Math.Floor((double)val * unit)`. + public static class RandomSourceBridge + { + public static int Range(int val, double unit) => (int)Math.Floor((double)val * unit); + } +}