determinism-ledger: every ItemManager states its seed; scatter on the zone's dice

- ItemManager(uint seed, ...) refuses seed 0 and seeds Random from it.
- RunStart.ItemSeed(galaxy, step) is public (formula unchanged), refuses a
  zero result; named steps ArenaStep, StageStep, ContinueStep and NoRunSeed.
- Continue seeds the item stream from the saved run's seed (ContinueStep);
  a scene with no galaxy uses NoRunSeed.
- Zone's unpack scatter draws SimulationDice.For(CombatSeed, ScatterStream,
  1-based pack ordinal), never the item stream.
- Loadout's scratch manager and AetherDb's catalog command take fixed seeds;
  every test and Editor construction gains 1u (mechanical rewrite).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-10-21 19:01:41 +02:00
commit cc0c164574
85 changed files with 200 additions and 160 deletions

View file

@ -17,6 +17,8 @@ using float4 = CultMath.float4;
public class ItemManager
{
// The item stream: each lot's quality and each generated fit. Seeded by whoever constructs the manager and reseeded
// by RunStart at each step of a new run, never by the clock.
public Random Random;
private Action<string> _logger;
@ -25,8 +27,10 @@ public class ItemManager
public ProvenanceLedger Lots { get; }
public GameplaySettings GameplaySettings { get; }
public ItemManager(CultCache itemData, ProvenanceLedger lots, GameplaySettings settings, Action<string> logger)
public ItemManager(uint seed, CultCache itemData, ProvenanceLedger lots, GameplaySettings settings, Action<string> logger)
{
if (seed == 0) throw new ArgumentOutOfRangeException(nameof(seed), "an item stream needs a nonzero seed");
Random = new Random(seed);
ItemData = itemData;
Lots = lots;
GameplaySettings = settings;

View file

@ -127,6 +127,9 @@ public static class Loadouts
// The quality a design no manufacturer makes is built at: the console give's.
public const float UnbrandedQuality = .95f;
// The seed of the scratch item manager a fit is tried on. It draws nothing, so any nonzero constant does.
private const uint ScratchSeed = 1u;
// All-or-nothing: returns an entity only when every design resolved and fitted, the loadout has at most
// WeaponGroupCount weapon groups, and every group index names a weapon slot. Every failure of a stage is listed; on any
// failure nothing is returned and the zone and cache are unchanged. The stages run in the order that puts refusal
@ -166,7 +169,7 @@ public static class Loadouts
// The cells fit or they do not whatever brand a design-only slot takes: the shape is the design's. Tried on its own
// item manager, so nothing is minted into the live ledger and nothing is drawn.
var scratch = new ItemManager(cache, new ProvenanceLedger(), itemManager.GameplaySettings, _ => { });
var scratch = new ItemManager(ScratchSeed, cache, new ProvenanceLedger(), itemManager.GameplaySettings, _ => { });
Assemble(scratch, null, hullData, loadout, Unbranded(scratch, hullSettled), slotsSettled.Select(settled => Unbranded(scratch, settled)).ToArray(), failures, out _);
if (failures.Count > reported) return null;

View file

@ -25,7 +25,7 @@ public static class RunStart
public static ZonePack GenerateArena(ItemManager itemManager, ZoneGenerationSettings zoneSettings, PlanetSettings planetSettings, Galaxy galaxy, Scenario scenario)
{
SeedItems(itemManager, galaxy, 1);
SeedItems(itemManager, galaxy, ArenaStep);
return galaxy.Entrance.PackedContents = ZoneGenerator.GenerateZone(
itemManager, zoneSettings, planetSettings, galaxy, galaxy.Entrance, galaxy.IsPrelude, scenario.Ambient);
}
@ -36,7 +36,7 @@ public static class RunStart
public static Staged Stage(ItemManager itemManager, Zone arena, Scenario scenario, string startingHull,
TutorialGenerationSettings tutorialGenerationSettings, List<string> failures)
{
SeedItems(itemManager, arena.Galaxy, 2);
SeedItems(itemManager, arena.Galaxy, StageStep);
var stage = new ScenarioStage(itemManager, arena, startingHull, tutorialGenerationSettings);
scenario.Stage(stage);
var reported = failures.Count;
@ -62,5 +62,22 @@ public static class RunStart
// so a fixed seed fixes the arena's gear and the player's fit. Each step draws its own stream, so whatever runs
// between them (populating the level) cannot shift it.
private static void SeedItems(ItemManager itemManager, Galaxy galaxy, uint step) =>
itemManager.Random = new Random(galaxy.Seed * 0x9E3779B1u + step);
itemManager.Random = new Random(ItemSeed(galaxy, step));
// The steps of a run that draw items: generating its arena, staging its scenario, and continuing a saved run.
public const uint ArenaStep = 1;
public const uint StageStep = 2;
public const uint ContinueStep = 3;
// The item seed of an ActionGameManager scene opened with no galaxy. No run is loaded, so nothing a run decides
// draws from it.
public const uint NoRunSeed = 0x5EED0001u;
// The item stream's seed for one step of a run: the run's seed and the step. Never zero, which no stream accepts.
public static uint ItemSeed(Galaxy galaxy, uint step)
{
var seed = galaxy.Seed * 0x9E3779B1u + step;
if (seed == 0) throw new InvalidOperationException($"Item seed is zero for galaxy seed {galaxy.Seed} at step {step}.");
return seed;
}
}

View file

@ -14,6 +14,10 @@ public static class SimulationDice
// never rides ItemManager.Random, so minting items cannot change how a pilot fights.
public const uint CombatSampleStream = 3;
public const uint PatrolStream = 4;
// Generation placement of an unpacked ship packed at the origin, ordinal its 1-based index in the pack, on the zone's
// seed (CombatSeed): the run seed feeds drops and mines only (ruling run-seed-drops-only), and placement never rides
// ItemManager.Random.
public const uint ScatterStream = 5;
// 9.1 (docs/fire-control-cut.md): fmix32, MurmurHash3's 32-bit finalizer -- xor-shift, multiply,
// xor-shift, multiply, xor-shift. Diffuses a structured seed (a zone seed XORed with a small,

View file

@ -266,11 +266,18 @@ public class Zone
foreach (var wear in pack.ChunkWear)
_wear[new ChunkId(wear.Field, wear.Index)] = new ChunkWear { Damage = wear.Damage, BrokenUntil = wear.BrokenUntil };
var ordinal = 0u;
foreach (var entityPack in pack.Entities)
{
ordinal++;
var entity = EntitySerializer.Unpack(_itemManager, this, entityPack);
var ship = entity as Ship;
Admit(entity, piloted: ship is {IsPlayerShip: false});
// Generation placement, not admission: a ship packed at the origin is scattered across the zone, on the
// zone's own dice, so nothing drawn from the item stream can move it.
if (ship is {IsPlayerShip: false} && lengthsq(ship.Position) < 1)
ship.Position = SimulationDice.For(CombatSeed, SimulationDice.ScatterStream, ordinal)
.NextFloat3(float3(-pack.Radius * .5f), float3(pack.Radius * .5f));
}
// TODO: Associate planets with stored entities for planetary colonies