diff --git a/Assets/Scripts/ServerShared/RunStart.cs b/Assets/Scripts/ServerShared/RunStart.cs index 754c61a89..80f503fd3 100644 --- a/Assets/Scripts/ServerShared/RunStart.cs +++ b/Assets/Scripts/ServerShared/RunStart.cs @@ -30,6 +30,32 @@ public static class RunStart itemManager, zoneSettings, planetSettings, galaxy, galaxy.Entrance, scenario.EntranceStations, scenario.Ambient); } + // The arena's live Zone, which staging needs before the rest of the region exists. Idempotent. + public static Zone BuildArena(ItemManager itemManager, PlanetSettings planetSettings, Galaxy galaxy) => + Live(itemManager, planetSettings, galaxy, galaxy.Entrance); + + // The one owner of whether a zone of the run exists: every zone, in index order, has a generation snapshot and a live + // Zone. A zone draws its items from the run seed and its index alone (RegionStep), so the order zones are generated + // 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. + public static void Materialize(ItemManager itemManager, ZoneGenerationSettings zoneSettings, PlanetSettings planetSettings, Galaxy galaxy) + { + for (var i = 0; i < galaxy.Zones.Length; i++) + { + var zone = galaxy.Zones[i]; + if (zone.PackedContents == null) + { + SeedItems(itemManager, galaxy, RegionStep + (uint) i); + zone.PackedContents = ZoneGenerator.GenerateZone(itemManager, zoneSettings, planetSettings, galaxy, zone); + } + Live(itemManager, planetSettings, galaxy, zone); + } + } + + private static Zone Live(ItemManager itemManager, PlanetSettings planetSettings, Galaxy galaxy, GalaxyZone zone) => + zone.Contents ??= new Zone(itemManager, planetSettings, zone.PackedContents, zone, galaxy); + // 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) @@ -69,11 +95,15 @@ public static class RunStart private static void SeedItems(ItemManager itemManager, Galaxy galaxy, uint 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. + // The steps of a run that draw items: generating its arena, staging its scenario, continuing a saved run, and generating each zone of the region. public const uint ArenaStep = 1; public const uint StageStep = 2; 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 + + 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 // draws from it. public const uint NoRunSeed = 0x5EED0001u; diff --git a/tests/Aetheria.Shared.Tests/RegionZonesTests.cs b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs new file mode 100644 index 000000000..dfb0584ac --- /dev/null +++ b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs @@ -0,0 +1,144 @@ +/* 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 Xunit; + +// The region's zones (aetheria-living-world, cut region-zones-live; ruling demo-chain-spans-the-region): every zone of +// a run exists as a live Zone from the start of the run and on Continue, and RunStart.Materialize owns that. +public sealed partial class RunStartTests +{ + private static readonly uint[] RegionSeeds = { 1, 2, 3, 69, 97 }; + + // What MainMenu.Launch does after the arena stages: the demo's galaxy and arena, then every other zone. + private (Galaxy galaxy, Zone arena, RunStart.Staged staged) RegionRun(uint seed, bool materialize = true) + { + var (galaxy, arena, staged, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); + Assert.True(failures.Count == 0, $"seed {seed}: " + string.Join("; ", failures)); + galaxy.Entrance.Contents = arena; + if (materialize) RunStart.Materialize(_items, _zoneSettings, _planetSettings, galaxy); + return (galaxy, arena, staged); + } + + // What a zone's generation drew, as a person reads it: each entity's hull, faction, kind, equipment and cargo designs. + // A lot's workmanship is a draw; its serial counts the run's mints, which no zone's draws decide, so it is left out. + private string[] Drawn(GalaxyZone zone) => zone.PackedContents.Entities.Select(entity => + { + string Design(ItemInstance item) => _items.GetData(item).Name + + (item is ICommodityStack stack ? "x" + stack.Quantity : "") + + (item is CraftedItemInstance crafted && crafted.Lot != 0 ? "@" + _items.Lots[crafted.Lot].EffectiveQuality().ToString("R") : ""); + var kind = entity is OrbitalEntityPack orbital ? orbital.Kind.ToString() : ""; + return string.Join("|", entity.Name, Design(entity.Hull), entity.Faction.ToString(), kind, + string.Join(",", entity.Equipment.Select(e => Design(e.item))), + string.Join(",", entity.CargoContents.SelectMany(bay => bay).Select(c => Design(c.item)))); + }).ToArray(); + + [Fact] + public void EveryZoneIsLive() + { + foreach (var seed in RegionSeeds) + { + var (galaxy, arena, _) = RegionRun(seed); + Assert.True(galaxy.Zones.Length > 1, $"seed {seed}: one zone"); + for (var i = 0; i < galaxy.Zones.Length; i++) + { + Assert.True(galaxy.Zones[i].Contents != null, $"seed {seed}: zone {i} has no Contents"); + Assert.True(galaxy.Zones[i].PackedContents != null, $"seed {seed}: zone {i} has no pack"); + } + Assert.Same(arena, galaxy.Entrance.Contents); + Assert.Contains(galaxy.Zones, zone => zone != galaxy.Entrance && zone.PackedContents.Entities.Count > 0); + } + } + + // A zone draws its items from the run seed and its index, not from what was generated before it. + [Fact] + public void GenerationOrderMovesNoZone() + { + var (all, _, _) = RegionRun(1); + var picks = new[] { 1, all.Zones.Length / 2, all.Zones.Length - 1 }; + foreach (var pick in picks) + { + var (alone, _, _) = RegionRun(1, materialize: false); + Assert.NotSame(all.Zones[pick], alone.Zones[pick]); + // every other zone is already generated (empty), so only `pick` draws; it draws after nothing + for (var i = 0; i < alone.Zones.Length; i++) + if (i != pick && alone.Zones[i].PackedContents == null) alone.Zones[i].PackedContents = new ZonePack(); + RunStart.Materialize(_items, _zoneSettings, _planetSettings, alone); + var expected = Drawn(all.Zones[pick]); + Assert.NotEmpty(expected); + Assert.Equal(expected, Drawn(alone.Zones[pick])); + } + } + + [Fact] + public void MaterializeIsIdempotent() + { + var (galaxy, _, _) = RegionRun(1); + var contents = galaxy.Zones.Select(zone => zone.Contents).ToArray(); + var packs = galaxy.Zones.Select(zone => zone.PackedContents).ToArray(); + _items.Random = new CultMath.Random(12345u); // a canary: any reseed in Materialize replaces it + RunStart.Materialize(_items, _zoneSettings, _planetSettings, galaxy); + for (var i = 0; i < galaxy.Zones.Length; i++) + { + Assert.Same(contents[i], galaxy.Zones[i].Contents); + Assert.Same(packs[i], galaxy.Zones[i].PackedContents); + } + Assert.Equal(new CultMath.Random(12345u).NextUInt(), _items.Random.NextUInt()); // nothing reseeded, so nothing generated + } + + // A run restored from its save builds every zone from what the save holds; a zone the save never generated draws what + // a fresh run gives it. + [Fact] + public void ContinueBuildsEveryZone() + { + var (galaxy, arena, staged) = RegionRun(1); + 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)); + RunStart.Materialize(_items, _zoneSettings, _planetSettings, restored); + Assert.Equal(galaxy.Zones.Length, restored.Zones.Length); + for (var i = 0; i < galaxy.Zones.Length; i++) + { + Assert.NotNull(restored.Zones[i].Contents); + Assert.Equal(galaxy.Zones[i].Contents.Entities.Count(entity => entity is OrbitalEntity), + restored.Zones[i].Contents.Entities.Count(entity => entity is OrbitalEntity)); + } + } + + [Fact] + public void ASavedRunWithoutAZonePackGeneratesWhatAFreshRunGives() + { + var (fresh, _, _) = RegionRun(1); + var (saved, arena, staged) = RegionRun(1, materialize: false); // a save from before the region was live + var (game, zones) = RunSave.Capture(_cache, saved, arena, staged.Player, new SavedActionBarBinding[0], 0); + RunSave.Commit(_cache, game, zones, _items.Lots); + var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); + var absent = restored.Zones.Select((zone, i) => (zone, i)).Where(x => x.zone.PackedContents == null).Select(x => x.i).ToArray(); + Assert.NotEmpty(absent); + RunStart.Materialize(_items, _zoneSettings, _planetSettings, restored); + foreach (var i in absent) + { + Assert.NotNull(restored.Zones[i].Contents); + Assert.Equal(Drawn(fresh.Zones[i]), Drawn(restored.Zones[i])); + } + } + + // Materialize follows a staged scenario (MainMenu.Launch); staging that fails leaves the rest of the region ungenerated. + [Fact] + public void AFailedStartMaterializesNothing() + { + var scenario = new Scripted(true, _ => { }, generate: stage => new DemoTerminus().Generate(stage)); + var (galaxy, _, staged, failures) = Launch(scenario, Inputs(() => 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); + }); + } +}