A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Save: a finished old save without both its ending card and its lost marker loads (Refs #2733, item 19) (#3046)
Eyal Golan9b48e898aacb
diff --git a/changelog.d/2733-item19.md b/changelog.d/2733-item19.mdnew file mode 100644index 000000000..20adc4761--- /dev/null+++ b/changelog.d/2733-item19.md@@ -0,0 +1,5 @@+### Fixed++- A finished game saved before both the ending-card change (#2536) and the stayed lost markers (#2542) now loads with no+ "some parts differ" notice: the loader takes both out of the replay together, as it already did each alone (R-SAVE-005,+ #2733).diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex 621eef89e..20807688c 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -90,7 +90,7 @@ R-SAVE-003: Save schema 3 adds the open turn: after the last `turn`, a line `opeR-SAVE-004: A schema 2 save of the GDD game with a player attack (`order <unit> attack <q> <r>`) is refused with the code `older_rules` (R-SAVE-001): its attacks were standing orders, so it would replay differently (the owner, 23 October 2026: "Refuse with a clear message"). So is a schema 2 GDD save whose replay under the current rules does not end on its saved hash (instant capture, the full-points step). Other schema 2 saves load.-R-SAVE-005: Since #2605 the state records the soft events the cap left waiting (R-EVT-001), so every ruleset writes schema 5. An older save of a valid schema for its ruleset (1 to 3 for `legacy`, 2 to 3 for `gdd-0.1`, 4 for `gdd-0.3`; schema 1 has no ruleset line, so it is `legacy`) still loads, and is written back in the current schema (R-SAVE-006). #2536's exception for a finished game without its ending cards applies to such a save only. It was played under the earlier cap, so with the same content a replay that does not end on its saved hash, or an open-turn order the replay now refuses, is the rules' change, not damage: it loads with the `replay_differs` warning. R-SAVE-004's schema 2 refusals come first. A schema 5 save is R-SAVE-006's.+R-SAVE-005: Since #2605 the state records the soft events the cap left waiting (R-EVT-001), so every ruleset writes schema 5. An older save of a valid schema for its ruleset (1 to 3 for `legacy`, 2 to 3 for `gdd-0.1`, 4 for `gdd-0.3`; schema 1 has no ruleset line, so it is `legacy`) still loads, and is written back in the current schema (R-SAVE-006). #2536's exception for a finished game without its ending cards applies to such a save only, as does #2542's for a same-content save without its stayed lost markers; a same-content save without both is the same game when taking both out of the replay gives its saved hash, and loads with no warning (#2733). It was played under the earlier cap, so with the same content a replay that does not end on its saved hash, or an open-turn order the replay now refuses, is the rules' change, not damage: it loads with the `replay_differs` warning. R-SAVE-004's schema 2 refusals come first. A schema 5 save is R-SAVE-006's.R-SAVE-006: Since #2544 a player's order to a unit in an operation cancels the operation's remaining orders for its other units (R-ENEMY-009), so every ruleset writes schema 6. A schema 5 save, of any ruleset, still loads and is written back as schema 6. It was played before that cancel, so with the same content a replay that does not end on its saved hash, or an open-turn order the replay now refuses, is the rules' change, not damage: it loads with the `replay_differs` warning. The exceptions for a missing ending card (#2536) and a missing lost marker (#2542) still apply only to saves older than schema 5, as before. A schema 6 save that does either on the same content is damaged (`SaveException`).diff --git a/sim/Statecraft.Sim.Tests/SaveGameTests.cs b/sim/Statecraft.Sim.Tests/SaveGameTests.csindex ceca97063..d7988f7eb 100644--- a/sim/Statecraft.Sim.Tests/SaveGameTests.cs+++ b/sim/Statecraft.Sim.Tests/SaveGameTests.cs@@ -160,6 +160,69 @@ public class SaveGameTestsAssert.Equal("The replay does not match the saved game: the save is damaged or was edited, or the game has a bug.", ex.Message);}+ /// <summary>#2733 item 19 (R-SAVE-005): a finished game saved before both #2536 and #2542 lacks its ending card and its+ /// stayed lost marker, so its replay differs by both. Removing both gives its saved hash: the same game, so it loads+ /// with no warning, as a save that lacks only one of them does. The fixture's one-turn game takes town_a, raids it+ /// and ends on its first turn's boundary with an objective recorded then; its card is the ending card.</summary>+ [Fact]+ public void AFinishedOldSaveWithoutItsEndingCardAndLostMarkerLoads()+ {+ const string card = "id: first_card\ntitle: Done\ntext: Done.\nfrom: 1948-05-15\nto: 1957-01-01\nobjective: first_goal\n"+ + "options:\n - id: continue\n text: Continue\n historical: true\nsources: [{ url: https://example.org/event, checked: 2026-10-18 }]\n";+ var c = CaptureTests.Load(files =>+ {+ files["map/units_1948.yaml"] = "units:\n- id: starter\n service: army\n side: israel\n status: draft\n strength: { value: 20, as_of: '1948-05-15' }\n anchors:\n - { place: Here, lon: 34.29, lat: 33.35, from: '1948-05-15', to: '1949-01-01' }\n";+ files["map/unit_anchor_hexes.yaml"] = "anchors:\n - { id: starter, anchor: 0, q: 0, r: 1 }\n";+ files["objectives.yaml"] = "objectives:\n - { id: first_goal, name: First goal, completes_when: { site_built: site_a }, reference_date: 1949-06-12, card: first_card }\n";+ files["events/first_card.yaml"] = card;+ }, CaptureTests.AllTargets);+ var probe = Simulation.NewGame(c, 1, Mode.Historical);+ c.Rules.Mvp!.End = Simulation.TurnEnd(c, probe);+ c.Objectives.Single().CompletesWhen = new ObjectiveCondition+ {+ Absorbed = new AbsorbCondition { PopulationAtLeast = 0, OverflowAtMost = 1m, EmployedAtLeast = 0m },+ };+ var played = GameSession.New(c, 1, Mode.Historical);+ var result = played.Step(new Command[] { new Move(0, 1, 0), new ResolveCapture("town_a", SettlementControl.Raid) });+ Assert.Empty(result.Rejected);+ Assert.Equal(GameOverCodes.MvpEnded, played.State.GameOver);+ Assert.Equal(new[] { "first_card" }, played.State.PendingEvents);+ var lost = played.State.Settlements["town_a"];+ Assert.Equal(("", "", false), (lost.Owner, lost.State, lost.Fled));++ var noCard = played.State.Clone(); noCard.FiredEvents.Remove("first_card"); noCard.PendingEvents.Clear();+ var noMarker = played.State.Clone(); noMarker.Settlements.Remove("town_a");+ var neither = noCard.Clone(); neither.Settlements.Remove("town_a");+ var text = played.Save.Write();+ var endLine = "end " + played.State.Hash();+ Assert.Contains(endLine + "\n", text);+ Assert.StartsWith(FormattableString.Invariant($"statecraft-save {SaveGame.SchemaVersion}\n"), text);+ // gdd-0.1 wrote schema 3 before the soft-event cap (R-SAVE-005).+ string Old(GameState end) => text.Replace(endLine, "end " + end.Hash())+ .Replace(FormattableString.Invariant($"statecraft-save {SaveGame.SchemaVersion}\n"), "statecraft-save 3\n");++ // Controls: lacking only one of them loads as before.+ GameSession.Load(c, Old(noCard), out var w1);+ Assert.Empty(w1);+ GameSession.Load(c, Old(noMarker), out var w2);+ Assert.Empty(w2);+ // Lacking both: the same game, loaded as saved and over, with its card waiting.+ var loaded = GameSession.Load(c, Old(neither), out var warnings);+ Assert.Empty(warnings);+ Assert.Equal(played.State.Hash(), loaded.State.Hash());+ Assert.Equal(new[] { "first_card" }, loaded.State.PendingEvents);++ // Only exactly both: one more difference is the older rules' replay_differs, and a current save without them is damaged.+ var other = neither.Clone(); other.Treasury += 1;+ GameSession.Load(c, Old(other), out var w3);+ Assert.Equal(new[] { GameSession.ReplayDiffers }, w3);+ Assert.Null(Assert.Throws<SaveException>(() => GameSession.Load(c, text.Replace(endLine, "end " + neither.Hash()), out _)).Code);+ // With other content the ending-card exception alone applies (as for #2536), and lost markers need the same content.+ c.Hash = new string('c', 64);+ GameSession.Load(c, Old(neither), out var w4);+ Assert.Equal(new[] { GameSession.ContentChanged, GameSession.ReplayDiffers }, w4);+ }+[Fact]// R-ALT-009public void ARejoinedRoadRoundTripsThroughASave()diff --git a/sim/Statecraft.Sim/GameSession.cs b/sim/Statecraft.Sim/GameSession.csindex 5538a3846..0df1c3d3a 100644--- a/sim/Statecraft.Sim/GameSession.cs+++ b/sim/Statecraft.Sim/GameSession.cs@@ -114,8 +114,9 @@ namespace Statecraft.Sim// game, so accept that exact same-content difference; every other replay mismatch keeps the existing handling.// Only a save older than schema 5 can be from before the markers: a later one always has them, so one without them// was edited (Codex review of 47da9b207). Both stay at schema 5 when the schema moves on (#2544).- if (session.State.Hash() != saved.FinalHash && !(saved.Version < SaveGame.SoftEventCapSchemaVersion && OnlyEndingCardsAdded(c, session.State, saved.FinalHash))- && !(saved.Version < SaveGame.SoftEventCapSchemaVersion && sameContent && OnlyLostMarkersAdded(session.State, saved.FinalHash)))+ // A save from before both lacks both, so it is the same game when taking both out gives its hash (#2733 item 19).+ if (session.State.Hash() != saved.FinalHash+ && !(saved.Version < SaveGame.SoftEventCapSchemaVersion && OnlyLaterAdditions(c, session.State, saved.FinalHash, sameContent))){if (saved.Version < 3 && saved.Ruleset == SaveGame.GddRuleset)throw new SaveException(0, "This save was made with an earlier version of the game's rules.", SaveException.OlderRules);@@ -134,35 +135,46 @@ namespace Statecraft.Simreturn session;}- /// <summary>#2536: whether <paramref name="s"/>, a replayed game, differs from the saved final state only by the ending- /// turn's objective cards (EventSystem.EndingCard), which games finished before that change never fired. Before it,- /// a finished game had nothing pending (Delegate resolves every card before a turn, and an ended game raised no+ /// <summary>Whether <paramref name="s"/>, a replayed game, differs from the saved final state only by what #2536 and+ /// #2542 added: its ending cards, or (same content only) its stayed lost markers, or both (#2733 item 19).</summary>+ static bool OnlyLaterAdditions(Content c, GameState s, string savedHash, bool sameContent)+ {+ var noCards = WithoutEndingCards(c, s);+ if (noCards != null && noCards.Hash() == savedHash) return true;+ if (!sameContent) return false;+ if (WithoutLostMarkers(s) is GameState noMarkers && noMarkers.Hash() == savedHash) return true;+ return noCards != null && WithoutLostMarkers(noCards) is GameState neither && neither.Hash() == savedHash;+ }++ /// <summary>#2536: <paramref name="s"/>, a replayed game, without the ending turn's objective cards+ /// (EventSystem.EndingCard), which games finished before that change never fired, or null when it has none. Before+ /// it, a finished game had nothing pending (Delegate resolves every card before a turn, and an ended game raised no/// event), so taking exactly the pending cards out of both the fired and the pending lists must give the saved hash.- /// Narrow by design: the game is over, every pending event is an ending card, and the hash matches exactly.</summary>- static bool OnlyEndingCardsAdded(Content c, GameState s, string savedHash)+ /// Narrow by design: the game is over, every pending event is an ending card, and the hash must match exactly.</summary>+ static GameState? WithoutEndingCards(Content c, GameState s){- if (s.GameOver == null || s.PendingEvents.Count == 0) return false;+ if (s.GameOver == null || s.PendingEvents.Count == 0) return null;if (!s.PendingEvents.All(id => c.Events.FirstOrDefault(e => e.Id == id) is GameEvent e && EventSystem.EndingCard(s, e)))- return false;+ return null;var before = s.Clone();foreach (var id in s.PendingEvents) before.FiredEvents.Remove(id);before.PendingEvents.Clear();- return before.Hash() == savedHash;+ return before;}- /// <summary>#2542: whether a replay differs from its saved final state only by ownerless, inhabited, stayed markers- /// for places captured and later lost. Before this change those records were removed. Narrow by design: remove every- /// record having exactly the new lost-marker shape, then require the saved hash to match.</summary>- static bool OnlyLostMarkersAdded(GameState s, string savedHash)+ /// <summary>#2542: a replay without its ownerless, inhabited, stayed markers for places captured and later lost, or+ /// null when it has none. Before this change those records were removed. Narrow by design: remove every record+ /// having exactly the new lost-marker shape; the saved hash must then match.</summary>+ static GameState? WithoutLostMarkers(GameState s){var ids = s.Settlements.Where(kv => kv.Value.Owner == "" && kv.Value.State == ""&& kv.Value.Since != default && !kv.Value.Depopulated && !kv.Value.Fled&& kv.Value.Combatants == 0 && kv.Value.Buildings == "" && kv.Value.Turns == 0).Select(kv => kv.Key).ToList();- if (ids.Count == 0) return false;+ if (ids.Count == 0) return null;var before = s.Clone();foreach (var id in ids) before.Settlements.Remove(id);- return before.Hash() == savedHash;+ return before;}}}