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.

Civ movement S5a: Why lines carry the units they name (Refs #1398, #1399) (#1449)

Eyal Golanf5bfeec5478c
diff --git a/changelog.d/1399-s5a.md b/changelog.d/1399-s5a.md
new file mode 100644
index 000000000..395820383
--- /dev/null
+++ b/changelog.d/1399-s5a.md
@@ -0,0 +1,3 @@
+- Sim: the Why lines for a move, fortify, hold, a lost fortify, Send home and an attack ended by a truce carry a key,
+ a snapshot of each unit they name and their values (`Cause.Key`, `Units`, `Args`), so the screen can name a raised
+ or removed unit (#1399, #1398). Their English text is unchanged.
diff --git a/sim/Statecraft.Sim.Tests/UnitLinesTests.cs b/sim/Statecraft.Sim.Tests/UnitLinesTests.cs
new file mode 100644
index 000000000..22a185966
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/UnitLinesTests.cs
@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Statecraft.Sim;
+using Xunit;
+
+/// <summary>#1399 (spec 2026-10-23 section 8.4): every Why line that names a unit carries its key, a snapshot of each unit
+/// it names and its values, so the UI can name the unit in the player's language, even once the unit has left the state.</summary>
+public class UnitLinesTests
+{
+ static readonly Content C = ContentLoader.Load(RealContentTests.ContentRoot());
+
+ /// <summary>The real start, plus a raised recruit (an id past the starting forces, home Tel Aviv) on Golani's hex.</summary>
+ static (GameState s, int raised) Start()
+ {
+ var s = Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical);
+ var id = s.NextId;
+ var golani = s.Units[0];
+ s.Units.Add(id, new Unit(id, "recruit", golani.Q, golani.R, side: "israel", strength: 2, homeCity: "tel_aviv"));
+ s.NextId = id + 1;
+ return (s, id);
+ }
+
+ static void Names(Cause line, string key, GameState s, int id)
+ {
+ Assert.Equal(key, line.Key);
+ var u = Assert.Single(line.Units);
+ Assert.Equal((id, s.Units[id].Type, s.Units[id].HomeCity), (u.Id, u.Type, u.HomeCity));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void AMoveNamesItsUnit(bool raised)
+ {
+ var (s, r) = Start();
+ var id = raised ? r : 0;
+ var to = Movement.Reachable(C, s, id).Keys.First();
+ var line = Simulation.Step(C, s, new Command[] { new Move(id, to.Q, to.R) }).Why.Single(w => w.Key == WhyKeys.UnitMoved && w.Units[0].Id == id);
+ Names(line, WhyKeys.UnitMoved, s, id);
+ Assert.Equal($"{to.Q},{to.R}", line.Args[0]);
+ }
+
+ // The enemy's units fortify and hold in the same turn, so each line is picked by our unit's id.
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void FortifyHoldAndFortifyLostNameTheirUnit(bool raised)
+ {
+ var (s, r) = Start();
+ var id = raised ? r : 0;
+ var hex = $"{s.Units[id].Q},{s.Units[id].R}";
+ Names(Simulation.Step(C, s, new Command[] { new UnitOrder(id, Orders.Fortify) }).Why.Single(w => w.Key == WhyKeys.Fortifying && w.Units[0].Id == id), WhyKeys.Fortifying, s, id);
+ Names(Simulation.Step(C, s, new Command[] { new UnitOrder(id, Orders.Hold) }).Why.Single(w => w.Key == WhyKeys.Holds && w.Units[0].Id == id), WhyKeys.Holds, s, id);
+ var u = s.Units[id];
+ s.Units[id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, Orders.Fortify, hex, u.HomeCity);
+ var to = Movement.Reachable(C, s, id).Keys.First();
+ var lost = Simulation.Step(C, s, new Command[] { new Move(id, to.Q, to.R) }).Why.Single(w => w.Key == WhyKeys.FortifyLost && w.Units[0].Id == id);
+ Names(lost, WhyKeys.FortifyLost, s, id);
+ Assert.Equal(hex, lost.Args[0]);
+ }
+
+ // The unit leaves the state when it goes home; its line keeps its identity.
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void SentHomeKeepsTheUnitsIdentity(bool raised)
+ {
+ var (s, r) = Start();
+ var id = raised ? r : 0;
+ foreach (var front in Prisoners.Fronts) s.ChosenOptions["armistice_" + front] = "sign";
+ Assert.Null(Demobilization.Check(C, s, id)); // precondition: every armistice signed
+ var ended = Simulation.Step(C, s, new Command[] { new UnitOrder(id, Demobilization.SendHome) });
+ Assert.False(ended.State.Units.ContainsKey(id));
+ Names(ended.Why.Single(w => w.Key == WhyKeys.SentHome), WhyKeys.SentHome, s, id);
+ }
+
+ // The player's standing attack ended by a truce (Combat.EndAttacksForTruce) names its unit and the hex.
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void APlayerAttackEndedByATruceNamesItsUnit(bool raised)
+ {
+ var (s, r) = Start();
+ var id = raised ? r : 0;
+ var u = s.Units[id];
+ s.Units[id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, Combat.Attack, "10,10", u.HomeCity);
+ var why = new List<Cause>();
+ Combat.EndAttacksForTruce(s, why);
+ var line = why.Single(w => w.Key == WhyKeys.AttackEndedTruce);
+ Names(line, WhyKeys.AttackEndedTruce, s, id);
+ Assert.Equal("10,10", line.Args[0]);
+ }
+
+ [Fact]
+ public void AnEnemyAttackEndedByATruceNamesTheEnemyUnit()
+ {
+ var s = Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical);
+ var (id, e) = s.Units.First(kv => kv.Value.Side != "" && kv.Value.Side != Movement.PlayerSide);
+ s.Units[id] = new Unit(e.Id, e.Type, e.Q, e.R, e.Service, e.Side, e.Strength, Combat.Attack, $"{e.Q + 1},{e.R}", e.HomeCity);
+ s.Date = new DateTime(1948, 6, 15); // a truce turn (R-ENEMY-006)
+ var why = new List<Cause>();
+ EnemyAI.Order(C, s, why, new DateTime(1948, 7, 15));
+ Assert.Equal(id, why.Single(w => w.Key == WhyKeys.AttackEndedTruce).Units[0].Id);
+ }
+
+ [Fact]
+ public void EveryKeyIsListedOnce() => Assert.Equal(WhyKeys.All.Distinct().Count(), WhyKeys.All.Count);
+}
diff --git a/sim/Statecraft.Sim/Cause.cs b/sim/Statecraft.Sim/Cause.cs
index 8cc2f1470..eb229f24d 100644
--- a/sim/Statecraft.Sim/Cause.cs
+++ b/sim/Statecraft.Sim/Cause.cs
@@ -18,10 +18,18 @@ namespace Statecraft.Sim
public string EnemyOrder { get; }
/// <summary>With EnemyOrder Combat.Attack: the enemy units that attacked in the engagement, in id order; else empty.</summary>
public IReadOnlyList<int> EnemyAttackers { get; }
- public Cause(string system, string target, double delta, string message, string enemyOrder = "", IReadOnlyList<int>? enemyAttackers = null)
+ /// <summary>A stable localization key when this line names units (#1399); else empty.</summary>
+ public string Key { get; }
+ /// <summary>Snapshots of the units this line names, in placeholder order (#1399); else empty.</summary>
+ public IReadOnlyList<UnitRef> Units { get; }
+ /// <summary>The line's other placeholder values, as invariant text and in placeholder order (#1399); else empty.</summary>
+ public IReadOnlyList<string> Args { get; }
+ public Cause(string system, string target, double delta, string message, string enemyOrder = "", IReadOnlyList<int>? enemyAttackers = null,
+ string key = "", IReadOnlyList<UnitRef>? units = null, IReadOnlyList<string>? args = null)
{
System = system; Target = target; Delta = delta; Message = PlayerText.Capitalised(message); // sentence case (playtest 2, P2-6)
EnemyOrder = enemyOrder; EnemyAttackers = enemyAttackers ?? Array.Empty<int>();
+ Key = key; Units = units ?? Array.Empty<UnitRef>(); Args = args ?? Array.Empty<string>();
}
public override string ToString() => $"[{System}] {Message}";
}
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index bfcfba3ae..d28bcab5c 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -79,7 +79,8 @@ namespace Statecraft.Sim
var u = s.Units[id];
if (!Player(u) || u.Order != Attack) continue;
s.Units[id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, "", "", u.HomeCity);
- why.Add(new Cause("map", id.ToString(CultureInfo.InvariantCulture), 0, $"Unit {id}'s attack on hex {u.OrderTarget} ends: a truce is on"));
+ why.Add(UnitLines.Line(WhyKeys.AttackEndedTruce,
+ $"Unit {id}'s attack on hex {u.OrderTarget} ends: a truce is on", new[] { u }, u.OrderTarget));
}
}
diff --git a/sim/Statecraft.Sim/Demobilization.cs b/sim/Statecraft.Sim/Demobilization.cs
index 1dfd4d241..a78b1e92e 100644
--- a/sim/Statecraft.Sim/Demobilization.cs
+++ b/sim/Statecraft.Sim/Demobilization.cs
@@ -71,7 +71,9 @@ namespace Statecraft.Sim
s.Cities[city] = x.WithCounts(x.Civilians + u.Strength, x.Employed, x.Refugees);
var ppu = c.RulesAt(s.Date).Mvp!.PeoplePerUnit;
why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), u.Strength, FormattableString.Invariant(
- $"Unit {unitId} is sent home: its {u.Strength * ppu:N0} people are civilians in {PlayerText.Settlement(c, city)} now")));
+ $"Unit {unitId} is sent home: its {u.Strength * ppu:N0} people are civilians in {PlayerText.Settlement(c, city)} now"),
+ key: WhyKeys.SentHome, units: new[] { new UnitRef(u) },
+ args: new[] { (u.Strength * ppu).ToString(CultureInfo.InvariantCulture), city }));
return null;
}
diff --git a/sim/Statecraft.Sim/EnemyAI.cs b/sim/Statecraft.Sim/EnemyAI.cs
index 14f3bfa36..fee8d3fe8 100644
--- a/sim/Statecraft.Sim/EnemyAI.cs
+++ b/sim/Statecraft.Sim/EnemyAI.cs
@@ -56,8 +56,8 @@ namespace Statecraft.Sim
if (truce && u.Order == Combat.Attack)
{
s.Units[id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, "", "", u.HomeCity);
- why.Add(new Cause("map", id.ToString(System.Globalization.CultureInfo.InvariantCulture), 0,
- $"Unit {id}'s attack on hex {u.OrderTarget} ends: a truce is on"));
+ why.Add(UnitLines.Line(WhyKeys.AttackEndedTruce,
+ $"Unit {id}'s attack on hex {u.OrderTarget} ends: a truce is on", new[] { u }, u.OrderTarget));
}
if (Script(c, s, u) is (int q, int r) && Movement.Toward(c, s, u, q, r) is (int tq, int tr)
&& Movement.Move(c, s, id, tq, tr, spent, why, enemy: true) == null) continue;
diff --git a/sim/Statecraft.Sim/Movement.cs b/sim/Statecraft.Sim/Movement.cs
index 0249e0c92..571a8c82d 100644
--- a/sim/Statecraft.Sim/Movement.cs
+++ b/sim/Statecraft.Sim/Movement.cs
@@ -108,10 +108,12 @@ namespace Statecraft.Sim
s.Units[unitId] = new Unit(u.Id, u.Type, q, r, u.Service, u.Side, u.Strength, fortified ? "" : u.Order, fortified ? "" : u.OrderTarget, u.HomeCity);
spent[unitId] = used + cost;
why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), cost,
- FormattableString.Invariant($"Unit {unitId} moved to hex {q},{r} for {cost} movement points"), enemy ? Kind : ""));
+ FormattableString.Invariant($"Unit {unitId} moved to hex {q},{r} for {cost} movement points"), enemy ? Kind : "",
+ key: WhyKeys.UnitMoved, units: new[] { new UnitRef(u) },
+ args: new[] { FormattableString.Invariant($"{q},{r}"), cost.ToString(CultureInfo.InvariantCulture) }));
if (u.Order == "fortify")
- why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), 0,
- $"Unit {unitId} left hex {u.OrderTarget} and is no longer fortified"));
+ why.Add(UnitLines.Line(WhyKeys.FortifyLost,
+ $"Unit {unitId} left hex {u.OrderTarget} and is no longer fortified", new[] { u }, u.OrderTarget));
return null;
}
diff --git a/sim/Statecraft.Sim/Orders.cs b/sim/Statecraft.Sim/Orders.cs
index a011e16ef..58cdc4f9d 100644
--- a/sim/Statecraft.Sim/Orders.cs
+++ b/sim/Statecraft.Sim/Orders.cs
@@ -54,7 +54,8 @@ namespace Statecraft.Sim
s.Units[unitId] = order == Fortify ? With(u, Fortifying, here) : With(u, Hold, "");
why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), 0, order == Fortify
? $"Unit {unitId} is fortifying hex {here}; it is fortified from the end of the turn"
- : $"Unit {unitId} holds hex {here}", enemy ? order : ""));
+ : $"Unit {unitId} holds hex {here}", enemy ? order : "", key: order == Fortify ? WhyKeys.Fortifying : WhyKeys.Holds,
+ units: new[] { new UnitRef(u) }, args: new[] { here }));
return null;
}
@@ -69,8 +70,8 @@ namespace Statecraft.Sim
{
s.Units[u.Id] = With(u, "", "");
if (u.Order == Fortify)
- why.Add(new Cause("map", u.Id.ToString(CultureInfo.InvariantCulture), 0,
- $"Unit {u.Id} left hex {u.OrderTarget} and is no longer fortified"));
+ why.Add(UnitLines.Line(WhyKeys.FortifyLost,
+ $"Unit {u.Id} left hex {u.OrderTarget} and is no longer fortified", new[] { u }, u.OrderTarget));
}
else if (u.Order == Fortifying) s.Units[u.Id] = With(u, Fortify, u.OrderTarget);
}
diff --git a/sim/Statecraft.Sim/UnitLines.cs b/sim/Statecraft.Sim/UnitLines.cs
new file mode 100644
index 000000000..1beadacb6
--- /dev/null
+++ b/sim/Statecraft.Sim/UnitLines.cs
@@ -0,0 +1,48 @@
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+
+namespace Statecraft.Sim
+{
+ /// <summary>#1399: a unit as a Why line names it, taken when the line is written, so a unit that then leaves the state
+ /// (destroyed, surrendered, sent home) keeps its identity. The UI names it: a starting force by its row, a raised unit by
+ /// its type and home city.</summary>
+ public sealed class UnitRef
+ {
+ public int Id { get; }
+ public string Type { get; }
+ public string Side { get; }
+ public string HomeCity { get; }
+
+ public UnitRef(Unit u)
+ {
+ Id = u.Id; Type = u.Type; Side = u.Side; HomeCity = u.HomeCity;
+ }
+ }
+
+ /// <summary>#1399: the keys of the Why lines that name units; the UI words each as why.line.&lt;key&gt;.</summary>
+ public static class WhyKeys
+ {
+ public const string UnitMoved = "unit_moved", FortifyLost = "fortify_lost", Fortifying = "fortifying", Holds = "holds",
+ AttackEndedTruce = "attack_ended_truce", AttackOrdered = "attack_ordered", AttackEnded = "attack_ended",
+ HoldsFire = "holds_fire", AttackCalledOff = "attack_called_off", Engagement = "engagement", Destroyed = "destroyed",
+ Retreated = "retreated", SurrenderedPrisoners = "surrendered_prisoners", SurrenderedHeld = "surrendered_held",
+ SentHome = "sent_home", Withdrew = "withdrew", OnItsWay = "on_its_way",
+ StoppedNoRoute = "stopped_no_route", StoppedFull = "stopped_full", Arrived = "arrived";
+
+ public static readonly IReadOnlyList<string> All = new[]
+ {
+ UnitMoved, FortifyLost, Fortifying, Holds, AttackEndedTruce, AttackOrdered, AttackEnded, HoldsFire,
+ AttackCalledOff, Engagement, Destroyed, Retreated, SurrenderedPrisoners, SurrenderedHeld, SentHome,
+ Withdrew, OnItsWay, StoppedNoRoute, StoppedFull, Arrived,
+ };
+ }
+
+ static class UnitLines
+ {
+ /// <summary>A map line naming <paramref name="units"/> (its target the first one's id), with its key and values.</summary>
+ internal static Cause Line(string key, string message, IReadOnlyList<Unit> units, params string[] args) =>
+ new Cause("map", units[0].Id.ToString(CultureInfo.InvariantCulture), 0, message,
+ key: key, units: units.Select(u => new UnitRef(u)).ToList(), args: args);
+ }
+}