diff --git a/Assets/Scripts/ServerShared/Galaxy.cs b/Assets/Scripts/ServerShared/Galaxy.cs index 32186c670..2c4cbaec7 100644 --- a/Assets/Scripts/ServerShared/Galaxy.cs +++ b/Assets/Scripts/ServerShared/Galaxy.cs @@ -21,6 +21,10 @@ public class Galaxy // 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. + // The run's progress sink, the one generation reports to. RunStart.Begin reports the run's last steps through it and + // ends with "Done!", so the loading screen's last word comes after every zone exists. Null for a restored galaxy. + public Action Progress { get; } + public HashSet VisitedZones = new HashSet(); public SectorBackgroundSettings Background { get; } @@ -139,6 +143,7 @@ public class Galaxy InfluenceScale = 1; Background = background; Log = log; + Progress = progressCallback; var factions = cache.GetAll(); Seed = seed != 0 ? seed : throw new ArgumentOutOfRangeException(nameof(seed), "a generated galaxy takes its run's seed from GalaxyStage, never zero"); var random = new Random(Seed); @@ -168,8 +173,6 @@ public class Galaxy NamePlaces(ref random, progressCallback); - progressCallback?.Invoke("Done!"); - if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao } // Each zone's index and the links as indices: the graph Provinces reads. @@ -244,6 +247,7 @@ public class Galaxy Background = background; Log = log; + Progress = progressCallback; Seed = seed != 0 ? seed : throw new ArgumentOutOfRangeException(nameof(seed), "a generated galaxy takes its run's seed from GalaxyStage, never zero"); var random = new Random(Seed); @@ -333,8 +337,6 @@ public class Galaxy NamePlaces(ref random, progressCallback); - progressCallback?.Invoke("Done!"); - if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao } private GalaxyZone[] GenerateZones(int zoneCount, ref Random random, Action progressCallback = null) diff --git a/Assets/Scripts/ServerShared/RunStart.cs b/Assets/Scripts/ServerShared/RunStart.cs index 79b235772..0c8174c2f 100644 --- a/Assets/Scripts/ServerShared/RunStart.cs +++ b/Assets/Scripts/ServerShared/RunStart.cs @@ -43,10 +43,13 @@ public static class RunStart // 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) + // `progress` hears "Materializing zones: k of n" as each zone is reached, the loading screen's last long step. + public static void Materialize(ItemManager itemManager, ZoneGenerationSettings zoneSettings, PlanetSettings planetSettings, Galaxy galaxy, + Action progress = null) { for (var i = 0; i < galaxy.Zones.Length; i++) { + progress?.Invoke($"Materializing zones: {i + 1} of {galaxy.Zones.Length}"); var zone = galaxy.Zones[i]; if (zone.PackedContents == null) { @@ -70,7 +73,9 @@ public static class RunStart var arena = BuildArena(itemManager, planetSettings, galaxy); enter(); var staged = Stage(itemManager, arena, scenario, startingHull, failures); - if (staged != null) Materialize(itemManager, zoneSettings, planetSettings, galaxy); + if (staged == null) return null; + Materialize(itemManager, zoneSettings, planetSettings, galaxy, galaxy.Progress); + galaxy.Progress?.Invoke("Done!"); // generation's last word comes after the last zone exists return staged; } diff --git a/tests/Aetheria.Shared.Tests/RegionZonesTests.cs b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs index 9e03d0a4a..3784c5412 100644 --- a/tests/Aetheria.Shared.Tests/RegionZonesTests.cs +++ b/tests/Aetheria.Shared.Tests/RegionZonesTests.cs @@ -187,4 +187,34 @@ public sealed partial class RunStartTests Assert.Equal(new[] { Array.IndexOf(restored.Zones, restored.Entrance) }, restored.VisitedZones.Select(zone => Array.IndexOf(restored.Zones, zone))); } + + // The loading screen hears every step: each zone as Materialize reaches it, and "Done!" once, after the last zone. + [Fact] + public void MaterializeReportsEachZoneAndDoneComesLast() + { + var heard = new List(); + var scenario = new DemoTerminus(); + var galaxy = RunStart.Generate(scenario, Inputs(() => 1, heard.Add)); + Assert.DoesNotContain("Done!", heard); + var failures = new List(); + var staged = RunStart.Begin(_items, _zoneSettings, _planetSettings, galaxy, scenario, _startingHull, failures, + () => galaxy.VisitedZones.Add(galaxy.Entrance)); + Assert.NotNull(staged); + var count = galaxy.Zones.Length; + var zones = heard.Where(line => line.StartsWith("Materializing zones: ")).ToList(); + Assert.Equal(Enumerable.Range(1, count).Select(k => $"Materializing zones: {k} of {count}"), zones); + Assert.Equal(1, heard.Count(line => line == "Done!")); + Assert.Equal("Done!", heard[heard.Count - 1]); + Assert.True(heard.IndexOf(zones[0]) > heard.Count(line => line != "Done!" && !line.StartsWith("Materializing")) - 1, "generation reports precede materialization"); + } + + [Fact] + public void AFailedStartReportsNeitherZonesNorDone() + { + var heard = new List(); + var scenario = new Scripted(true, _ => { }, generate: stage => new DemoTerminus().Generate(stage)); + var galaxy = RunStart.Generate(scenario, Inputs(() => 1, heard.Add)); + Assert.Null(RunStart.Begin(_items, _zoneSettings, _planetSettings, galaxy, scenario, _startingHull, new List(), () => { })); + Assert.DoesNotContain(heard, line => line.StartsWith("Materializing") || line == "Done!"); + } } diff --git a/tests/Aetheria.Shared.Tests/ScenarioTests.cs b/tests/Aetheria.Shared.Tests/ScenarioTests.cs index dd866a4c8..8869930b3 100644 --- a/tests/Aetheria.Shared.Tests/ScenarioTests.cs +++ b/tests/Aetheria.Shared.Tests/ScenarioTests.cs @@ -48,14 +48,14 @@ public sealed partial class RunStartTests private static IEnumerable EveryScenario => Scenarios.Modes.Concat(Scenarios.Development).Concat(Scenarios.Tests); // The galaxy inputs the menu hands to RunStart, read from the authored settings. - private GalaxyStage Inputs(Func clock = null) + private GalaxyStage Inputs(Func clock = null, Action progress = null) { var authored = AuthoredSettings.Load(FindRepoRoot()); return new GalaxyStage( authored.Read("SectorGenerationSettings"), authored.Read("SectorBackgroundSettings"), authored.Read("CastBackgroundSettings"), - _cache, _ => { }, null, clock); + _cache, _ => { }, progress, clock); } // What MainMenu.Launch and StartGame do with a scenario: its galaxy, its arena, then staging. beforeStaging stands in