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.

UI: the turn report names a standing enemy attack on every turn it engages (Fixes #1297) (#1302)

Beri76f1630ee737
diff --git a/changelog.d/1297.md b/changelog.d/1297.md
new file mode 100644
index 000000000..b54267971
--- /dev/null
+++ b/changelog.d/1297.md
@@ -0,0 +1,3 @@
+- The turn report names an enemy attack on every turn it engages, a standing one too, not only on the turn it is ordered
+ (#1297). The sim tags each engagement the enemy's units lead (`Cause.EnemyOrder` attack, `Cause.EnemyAttackers`) and
+ no longer tags the attack order itself, so the order turn has one line, not two; no golden moves.
diff --git a/sim/Statecraft.Sim.Tests/EnemyAITests.cs b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
index 87e49b1c5..7df1be026 100644
--- a/sim/Statecraft.Sim.Tests/EnemyAITests.cs
+++ b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
@@ -393,15 +393,17 @@ public class EnemyAITests
Assert.Empty(s.Settlements);
}
- // R-ENEMY-001: each order the enemy AI gives is named on its cause (Cause.EnemyOrder), for the turn report (#1293): a
- // scripted move, an attack and a fortify. The player's own orders, through the same checks, name none.
+ // R-ENEMY-001: what the enemy does is named on its cause (Cause.EnemyOrder), for the turn report (#1293): a scripted
+ // move, an attack (on its engagement, #1297) and a fortify. The player's own orders, through the same checks, name none.
[Fact]
public void EachEnemyOrderIsNamedOnItsCause()
{
var c = Load();
- var why = new List<Cause>();
- EnemyAI.Order(c, CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10)), why);
- Assert.Equal(Combat.Attack, Assert.Single(why).EnemyOrder);
+ var fought = Simulation.Step(c, CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10)), Array.Empty<Command>());
+ Assert.Equal(new[] { Combat.Attack }, fought.Why.Where(x => x.EnemyOrder != "").Select(x => x.EnemyOrder));
+ var mine = Simulation.Step(c, CombatTests.With(c, Soldier(1, 3, 0, 10), Enemy(2, 2, 0, 1)), new Command[] { new UnitOrder(1, Combat.Attack, 2, 0) });
+ Assert.Contains(mine.Why, x => x.Message.StartsWith("The attack on hex 2,0", StringComparison.Ordinal));
+ Assert.DoesNotContain(mine.Why, x => x.EnemyOrder == Combat.Attack);
var r = Simulation.Step(c, CombatTests.With(c, Soldier(1, 0, 0, 5), Enemy(2, 5, 2, 1)),
new Command[] { new Move(1, 1, 0), new UnitOrder(1, Orders.Hold) });
Assert.Equal(new[] { "", "", Orders.Fortify }, r.Why.Where(x => x.System == "map").Select(x => x.EnemyOrder).ToArray());
@@ -409,4 +411,29 @@ public class EnemyAITests
var scripted = Real.StartingForces.Select((f, i) => (f, i)).Where(x => x.f.Script != null).Select(x => x.i.ToString(System.Globalization.CultureInfo.InvariantCulture));
Assert.Equal(scripted, first.Why.Where(x => x.EnemyOrder == Movement.Kind).Select(x => x.Target));
}
+
+ // #1297: a standing attack the AI keeps (R-ENEMY-003) still engages every turn (R-COMBAT-002), so the turn report must
+ // name it on each turn it engages, not only on the turn it is ordered. With no retreat at 2:1, unit 1 holds its hex
+ // and unit 2's attack stands into the second turn.
+ [Fact]
+ public void AStandingEnemyAttackIsNamedOnEveryTurnItEngages()
+ {
+ var files = Fixture.Gdd(new Dictionary<string, string>
+ {
+ ["unit_types.yaml"] = Types, ["map/hexes.json"] = MovementTests.Board(), ["corridors.yaml"] = "corridors: []\n"
+ });
+ files["rules.yaml"] = files["rules.yaml"].Replace("defender_retreats: [1, 1]", "defender_retreats: [0, 0]");
+ var c = ContentLoader.Load(Fixture.Write(files));
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10));
+ for (int turn = 0; turn < 2; turn++)
+ {
+ var r = Simulation.Step(c, s, Array.Empty<Command>());
+ Assert.Equal((Combat.Attack, "3,0"), (r.State.Units[2].Order, r.State.Units[2].OrderTarget));
+ Assert.Equal(4 - turn, r.State.Units[1].Strength); // it engaged this turn
+ var attack = Assert.Single(r.Why, x => x.EnemyOrder == Combat.Attack);
+ Assert.Equal("3,0", attack.Target);
+ Assert.Equal(new[] { 2 }, attack.EnemyAttackers);
+ s = r.State;
+ }
+ }
}
diff --git a/sim/Statecraft.Sim/Cause.cs b/sim/Statecraft.Sim/Cause.cs
index fde95cc63..8cc2f1470 100644
--- a/sim/Statecraft.Sim/Cause.cs
+++ b/sim/Statecraft.Sim/Cause.cs
@@ -1,3 +1,6 @@
+using System;
+using System.Collections.Generic;
+
namespace Statecraft.Sim
{
/// <summary>One line of the "Why did this happen?" report.</summary>
@@ -7,13 +10,19 @@ namespace Statecraft.Sim
public string Target { get; }
public double Delta { get; }
public string Message { get; }
- /// <summary>The order the enemy AI gave unit Target, when this cause records one (R-ENEMY-001): Movement.Kind,
- /// Combat.Attack or Orders.Fortify; else empty. The turn report words it in the player's language (#1293), as Message
- /// is English only. An order the unit already has is kept with no cause (R-ENEMY-003), so it is named only on the turn
- /// it is given.</summary>
+ /// <summary>What the enemy did, when this cause records it (R-ENEMY-001): Movement.Kind or Orders.Fortify for the order
+ /// the enemy AI gave unit Target, or Combat.Attack for an engagement its units led on hex Target (#1297); else empty.
+ /// The turn report words it in the player's language (#1293), as Message is English only. A move or fortify the unit
+ /// already has is kept with no cause (R-ENEMY-003), so it is named only on the turn it is given; an attack is named on
+ /// every turn it engages, as a standing one still does.</summary>
public string EnemyOrder { get; }
- public Cause(string system, string target, double delta, string message, string enemyOrder = "")
- { System = system; Target = target; Delta = delta; Message = PlayerText.Capitalised(message); EnemyOrder = enemyOrder; } // sentence case (playtest 2, P2-6)
+ /// <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)
+ {
+ System = system; Target = target; Delta = delta; Message = PlayerText.Capitalised(message); // sentence case (playtest 2, P2-6)
+ EnemyOrder = enemyOrder; EnemyAttackers = enemyAttackers ?? Array.Empty<int>();
+ }
public override string ToString() => $"[{System}] {Message}";
}
}
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index a061948bb..bfcfba3ae 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -121,7 +121,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}", enemy ? Attack : ""));
+ why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), 0, $"Unit {unitId} will attack hex {target}"));
return null;
}
@@ -269,7 +269,9 @@ namespace Statecraft.Sim
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);
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")));
+ $"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"));
if (row.DefenderRetreats[face] != 1) return;
diff --git a/unity/Assets/Scripts/UI/TurnReportPresenter.cs b/unity/Assets/Scripts/UI/TurnReportPresenter.cs
index fa90aca48..b37942d9a 100644
--- a/unity/Assets/Scripts/UI/TurnReportPresenter.cs
+++ b/unity/Assets/Scripts/UI/TurnReportPresenter.cs
@@ -109,8 +109,9 @@ namespace Statecraft.Game.UI
return lines;
}
- /// <summary>#1293: every order the enemy AI gave in the last turn, as the sim names it on its cause (Cause.EnemyOrder,
- /// R-ENEMY-001), in the sim's order, in the player's language: Message is English only. The unit is named by its row
+ /// <summary>#1293: every order the enemy AI gave in the last turn, and every attack its units made, a standing one too
+ /// (#1297), as the sim names it on its cause (Cause.EnemyOrder, R-ENEMY-001), in the sim's order, in the player's
+ /// language: Message is English only. The unit is named by its row
/// (unit i of a new game is StartingForces[i], R-UNIT-006), in Hebrew by its name_he where it has one.</summary>
static IReadOnlyList<string> EnemyOrders(SimBridge bridge, UiText text, IReadOnlyList<UnitEntry>? units)
{
@@ -123,8 +124,11 @@ namespace Statecraft.Game.UI
if (row == null) return text.Format("report.unit", id);
return hebrew && !string.IsNullOrEmpty(row.NameHe) ? row.NameHe : row.Name;
}
+ // An engagement (#1297) names its enemy attackers, one line each, and gives no line if it names none: its Target is
+ // a hex, not a unit. An order names its unit, Target.
return turn.Why.Where(w => w.EnemyOrder != "")
- .Select(w => text.Format("report.enemy_" + w.EnemyOrder, Name(int.Parse(w.Target, CultureInfo.InvariantCulture)))).ToList();
+ .SelectMany(w => (w.EnemyOrder == Combat.Attack ? w.EnemyAttackers : new[] { int.Parse(w.Target, CultureInfo.InvariantCulture) })
+ .Select(id => text.Format("report.enemy_" + w.EnemyOrder, Name(id)))).ToList();
}
void Show()
diff --git a/unity/Assets/Tests/EditMode/TurnReportTests.cs b/unity/Assets/Tests/EditMode/TurnReportTests.cs
index 4e577d657..96c00ed31 100644
--- a/unity/Assets/Tests/EditMode/TurnReportTests.cs
+++ b/unity/Assets/Tests/EditMode/TurnReportTests.cs
@@ -113,6 +113,44 @@ public class TurnReportTests
}
}
+ /// <summary>#1297: a standing enemy attack (R-ENEMY-003) is reported on every turn it engages, not only on the turn it is
+ /// ordered. The opening turn is set up as a later one: the Arab Liberation Army already attacks Golani's hex, which it keeps
+ /// (no order is given this turn) and which engages in step 2 (R-COMBAT-002).</summary>
+ [Test]
+ public void AStandingEnemyAttackIsReportedOnATurnItIsNotOrdered()
+ {
+ var rows = UnitData.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", UnitData.DefaultFileName)));
+ int golani = C.StartingForces.ToList().FindIndex(f => f.Row == "golani"), ala = C.StartingForces.ToList().FindIndex(f => f.Row == "arab_liberation_army");
+ foreach (var hebrew in new[] { false, true })
+ {
+ var text = hebrew ? He : En;
+ using var k = new Kit(text);
+ k.Report.Units = rows;
+ var s = k.Bridge.Session.State; var g = s.Units[golani]; var a = s.Units[ala];
+ s.Units[golani] = new Unit(g.Id, g.Type, a.Q, a.R - 1, g.Service, g.Side, 1); // next to the ALA, weak enough to attack
+ s.Units[ala] = new Unit(a.Id, a.Type, a.Q, a.R, a.Service, a.Side, a.Strength, Combat.Attack, $"{a.Q},{a.R - 1}");
+ var turn = k.Bridge.EndTurn();
+ Assert.IsFalse(turn.Why.Any(w => w.Message.StartsWith($"Unit {ala} will attack")), "the attack stands: no order this turn");
+ Assert.IsTrue(turn.Why.Any(w => w.Message.StartsWith($"The attack on hex {a.Q},{a.R - 1}")), "and it engages");
+ var lines = k.Ui.Q<Label>("turn-report-causes-" + TurnReportPresenter.EnemyRow).text.Split('\n');
+ CollectionAssert.Contains(lines, hebrew ? "צבא ההצלה פתח בהתקפה על אחת היחידות שלכם" : "Arab Liberation Army launched an attack on one of your units");
+ }
+ }
+
+ /// <summary>#1297 review: an attack cause's Target is a hex ("q,r"), not a unit, so one that names no attackers gives no
+ /// line rather than a parse that throws. Combat fills the attackers for every enemy engagement today, so the turn is
+ /// replaced with one such cause through Session.Last's private setter.</summary>
+ [Test]
+ public void AnAttackThatNamesNoAttackersGivesNoLine()
+ {
+ using var k = new Kit(En);
+ k.Bridge.EndTurn();
+ var last = k.Bridge.Session.Last!;
+ typeof(GameSession).GetProperty(nameof(GameSession.Last))!.SetValue(k.Bridge.Session,
+ new StepResult(last.State, new[] { new Cause("map", "3,0", 0, "The attack on hex 3,0", Combat.Attack) }, last.Rejected));
+ Assert.IsFalse(TurnReportPresenter.Lines(k.Bridge, En).Any(l => l.Meter == TurnReportPresenter.EnemyRow));
+ }
+
[Test]
public void ItCoversTheHudUntilClosedAndThenTheCardIsBack()
{
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index c56aaf897..3dc89d090 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -266,7 +266,8 @@ report.intro: "What changed in the top bar, and the main causes:"
report.line: "{0}: {1}"
report.all: "See all in Why?"
report.close: "Close"
-# The enemy's orders this turn (#1293): one line per order the enemy AI gave (R-ENEMY-001), {0} the unit's name.
+# The enemy's orders this turn (#1293): one line per order the enemy AI gave (R-ENEMY-001), and one per enemy unit in
+# each attack that engaged, a standing one too (#1297); {0} the unit's name.
report.enemy: "What the Arab forces did"
report.enemy_move: "{0} moved to a new position"
report.enemy_attack: "{0} launched an attack on one of your units"