Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

fix(sim): count accepted map actions so camp-to-housing upgrades cannot bypass the turn cap; specific reasons before the cap

eyalgolan5763a4c50a6b
diff --git a/docs/determinism.md b/docs/determinism.md
index 756b950f5..6856773bc 100644
--- a/docs/determinism.md
+++ b/docs/determinism.md
@@ -22,8 +22,8 @@ with `GoldenRun.Seed = 1948` on the real `content/`, then steps with `Historical
| Run | Ends | Hash |
|---|---|---|
-| to 1950-01-01 (`Hash1950`) | 1950-02-26, turn 45 | `f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42` |
-| to 1952-01-01 (`Hash1952`) | 1952-02-26, turn 53 | `d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52` |
+| to 1950-01-01 (`Hash1950`) | 1950-02-26, turn 45 | `ee5d0826cdca5aef578e48c6273bc1453fa7745bd5ba1adf6f4a946ade828590` |
+| to 1952-01-01 (`Hash1952`) | 1952-02-26, turn 53 | `91bb6fb5f1295cee033998093651f4f303d1a61881a25a9015a8291798a35d80` |
Each constant is written in three places that must agree:
- `sim/Statecraft.Sim/GoldenRun.cs`, which Unity code and players compare against;
@@ -55,3 +55,5 @@ rules or content, treat it as a determinism bug.
| M3 Task 6 | 1952 | `860928f2` | same |
| M3 Task 8 | 1950 | `f52fa804` | hash format adds map placements (empty in the run); no state change |
| M3 Task 8 | 1952 | `d07cd3d2` | same |
+| M3 Task 8 fix | 1950 | `ee5d0826` | Task 8 fix: per-turn map-action counter in state (hash text gains mapturn=); no state change in the run |
+| M3 Task 8 fix | 1952 | `91bb6fb5` | same |
diff --git a/sim/Statecraft.Sim.Tests/DeterminismTests.cs b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
index aafd49319..84836797e 100644
--- a/sim/Statecraft.Sim.Tests/DeterminismTests.cs
+++ b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
@@ -19,7 +19,7 @@ public class DeterminismTests
/// <summary>Any change to this value is a deliberate rules or content change: update it here, in
/// GoldenRun.Hash1950 and in docs/determinism.md in the same commit.</summary>
- public const string GoldenHash = "f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42";
+ public const string GoldenHash = "ee5d0826cdca5aef578e48c6273bc1453fa7745bd5ba1adf6f4a946ade828590";
/// <summary>Real content, seed 1948, Historical mode, HistoricalAI until the date reaches 1950-01-01.</summary>
public static GameState GoldenPlaythrough(Content c) => GoldenRun.Play(c, GoldenRun.Until1950);
@@ -34,7 +34,7 @@ public class DeterminismTests
}
/// <summary>Same rule for the second golden run, which covers the M3 content range (to 1952-01-01).</summary>
- public const string GoldenHash1952 = "d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52";
+ public const string GoldenHash1952 = "91bb6fb5f1295cee033998093651f4f303d1a61881a25a9015a8291798a35d80";
[Fact]
public void GoldenPlaythroughTo1952HashIsFixed()
diff --git a/sim/Statecraft.Sim.Tests/MapActionTests.cs b/sim/Statecraft.Sim.Tests/MapActionTests.cs
index 0b6612339..359d4accd 100644
--- a/sim/Statecraft.Sim.Tests/MapActionTests.cs
+++ b/sim/Statecraft.Sim.Tests/MapActionTests.cs
@@ -97,6 +97,78 @@ public class MapActionTests
Assert.Null(MapActions.Check(C, s, "housing", 0, 1));
}
+ [Fact]
+ public void UpgradingACampDoesNotEscapeTheTurnCap()
+ {
+ var s = Ready();
+ Assert.Null(MapActions.Apply(C, s, "transit_camp", 2, 0, new List<Cause>()));
+ Assert.Null(MapActions.Apply(C, s, "housing", 2, 0, new List<Cause>())); // replaces the camp: still a second action
+ Assert.Equal("No more than 2 map actions in one turn", MapActions.Apply(C, s, "housing", 1, 1, new List<Cause>()));
+ Assert.Null(MapActions.At(s, 1, 1));
+ }
+
+ [Fact]
+ public void SpecificReasonsComeBeforeTheTurnCap()
+ {
+ var s = Ready();
+ MapActions.Apply(C, s, "housing", 2, 0, new List<Cause>());
+ MapActions.Apply(C, s, "housing", 1, 1, new List<Cause>());
+ Assert.Equal("There is no land hex at (7, 7)", MapActions.Check(C, s, "housing", 7, 7));
+ Assert.Equal("(4, 0) is more than 1 hex from a settlement", MapActions.Check(C, s, "housing", 4, 0));
+ Assert.Equal("(2, 0) already has a housing project", MapActions.Check(C, s, "housing", 2, 0));
+ Assert.Equal("Unknown map action 'airport'", MapActions.Check(C, s, "airport", 0, 1));
+ Assert.Equal("No more than 2 map actions in one turn", MapActions.Check(C, s, "housing", 0, 1));
+ }
+
+ [Fact]
+ public void AnActionTheContentDoesNotDefineIsNotAvailable()
+ {
+ var rules = Fixture.Files["rules.yaml"].Replace(" housing: { cost: 8, housing: 0.08, morale: 0.01 }\n", "");
+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = rules }));
+ var s = Simulation.NewGame(c, 42, Mode.Historical);
+ s.Date = new DateTime(1948, 6, 1);
+ Assert.Equal("Map action 'housing' is not available in this content", MapActions.Check(c, s, "housing", 2, 0));
+ }
+
+ [Fact]
+ public void ALakeHexIsRefused()
+ {
+ var hexes = Fixture.Files["map/hexes.json"].Replace(@"{""q"":2,""r"":0,""lon"":34.38,""lat"":33.35,""elev_m"":250.0,""land"":true,""biome"":""hills""}",
+ @"{""q"":2,""r"":0,""lon"":34.38,""lat"":33.35,""elev_m"":0.0,""land"":false,""biome"":""lake""}");
+ Assert.NotEqual(Fixture.Files["map/hexes.json"], hexes);
+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["map/hexes.json"] = hexes }));
+ var s = Simulation.NewGame(c, 42, Mode.Historical);
+ s.Date = new DateTime(1948, 6, 1);
+ Assert.Equal("There is no land hex at (2, 0)", MapActions.Check(c, s, "housing", 2, 0));
+ }
+
+ [Fact]
+ public void AHexBesideTwoSettlementsAndAHexTwoSettlementsShareAreAccepted()
+ {
+ // town_b shares town_a's hex (1, 0), as Jaffa and Tel Aviv share (-3, 19); town_c sits on (3, 0), so (2, 0) touches two settlement hexes.
+ var hexes = Fixture.Files["map/settlement_hexes.yaml"] + " - { id: town_b, q: 1, r: 0 }\n - { id: town_c, q: 3, r: 0 }\n";
+ var towns = Fixture.Files["map/settlements_1948.yaml"]
+ + " - { id: town_b, name: Town B, lon: 34.29, lat: 33.35, type: town, founded: 1900, pre_modern: false }\n"
+ + " - { id: town_c, name: Town C, lon: 34.47, lat: 33.35, type: town, founded: 1900, pre_modern: false }\n";
+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["map/settlement_hexes.yaml"] = hexes, ["map/settlements_1948.yaml"] = towns }));
+ var s = Simulation.NewGame(c, 42, Mode.Historical);
+ s.Date = new DateTime(1948, 6, 1);
+ Assert.Null(MapActions.Check(c, s, "housing", 2, 0));
+ Assert.Null(MapActions.Check(c, s, "housing", 1, 0));
+ }
+
+ [Fact]
+ public void JaffaAndTelAvivShareAHexAndBuildingThereWorks()
+ {
+ var c = ContentLoader.Load(RealContentTests.ContentRoot());
+ var jaffa = c.SettlementHexes.Single(x => x.Id == "jaffa");
+ var tlv = c.SettlementHexes.Single(x => x.Id == "tel_aviv");
+ Assert.Equal((-3, 19), (tlv.Q, tlv.R));
+ Assert.Equal((tlv.Q, tlv.R), (jaffa.Q, jaffa.R));
+ var s = GoldenRun.Play(c, new DateTime(1950, 9, 1));
+ Assert.Null(MapActions.Check(c, s, "housing", tlv.Q, tlv.R));
+ }
+
[Fact]
public void AnOccupiedHexIsRejected()
{
diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.cs
index d3e44cf5b..8cd48b1f1 100644
--- a/sim/Statecraft.Sim/GameState.cs
+++ b/sim/Statecraft.Sim/GameState.cs
@@ -39,6 +39,10 @@ namespace Statecraft.Sim
public string AppointedUnder = "";
/// <summary>Map actions built so far, in order (MapActions).</summary>
public List<Placement> Placements = new List<Placement>();
+ /// <summary>The turn (its date) the map-action counter belongs to, and the actions accepted in it. Counted on
+ /// acceptance, so replacing a camp with housing cannot reopen the per-turn cap; a new date starts from zero.</summary>
+ public DateTime MapActionsTurnDate;
+ public int MapActionsThisTurn;
public string? GameOver;
public double TotalPopulation => Population.Values.Sum();
@@ -90,6 +94,7 @@ namespace Statecraft.Sim
foreach (var p in Placements)
sb.Append(p.Action).Append('@').Append(p.Q).Append(',').Append(p.R).Append('@').Append(p.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';');
sb.Append(']');
+ sb.Append("mapturn=").Append(MapActionsTurnDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(':').Append(MapActionsThisTurn).Append(';');
sb.Append("over=").Append(GameOver ?? "");
using var sha = SHA256.Create();
var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
diff --git a/sim/Statecraft.Sim/GoldenRun.cs b/sim/Statecraft.Sim/GoldenRun.cs
index 608873737..e0d6ea646 100644
--- a/sim/Statecraft.Sim/GoldenRun.cs
+++ b/sim/Statecraft.Sim/GoldenRun.cs
@@ -11,12 +11,12 @@ namespace Statecraft.Sim
/// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change:
/// update it, DeterminismTests and docs/determinism.md in the same commit and say why in the message.</summary>
- public const string Hash1950 = "f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42";
+ public const string Hash1950 = "ee5d0826cdca5aef578e48c6273bc1453fa7745bd5ba1adf6f4a946ade828590";
public static readonly DateTime Until1950 = new DateTime(1950, 1, 1);
/// <summary>Final hash of Play(content, Until1952): the M3 content range (to 31 December 1951).</summary>
- public const string Hash1952 = "d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52";
+ public const string Hash1952 = "91bb6fb5f1295cee033998093651f4f303d1a61881a25a9015a8291798a35d80";
public static readonly DateTime Until1952 = new DateTime(1952, 1, 1);
diff --git a/sim/Statecraft.Sim/MapActions.cs b/sim/Statecraft.Sim/MapActions.cs
index b560bf84d..c8df76ab0 100644
--- a/sim/Statecraft.Sim/MapActions.cs
+++ b/sim/Statecraft.Sim/MapActions.cs
@@ -33,6 +33,8 @@ namespace Statecraft.Sim
/// <summary>The placement on (q, r), or null.</summary>
public static Placement? At(GameState s, int q, int r) => s.Placements.FirstOrDefault(p => p.Q == q && p.R == r);
+ static int ActionsThisTurn(GameState s) => s.MapActionsTurnDate == s.Date ? s.MapActionsThisTurn : 0;
+
static string Name(string action) => action == TransitCamp ? "a transit camp" : "a housing project";
/// <summary>Null when <paramref name="action"/> may be placed on (q, r) now, else the reason shown to the player.</summary>
@@ -41,10 +43,6 @@ namespace Statecraft.Sim
if (!Actions.Contains(action)) return $"Unknown map action '{action}'";
var rule = c.Rules.MapActions.TryGetValue(action, out var x) ? x : null;
if (rule == null) return $"Map action '{action}' is not available in this content";
- // Placements carry the date they were built, and the date only advances at the end of a turn.
- var built = s.Placements.Count(p => p.Date == s.Date);
- if (built >= c.Rules.MapActionsPerTurn)
- return FormattableString.Invariant($"No more than {c.Rules.MapActionsPerTurn} map actions in one turn");
if (s.Date < c.Rules.MapActionsFrom)
return FormattableString.Invariant($"Transit camps and housing projects can be placed from {c.Rules.MapActionsFrom:yyyy-MM-dd}");
if (action == TransitCamp && !(s.Policies.TryGetValue(MaabarotPolicy, out var m) && m >= 0.5))
@@ -57,6 +55,10 @@ namespace Statecraft.Sim
var here = At(s, q, r);
if (here != null && !(action == Housing && here.Action == TransitCamp))
return FormattableString.Invariant($"({q}, {r}) already has {Name(here.Action)}");
+ // The cap comes last so a player who is over it still sees the specific reason an order is illegal. It counts
+ // accepted actions (not surviving placements): housing on a camp removes the camp but still used an action.
+ if (ActionsThisTurn(s) >= c.Rules.MapActionsPerTurn)
+ return FormattableString.Invariant($"No more than {c.Rules.MapActionsPerTurn} map actions in one turn");
return null;
}
@@ -66,6 +68,8 @@ namespace Statecraft.Sim
var reason = Check(c, s, action, q, r);
if (reason != null) return reason;
var rule = c.Rules.MapActions[action];
+ if (s.MapActionsTurnDate != s.Date) { s.MapActionsTurnDate = s.Date; s.MapActionsThisTurn = 0; }
+ s.MapActionsThisTurn++;
s.Placements.RemoveAll(p => p.Q == q && p.R == r);
s.Placements.Add(new Placement(action, q, r, s.Date));
s.Treasury -= rule.Cost;