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.

Enemy AI: the attack preview again counts attacks already ordered on the hex (Fixes #2586) (#2596)

Eyal Goland952254b6870
diff --git a/changelog.d/2586-enemy-joint-preview.md b/changelog.d/2586-enemy-joint-preview.md
new file mode 100644
index 000000000..04a17943c
--- /dev/null
+++ b/changelog.d/2586-enemy-joint-preview.md
@@ -0,0 +1,3 @@
+### Fixed
+
+- The enemy again counts its units already ordered on a hex before attacking it.
diff --git a/docs/rules/mvp.md b/docs/rules/mvp.md
index 3581d4990..58c79d81e 100644
--- a/docs/rules/mvp.md
+++ b/docs/rules/mvp.md
@@ -360,7 +360,7 @@ R-ENEMY-001: Resolver step 1, before combat, gives every enemy military unit its
R-ENEMY-002: In Historical mode each starting Arab force (R-UNIT-006) has a script from `map/units_1948.yaml` (Q-ENEMY-1; `research_notes/GDD phase 5/arab-forces-1948.md` section 10). Its 15 May 1948 position is the start. Its script is every anchor of the row from 11 June 1948 on that cites at least two sources other than Wikipedia and Wikidata, each on its hex (`map/unit_anchor_hexes.yaml`), in the file's order. On each turn its target is the first of them whose window has not ended when the turn starts (its `to`; owner, 31 October 2026, #1270 ruling a: "First listed (as today)"): to 8 July 1948 Isdud for Egypt (the forward base, listed before Majdal), Latrun for the Arab Legion, the bridgehead at Mishmar HaYarden for Syria and Jenin for Iraq; the ALA has none. After 8 July only Syria has one, the same bridgehead to the Syrian armistice of 20 July 1949 (#1270, anchor S1; the other forces' anchors past the first truce are #1270's later slices). The unit moves along its cheapest path to the target (R-MOVE-002) as far as its movement points reach. A unit on its target, with no path to it or whose move is refused follows R-ENEMY-003 that turn, and so does every unit from the turn its last window has ended. The script never places a unit anywhere but its own path to a sourced anchor.
-R-ENEMY-003: An enemy unit off its script attacks the first neighbouring hex, in the fixed order (R-MOVE-002), holding a player military unit where the attack preview (`Combat.Preview`, its own strength plus its side's units already ordered on that hex) reaches `mvp.enemy_attack_ratio_percent` (real value 200, the 2:1 row; a labelled design value). Under exact `gdd-0.3` the threshold is the catalogue's `combat.enemy_attack_ratio` instead, 1.5:1 (150): owner, 1 November 2026, O4 on #1668, "1.5:1 in gdd-0.3 (Recommended)" (#2114). A standing attack on that hex is kept. Otherwise it fortifies its hex (R-ORDER-002; a fortify it has is kept). Step 2 resolves its attack in the same turn. Inside a truce it only fortifies (R-ENEMY-006).
+R-ENEMY-003: An enemy unit off its script attacks the first neighbouring hex, in the fixed order (R-MOVE-002), holding a player military unit where the enemy attack preview (`Combat.EnemyPreview`, its own strength plus its side's units that already carry a standing attack on that hex and can still reach it) reaches `mvp.enemy_attack_ratio_percent` (real value 200, the 2:1 row; a labelled design value). Under exact `gdd-0.3` the threshold is the catalogue's `combat.enemy_attack_ratio` instead, 1.5:1 (150): owner, 1 November 2026, O4 on #1668, "1.5:1 in gdd-0.3 (Recommended)" (#2114). A standing attack on that hex is kept. Otherwise it fortifies its hex (R-ORDER-002; a fortify it has is kept). Step 2 resolves its attack in the same turn. Inside a truce it only fortifies (R-ENEMY-006).
R-ENEMY-004: No enemy unit arrives or grows in Phase 5 (Q-ENEMY-3): no turn adds an enemy unit, until a dated per-army figure is chosen under CONTENT_POLICY section 1.
diff --git a/sim/Statecraft.Sim.Tests/EnemyAITests.cs b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
index 5525eed4c..f838e7241 100644
--- a/sim/Statecraft.Sim.Tests/EnemyAITests.cs
+++ b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
@@ -91,7 +91,7 @@ public class EnemyAITests
}
// R-ENEMY-003: with no script, the unit attacks the first adjacent hex with a player unit, in the fixed neighbour order,
- // where Combat.Preview's ratio reaches mvp.enemy_attack_ratio_percent; otherwise it fortifies its hex.
+ // where Combat.EnemyPreview's ratio reaches mvp.enemy_attack_ratio_percent; otherwise it fortifies its hex.
[Theory]
[InlineData(10, Combat.Attack, "3,0")] // 10 against 5: 200%, the threshold
[InlineData(9, Orders.Fortifying, "2,0")] // 180%: below it
@@ -103,6 +103,33 @@ public class EnemyAITests
Assert.Equal((order, target), (s.Units[2].Order, s.Units[2].OrderTarget));
}
+ // R-ENEMY-003: each enemy is only 1:1 against the player unit, but an attack already standing on the target makes the
+ // joining unit's preview 2:1. Step 2 will group those same-target attacks into one engagement.
+ [Fact]
+ public void AUnitJoinsAnAlreadyOrderedAttackWhenTogetherTheyReachTheThreshold()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 3, 1, 5), Enemy(3, 2, 0, 5));
+ Assert.Null(Combat.Give(c, s, 3, 3, 0, new List<Cause>(), enemy: true));
+
+ EnemyAI.Order(c, s, new List<Cause>());
+
+ Assert.Equal((Combat.Attack, "3,0"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ Assert.Equal((Combat.Attack, "3,0"), (s.Units[3].Order, s.Units[3].OrderTarget));
+ }
+
+ // R-ENEMY-003: the same 1:1 unit has no qualifying attack without a same-target standing attacker, so it fortifies.
+ [Fact]
+ public void AUnitAloneBelowTheAttackThresholdFortifies()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 3, 1, 5));
+
+ EnemyAI.Order(c, s, new List<Cause>());
+
+ Assert.Equal((Orders.Fortifying, "3,1"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ }
+
// R-ENEMY-003: the attack resolves in the same turn's step 2, and a unit already fortified keeps its order quietly.
[Fact]
public void TheEnemyAttackResolvesInStepTwo()
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index 779c0d97a..e51f11cf9 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -168,6 +168,25 @@ namespace Statecraft.Sim
return (ratio, Row(mvp, ratio));
}
+ /// <summary>R-ENEMY-003: the enemy AI's attack preview. Unlike the player's <see cref="Preview"/>, it counts the
+ /// unit plus every other unit on its team that already has a standing attack on this hex and can still reach it, as
+ /// resolver step 2 groups them into the same engagement (the preview before #1454).</summary>
+ internal static (int RatioPercent, int Row)? EnemyPreview(Content c, GameState s, int unitId, int q, int r)
+ {
+ if (!s.Units.TryGetValue(unitId, out var u) || !Military(u) || Player(u) || !InReach(c, u, q, r)) return null;
+ var defenders = Defenders(s, u, q, r);
+ if (defenders.Count == 0) return null;
+ var target = Hex(q, r);
+ var attackers = new List<Unit> { u };
+ attackers.AddRange(s.Units.Values
+ .Where(x => x.Id != unitId && x.Order == Attack && x.OrderTarget == target &&
+ Player(x) == Player(u) && Military(x) && InReach(c, x, q, r))
+ .OrderBy(x => x.Id));
+ var mvp = c.RulesAt(s.Date).Mvp!;
+ var ratio = Ratio(c, mvp, attackers, defenders, q, r);
+ return (ratio, Row(mvp, ratio));
+ }
+
/// <summary>The outcome bands of combat_table row <paramref name="row"/> (Q-UI-1: the card shows them beside Preview's
/// ratio): the row's faces with the same outcome taken together, in the order each first appears, each with how many
/// faces of <c>Of</c> give it, the people the attackers and the defenders lose (population units times
diff --git a/sim/Statecraft.Sim/EnemyAI.cs b/sim/Statecraft.Sim/EnemyAI.cs
index 3a97eff74..5021a8919 100644
--- a/sim/Statecraft.Sim/EnemyAI.cs
+++ b/sim/Statecraft.Sim/EnemyAI.cs
@@ -91,7 +91,7 @@ namespace Statecraft.Sim
foreach (var (dq, dr) in truce ? Array.Empty<(int Q, int R)>() : Movement.Neighbours) // a truce: straight to fortify
{
int q = u.Q + dq, r = u.R + dr;
- if (!(Combat.Preview(c, s, u.Id, q, r) is (int ratio, _)) || ratio < threshold) continue;
+ if (!(Combat.EnemyPreview(c, s, u.Id, q, r) is (int ratio, _)) || ratio < threshold) continue;
var target = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1}", q, r);
if (u.Order == Combat.Attack && u.OrderTarget == target) return;
if (Combat.Give(c, s, u.Id, q, r, why, enemy: true) == null) return;