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 S5b: the combat Why lines carry their units (Refs #1398, #1399) (#1451)

Eyal Golan84c1d52c502a
diff --git a/changelog.d/1399-s5b.md b/changelog.d/1399-s5b.md
new file mode 100644
index 000000000..7f50c4e9a
--- /dev/null
+++ b/changelog.d/1399-s5b.md
@@ -0,0 +1,3 @@
+- Sim: the combat Why lines (an attack ordered, ended or called off, guns holding fire, the engagement, a unit destroyed,
+ retreated or surrendered) carry their key, a snapshot of each unit they name and their values, so the screen can name
+ a unit even after it has left the map (#1399, #1398). Their English text is unchanged.
diff --git a/sim/Statecraft.Sim.Tests/UnitLinesTests.cs b/sim/Statecraft.Sim.Tests/UnitLinesTests.cs
index 22a185966..d63516ede 100644
--- a/sim/Statecraft.Sim.Tests/UnitLinesTests.cs
+++ b/sim/Statecraft.Sim.Tests/UnitLinesTests.cs
@@ -106,4 +106,99 @@ public class UnitLinesTests
[Fact]
public void EveryKeyIsListedOnce() => Assert.Equal(WhyKeys.All.Distinct().Count(), WhyKeys.All.Count);
+
+ // Task 4b: the combat lines, on CombatTests' fixture (below 2:1 both sides lose 1; from 2:1 the defenders lose 1 and
+ // retreat, whatever the draw).
+ static Content Fixture6x3() => TurnPreviewTests.Plain();
+ static Unit Ours(int id, int q, int r, int strength) => new Unit(id, "soldier", q, r, side: "israel", strength: strength, homeCity: "town_a");
+ static Unit Theirs(int id, int q, int r, int strength, int tq, int tr) =>
+ new Unit(id, "enemy", q, r, side: "egypt", strength: strength, order: Combat.Attack, orderTarget: $"{tq},{tr}");
+
+ // The engagement names the attackers, then the defenders, and the count of attackers; a destroyed unit keeps its identity.
+ [Fact]
+ public void TheEngagementAndADestroyedUnitNameTheirUnits()
+ {
+ var c = Fixture6x3();
+ var s = CombatTests.With(c, Ours(1, 2, 1, 1), Theirs(2, 3, 1, 9, 2, 1));
+ var why = new List<Cause>();
+ Combat.Resolve(c, s, why);
+ var fight = why.Single(w => w.Key == WhyKeys.Engagement);
+ Assert.Equal(new[] { 2, 1 }, fight.Units.Select(u => u.Id));
+ Assert.Equal(("2,1", "1"), (fight.Args[0], fight.Args[4]));
+ Assert.False(s.Units.ContainsKey(1));
+ var gone = why.Single(w => w.Key == WhyKeys.Destroyed).Units.Single();
+ Assert.Equal((1, "soldier", "town_a"), (gone.Id, gone.Type, gone.HomeCity));
+ }
+
+ [Fact]
+ public void ARetreatNamesTheUnit()
+ {
+ var c = Fixture6x3();
+ var s = CombatTests.With(c, Ours(1, 2, 1, 3), Theirs(2, 3, 1, 9, 2, 1));
+ var why = new List<Cause>();
+ Combat.Resolve(c, s, why);
+ Assert.Equal(1, why.Single(w => w.Key == WhyKeys.Retreated).Units.Single().Id);
+ }
+
+ // 0,0's land neighbours on the 6 x 3 board are 1,0 and 0,1: with enemy units on both, the defender has nowhere to go.
+ [Fact]
+ public void ASurrenderNamesTheUnit()
+ {
+ var c = Fixture6x3();
+ var s = CombatTests.With(c, Ours(1, 0, 0, 3), Theirs(2, 1, 0, 9, 0, 0), new Unit(3, "enemy", 0, 1, side: "egypt", strength: 1));
+ var why = new List<Cause>();
+ Combat.Resolve(c, s, why);
+ var line = why.Single(w => w.Key == WhyKeys.SurrenderedHeld); // the player's soldiers, held by the enemy (Prisoners.cs:40)
+ Assert.Equal((1, "town_a"), (line.Units.Single().Id, line.Units.Single().HomeCity));
+ }
+
+ // An enemy unit that surrenders to the player's attack becomes prisoners: the other key.
+ [Fact]
+ public void AnEnemySurrenderIsPrisoners()
+ {
+ var c = Fixture6x3();
+ var mine = new Unit(2, "soldier", 1, 0, side: "israel", strength: 9, order: Combat.Attack, orderTarget: "0,0");
+ var s = CombatTests.With(c, new Unit(1, "enemy", 0, 0, side: "egypt", strength: 3), mine, Ours(3, 0, 1, 1));
+ var why = new List<Cause>();
+ Combat.Resolve(c, s, why);
+ Assert.Equal(1, why.Single(w => w.Key == WhyKeys.SurrenderedPrisoners).Units.Single().Id);
+ }
+
+ // The attack order, an attack that ends with no enemy there, guns holding fire and an attack called off for Arms name
+ // their unit and the hex (the called-off line also the need and the stock).
+ [Fact]
+ public void TheAttackOrderLinesNameTheirUnit()
+ {
+ var c = ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>
+ {
+ ["unit_types.yaml"] = Fixture.GddUnitTypes
+ + " - { id: soldier, kind: unit, side: israel, class: infantry, movement_points: 3 }\n"
+ + " - { id: gun, kind: unit, side: israel, class: artillery, movement_points: 2, range: 2, arms_per_fire: 3 }\n"
+ + " - { id: enemy, kind: unit, side: egypt, class: infantry, movement_points: 3 }\n",
+ ["map/hexes.json"] = MovementTests.Board(), ["corridors.yaml"] = "corridors: []\n"
+ })));
+ var s = CombatTests.With(c, Ours(1, 3, 0, 5), new Unit(9, "enemy", 2, 0, side: "egypt", strength: 3));
+ var given = new List<Cause>();
+ Assert.Null(Combat.Give(c, s, 1, 2, 0, given));
+ var ordered = given.Single(w => w.Key == WhyKeys.AttackOrdered);
+ Assert.Equal((1, "town_a", "2,0"), (ordered.Units.Single().Id, ordered.Units.Single().HomeCity, ordered.Args[0]));
+ s.Units.Remove(9);
+ var ended = new List<Cause>();
+ Combat.Resolve(c, s, ended);
+ Assert.Equal((1, "2,0"), (ended.Single(w => w.Key == WhyKeys.AttackEnded).Units.Single().Id, ended.Single(w => w.Key == WhyKeys.AttackEnded).Args[0]));
+
+ var gun = new Unit(5, "gun", 4, 0, side: "israel", strength: 2, order: Combat.Attack, orderTarget: "2,0", homeCity: "town_a");
+ var quiet = CombatTests.With(c, gun, new Unit(9, "enemy", 2, 0, side: "egypt", strength: 3));
+ var held = new List<Cause>();
+ Combat.Resolve(c, quiet, held);
+ Assert.Equal((5, "2,0"), (held.Single(w => w.Key == WhyKeys.HoldsFire).Units.Single().Id, held.Single(w => w.Key == WhyKeys.HoldsFire).Args[0]));
+
+ var poor = CombatTests.With(c, gun, new Unit(8, "soldier", 3, 0, side: "israel", strength: 2, order: Combat.Attack, orderTarget: "2,0"),
+ new Unit(9, "enemy", 2, 0, side: "egypt", strength: 3));
+ poor.Arms = 2;
+ var off = new List<Cause>();
+ Combat.Resolve(c, poor, off);
+ var line = off.Single(w => w.Key == WhyKeys.AttackCalledOff && w.Units[0].Id == 5);
+ Assert.Equal(new[] { "2,0", "3", "0" }, line.Args);
+ }
}
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index d28bcab5c..a0b9758df 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -122,7 +122,7 @@ namespace Statecraft.Sim
if (refused != null) return refused;
var u = s.Units[unitId]; var target = Hex(q!.Value, r!.Value);
s.Units[unitId] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, Attack, target, u.HomeCity);
- why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), 0, $"Unit {unitId} will attack hex {target}"));
+ why.Add(UnitLines.Line(WhyKeys.AttackOrdered, $"Unit {unitId} will attack hex {target}", new[] { u }, target));
return null;
}
@@ -211,10 +211,16 @@ namespace Statecraft.Sim
foreach (var x in group)
{
done.Add(x.Id);
- if (defenders.Count == 0 || !InReach(c, x, q, r)) { End(s, x, $"Unit {x.Id}'s attack on hex {x.OrderTarget} ends: no enemy unit is there", why); continue; }
+ if (defenders.Count == 0 || !InReach(c, x, q, r))
+ {
+ End(s, x, WhyKeys.AttackEnded,
+ $"Unit {x.Id}'s attack on hex {x.OrderTarget} ends: no enemy unit is there", why, x.OrderTarget);
+ continue;
+ }
if (Guns(c, x) && attackers.Count == 0)
{
- why.Add(new Cause("map", x.Id.ToString(CultureInfo.InvariantCulture), 0, $"Unit {x.Id} holds its fire: no attack on hex {x.OrderTarget} to support"));
+ why.Add(UnitLines.Line(WhyKeys.HoldsFire,
+ $"Unit {x.Id} holds its fire: no attack on hex {x.OrderTarget} to support", new[] { x }, x.OrderTarget));
continue;
}
if (Player(x))
@@ -222,7 +228,9 @@ namespace Statecraft.Sim
var need = Need(c, mvp, x);
if (s.Arms < need)
{
- End(s, x, $"Unit {x.Id}'s attack on hex {x.OrderTarget} is called off: it needs {need.ToString(CultureInfo.InvariantCulture)} Arms and the stock has {s.Arms.ToString(CultureInfo.InvariantCulture)}", why);
+ End(s, x, WhyKeys.AttackCalledOff,
+ $"Unit {x.Id}'s attack on hex {x.OrderTarget} is called off: it needs {need.ToString(CultureInfo.InvariantCulture)} Arms and the stock has {s.Arms.ToString(CultureInfo.InvariantCulture)}",
+ why, x.OrderTarget, need.ToString(CultureInfo.InvariantCulture), s.Arms.ToString(CultureInfo.InvariantCulture));
continue;
}
s.Arms -= need;
@@ -236,7 +244,8 @@ namespace Statecraft.Sim
{
var (q, r) = Target(u);
if (Defenders(s, u, q, r).Count == 0 || !InReach(c, u, q, r))
- End(s, u, $"Unit {u.Id}'s attack on hex {u.OrderTarget} ends: no enemy unit is there", why);
+ End(s, u, WhyKeys.AttackEnded,
+ $"Unit {u.Id}'s attack on hex {u.OrderTarget} ends: no enemy unit is there", why, u.OrderTarget);
}
}
@@ -246,10 +255,10 @@ namespace Statecraft.Sim
return (int.Parse(parts[0], CultureInfo.InvariantCulture), int.Parse(parts[1], CultureInfo.InvariantCulture));
}
- static void End(GameState s, Unit u, string line, List<Cause> why)
+ static void End(GameState s, Unit u, string key, string line, List<Cause> why, params string[] args)
{
s.Units[u.Id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, "", "", u.HomeCity);
- why.Add(new Cause("map", u.Id.ToString(CultureInfo.InvariantCulture), 0, line));
+ why.Add(UnitLines.Line(key, line, new[] { u }, args));
}
/// <summary>R-COMBAT-002, R-COMBAT-003, R-COMBAT-006: one engagement. The face is the one draw; its row's losses fall on
@@ -265,16 +274,21 @@ namespace Statecraft.Sim
s.RngState = rng.State;
var target = Hex(q, r);
var ppu = mvp.PeoplePerUnit;
- var removed = new List<int>();
+ var removed = new List<Unit>();
// R-COMBAT-007: the guns fire from behind the line, so the attackers' losses fall on the others only.
var attackersLost = Take(s, attackers.Where(x => !Guns(c, x)).Select(x => x.Id).ToList(), row.AttackerLoss[face], removed);
var defendersLost = Take(s, defenders.Select(x => x.Id).ToList(), row.DefenderLoss[face], removed);
+ var named = attackers.OrderBy(x => x.Id).Concat(defenders.OrderBy(x => x.Id)).Select(x => new UnitRef(x)).ToList();
why.Add(new Cause("map", target, 0, FormattableString.Invariant(
$"The attack on hex {target} ({Ids(attackers)} against {Ids(defenders)}, strength ratio {ratio}%): the attackers lost {attackersLost * ppu:N0} people and the defenders lost {defendersLost * ppu:N0} people"),
// #1297: the turn report names an enemy attack on every turn it engages, a standing one too (R-ENEMY-003)
- Player(attackers[0]) ? "" : Attack, Player(attackers[0]) ? null : attackers.Select(x => x.Id).OrderBy(x => x).ToList()));
- foreach (var id in removed)
- why.Add(new Cause("map", id.ToString(CultureInfo.InvariantCulture), 0, $"Unit {id} was destroyed"));
+ enemyOrder: Player(attackers[0]) ? "" : Attack,
+ enemyAttackers: Player(attackers[0]) ? null : attackers.Select(x => x.Id).OrderBy(x => x).ToList(),
+ key: WhyKeys.Engagement, units: named, args: new[] { target, ratio.ToString(CultureInfo.InvariantCulture),
+ (attackersLost * ppu).ToString(CultureInfo.InvariantCulture), (defendersLost * ppu).ToString(CultureInfo.InvariantCulture),
+ attackers.Count.ToString(CultureInfo.InvariantCulture) }));
+ foreach (var u in removed)
+ why.Add(UnitLines.Line(WhyKeys.Destroyed, $"Unit {u.Id} was destroyed", new[] { u }));
if (row.DefenderRetreats[face] != 1) return;
foreach (var d in defenders)
{
@@ -282,7 +296,8 @@ namespace Statecraft.Sim
if (Retreat(c, s, mvp, u) is (int, int) to)
{
s.Units[u.Id] = new Unit(u.Id, u.Type, to.Item1, to.Item2, u.Service, u.Side, u.Strength, u.Order, u.OrderTarget, u.HomeCity);
- why.Add(new Cause("map", u.Id.ToString(CultureInfo.InvariantCulture), 0, $"Unit {u.Id} retreated to hex {Hex(to.Item1, to.Item2)}"));
+ var hex = Hex(to.Item1, to.Item2);
+ why.Add(UnitLines.Line(WhyKeys.Retreated, $"Unit {u.Id} retreated to hex {hex}", new[] { u }, hex));
}
else
{
@@ -291,8 +306,9 @@ namespace Statecraft.Sim
// player's soldiers (R-POW-005).
s.Units.Remove(u.Id);
var held = Prisoners.Take(s, u, attackers.OrderBy(x => x.Id).First());
- why.Add(new Cause("map", u.Id.ToString(CultureInfo.InvariantCulture), 0,
- FormattableString.Invariant($"Unit {u.Id} surrendered with {u.Strength * ppu:N0} people: it had no hex to retreat to. {held}")));
+ why.Add(UnitLines.Line(u.Side == Movement.PlayerSide ? WhyKeys.SurrenderedHeld : WhyKeys.SurrenderedPrisoners, // Prisoners.Take's two cases
+ FormattableString.Invariant($"Unit {u.Id} surrendered with {u.Strength * ppu:N0} people: it had no hex to retreat to. {held}"),
+ new[] { u }, (u.Strength * ppu).ToString(CultureInfo.InvariantCulture)));
}
}
}
@@ -303,7 +319,7 @@ namespace Statecraft.Sim
/// <summary>Takes up to <paramref name="loss"/> population units from <paramref name="ids"/>, one at a time from the
/// strongest unit left (a tie on the lower id; the list is in id order), removing each that reaches 0 (its id is never
/// reused: NextId is not touched) and adding it to <paramref name="removed"/>. Returns the units taken.</summary>
- static int Take(GameState s, List<int> ids, int loss, List<int> removed)
+ static int Take(GameState s, List<int> ids, int loss, List<Unit> removed)
{
int taken = 0;
for (; taken < loss; taken++)
@@ -312,7 +328,7 @@ namespace Statecraft.Sim
foreach (var id in ids)
if (s.Units.TryGetValue(id, out var x) && (top == null || x.Strength > top.Strength)) top = x;
if (top == null) break;
- if (top.Strength <= 1) { s.Units.Remove(top.Id); removed.Add(top.Id); }
+ if (top.Strength <= 1) { s.Units.Remove(top.Id); removed.Add(top); }
else s.Units[top.Id] = new Unit(top.Id, top.Type, top.Q, top.R, top.Service, top.Side, top.Strength - 1, top.Order, top.OrderTarget, top.HomeCity);
}
return taken;