A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Boot: on Continue with a decision waiting, the card's photo waits for the start screen to close (refs #592) (#620)
Eyal Golanc02d4eb04c7c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a6740ec9d..082d40490 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -346,9 +346,12 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB- For a new game, the event photo catalog (`EventImageCatalog`, with its 24 crunched photographs) loads in the backgrounda second after the game starts, while the start screen is up, instead of in `GameRoot.Begin` (refs #321). The "screenswired" step is about 80 ms shorter at `--cpu 4`. The first event card shows its photograph as before, in a frame no- longer than with the old load; a card that comes before the load ends waits for it. A Continue whose turn has a- decision waiting still loads the catalog in `Begin`, as before (#592). `?bootlog` logs when the card first gets the- catalog. Presentation only.+ longer than with the old load; a card that comes before the load ends waits for it. `?bootlog` logs when the card+ first gets the catalog. Presentation only.+- A Continue whose turn has a decision waiting no longer loads the event photo catalog in `GameRoot.Begin` either+ (refs #592, #321): the card is built under the start screen and asks for its photograph only when the screen closes,+ by when the background load has usually finished. "Screens wired" is about 90 ms shorter on such a Continue at+ `--cpu 4`. The card still opens with its photograph, never the minister's portrait first. Presentation only.- The units file (`units_1948.yaml`) is parsed once at boot instead of twice: the map keeps its parse and the unitslayer draws from the same list (refs #321). The units and where they stand are unchanged. The second parse cost about25 ms at `--cpu 4` between the map and the start screen; the map build itself is unchanged. `?bootlog` has three morediff --git a/unity/Assets/Scripts/UI/EventCardPresenter.cs b/unity/Assets/Scripts/UI/EventCardPresenter.csindex 1f5f2c887..ee97b3e18 100644--- a/unity/Assets/Scripts/UI/EventCardPresenter.cs+++ b/unity/Assets/Scripts/UI/EventCardPresenter.cs@@ -32,6 +32,11 @@ namespace Statecraft.Game.UIreadonly EventImageCatalog? photos;/// <summary>The catalog when the first event shows (EventPhotos.Get: off the boot path, issue #321), when photos is null.</summary>readonly System.Func<EventImageCatalog?>? photosLater;+ /// <summary>The card was built under the start screen and it has not closed yet (issue #592): the photograph (and the+ /// portrait in its place) waits for onUncovered, so a Continue with a decision waiting loads no catalog in GameRoot.Begin.</summary>+ bool photoWaits;+ /// <summary>Tests: the card is waiting for the start screen to close before it looks up its photograph.</summary>+ internal bool PhotoWaits => photoWaits;readonly VisualElement band, head, scroll, options, layer, hudRoot; readonly Label credit;readonly Button hide, reopen, illustrationSwitch;readonly System.Action onHide, onReopen, onUncovered;@@ -61,9 +66,13 @@ namespace Statecraft.Game.UI/// card never offers the Illustration switch.</param>/// <param name="clipFor">An event's clip (event-videos.yaml), or null when it has none.</param>/// <param name="photosLater">With no <paramref name="photos"/>: gives the catalog, asked only once an event shows.</param>+ /// <param name="underStartScreen">The start screen goes up over the card once it is built (GameRoot.Begin): the card+ /// looks up no photograph until the screen closes, then renders again with it before it is first seen (issue #592).</param>public EventCardPresenter(VisualElement root, SimBridge bridge, UiText text, PortraitCatalog? portraits, EventImageCatalog? photos = null,- CardVideo? video = null, System.Func<string, EventVideoMapping.Row?>? clipFor = null, System.Func<EventImageCatalog?>? photosLater = null)+ CardVideo? video = null, System.Func<string, EventVideoMapping.Row?>? clipFor = null, System.Func<EventImageCatalog?>? photosLater = null,+ bool underStartScreen = false){+ photoWaits = underStartScreen;this.video = clipFor == null ? null : video; this.clipFor = clipFor;card = root.Q<VisualElement>("event-card"); this.bridge = bridge; this.text = text; this.portraits = portraits; this.photos = photos; this.photosLater = photosLater;band = card.Q<VisualElement>("event-photo"); credit = card.Q<Label>("event-photo-credit");@@ -77,7 +86,12 @@ namespace Statecraft.Game.UIhide.text = text["event.hide"]; reopen.text = text["event.reopen"];card.Q<Label>("event-draft").text = text["event.draft"];onHide = Collapse; onReopen = Reopen;- onUncovered = () => { focusedFor = null; if (Shown != null && !Collapsed) FocusCard(Shown.Id, force: true); };+ onUncovered = () =>+ {+ // The start screen has closed: the photograph now, in the same frame the card first shows (issue #592).+ if (photoWaits) { photoWaits = false; if (Shown != null) Render(); }+ focusedFor = null; if (Shown != null && !Collapsed) FocusCard(Shown.Id, force: true);+ };onHidePointer = _ => hideByPointer = true;onHideSubmit = _ => hideByPointer = false;onCancel = e => { if (Escape()) e.StopPropagation(); };@@ -175,13 +189,14 @@ namespace Statecraft.Game.UIvar e = Shown;var date = EventSystem.FillDate(e, s.Date);var holder = e.Office == "" ? null : Cabinet.Holder(c, s, e.Office, date);- // The header photograph when the event has one; otherwise the minister's portrait plate (the fallback).- var photo = (photos ?? photosLater?.Invoke())?.Find(e.Id);+ // The header photograph when the event has one; otherwise the minister's portrait plate (the fallback). Neither+ // while the card waits under the start screen (photoWaits): it is not seen, and the catalog is not loaded yet.+ var photo = photoWaits ? null : (photos ?? photosLater?.Invoke())?.Find(e.Id);var row = video != null && photo != null && !failed.Contains(e.Id) ? clipFor!(e.Id) : null;clip = row == null ? null : CardVideo.Url(row.File); clipAlt = row?.Alt;ShowPhoto(photo);var image = card.Q<VisualElement>("event-image");- var entry = photo == null ? portraits?.Find(holder?.Id) : null;+ var entry = photo == null && !photoWaits ? portraits?.Find(holder?.Id) : null;var portrait = entry?.image;image.style.backgroundImage = portrait == null ? (StyleBackground)StyleKeyword.None : new StyleBackground(portrait);image.style.display = portrait == null ? DisplayStyle.None : DisplayStyle.Flex;diff --git a/unity/Assets/Scripts/UI/EventImageCatalog.cs b/unity/Assets/Scripts/UI/EventImageCatalog.csindex 94176499e..40fcf5d83 100644--- a/unity/Assets/Scripts/UI/EventImageCatalog.cs+++ b/unity/Assets/Scripts/UI/EventImageCatalog.cs@@ -78,9 +78,9 @@ namespace Statecraft.Game.UI/// after the game starts, while the start screen is up; the card asks for the catalog (Get) only when it shows an event./// Get waits for a load still in flight, or loads now when none was started, so the first card always has its/// photograph and never the minister's portrait in its place. New Game reuses the loaded catalog.- /// A new game shows no card at turn 0, so its boot loads nothing. Continue is different: GameStart.Begin resumes the- /// autosave inside GameRoot.Begin, and when that turn has a decision waiting, the card's first Render (in its- /// constructor) calls Get, which loads the catalog then and there, as before this change (?bootlog: "on demand").</summary>+ /// A new game shows no card at turn 0, so its boot loads nothing. A Continue resumes the autosave inside GameRoot.Begin,+ /// and its turn may have a decision waiting; the card is then built under the start screen and asks for the catalog+ /// only when the screen closes (issue #592), by when the prefetch has usually finished (?bootlog: "prefetch").</summary>public static class EventPhotos{/// <summary>How long after GameRoot.Begin the prefetch starts: clear of the first frames and their shader compiles.</summary>diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 874ba7a05..6a76c0ef5 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -51,6 +51,8 @@ namespace Statecraft.Game.UIScreenNav? nav;StartScreenPresenter? start;EventCardPresenter? card;+ /// <summary>Tests: the decision card of the last Begin.</summary>+ internal EventCardPresenter? Card => card;OutcomePresenter? outcomes;CameraRig? rig;List<(string, int, int, DateTime)>? drawn;@@ -201,11 +203,16 @@ namespace Statecraft.Game.UIvar video = CardVideo.ForcedOff() ? null : Own(new CardVideo());var mapping = video == null ? default : EventVideoMapping.LoadBoth();var clips = video == null ? null : mapping.clips.ToDictionary(r => r.Event);- // Event photos: Resources, no scene field, loaded off a new game's boot path (issue #321): in the start screen's- // idle time, or by the first card if it comes sooner (a Continue with a decision waiting: at once, as before).+ // Event photos: Resources, no scene field, loaded off the boot path (issue #321): in the start screen's idle time,+ // or by the first card if it comes sooner. A Continue with a decision waiting builds the card under the start+ // screen, which asks for the photograph only when the screen closes (issue #592). One value says whether the+ // screen goes up (below) and whether the card waits for it: a card waiting under no screen would never show its photo.+ // A smoke run has no New Game, and a slice game's (?mvp=1, #380) would be dead: neither shows the start screen.+ bool showsStart = startScreen && smoke == 0 && !inputs.Slice;EventPhotos.ScheduleOn(root);card = Own(new EventCardPresenter(root, bridge, text, portraits, null, video,- clips == null ? null : id => clips.TryGetValue(id, out var r) ? r : null, EventPhotos.Get));+ clips == null ? null : id => clips.TryGetValue(id, out var r) ? r : null, EventPhotos.Get,+ underStartScreen: showsStart));// The End Turn outcome clips (decision videos, phase 3): none in the automated runs (no video there). Their own// player, so the card's loop never takes it; an open screen closes when one starts.outcomes = video == null ? null : Own(new OutcomePresenter(root, bridge, text, Own(new CardVideo()), mapping.outcomes,@@ -328,11 +335,11 @@ namespace Statecraft.Game.UISyncMinimap();// The start screen goes up in the same frame the loading state ends, so the HUD never flashes before it. While it// shows, the camera keys are off (keyboard pan would otherwise move the hidden map; plan amendment A4).- if (startScreen && newGame != null && !inputs.Slice) // no start screen in a slice game: its New Game would be dead+ if (showsStart) // newGame is made above whenever smoke == 0{var cam = cameraController;var recording = Own(new DeclarationRecording(root, text, toast.Show)); // never built in automated runs (Skipped)- start = Own(new StartScreenPresenter(root, newGame, resumed, shown => { if (cam != null) cam.enabled = !shown; }, recording, text));+ start = Own(new StartScreenPresenter(root, newGame!, resumed, shown => { if (cam != null) cam.enabled = !shown; }, recording, text));start.Show();}if (hebrew) Own(new UiDirection(root, bridge));diff --git a/unity/Assets/Tests/EditMode/EventPhotosLoadTests.cs b/unity/Assets/Tests/EditMode/EventPhotosLoadTests.csindex e63dc2b9d..63e190512 100644--- a/unity/Assets/Tests/EditMode/EventPhotosLoadTests.cs+++ b/unity/Assets/Tests/EditMode/EventPhotosLoadTests.cs@@ -215,6 +215,121 @@ public class EventPhotosLoadTestsfinally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }}+ /// <summary>An autosave whose turn has a decision waiting, with a photograph, as End Turn writes it (golden seed;+ /// about 40% of autosaves, issue #592). Returns that event's id.</summary>+ string SaveWithAPhotographedDecisionWaiting()+ {+ var played = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);+ for (int turn = 0; turn < 120; turn++)+ {+ played.EndTurn();+ var id = played.State.PendingEvents.FirstOrDefault();+ if (id != null && Shipped.Find(id) != null)+ {+ new SaveStore(saveDir).WriteAutosave(played.Session.Save.Write());+ return id;+ }+ }+ Assert.Fail("no autosave with a photographed decision waiting in 120 turns");+ return "";+ }++ static void AssertShowsPhoto(VisualElement ui, string eventId)+ {+ var entry = Shipped.Find(eventId)!.Value;+ var band = ui.Q<VisualElement>("event-photo");+ Assert.AreEqual(DisplayStyle.Flex, band.style.display.value, eventId + ": the photograph shows");+ Assert.AreEqual(entry.image, band.style.backgroundImage.value.texture, eventId + ": its own photograph");+ Assert.AreEqual(entry.credit, ui.Q<Label>("event-photo-credit").text);+ Assert.AreEqual(entry.alt, band.tooltip);+ Assert.AreEqual(DisplayStyle.None, ui.Q<VisualElement>("event-image").style.display.value, "no portrait in its place");+ }++ /// <summary>Issue #592: Continue with a decision waiting. Begin builds the card under the start screen and loads no+ /// catalog (before, the card's first Render loaded it synchronously in Begin). The card shows its photograph, and never+ /// the portrait, when Continue closes the screen: after the prefetch, from it; before it, loaded then.</summary>+ [Test]+ public void AContinueWithADecisionWaitingLoadsNoCatalogInBeginAndShowsThePhotographOnContinue([Values(true, false)] bool prefetchFirst)+ {+ var eventId = SaveWithAPhotographedDecisionWaiting();+ var scheduled = new System.Collections.Generic.List<System.Action>();+ EventPhotos.Schedule = (_, action, _) => scheduled.Add(action);+ EventPhotos.StartLoad = () => { starts++; return () => Shipped; };+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);+ try+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ Object.FindFirstObjectByType<MapScene>().Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(inputs, 0, startScreen: true);+ Assert.AreEqual(eventId, root.Bridge!.State.PendingEvents.FirstOrDefault(), "resumed with the decision waiting");+ var ui = Object.FindFirstObjectByType<UIDocument>().rootVisualElement;+ Assert.IsTrue(ui.Q("root").ClassListContains(StartScreenPresenter.OpenClass), "under the start screen");+ Assert.AreEqual(0, loadsNow, "no synchronous catalog load in Begin");+ Assert.AreEqual(0, starts);+ Assert.IsFalse(EventPhotos.Loaded);+ Assert.AreEqual(DisplayStyle.None, ui.Q<VisualElement>("event-image").style.display.value, "no portrait under the screen either");+ Assert.AreEqual(1, scheduled.Count);+ if (prefetchFirst) scheduled[0](); // the idle-time prefetch, a second after Begin+ UiTestKit.Press(ui.Q<Button>("start-continue"));+ Assert.IsFalse(ui.Q("root").ClassListContains(StartScreenPresenter.OpenClass));+ AssertShowsPhoto(ui, eventId);+ Assert.AreEqual(prefetchFirst ? 0 : 1, loadsNow, prefetchFirst ? "the prefetch's catalog" : "loaded when the screen closed");+ Assert.AreEqual(prefetchFirst ? 1 : 0, starts);+ Assert.AreEqual(1, EventPhotos.Loads, "one load for the page");+ if (!prefetchFirst) { scheduled[0](); Assert.AreEqual(0, starts, "the late prefetch loads nothing more"); }+ }+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }+ }++ /// <summary>Issue #592 review: the card waits for its photograph exactly when Begin shows the start screen (one value+ /// in GameRoot decides both; #598's slice game shows none). A card waiting under no screen would never be uncovered and+ /// never show its photograph.</summary>+ [Test]+ public void TheCardWaitsForThePhotographExactlyWhenTheStartScreenShows([Values(false, true)] bool startScreen, [Values(0, 5)] int smoke,+ [Values(false, true)] bool slice)+ {+ SaveWithAPhotographedDecisionWaiting();+ EventPhotos.Schedule = (_, _, _) => { };+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap, slice);+ try+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ Object.FindFirstObjectByType<MapScene>().Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ if (smoke > 0) UnityEngine.TestTools.LogAssert.Expect(LogType.Warning, "SMOKE FAIL the smoke run needs play mode or a player");+ root.Begin(inputs, smoke, startScreen);+ var open = Object.FindFirstObjectByType<UIDocument>().rootVisualElement.Q("root").ClassListContains(StartScreenPresenter.OpenClass);+ Assert.AreEqual(startScreen && smoke == 0 && !slice, open, "the start screen shows only outside a smoke run and the slice");+ Assert.AreEqual(open, root.Card!.PhotoWaits, "the card waits for the photograph exactly when the screen covers it");+ }+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }+ }++ /// <summary>Issue #592 with the real loads: a Continue with a decision waiting adds no Resources load to Begin.</summary>+ [Test]+ public void GameRootBeginOnAContinueWithADecisionWaitingLoadsNoPhotoCatalog()+ {+ SaveWithAPhotographedDecisionWaiting();+ EventPhotos.Reset(); // the real loads, counted by EventImageCatalog+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);+ try+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ Object.FindFirstObjectByType<MapScene>().Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ var before = EventImageCatalog.ResourceLoads;+ root.Begin(inputs, 0, startScreen: true);+ Assert.IsNotEmpty(root.Bridge!.State.PendingEvents);+ Assert.AreEqual(before, EventImageCatalog.ResourceLoads, "no catalog load in Begin");+ Assert.IsFalse(EventPhotos.Loaded);+ }+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }+ }+[Test]public void GameRootBeginLoadsNoPhotoCatalog([Values(false, true)] bool startScreen){