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.

sim: turns end on the next era's start date, spec 4.1 (refs #59) (#301)

agamrafaeli-instinct7686393cf852
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 35e77766e..2270b1538 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
## [Unreleased]
### Added
+- A turn now ends on the next era's start date, not only on the last era's end (chapters spec 4.1). A turn that would
+ leave under 14 days before the boundary is lengthened to it. Real days scale the turn's rates. With one era, or no
+ boundary in a run, nothing changes; goldens unchanged (refs #59).
- Writer preview: `dotnet run --project sim/Statecraft.Cli -- --preview --set key=value` plays the historical run with a
rules.yaml change applied in memory and prints the changed state lines and a table of chosen values, without Unity and
without touching `content/` (fixes #295).
diff --git a/sim/Statecraft.Sim.Tests/EraRulesSwitchTests.cs b/sim/Statecraft.Sim.Tests/EraRulesSwitchTests.cs
index e03c986b4..b4dcd9bfd 100644
--- a/sim/Statecraft.Sim.Tests/EraRulesSwitchTests.cs
+++ b/sim/Statecraft.Sim.Tests/EraRulesSwitchTests.cs
@@ -145,35 +145,36 @@ eras:
- id: founding
name: The Founding
from: 1948-05-15
- to: 1948-06-01
+ to: 1948-06-05
- id: later
name: A Later Chapter
- from: 1948-06-01
+ from: 1948-06-05
to: 1957-01-01
" });
Directory.CreateDirectory(Path.Combine(dir, "rules"));
File.WriteAllText(Path.Combine(dir, "rules", "later.yaml"), "attention_per_turn: 7.5\ntax_per_capita_per_year: 1.0\n");
var c = ContentLoader.Load(dir);
- var boundary = new DateTime(1948, 6, 1);
+ var boundary = new DateTime(1948, 6, 5);
var s = Simulation.NewGame(c, 1, Mode.Historical);
Assert.Equal(c.Rules.AttentionPerTurn, s.Attention);
- var crossed = false;
+ var lengths = new List<int>();
while (s.Date < boundary.AddDays(7)) // through the first whole turn of the new era (the fixture's ceasefire ends the weekly turns after it)
{
var start = s.Date;
var step = Simulation.Step(c, s, new List<Command>());
var end = step.State.Date;
- Assert.Equal(7, (end - start).Days); // weekly turns; no turn here is adjusted to the boundary yet (spec 4.1)
+ var days = (end - start).Days;
+ lengths.Add(days);
var tax = start < boundary ? c.Rules.TaxPerCapitaPerYear : 1.0;
- var expected = (tax - c.Rules.SpendingPerCapitaPerYear) * step.State.TotalPopulation * (7 / 365.0)
- - c.Rules.MobilizationCostPerYear * s.Mobilization * (7 / 365.0);
+ var expected = (tax - c.Rules.SpendingPerCapitaPerYear) * step.State.TotalPopulation * (days / 365.0)
+ - c.Rules.MobilizationCostPerYear * s.Mobilization * (days / 365.0);
var treasury = step.Why.Single(w => w.System == "economy" && w.Target == "treasury").Delta;
Assert.True(Math.Abs(expected - treasury) < 1e-9, $"{start:yyyy-MM-dd}: treasury {treasury}, expected {expected}");
Assert.Equal(end < boundary ? c.Rules.AttentionPerTurn : 7.5, step.State.Attention);
- crossed |= start < boundary && end > boundary;
s = step.State;
}
- Assert.True(crossed);
+ // 22 May + 7 would leave 7 days, under 14, so that turn runs 14 days to the boundary (spec 4.1); the rest are weeks.
+ Assert.Equal(new[] { 7, 14, 7 }, lengths);
}
/// <summary>The only places the sim reads the base rules directly are the values <c>NewGame</c> reads once (an era
diff --git a/sim/Statecraft.Sim.Tests/EraSystemsTests.cs b/sim/Statecraft.Sim.Tests/EraSystemsTests.cs
index 9ad60b280..33b9bb17d 100644
--- a/sim/Statecraft.Sim.Tests/EraSystemsTests.cs
+++ b/sim/Statecraft.Sim.Tests/EraSystemsTests.cs
@@ -114,22 +114,22 @@ eras:
- id: yishuv
name: An Assembly Chapter
from: 1948-05-15
- to: 1948-06-01
+ to: 1948-06-05
systems: { legislature: assembly, map_actions: [] }
- id: founding
name: The Founding
- from: 1948-06-01
+ from: 1948-06-05
to: 1957-01-01
",
["governments.yaml"] = Fixture.Files["governments.yaml"].Replace("coalition: [alpha]", "coalition: [beta]"),
};
var c = ContentLoader.Load(Fixture.Write(o));
- var boundary = new DateTime(1948, 6, 1);
+ var boundary = new DateTime(1948, 6, 5);
var s = Simulation.NewGame(c, 1, Mode.Historical);
- while (s.Date.AddDays(7) <= boundary) s = Simulation.Step(c, s, new Command[0]).State;
+ while ((boundary - s.Date).Days > 14) s = Simulation.Step(c, s, new Command[0]).State;
Assert.True(s.Date < boundary);
var crossing = Simulation.Step(c, s, new Command[] { new SetPolicy("rationing", 1.0) });
- Assert.True(crossing.State.Date > boundary); // the turn ends in the state era
+ Assert.True(crossing.State.Date == boundary); // the turn ends on the boundary, in the state era
Assert.Equal(1.0, crossing.State.Policies["rationing"]);
Assert.Equal("assembly", c.SystemsAt(s.Date).Legislature);
var next = Simulation.Step(c, crossing.State, new Command[] { new SetPolicy("rationing", 0.0) });
diff --git a/sim/Statecraft.Sim.Tests/EraTests.cs b/sim/Statecraft.Sim.Tests/EraTests.cs
index 00c53acdb..d49cd80d1 100644
--- a/sim/Statecraft.Sim.Tests/EraTests.cs
+++ b/sim/Statecraft.Sim.Tests/EraTests.cs
@@ -188,6 +188,25 @@ public class EraTests
Assert.All(dates.Zip(dates.Skip(1), (a, b) => (b - a).Days), d => Assert.InRange(d, 7, 21));
}
+ [Fact]
+ public void ATurnLandsOnTheNextErasStartAndTheGameGoesOn()
+ {
+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string>
+ {
+ ["eras.yaml"] = Eras(Era("founding", "1948-05-15", "1948-07-01"), Era("next", "1948-07-01", "1948-10-10"),
+ Era("last", "1948-10-10", "1948-12-01")),
+ }));
+ var s = PlayToTheEnd(c, out var dates);
+ // Weekly while the fixture's war lasts, then quarterly from 12 June (the ceasefire ends the weekly turns).
+ // 12 June: the quarter is cut at the nearest boundary, 1 July (not the later 10 October).
+ // 1 July: the quarter would end 1 October, 9 days short of 10 October, so it is lengthened to it.
+ // 10 October: the quarter is cut at the last era's end, which ends the game.
+ var expected = new[] { (5, 15), (5, 22), (5, 29), (6, 5), (6, 12), (7, 1), (10, 10), (12, 1) }
+ .Select(md => new DateTime(1948, md.Item1, md.Item2));
+ Assert.Equal(expected, dates);
+ Assert.Equal(GameOverCodes.EraEnded, s.GameOver); // only the last era's end ends the game
+ }
+
[Fact]
public void TheEndReasonShowsNoIdAndNoIsoDate()
{
diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.cs
index 50a54e563..934e9ab5d 100644
--- a/sim/Statecraft.Sim/Simulation.cs
+++ b/sim/Statecraft.Sim/Simulation.cs
@@ -53,9 +53,10 @@ namespace Statecraft.Sim
EventSystem.Delegate(c, s, why);
var len = SecuritySystem.TurnLengthFor(s.Rung);
- // The last era's end is a boundary: the turn that reaches it ends on it, with that turn's real days (spec 4.1, 4.5).
+ // The next era's start, or the last era's end, is a boundary: the turn that reaches it ends on it, with that turn's real days (spec 4.1, 4.5).
var end = c.Eras[c.Eras.Count - 1].To;
- var next = SimTime.AdvanceTo(s.Date, len, end);
+ var boundary = c.Eras.Select(e => e.From).Where(f => f > s.Date).DefaultIfEmpty(end).Min();
+ var next = SimTime.AdvanceTo(s.Date, len, boundary);
var adjusted = next != SimTime.Advance(s.Date, len);
var days = (next - s.Date).Days;
var yf = adjusted ? days / 365.0 : SimTime.YearFraction(len);