2026-10-21 19:04:51 +02:00
/ * This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at https : //mozilla.org/MPL/2.0/. */
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Runtime.InteropServices ;
using System.Security.Cryptography ;
using System.Text ;
using System.Text.RegularExpressions ;
using CultMath ;
using UniRx ;
using Xunit ;
using static CultMath . math ;
using float2 = CultMath . float2 ;
// The determinism ledger (aetheria-arena, cut determinism-ledger): every seed a match reaches is stated, never the
// clock's. The item stream is seeded by whoever builds the ItemManager and by RunStart at each step of a run; the unpack
// scatter rides the zone's own dice (ruling run-seed-drops-only); a scenario has a stable Id; and one fight between two
// piloted ships is one history, pinned for the host class that records replays.
public sealed partial class RunStartTests
{
// The two-AI fight's digest, captured on Yggdrasil. Replays are trusted only on the host class that recorded them
// (ruling replays-per-host-class), and arenas run on Linux.
2026-10-22 18:26:03 +02:00
// Repinned twice, and the second repin has two causes (Soul measured the steps: group 1 + seed = f19b9992, + the Follow
// gate = ce81a426; group 2 without the seed = bf586806).
// First for the side split: the two fighters are unaffiliated, were one side and each followed itself (1b2ffe51).
// Then for staged knowledge (ruling repin-for-staged-knowledge), which moved it twice: the fighters are staged knowing
// each other, so they engage at step 0 (f19b9992); and Follow now flies only on an anchor the pilot itself tracks, so
// once the sensors drop the staged anchor the follower stops following it (ce81a426).
2026-10-22 21:28:09 +02:00
// Repinned a third time, once, for two causes (sim-rate cut): the rules went step-exact (timers keep their overshoot,
// passive sensing decays exactly), which alone gives 4f4a1c8b88308d0a368527549f33baf0e157a056b4c0fd33aeb110b027b862e1
2026-10-23 00:43:55 +02:00
// at 60 Hz; and the game step is now SimClock.GameStep (30 Hz), 1800 steps of the same 60 simulated seconds: d5e16969d5366349f2cddeec53f7e8e8c69074dcf5aa7aabb365e9a91a34511f.
// Repinned a fourth time, once (rounds-per-step): a round is born at its due moment inside its step, so its FireTime, origin
// and frozen target pose are carried back by its age (a pull's first round leaves at the step's start, not its end) and
// its commit and arrival times follow. Rebasing onto master moved nothing (d5e16969 measured at the rebased h1 head).
2026-10-23 02:02:54 +02:00
// Repinned a fifth time, once (h3): the shot queue is kept in fire-time order and resolved oldest first, ties by shot id, so within a step a
// later shooter's earlier round resolves before an earlier shooter's later one (19710609 before). The trigger being the weapon's own state moved nothing
// (measured alone: 19710609).
2026-10-23 06:22:01 +02:00
// Repinned a sixth time, once (h5): a step commits its due shots in fire-time order, then applies the arrived ones in arrival
// order, ties by shot id, so every commit of a step precedes its applies (it was commit then apply, shot by shot, in fire
// order). The two parts measured: commit-then-apply split alone a1318ee0, with the arrival sort 55ecd858; the old walk at the
// h5 head, rebased onto master, still measures 108c93dc, so the rebase and the fire-mode property moved nothing.
2026-10-23 15:31:43 +02:00
// Repinned a seventh time, once (h6), for two Combat changes, each of which moves the digest alone (Soul measured them against
// 55ecd858, the digest before): a held AutoWeapon stays a candidate between its rounds (the sweep disabled: b559789a), and Combat lets
// go of a held weapon it no longer commands (target gone, untracked, another group the pick, Combat ended), where a held gun fired on
// until a later step selected its group (the candidate change reverted: d9493bbe). Both together: the digest below. Releasing a beam or a
// lock the same way (h7) moved nothing.
2026-11-11 05:20:46 +00:00
// Repinned an eighth time, once (suite-honest), for the flight-model changes that landed after h6 and never repinned this
// entry (bisected on master, each intended): e0c1daa4e made ThrustScale default 0.1, 845f6c3aa removed drag below top speed,
// ffdba39ac made ThrustScale scale the push only, and 8c5a904cb pinned the fixtures to ThrustScale=1. Pull requests 8 and 9
// moved the digest and returned it to 52fbb10d, which master already measured before this cut. The authored-cargo and
// ammunition-data blob of this cut moved nothing (52fbb10d measured with it merged in). Fixtures run ThrustScale=1 while play
// uses 0.1, so this digest pins the fight's logic, not the shipped feel.
2026-11-11 12:43:37 +00:00
// Repinned a ninth time, once (scenarios-batch4-minors): the catalog restores the five station-reactor products' quality
// spread (station-reactor-spread), and that blob alone moved the digest (the same tree with the old catalog measured 52fbb10d;
2026-11-11 13:10:13 +00:00
// traced: only Notorious Station moves it, because the zone's station Zenith carries a Notorious Station reactor lot whose
// minted quality changes with the role spread, and Snapshot hashes every zone entity's heat and shield).
2026-11-27 18:27:52 +01:00
// Repinned a tenth time (hardpoint-fit-1): the Zenith's (11,6) radiator mount was rotation None and is now Clockwise, so the
// zone's generated station Zenith can fit a radiator there and Snapshot hashes its heat. windows-x64 133e5d76 -> e4a3d6e5,
// linux-x64 9c70a3ee -> 6b8b31ad (measured on Yggdrasil). The fight itself stages hand-fitted Longinus ships and a Bare Djinni,
// so the shields now on sale do not move it: the same catalog with only the Zenith rotation put back to None measures the old digest.
2026-11-28 01:24:36 +01:00
// Station packages: the Arsenal Depot (a station hull in the zone the fight runs in) moved both digests; linux-x64 6b8b31ad -> 99e358a0
// measured on Yggdrasil, windows-x64 e4a3d6e5 -> 5201ca08 measured on Starfire.
2026-11-28 05:50:15 +01:00
// Station facility fit (cut station-facility-fit): the zone's station Zenith seats its kind's facilities before its docking bay and heater,
// which moves its gear; linux-x64 99e358a0 -> d80cc78c (Yggdrasil), windows-x64 5201ca08 -> 77e8a2f3 (Starfire).
2026-11-28 16:57:01 +01:00
// Hull conductivity from construction (Entity.MapEntity): the fight's hand-fitted ships and the zone's stations now conduct heat, which Snapshot
// hashes; linux-x64 d80cc78c -> 42729ca4 (Yggdrasil), windows-x64 77e8a2f3 -> e0f230c3 (Starfire).
2026-11-28 20:02:26 +01:00
// Facility output room (cut facility-output-room): a Facility row is no free cell for general gear (Entity.UnoccupiedSpace and ItemFits), so Zenith
// draws its docking bay, heater and cargo bay from a smaller free space; with the conduction change in, linux-x64 42729ca4 -> 57303576 (Yggdrasil),
// windows-x64 e0f230c3 -> 344fec28 (Starfire).
2026-11-28 23:49:24 +01:00
// Station deck (cut region-chain-deck): the zone's station count is dealt, not drawn in its stream, so every later draw of the zone Zenith stands in
// moves; linux-x64 57303576 -> 1d1f9901 (Yggdrasil), windows-x64 344fec28 -> 5afdd8f9 (Starfire).
2026-11-29 05:49:48 +01:00
// Crew Quarters (cut habitation-gear): every station hull gains a 2x2 Facility row and seats a Crew Quarters on it beside its facilities,
// which makes one more lot; Zenith's second row takes interior cells its gear drew from; linux-x64 1d1f9901 -> 012efbf8 (Yggdrasil),
// windows-x64 5afdd8f9 -> 7ccb5487 (Starfire).
2026-12-03 05:04:46 +01:00
// Station role bays (cut station-role-bays): a station's interior cells go to role bays and a stock share, so the zone station Zenith's gear and'
// every draw after it move; linux-x64 012efbf8 -> 410bcb86 (Yggdrasil, ygg-verify), windows-x64 7ccb5487 -> 67ba8ecf (Starfire).
2026-11-26 03:23:01 +01:00
private static readonly System . Collections . Generic . Dictionary < string , string > TwoAiPinnedDigests = new ( )
{
2026-12-03 05:04:46 +01:00
["linux-x64"] = "410bcb86959be2c612290a2dfb9a538d8f4d4157c2721c3fced6e49a97f703c6" ,
2026-11-26 03:23:01 +01:00
// Measured on windows-x64 at the commit that added this pin (the fight is one history there: TwoAiFightIsOneHistory).
2026-12-03 05:04:46 +01:00
["windows-x64"] = "67ba8ecfbb08c2a11872ba9303efa01627f95a0828a19f4961dc39fbbb0dcae8" ,
2026-11-26 03:23:01 +01:00
} ;
2026-10-22 20:51:59 +02:00
private const float TwoAiSeconds = 60f ;
2026-10-21 19:04:51 +02:00
[Fact]
public void ItemManagerRefusesSeedZero ( )
{
var refused = Assert . Throws < ArgumentOutOfRangeException > ( ( ) = >
new ItemManager ( 0 u , _cache , new ProvenanceLedger ( ) , _items . GameplaySettings , _ = > { } ) ) ;
Assert . Equal ( "seed" , refused . ParamName ) ;
var seeded = new ItemManager ( 0xBEEF u , _cache , new ProvenanceLedger ( ) , _items . GameplaySettings , _ = > { } ) ;
Assert . Equal ( new CultMath . Random ( 0xBEEF u ) . state , seeded . Random . state ) ;
}
[Fact]
public void ItemSeedIsTheRunSeedAndStep ( )
{
var galaxy = Launch ( new Scripted ( false , stage = > stage . Player ( stage . Bare ( "Djinni" ) , float2 ( 0 , 0 ) ) , seed : 4242 ) ) . galaxy ;
Assert . Equal ( 4242 u , galaxy . Seed ) ;
2026-10-21 19:06:49 +02:00
Assert . Equal ( unchecked ( 4242 u * 0x9E3779B1 u + 1 ) , RunStart . ItemSeed ( galaxy , RunStart . ArenaStep ) ) ;
Assert . Equal ( unchecked ( 4242 u * 0x9E3779B1 u + 2 ) , RunStart . ItemSeed ( galaxy , RunStart . StageStep ) ) ;
2026-10-21 19:04:51 +02:00
var arena = RunStart . ItemSeed ( galaxy , RunStart . ArenaStep ) ;
var stage = RunStart . ItemSeed ( galaxy , RunStart . StageStep ) ;
var resumed = RunStart . ItemSeed ( galaxy , RunStart . ContinueStep ) ;
Assert . NotEqual ( arena , resumed ) ;
Assert . NotEqual ( stage , resumed ) ;
Assert . NotEqual ( 0 u , RunStart . NoRunSeed ) ;
// The stream a new run's arena generates on is the one ItemSeed names.
var items = new ItemManager ( 7 u , _cache , new ProvenanceLedger ( ) , _items . GameplaySettings , _ = > { } ) ;
RunStart . GenerateArena ( items , _zoneSettings , _planetSettings , galaxy , new Scripted ( false , _ = > { } , seed : 4242 ) ) ;
var expected = new ItemManager ( arena , _cache , new ProvenanceLedger ( ) , _items . GameplaySettings , _ = > { } ) ;
RunStart . GenerateArena ( expected , _zoneSettings , _planetSettings , galaxy , new Scripted ( false , _ = > { } , seed : 4242 ) ) ;
Assert . Equal ( expected . Random . state , items . Random . state ) ;
}
2026-10-21 23:38:03 +02:00
private const int OriginDrifters = 3 ;
// The galaxy seed whose ContinueStep item seed wraps to zero: -ContinueStep times the inverse of 0x9E3779B1, mod 2^32.
private const uint ZeroAtContinueSeed = 0xD45E720D u ;
[Fact]
public void ItemSeedRefusesAZeroSeed ( )
{
var galaxy = Launch ( new Scripted ( false , stage = > stage . Player ( stage . Bare ( "Djinni" ) , float2 ( 0 , 0 ) ) ,
seed : ZeroAtContinueSeed ) ) . galaxy ;
Assert . Equal ( ZeroAtContinueSeed , galaxy . Seed ) ;
Assert . NotEqual ( 0 u , RunStart . ItemSeed ( galaxy , RunStart . ArenaStep ) ) ;
var refused = Assert . Throws < InvalidOperationException > ( ( ) = > RunStart . ItemSeed ( galaxy , RunStart . ContinueStep ) ) ;
Assert . Contains ( "zero" , refused . Message ) ;
}
// A pack holding three unpiloted ships at the origin besides the player, from a staged arena. More than one, so
// each ship's own ordinal is visible: ships sharing a draw would stack on one point.
2026-10-21 19:04:51 +02:00
private ( ZonePack pack , Galaxy galaxy ) OriginPack ( )
{
2026-10-21 23:38:03 +02:00
var drifters = new List < Ship > ( ) ;
2026-10-21 19:04:51 +02:00
var ( galaxy , arena , staged , failures ) = Launch ( new Scripted ( false , stage = >
{
stage . Player ( stage . Bare ( "Djinni" ) , float2 ( - 50000 , - 50000 ) ) ;
2026-10-21 23:38:03 +02:00
for ( var i = 0 ; i < OriginDrifters ; i + + )
drifters . Add ( stage . Place ( stage . Bare ( "Longinus" ) , float2 ( 0 , 0 ) ) as Ship ) ;
2026-10-21 19:04:51 +02:00
} ) ) ;
Assert . True ( failures . Count = = 0 , string . Join ( "; " , failures ) ) ;
2026-10-21 23:38:03 +02:00
Assert . All ( drifters , drifter = >
{
Assert . NotNull ( drifter ) ;
Assert . False ( drifter . IsPlayerShip ) ;
Assert . True ( lengthsq ( drifter . Position ) < 1 , "the fixture's drifters are packed at the origin" ) ;
} ) ;
2026-10-21 19:04:51 +02:00
return ( arena . PackZone ( ) , galaxy ) ;
}
private List < CultMath . float3 > Scattered ( ZonePack pack , GalaxyZone galaxyZone , Galaxy galaxy )
{
var zone = new Zone ( _items , _planetSettings , pack , galaxyZone , galaxy ) ;
return zone . Entities . OfType < Ship > ( ) . Where ( ship = > ! ship . IsPlayerShip ) . Select ( ship = > ship . Position ) . ToList ( ) ;
}
[Fact]
public void ScatterIgnoresTheItemStream ( )
{
var ( pack , galaxy ) = OriginPack ( ) ;
_items . Random = new CultMath . Random ( 11 ) ;
var first = Scattered ( pack , galaxy . Entrance , galaxy ) ;
_items . Random = new CultMath . Random ( 12 ) ;
for ( var i = 0 ; i < 37 ; i + + ) _items . Random . NextUInt ( ) ;
var second = Scattered ( pack , galaxy . Entrance , galaxy ) ;
Assert . NotEmpty ( first ) ;
Assert . All ( first , position = > Assert . True ( lengthsq ( position ) > = 1 , "the drifter was scattered" ) ) ;
Assert . Equal ( first , second ) ;
}
2026-10-21 23:38:03 +02:00
// Each origin ship is placed by its own draw: the 1-based ordinal of its entry in the pack, on the zone's
// ScatterStream, within half the zone's radius of the centre on every axis.
[Fact]
public void ScatterDrawsEachShipsOrdinalWithinHalfTheRadius ( )
{
var ( pack , galaxy ) = OriginPack ( ) ;
var zone = new Zone ( _items , _planetSettings , pack , galaxy . Entrance , galaxy ) ;
Assert . Equal ( pack . Entities . Count , zone . Entities . Count ) ;
var half = pack . Radius * . 5f ;
var expected = new List < CultMath . float3 > ( ) ;
var actual = new List < CultMath . float3 > ( ) ;
for ( var i = 0 ; i < zone . Entities . Count ; i + + )
{
if ( zone . Entities [ i ] is not Ship { IsPlayerShip : false } ship ) continue ;
expected . Add ( SimulationDice . For ( zone . CombatSeed , SimulationDice . ScatterStream , ( uint ) i + 1 )
. NextFloat3 ( float3 ( - half ) , float3 ( half ) ) ) ;
actual . Add ( ship . Position ) ;
}
Assert . Equal ( OriginDrifters , actual . Count ) ;
Assert . Equal ( expected , actual ) ;
Assert . Equal ( OriginDrifters , actual . Distinct ( ) . Count ( ) ) ;
Assert . All ( actual , position = > Assert . True ( all ( abs ( position ) < = half ) , $"{position} is within half the radius {half}" ) ) ;
}
2026-10-21 19:04:51 +02:00
[Fact]
public void ScatterIsPerZone ( )
{
var ( pack , galaxy ) = OriginPack ( ) ;
var other = galaxy . Zones . First ( zone = > zone ! = galaxy . Entrance & & zone . Name ! = galaxy . Entrance . Name ) ;
var here = Scattered ( pack , galaxy . Entrance , galaxy ) ;
var there = Scattered ( pack , other , galaxy ) ;
Assert . NotEmpty ( here ) ;
Assert . Equal ( here . Count , there . Count ) ;
Assert . NotEqual ( here , there ) ;
}
[Fact]
public void SimulationDiceStreamsAreDistinct ( )
{
var streams = new [ ]
{
SimulationDice . LootStream , SimulationDice . MineStream , SimulationDice . CombatSampleStream ,
SimulationDice . PatrolStream , SimulationDice . ScatterStream
} ;
Assert . Equal ( streams . Length , streams . Distinct ( ) . Count ( ) ) ;
}
[Fact]
public void ScenarioIdsAreUniqueKebabAndStable ( )
{
var kebab = new Regex ( "^[a-z0-9]+(-[a-z0-9]+)*$" ) ;
var every = EveryScenario . ToList ( ) ;
Assert . All ( every , scenario = > Assert . True ( scenario . Id ! = null & & kebab . IsMatch ( scenario . Id ) ,
$"{scenario.GetType().Name}: Id is not lowercase kebab" ) ) ;
Assert . Equal ( every . Count , every . Select ( scenario = > scenario . Id ) . Distinct ( ) . Count ( ) ) ;
Assert . Equal ( "duel" , new Duel ( ) . Id ) ;
Assert . Equal ( "demo-terminus" , new DemoTerminus ( ) . Id ) ;
}
// Two piloted fighters about 900 apart, each approaching the other, hostile both ways; the player bare, unpiloted
2026-10-22 20:51:59 +02:00
// and far from the fight. Steps TwoAiSeconds at `dt` (the game's step by default) and returns the SHA-256 of the joined
// snapshots, the hull each fighter ends with and the sim time a hull first reaches zero (TwoAiSeconds when none does).
2026-10-22 21:05:13 +02:00
private ( string digest , bool bothPiloted , int hits , bool hullLost , float eastHull , float westHull , float firstKill , double stepSeconds ) TwoAiFight ( uint seed = GalaxySeed , float dt = SimClock . GameStep )
2026-10-21 19:04:51 +02:00
{
Ship east = null , west = null ;
var ( _ , arena , _ , failures ) = Launch ( new Scripted ( false , stage = >
{
stage . Player ( stage . Bare ( "Djinni" ) , float2 ( - 50000 , - 50000 ) ) ;
east = stage . Place ( EvLonginus ( stage ) , float2 ( 450 , 0 ) , facing : float2 ( - 1 , 0 ) , piloted : true ) as Ship ;
west = stage . Place ( EvLonginus ( stage ) , float2 ( - 450 , 0 ) , facing : float2 ( 1 , 0 ) , piloted : true ) as Ship ;
stage . Approach ( east , west ) ;
stage . Approach ( west , east ) ;
2026-10-22 20:51:59 +02:00
} , seed ) ) ;
2026-10-21 19:04:51 +02:00
Assert . True ( failures . Count = = 0 , string . Join ( "; " , failures ) ) ;
east . SetIff ( west , true ) ;
west . SetIff ( east , true ) ;
var hullAtStart = east . Hull . Durability + west . Hull . Durability ;
var bothPiloted = arena . Agents . Any ( agent = > agent . Ship = = east ) & & arena . Agents . Any ( agent = > agent . Ship = = west ) ;
var shots = new List < string > ( ) ;
var hits = 0 ;
using var _ = arena . ShotResolved . Subscribe ( outcome = >
{
shots . Add ( $"{outcome.ShotId}:{outcome.Result}" ) ;
if ( outcome . Hit ) hits + + ;
} ) ;
var history = new StringBuilder ( ) ;
2026-10-22 20:51:59 +02:00
var firstKill = TwoAiSeconds ;
var steps = ( int ) MathF . Round ( TwoAiSeconds / dt ) ;
2026-10-22 21:05:13 +02:00
var clock = System . Diagnostics . Stopwatch . StartNew ( ) ;
2026-10-22 20:51:59 +02:00
for ( var step = 0 ; step < steps ; step + + )
2026-10-21 19:04:51 +02:00
{
2026-10-22 20:51:59 +02:00
arena . Update ( dt ) ;
2026-10-21 19:04:51 +02:00
history . Append ( Snapshot ( arena , shots ) ) . Append ( '\n' ) ;
2026-10-22 20:51:59 +02:00
if ( firstKill = = TwoAiSeconds & & ( east . Hull . Durability < = 0 | | west . Hull . Durability < = 0 ) ) firstKill = ( step + 1 ) * dt ;
2026-10-21 19:04:51 +02:00
}
var digest = Convert . ToHexString ( SHA256 . HashData ( Encoding . UTF8 . GetBytes ( history . ToString ( ) ) ) ) . ToLowerInvariant ( ) ;
2026-10-22 21:05:13 +02:00
return ( digest , bothPiloted , hits , east . Hull . Durability + west . Hull . Durability < hullAtStart , east . Hull . Durability , west . Hull . Durability , firstKill , clock . Elapsed . TotalSeconds ) ;
2026-10-21 19:04:51 +02:00
}
[Fact]
public void TwoAiFightIsOneHistory ( )
{
var first = TwoAiFight ( ) ;
var second = TwoAiFight ( ) ;
Assert . True ( first . bothPiloted , "both fighters are piloted" ) ;
Assert . True ( first . hits > 0 , "at least one shot resolved as a hit" ) ;
Assert . True ( first . hullLost , "at least one fighter lost hull" ) ;
Assert . Equal ( first . digest , second . digest ) ;
}
MiningCut3Tests: ChunkInfo's pinned bits are per host class; the Linux bits are the base's own
ChunkInfoIsBitIdenticalToItsValueBeforeTheExtraction never passed on Linux: at c86f28e93 (the commit that added it) it already measures 1039274088 for the second observer where the recording says 1039274086, and base 12dfcef5, the code the recording claims to be taken from, measures the same ...088 on Linux (CultMath 0.4.1 and 0.5.0 alike). The sensor's bearing term (acos, curve) is libm arithmetic, which rounds differently on Windows, where the recording was made. It is a host-class recording read as universal, not a regression: no commit moved it.
The pin is now per host class like the two-AI digest (ruling replays-per-host-class): windows-x64 keeps the original recording, linux-x64 is base 12dfcef5 run on Linux, and the current code equals it. A host with no recording fails and prints its bits. RunStartTests.HostClass is now internal, the one host-class name.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 05:06:09 +00:00
internal static string HostClass ( ) = >
2026-10-21 19:04:51 +02:00
( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? "linux"
: RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? "windows"
: RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ? "osx" : "other" )
+ "-" + RuntimeInformation . ProcessArchitecture . ToString ( ) . ToLowerInvariant ( ) ;
[Fact]
public void TwoAiDigestIsPinned ( )
{
var host = HostClass ( ) ;
var digest = TwoAiFight ( ) . digest ;
2026-11-26 03:23:01 +01:00
Assert . True ( TwoAiPinnedDigests . TryGetValue ( host , out var pinned ) ,
$"TWO-AI-DIGEST host class {host} has no pin (pinned: {string.Join(" , ", TwoAiPinnedDigests.Keys)}); measured digest {digest}" ) ;
Assert . True ( digest = = pinned , $"TWO-AI-DIGEST {host} measured {digest}, pinned {pinned}" ) ;
2026-10-21 19:04:51 +02:00
}
}