From 6c92f464712b61b012f7738c0a0056fe4931322f Mon Sep 17 00:00:00 2001 From: soul Date: Sat, 28 Nov 2026 18:11:30 +0100 Subject: [PATCH] Visits are the run's fact, not contents; one start path for game and tests; name the conjuring Materialize spreads Galaxy.VisitedZones (saved as SavedGame.VisitedZones, key 19; a save without it reads its packs as visits) is written when PopulateLevel enters a zone and read by SectorRenderer, so unvisited zones show 'Has not been visited' as before. RunStart.Begin owns the new-run order (arena, enter, stage, materialize only if staged) and ActionGameManager calls it; AFailedStartMaterializesNothing runs it. Materialize's comment names the generated stock and loadouts as scaffolding deleted by follow-up conjuring-site-deletions; RegionStep and RequireDesigns comments corrected. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/Gameplay/ActionGameManager.cs | 9 +-- Assets/Scripts/ServerShared/Galaxy.cs | 9 +++ Assets/Scripts/ServerShared/RunStart.cs | 20 +++++- Assets/Scripts/ServerShared/SavedGame.cs | 8 ++- Assets/Scripts/UI/Menu/SectorRenderer.cs | 2 +- .../Aetheria.Shared.Tests/RegionZonesTests.cs | 62 ++++++++++++++++--- 6 files changed, 93 insertions(+), 17 deletions(-) diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index 82ac9583a..6c0de7ae6 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -850,6 +850,7 @@ public class ActionGameManager : MonoBehaviour if (galaxyZone == null) throw new ArgumentNullException(nameof(galaxyZone)); // RunStart.Materialize owns every zone's existence; entering only reads it. + CurrentGalaxy.VisitedZones.Add(galaxyZone); Zone = galaxyZone.Contents ?? throw new InvalidOperationException($"Zone {galaxyZone.Name} has no live contents."); _zoneEntered = true; // the clock forgets its lead between frames, never inside a step PlayMusic(MusicType.Overworld); @@ -967,12 +968,8 @@ public class ActionGameManager : MonoBehaviour RunSave.Simulation simulation = null; Func start = () => { - RunStart.GenerateArena(ItemManager, Settings.ZoneSettings, Settings.PlanetSettings, CurrentGalaxy, scenario); - RunStart.BuildArena(ItemManager, Settings.PlanetSettings, CurrentGalaxy); - PopulateLevel(CurrentGalaxy.Entrance); - var placed = RunStart.Stage(ItemManager, Zone, scenario, Settings.StartingHullName, failures); - if (placed != null) RunStart.Materialize(ItemManager, Settings.ZoneSettings, Settings.PlanetSettings, CurrentGalaxy); - return placed; + return RunStart.Begin(ItemManager, Settings.ZoneSettings, Settings.PlanetSettings, CurrentGalaxy, scenario, + Settings.StartingHullName, failures, () => PopulateLevel(CurrentGalaxy.Entrance)); }; var staged = RunStart.Launch(() => { diff --git a/Assets/Scripts/ServerShared/Galaxy.cs b/Assets/Scripts/ServerShared/Galaxy.cs index 5861f7131..32186c670 100644 --- a/Assets/Scripts/ServerShared/Galaxy.cs +++ b/Assets/Scripts/ServerShared/Galaxy.cs @@ -18,6 +18,10 @@ public class Galaxy public Dictionary HomeZones = new Dictionary(); public Dictionary BossZones = new Dictionary(); public HashSet DiscoveredZones = new HashSet(); + + // The zones the player has entered: what the player knows of a zone's contents (the sector map shows them for these + // and for no other). Every zone has contents from the start of a run, so contents say nothing of a visit. + public HashSet VisitedZones = new HashSet(); public SectorBackgroundSettings Background { get; } public Faction[] Factions { get; } @@ -88,6 +92,11 @@ public class Galaxy }; }).ToArray(); foreach (var i in savedGame.DiscoveredZones) DiscoveredZones.Add(Zones[i]); + // A save from before visits were recorded generated a zone only on entering it: its packs are its visits. + if (savedGame.VisitedZones != null) + foreach (var i in savedGame.VisitedZones) VisitedZones.Add(Zones[i]); + else + foreach (var zone in Zones.Where(zone => zone.PackedContents != null)) VisitedZones.Add(zone); for (var i = 0; i < Zones.Length; i++) { Zones[i].AdjacentZones = savedZones[i].AdjacentZones.Select(azi => Zones[azi]).ToList(); diff --git a/Assets/Scripts/ServerShared/RunStart.cs b/Assets/Scripts/ServerShared/RunStart.cs index 80f503fd3..79b235772 100644 --- a/Assets/Scripts/ServerShared/RunStart.cs +++ b/Assets/Scripts/ServerShared/RunStart.cs @@ -39,6 +39,10 @@ public static class RunStart // in, or whether the rest were generated at all, moves none of them. A fresh run generates every zone but its arena // (GenerateArena's); a saved run generates only a zone its save holds no pack for. Idempotent, and ambient: only the // arena takes the scenario's orders and Ambient rule. Nothing steps but the player's zone, so the others stay frozen. + // SCAFFOLDING (ruling economy-is-never-faked): GenerateZone conjures every zone's station stock and loadouts (the + // generated station gear, hardpoint fit, facilities, consumable stock, and the ships and turrets of its ambient + // populace). This makes them exist across the whole region from tick zero; they stay until the production that should + // supply them exists, and follow-up conjuring-site-deletions deletes them. public static void Materialize(ItemManager itemManager, ZoneGenerationSettings zoneSettings, PlanetSettings planetSettings, Galaxy galaxy) { for (var i = 0; i < galaxy.Zones.Length; i++) @@ -56,6 +60,20 @@ public static class RunStart private static Zone Live(ItemManager itemManager, PlanetSettings planetSettings, Galaxy galaxy, GalaxyZone zone) => zone.Contents ??= new Zone(itemManager, planetSettings, zone.PackedContents, zone, galaxy); + // What a new run does, in order, for the game and for tests alike: the arena is generated and given its Zone, `enter` + // makes it the player's zone, the scenario stages, and only a staged scenario has the rest of the region materialized, + // so a refused start leaves no zone but the arena. Returns null, with the failures listed, when staging fails. + public static Staged Begin(ItemManager itemManager, ZoneGenerationSettings zoneSettings, PlanetSettings planetSettings, + Galaxy galaxy, Scenario scenario, string startingHull, List failures, Action enter) + { + GenerateArena(itemManager, zoneSettings, planetSettings, galaxy, scenario); + var arena = BuildArena(itemManager, planetSettings, galaxy); + enter(); + var staged = Stage(itemManager, arena, scenario, startingHull, failures); + if (staged != null) Materialize(itemManager, zoneSettings, planetSettings, galaxy); + return staged; + } + // Runs a start and answers whether it produced a run: null, with the reason in `failures`, when `start` throws or // returns nothing. The caller shows the failure and clears its run; nothing past a null may run the game. public static Staged Launch(Func start, List failures, Action log = null) @@ -101,7 +119,7 @@ public static class RunStart public const uint ContinueStep = 3; // Zone i of the region draws its items from ItemSeed(galaxy, RegionStep + i). It sits clear of the steps above, since - + // zone indices start at 0 and the steps end at ContinueStep (3): no zone's stream is another step's. public const uint RegionStep = 0x100; // The item seed of an ActionGameManager scene opened with no galaxy. No run is loaded, so nothing a run decides diff --git a/Assets/Scripts/ServerShared/SavedGame.cs b/Assets/Scripts/ServerShared/SavedGame.cs index 01a0b876d..fde5eaa01 100644 --- a/Assets/Scripts/ServerShared/SavedGame.cs +++ b/Assets/Scripts/ServerShared/SavedGame.cs @@ -78,6 +78,11 @@ public class SavedGame // no gate antagonist, set but not one of Factions refuses Continue. Keyed by the record, as Standings is. [Key(18)] public CultRecordRef Antagonist; + + // The zones the player has entered (Galaxy.VisitedZones), as indices into Zones. Written only by RunSave.Capture and + // read only by Galaxy's save constructor; null in a save written before it, which read a pack as a visit. + [Key(19)] + public int[] VisitedZones; } [MessagePackObject] @@ -118,6 +123,7 @@ public static class RunSave var game = new SavedGame { DiscoveredZones = galaxy.DiscoveredZones.Select(dz => Array.IndexOf(galaxy.Zones, dz)).ToArray(), + VisitedZones = galaxy.VisitedZones.Select(vz => Array.IndexOf(galaxy.Zones, vz)).ToArray(), Background = galaxy.Background, Factions = factions.Select(f => cache.RefOf(f)).ToArray(), Standings = galaxy.Factions.Select(f => new SavedStanding @@ -165,7 +171,7 @@ public static class RunSave { var where = zoneRef.Key.Value; var zone = cache.Get(zoneRef) ?? throw MalformedSave($"zone {where} has no record"); - // A zone never visited has no contents: legitimately null. + // A zone's contents are null only in a save from before the region was live (RunStart.Materialize fills it). foreach (var pack in zone.Contents?.Entities ?? new List()) foreach (var item in EntitySerializer.Items(pack)) designs.Add((item ?? throw MalformedSave($"an item in {where} is null")).Data.Key); diff --git a/Assets/Scripts/UI/Menu/SectorRenderer.cs b/Assets/Scripts/UI/Menu/SectorRenderer.cs index c4acbf5ab..430a67dee 100644 --- a/Assets/Scripts/UI/Menu/SectorRenderer.cs +++ b/Assets/Scripts/UI/Menu/SectorRenderer.cs @@ -72,7 +72,7 @@ public class SectorRenderer : MonoBehaviour, IBeginDragHandler, IDragHandler, IS var mass = GameManager.Settings.ZoneSettings.ZoneMass.Evaluate(density); Properties.AddProperty("Mass", () => ActionGameManager.PlayerSettings.Format(mass)); Properties.AddProperty("Radius", () => ActionGameManager.PlayerSettings.Format(radius)); - if (zone.PackedContents == null) + if (!ActionGameManager.CurrentGalaxy.VisitedZones.Contains(zone)) { Properties.AddProperty(() => "Has not been visited"); } diff --git a/tests/Aetheria.Shared.Tests/RegionZonesTests.cs b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs index dfb0584ac..9e03d0a4a 100644 --- a/tests/Aetheria.Shared.Tests/RegionZonesTests.cs +++ b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs @@ -98,7 +98,7 @@ public sealed partial class RunStartTests var (game, zones) = RunSave.Capture(_cache, galaxy, arena, staged.Player, new SavedActionBarBinding[0], 0); RunSave.Commit(_cache, game, zones, _items.Lots); var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); - Assert.All(restored.Zones, zone => Assert.Null(zone.Contents)); + Assert.True(restored.Zones.All(zone => zone.Contents == null)); RunStart.Materialize(_items, _zoneSettings, _planetSettings, restored); Assert.Equal(galaxy.Zones.Length, restored.Zones.Length); for (var i = 0; i < galaxy.Zones.Length; i++) @@ -127,18 +127,64 @@ public sealed partial class RunStartTests } } - // Materialize follows a staged scenario (MainMenu.Launch); staging that fails leaves the rest of the region ungenerated. + // The real start, as the game runs it: only a staged scenario has the region materialized (RunStart.Begin). + private (Galaxy galaxy, RunStart.Staged staged, List failures) Begun(Scenario scenario, uint seed) + { + var galaxy = RunStart.Generate(scenario, Inputs(() => seed)); + var failures = new List(); + var staged = RunStart.Begin(_items, _zoneSettings, _planetSettings, galaxy, scenario, _startingHull, failures, + () => galaxy.VisitedZones.Add(galaxy.Entrance)); // PopulateLevel's entry + return (galaxy, staged, failures); + } + + [Fact] + public void ABegunStartMaterializesTheRegion() + { + var (galaxy, staged, failures) = Begun(new DemoTerminus(), 1); + Assert.True(failures.Count == 0, string.Join("; ", failures)); + Assert.NotNull(staged); + Assert.True(galaxy.Zones.All(zone => zone.Contents != null)); + } + [Fact] public void AFailedStartMaterializesNothing() { var scenario = new Scripted(true, _ => { }, generate: stage => new DemoTerminus().Generate(stage)); - var (galaxy, _, staged, failures) = Launch(scenario, Inputs(() => 1)); + var (galaxy, staged, failures) = Begun(scenario, 1); Assert.Null(staged); Assert.NotEmpty(failures); - Assert.All(galaxy.Zones.Where(zone => zone != galaxy.Entrance), zone => - { - Assert.Null(zone.Contents); - Assert.Null(zone.PackedContents); - }); + // indices and bools: xunit formats a failing zone into its message and a zone drags the galaxy with it + for (var i = 0; i < galaxy.Zones.Length; i++) + if (galaxy.Zones[i] != galaxy.Entrance) + Assert.True(galaxy.Zones[i].Contents == null && galaxy.Zones[i].PackedContents == null, $"a refused start generated zone {i}"); + } + + // Every zone has contents, so contents are no sign of a visit: the player has visited what the run recorded. + [Fact] + public void OnlyAZoneThePlayerEnteredIsVisited() + { + var (galaxy, staged, _) = Begun(new DemoTerminus(), 1); + Assert.Equal(new[] { galaxy.Entrance }, galaxy.VisitedZones); + var neighbour = galaxy.Entrance.AdjacentZones[0]; + galaxy.VisitedZones.Add(neighbour); + var (game, zones) = RunSave.Capture(_cache, galaxy, galaxy.Entrance.Contents, staged.Player, new SavedActionBarBinding[0], 0); + RunSave.Commit(_cache, game, zones, _items.Lots); + var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); + Assert.Equal(new[] { Array.IndexOf(galaxy.Zones, galaxy.Entrance), Array.IndexOf(galaxy.Zones, neighbour) }.OrderBy(i => i), + restored.VisitedZones.Select(zone => Array.IndexOf(restored.Zones, zone)).OrderBy(i => i)); + Assert.True(restored.Zones.Length > 2); + } + + // A save from before visits were recorded generated a zone only when entered: its packs are its visits. + [Fact] + public void ASaveWithoutVisitsReadsItsPacksAsVisits() + { + var (galaxy, arena, staged) = RegionRun(1, materialize: false); + var (game, zones) = RunSave.Capture(_cache, galaxy, arena, staged.Player, new SavedActionBarBinding[0], 0); + game.VisitedZones = null; + RunSave.Commit(_cache, game, zones, _items.Lots); + var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); + Assert.Equal(new[] { Array.IndexOf(restored.Zones, restored.Entrance) }, + restored.VisitedZones.Select(zone => Array.IndexOf(restored.Zones, zone))); } }