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: the enemy AI, scripted positions (GDD Phase 5 PR 10, part 1; refs #749) (#1263)

Beri07f01f21a32c
diff --git a/changelog.d/sim-gdd-enemy-ai-749.md b/changelog.d/sim-gdd-enemy-ai-749.md
new file mode 100644
index 000000000..070d7fdc7
--- /dev/null
+++ b/changelog.d/sim-gdd-enemy-ai-749.md
@@ -0,0 +1,13 @@
+- GDD Phase 5 PR 10, part 1: the enemy AI (refs #749, #385). Resolver step 1 now gives every Arab unit its orders from
+ the state alone, through the player's own move, attack and fortify checks with the sides swapped (R-ENEMY-001 to
+ R-ENEMY-004). In Historical mode the Egyptian force, the Arab Legion, the Syrian and the Iraqi forces march from
+ their 15 May 1948 start on their 11 June positions in `map/units_1948.yaml` (Isdud, Latrun, the Mishmar HaYarden
+ bridgehead, Jenin), taking only anchors with two sources that are not Wikipedia (research note arab-forces-1948.md,
+ section 10), on the turns before 8 July 1948. Otherwise a unit holds its hex fortified and attacks a neighbouring
+ Israeli unit at 2:1 or better (`mvp.enemy_attack_ratio_percent`, a new labelled design value). No RNG, no enemy
+ supply, no reinforcements. An enemy unit may not enter a locality that is not on `mvp.enemy_held_localities`, so no
+ enemy move ends on an Israeli locality before enemy capture exists. Every golden moves (re-pinned, docs/determinism.md).
+- No enemy attack inside the two truces (11 June to 8 July and 18 July to 15 October 1948; owner, 21 October 2026, #749):
+ on a turn whose middle day falls in a truce every enemy unit's standing attack ends, and the unit only moves on its
+ script or fortifies (R-ENEMY-006). A stopgap by date until the full war state (#750); of the goldens it moves only
+ smoke5 again. The march through the 15 June 1948 turn is the owner's reading (same ruling).
diff --git a/content/rules.yaml b/content/rules.yaml
index e37f75f6f..7c074df93 100644
--- a/content/rules.yaml
+++ b/content/rules.yaml
@@ -195,6 +195,11 @@ mvp:
attacker_loss: [1, 0, 0, 0, 0, 0] # DESIGN VALUE: owner-set balance, not a historical figure (D-G3, Q-COMBAT-3: population units the attackers lose, per face)
defender_loss: [1, 1, 1, 2, 2, 2] # DESIGN VALUE: owner-set balance, not a historical figure (D-G3, Q-COMBAT-3: population units the defenders lose, per face)
defender_retreats: [0, 1, 1, 1, 1, 1] # DESIGN VALUE: owner-set balance, not a historical figure (D-G3, Q-COMBAT-3: 1 where the defenders retreat, per face)
+ # The enemy's simple goals (R-ENEMY-003; Q-ENEMY-2, owner, 15 October 2026, #375: "Take all recommendations"). PROPOSED in
+ # Phase 5 PR 10 (#749) under "Agents propose design values"; the owner may override it. An enemy unit off its script
+ # attacks an adjacent player unit when the attack preview's ratio reaches this, else it holds its hex fortified. 200 is
+ # the 2:1 row of combat_table, the first where the defenders can be made to retreat. Not a historical figure.
+ enemy_attack_ratio_percent: 200 # DESIGN VALUE: owner-set balance, not a historical figure (D-G7, Q-ENEMY-2: the least attack : defence ratio an enemy unit attacks at, percent)
# Prisoners (R-POW-002, R-POW-004, R-POW-005; Q-POW-2, Q-POW-4, Q-POW-5, owner, 15 October 2026, #375). The amounts are
# agent defaults posted on #751 (https://github.com/eyalgolan/statecraft-israel/issues/751#issuecomment-6318787961) for
# the owner to set or accept; none is a historical figure. A prisoner unit eats the ration any population unit eats (one
diff --git a/docs/RELEASING.md b/docs/RELEASING.md
index 5272dfab5..d4b8096b1 100644
--- a/docs/RELEASING.md
+++ b/docs/RELEASING.md
@@ -27,10 +27,10 @@ macOS IL2CPP parity check is not an MVP release gate.
"$UNITY" -batchmode -CI false -GITHUB_ACTIONS false -TF_BUILD false -quit -projectPath unity -buildTarget WebGL -executeMethod Statecraft.Game.EditorTools.Build.Web -compression gzip -shareable -logFile "$PWD/unity-build-web-gz.log"
git checkout -- unity/Assets/Settings unity/ProjectSettings/ProjectSettings.asset
golden_expect=(); while read -r name hash; do [[ -z "$name" || "$name" == \#* ]] || golden_expect+=(--expect "GOLDEN OK $name $hash"); done < sim/Statecraft.Sim.Tests/golden/manifest.txt
- # one GOLDEN OK line per manifest golden (#394), e.g. "GOLDEN OK hash1950 a1ad42117a8523a79cf36cc03f33c63fbe887820d7f6fb89a834f2f952510548"
- # and "GOLDEN OK hash1952 9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40"
+ # one GOLDEN OK line per manifest golden (#394), e.g. "GOLDEN OK hash1950 1392f3eb726172c284555b2bcacc0be3f8d9e33d19306dfc3baf0e2c63fe249b"
+ # and "GOLDEN OK hash1952 cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5"
uv run tools/web_check.py unity/Builds/Web-gzip --query "golden&smoke=5" "${golden_expect[@]}" \
- --expect "SMOKE OK 5 turns 1948-10-15 ed761c3b9c4e9b76520af20080b2a7c8b984a1f44f1d52e5127aae1baa992a8d"
+ --expect "SMOKE OK 5 turns 1948-10-15 11c41014a5ed74f783bc2fa0af99ee86830d162d5e71c883318e36f68f47b9b4"
uv run tools/web_check.py unity/Builds/Web-gzip --startscreen new --timeout 300 --stall 90
uv run tools/web_check.py unity/Builds/Web-gzip --startscreen recording --timeout 300 --stall 90
uv run tools/web_check.py unity/Builds/Web-gzip --startscreen new --query lang=he --timeout 300 --stall 90
@@ -60,7 +60,7 @@ macOS IL2CPP parity check is not an MVP release gate.
"$UNITY" -batchmode -CI false -GITHUB_ACTIONS false -TF_BUILD false -quit -projectPath unity -buildTarget WebGL -executeMethod Statecraft.Game.EditorTools.Build.Web -compression gzip -decompressionFallback -shareable -out Builds/Web-pages -logFile "$PWD/unity-build-web-pages.log"
git checkout -- unity/Assets/Settings unity/ProjectSettings/ProjectSettings.asset
uv run tools/web_check.py unity/Builds/Web-pages --query "golden&smoke=5" "${golden_expect[@]}" \
- --expect "SMOKE OK 5 turns 1948-10-15 ed761c3b9c4e9b76520af20080b2a7c8b984a1f44f1d52e5127aae1baa992a8d"
+ --expect "SMOKE OK 5 turns 1948-10-15 11c41014a5ed74f783bc2fa0af99ee86830d162d5e71c883318e36f68f47b9b4"
uv run tools/web_check.py unity/Builds/Web-pages --startscreen new --timeout 300 --stall 90
uv run tools/web_check.py unity/Builds/Web-pages --startscreen recording --timeout 300 --stall 90
uv run tools/web_check.py unity/Builds/Web-pages --startscreen new --query lang=he --timeout 300 --stall 90
diff --git a/docs/determinism.md b/docs/determinism.md
index 9bf473394..bd5ddd919 100644
--- a/docs/determinism.md
+++ b/docs/determinism.md
@@ -36,9 +36,9 @@ final `GameState.Hash()` is pinned:
| Run | Ends | Hash |
|---|---|---|
-| to 1950-01-01 (`Hash1950`) | 1950-01-15, turn 20 | `a1ad42117a8523a79cf36cc03f33c63fbe887820d7f6fb89a834f2f952510548` |
-| to 1952-01-01 (`Hash1952`) | 1952-01-15, turn 44 (the game's end) | `9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40` |
-| to the game's end (`mvp-end`) | 1952-01-15, turn 44 | `9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40` |
+| to 1950-01-01 (`Hash1950`) | 1950-01-15, turn 20 | `1392f3eb726172c284555b2bcacc0be3f8d9e33d19306dfc3baf0e2c63fe249b` |
+| to 1952-01-01 (`Hash1952`) | 1952-01-15, turn 44 (the game's end) | `cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5` |
+| to the game's end (`mvp-end`) | 1952-01-15, turn 44 | `cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5` |
`hash1952` stops at the first turn on or after 1 January 1952, which is the game's last turn, so it is the same run as
`mvp-end` (`DeterminismTests.TheWholeGameToItsEndIsFixed`). Both names are kept (spec 2026-10-12 section 3 step 6): a
@@ -47,7 +47,7 @@ player check enumerate the manifest, so it and every scenario are checked withou
A further value is pinned for the browser smoke run (`?smoke=5`: the golden seed, five turns played through the UI's own
buttons, answering each event with its first option while the turn's Attention pays for it, the rest left to their
-ministers): `SMOKE OK 5 turns 1948-10-15 ed761c3b9c4e9b76520af20080b2a7c8b984a1f44f1d52e5127aae1baa992a8d`.
+ministers): `SMOKE OK 5 turns 1948-10-15 11c41014a5ed74f783bc2fa0af99ee86830d162d5e71c883318e36f68f47b9b4`.
It depends on the UI path as well as the sim (which option is first, the turn grid), so re-record it when either changes.
Check every WebGL build with every manifest entry (plus the UI-path smoke line) with:
@@ -58,7 +58,7 @@ while read -r name hash; do
golden_expect+=(--expect "GOLDEN OK $name $hash")
done < sim/Statecraft.Sim.Tests/golden/manifest.txt
uv run tools/web_check.py unity/Builds/Web-<compression> --query "golden&smoke=5" "${golden_expect[@]}" \
- --expect "SMOKE OK 5 turns 1948-10-15 ed761c3b9c4e9b76520af20080b2a7c8b984a1f44f1d52e5127aae1baa992a8d"
+ --expect "SMOKE OK 5 turns 1948-10-15 11c41014a5ed74f783bc2fa0af99ee86830d162d5e71c883318e36f68f47b9b4"
```
**When a golden moves.** The test fails with the lines that changed, grouped by section (`[ind]`, `[pol]`, `[bud]`,
@@ -350,3 +350,11 @@ The hash reads the state keys, so it never sees a record outside its window. An
| #1240: the seven IDF rows re-sourced to Ben-Gurion's diary of 15 May 1948, owner 21 Oct 2026 | scenario-cautious | `00ec7ec2` | same as reckless (was `b4e5caa1`) |
| #1240: the seven IDF rows re-sourced to Ben-Gurion's diary of 15 May 1948, owner 21 Oct 2026 | scenario-mvp-absorb-first | `ac5f51bb` | same as reckless (was `7d37c0d0`) |
| #1240: the seven IDF rows re-sourced to Ben-Gurion's diary of 15 May 1948, owner 21 Oct 2026 | scenario-mvp-defeat | `f2ad1114` | same as reckless; the defeat is unchanged (was `7bd5158f`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | 1950 | `1392f3eb` | Resolver step 1 gives the enemy its orders (R-ENEMY-001 to R-ENEMY-004). On 15 May and 15 June 1948 the four scripted forces march on their 11 June anchors (Egypt 7 to -8,25, one hex short of Isdud, round Yad Mordechai and the Negev brigade; the Arab Legion 8 to Latrun, -2,22; Syria 9 to 13,6, one short of the Mishmar HaYarden bridgehead; Iraq 10 to Jenin, 6,13), and every enemy unit fortifies from then on (`order=fortify`). The Syrian unit leaves Golani's side on the first turn (13,6 by the second), so no Israeli attack is ordered there; since #1240 none was (Golani's 4 against the Syrian 3 is under the policy's 3:2), and the Arms stay at 12. No enemy unit attacks on a turn whose middle day is inside a truce (R-ENEMY-006: 15 June to 15 September 1948). On the turn of 15 October 1948 the Legion attacks Harel beside Latrun at 4:1: one engagement, one draw, unit 5 is destroyed and the Legion is at 4. It is the run's only engagement, so the RNG state moves off the seed the header showed (was `a1ad4211`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | 1952 | `cc96cd0a` | same (was `9cbd73ed`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | mvp-end | `cc96cd0a` | same as 1952 (was `9cbd73ed`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | smoke5 | `11c41014` | the enemy's step 1 orders from the first turn, as in 1950. The run ends on 15 October 1948, inside the truce guard's reach (R-ENEMY-006), so Harel survives it: the Legion fortifies on the turns of 15 June to 15 September 1948 (was `ed761c3b`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | scenario-cautious | `230791a1` | the units only, as in 1950: the scripted marches, the fortify orders and the Legion's engagement with Harel (was `00ec7ec2`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | scenario-reckless | `d6fd9e89` | same as cautious (was `574c8a19`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | scenario-mvp-absorb-first | `de176931` | same as cautious (was `ac5f51bb`) |
+| GDD Phase 5 PR 10: the enemy AI, part 1 (#749) | scenario-mvp-defeat | `7404295a` | the units as in cautious; the engagement's draw comes before the delegated events' draws, so a delegated `iraqi_exodus` resolves to `pace` (was `airlift`), and housing, international standing, morale and stability follow. The defeat is unchanged (was `f2ad1114`) |
diff --git a/docs/rules/mvp.md b/docs/rules/mvp.md
index fd7437a4e..05273e36d 100644
--- a/docs/rules/mvp.md
+++ b/docs/rules/mvp.md
@@ -212,6 +212,20 @@ R-COMBAT-007: Artillery (a type of class `artillery`, which needs a `range` and
R-COMBAT-008: A military type may carry a `combat_percent` (at least 1; none is 100): its units' strength counts at that percent in the ratio, attacking and defending, in integers (R-COMBAT-002). The armoured car's is 150 (real value), with its 5 movement points (R-MOVE-001) and no other rule (Q-ARTY-2).
+## The enemy
+
+Phase 5 PR 10, part 1 (#749). Q-ENEMY-1 to Q-ENEMY-4 are the plan's recommendations (owner, 15 October 2026, #375: "Take all recommendations"). The march's turns (through the 15 June 1948 turn, until the first truce ends on 8 July) are the owner's (owner, 21 October 2026, #749); so is the truce guard (R-ENEMY-006, same ruling). The scripted anchor's choice, the goal order, `mvp.enemy_attack_ratio_percent` and the truce guard's turn-middle reading are agent defaults proposed on #749 and #1263. The operation cards' orders (Q-ENEMY-5, the plan's R-ENEMY-005) are part 2.
+
+R-ENEMY-001: Resolver step 1, before combat, gives every enemy military unit its orders, in ascending unit id, from the state alone: two equal states get the same orders. They go through the player's own checks with the sides swapped (`Movement.Move`, `Combat.Give`, `Orders.Give`), and a refused order changes nothing. So an enemy unit may not enter a hex holding a player unit, water or a full hex (R-MOVE-003), and, as R-MOVE-005's mirror, no locality's hex that is not on `mvp.enemy_held_localities`: while enemy capture (R-CTRL-006) is unbuilt, no enemy move ends on a player's locality. Its attacks pay no Arms (Q-ENEMY-4: no enemy supply). Step 1 draws nothing from the RNG (Q-ENEMY-2).
+
+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 target is the row's first anchor from 11 June 1948 that cites at least two sources other than Wikipedia and Wikidata, on that anchor's hex (`map/unit_anchor_hexes.yaml`): Isdud for Egypt (the forward base), Latrun for the Arab Legion, the bridgehead at Mishmar HaYarden for Syria and Jenin for Iraq; the ALA has none. On each turn that starts before the anchor's window ends (its `to`, 8 July 1948 for all four), 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 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). 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.
+
+R-ENEMY-006: No enemy unit attacks inside a truce window (owner, 21 October 2026, #749): the first truce from 11 June to 8 July 1948 and the second from 18 July to 15 October 1948, half-open, the dates of `events/first_truce.yaml`, `events/second_truce.yaml`, `events/ten_days.yaml` and `events/operation_yoav.yaml` (the same windows as `map/control_hexes_1948_49.yaml`'s truce periods). A turn is in a truce when its middle day is (agent default): a monthly turn spans a truce's edge, and the middle puts it on the side that holds most of its days. So the turns of 15 June to 15 September 1948 are truce turns, and those of 15 May and 15 October 1948 are not. On a truce turn every enemy unit's standing attack ends first, whatever the unit then does (owner, #1263): it may still move on its script (R-ENEMY-002), and a unit off its script fortifies (R-ENEMY-003's else branch). The guard is by date alone, in every mode, whatever the truce cards' answers: #750 replaces it with the full war state.
+
## Prisoners
Phase 5 PR 12 (#751). The impact plan's §4.3 as written (Q-POW-1), points per unit returned (Q-POW-2), a sourced event card per front (Q-POW-3), the player's soldiers counted with a cost (Q-POW-4) and a Food cost whose shortfall costs cohesion (Q-POW-5): the owner's picks of 15 October 2026 (#375). The four amounts (`rules.yaml` `mvp.food_per_prisoner_unit`, `mvp.prisoner_shortfall_cohesion`, `mvp.points_per_prisoner_unit_returned`, `mvp.held_soldier_cost_per_turn`), the front of each side and the captor of the player's soldiers are agent defaults posted on #751 (https://github.com/eyalgolan/statecraft-israel/issues/751#issuecomment-6318787961) for the owner to set or accept. The four sourced exchange cards are not in `content/` yet: they wait for a person's source check (CONTENT_POLICY §2) and the owner's sensitivity review.
diff --git a/sim/Statecraft.Sim.Tests/CombatTests.cs b/sim/Statecraft.Sim.Tests/CombatTests.cs
index 0bc0f16f5..a9397895d 100644
--- a/sim/Statecraft.Sim.Tests/CombatTests.cs
+++ b/sim/Statecraft.Sim.Tests/CombatTests.cs
@@ -259,10 +259,13 @@ public class CombatTests
var r = Step(c, s, new UnitOrder(1, Combat.Attack, 2, 0));
Assert.Equal((8, 4), (r.State.Units[1].Strength, r.State.Units[2].Strength)); // 9 against 5, below 2:1
Assert.Equal(Combat.Attack, r.State.Units[1].Order);
- r = Step(c, r.State); // no order: the attack stands
+ r = Step(c, r.State); // no order: the attack stands, against a unit its AI fortified on turn 1 (R-ENEMY-003)
+ Assert.Equal((7, 3, 2, 0), (r.State.Units[1].Strength, r.State.Units[2].Strength, r.State.Units[2].Q, r.State.Units[2].R)); // 8 against 4 x 150%, below 2:1
+ Assert.Equal(Combat.Attack, r.State.Units[1].Order);
+ r = Step(c, Step(c, r.State).State); // 7 against 3 x 150%, below 2:1; then 6 against 2 x 150%, 2:1
var e = r.State.Units[2];
- Assert.Equal((3, 1, 0), (e.Strength, e.Q, e.R)); // 8 against 4, 2:1: lost 1 and retreated
- Assert.Equal(s.Arms - 4, r.State.Arms);
+ Assert.Equal((1, 1, 0), (e.Strength, e.Q, e.R)); // lost 1 and retreated
+ Assert.Equal(s.Arms - 8, r.State.Arms);
Assert.Equal("", r.State.Units[1].Order);
Assert.Contains(r.Why, x => x.Message == "Unit 1's attack on hex 2,0 ends: no enemy unit is there");
}
@@ -308,10 +311,10 @@ public class CombatTests
Assert.Equal(7, s.Score);
}
- // R-COMBAT-005: over the reference war run (the goldens' run to the game's end), step 2 run on each turn's end state, with
- // its standing attacks, changes only the units, the Arms and the RNG. The run has engagements, so the test is not empty:
- // since #1240 the real start has none (Golani's 4 against the Syrian 3 is under the policy's 3:2), so unit 0 starts at
- // its old 5 (ReferencePlayerTests.AtFive).
+ // R-COMBAT-005, R-ENEMY-001: over the reference war run (the goldens' run to the game's end), steps 1 and 2 run on each
+ // turn's end state, with its standing attacks and the enemy's orders, change only the units, the Arms and the RNG. The run
+ // has engagements, so the test is not empty: since #1240 the real start has none (Golani's 4 against the Syrian 3 is under
+ // the policy's 3:2), so unit 0 starts at its old 5 (ReferencePlayerTests.AtFive).
[Fact]
public void CombatTouchesOnlyCombatants()
{
@@ -322,6 +325,7 @@ public class CombatTests
{
s = Simulation.Step(c, s, GoldenRun.Orders(c, s)).State;
var after = s.Clone();
+ EnemyAI.Order(c, after, new List<Cause>());
engagements += Resolve(c, after).Count(x => x.Message.StartsWith("The attack on hex", StringComparison.Ordinal));
AssertOnlyCombatantsChanged(s, after);
}
diff --git a/sim/Statecraft.Sim.Tests/DesignValuesTests.cs b/sim/Statecraft.Sim.Tests/DesignValuesTests.cs
index 6b71b79e2..8fa2b1208 100644
--- a/sim/Statecraft.Sim.Tests/DesignValuesTests.cs
+++ b/sim/Statecraft.Sim.Tests/DesignValuesTests.cs
@@ -62,6 +62,7 @@ public class DesignValuesTests
("rules.yaml", "attention_cost.unit_order"), // R-ORDER-001, #746 (D-A6)
("rules.yaml", "mvp.delegation_loyalty_floor"), // R-EVT-003, #107
("rules.yaml", "mvp.disloyal_effect_share"),
+ ("rules.yaml", "mvp.enemy_attack_ratio_percent"), // R-ENEMY-003, #749 (Q-ENEMY-2)
// Q-START-1 (owner, 15 October 2026, #375): no city has a sourced housing stock or job count, so every city's
// housing is its start people and its job slots its start employed; seven towns with no CBS working share take
// the national one. The census people and the five CBS shares are sourced, not design values (R-CITY-006).
diff --git a/sim/Statecraft.Sim.Tests/EnemyAITests.cs b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
new file mode 100644
index 000000000..fb2b08045
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/EnemyAITests.cs
@@ -0,0 +1,394 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Statecraft.Sim;
+
+/// <summary>The enemy AI, resolver step 1 (GDD Phase 5 PR 10, part 1; #749). Q-ENEMY-1 to Q-ENEMY-4 are the plan's
+/// recommendations, taken by the owner on 15 October 2026 ("Take all recommendations", #375): script the 15 May and 11 June
+/// 1948 positions two sources agree on (arab-forces-1948.md section 10), then the simple goals (hold fortified, attack at a
+/// design ratio), no RNG, no reinforcements, no enemy supply. The fixture's numbers are fixture values: MovementTests' 6 x 3
+/// coastal plain, 10 people a population unit, two units of a side per hex and an attack ratio of 200.</summary>
+public class EnemyAITests
+{
+ const string Types = Fixture.GddUnitTypes
+ + " - { id: soldier, kind: unit, side: israel, class: infantry, movement_points: 3 }\n"
+ + " - { id: enemy, kind: unit, side: egypt, class: infantry, movement_points: 3 }\n";
+
+ static Content Load() => ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>
+ {
+ ["unit_types.yaml"] = Types, ["map/hexes.json"] = MovementTests.Board(), ["corridors.yaml"] = "corridors: []\n"
+ })));
+
+ static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());
+
+ static Unit Soldier(int id, int q, int r, int strength) => new Unit(id, "soldier", q, r, side: "israel", strength: strength);
+ static Unit Enemy(int id, int q, int r, int strength) => new Unit(id, "enemy", q, r, side: "egypt", strength: strength);
+
+ static bool IsEnemy(Unit u) => u.Side != "" && u.Side != Movement.PlayerSide;
+
+ static int Distance(int q0, int r0, int q1, int r1)
+ {
+ int dq = q1 - q0, dr = r1 - r0;
+ return (Math.Abs(dq) + Math.Abs(dr) + Math.Abs(dq + dr)) / 2;
+ }
+
+ static GameState Play(Content c, GameState s, int turns)
+ {
+ for (int i = 0; i < turns && s.GameOver == null; i++) s = Simulation.Step(c, s, Array.Empty<Command>()).State;
+ return s;
+ }
+
+ // R-ENEMY-001: the enemy's orders come from the state alone. Two states equal in every field give the same orders, and the
+ // enemy unit acts (here it holds its hex, fortified, as its ratio is below the threshold: Q-ENEMY-2).
+ [Fact]
+ public void OrdersComeOnlyFromTheState()
+ {
+ var c = Load();
+ GameState Make() => CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 3));
+ var a = Simulation.Step(c, Make(), Array.Empty<Command>()).State;
+ var b = Simulation.Step(c, Make(), Array.Empty<Command>()).State;
+ Assert.Equal(a.Hash(), b.Hash());
+ Assert.Equal((Orders.Fortify, "2,0"), (a.Units[2].Order, a.Units[2].OrderTarget));
+ }
+
+ // R-ENEMY-001: an enemy order goes through the player's validation, with the sides swapped, and a refused one leaves the
+ // state as it was. The locality rule is R-MOVE-005's mirror: no enemy unit enters a locality off enemy_held_localities.
+ [Fact]
+ public void AnInvalidEnemyOrderIsRefusedLikeThePlayers()
+ {
+ var c = Load(); // town_a, a player's locality here (enemy_held_localities is empty), is on (1, 0)
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 3), Enemy(3, 2, 2, 1), Enemy(4, 2, 2, 1), Enemy(5, 5, 2, 1));
+ var before = s.Hash();
+ var spent = new Dictionary<int, int>();
+ var why = new List<Cause>();
+ Assert.Equal("That unit is not yours", Movement.Move(c, s, 1, 4, 0, spent, why, enemy: true));
+ Assert.Equal("An enemy unit holds that hex", Movement.Move(c, s, 2, 3, 0, spent, why, enemy: true));
+ Assert.Equal("Units may not enter an enemy-held locality before its capture is possible", Movement.Move(c, s, 2, 1, 0, spent, why, enemy: true));
+ Assert.Equal("Units cannot enter water or leave the map", Movement.Move(c, s, 2, 2, -1, spent, why, enemy: true));
+ Assert.Equal("That hex already holds 2 units of that side, the most one hex can hold", Movement.Move(c, s, 5, 2, 2, spent, why, enemy: true));
+ Assert.Equal("That unit is not yours", Combat.Give(c, s, 1, 2, 0, why, enemy: true));
+ Assert.Equal("That hex is not next to the unit", Combat.Give(c, s, 3, 3, 0, why, enemy: true));
+ Assert.Equal("That unit is not yours", Orders.Give(c, s, 1, Orders.Fortify, why, enemy: true));
+ Assert.Equal(before, s.Hash());
+ Assert.Empty(why);
+ }
+
+ // R-ENEMY-001: an enemy attack pays no Arms (Q-ENEMY-4) and an enemy unit cannot be ordered by the player.
+ [Fact]
+ public void AnEnemyAttackPaysNoArms()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 3));
+ s.Arms = 0;
+ Assert.Null(Combat.Give(c, s, 2, 3, 0, new List<Cause>(), enemy: true));
+ Assert.Equal((Combat.Attack, "3,0"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ var fought = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10)); // the AI attacks at 2:1 in step 1
+ var arms = fought.Arms;
+ var next = Simulation.Step(c, fought, Array.Empty<Command>()).State;
+ Assert.Equal(4, next.Units[1].Strength); // the engagement happened
+ Assert.Equal(arms, next.Arms); // and the enemy paid nothing for it
+ Assert.Equal("That unit is not yours", Assert.Single(Simulation.Step(c, s, new Command[] { new UnitOrder(2, Orders.Fortify) }).Rejected).Reason);
+ }
+
+ // 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.
+ [Theory]
+ [InlineData(10, Combat.Attack, "3,0")] // 10 against 5: 200%, the threshold
+ [InlineData(9, Orders.Fortifying, "2,0")] // 180%: below it
+ public void TheGoalsAttackAtTheThresholdElseFortify(int strength, string order, string target)
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, strength));
+ EnemyAI.Order(c, s, new List<Cause>());
+ Assert.Equal((order, target), (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()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10));
+ var next = Simulation.Step(c, s, Array.Empty<Command>()).State;
+ Assert.Equal(4, next.Units[1].Strength); // the fixture's 2:1 row: the defender loses 1 and retreats
+ Assert.NotEqual((3, 0), (next.Units[1].Q, next.Units[1].R));
+ var held = CombatTests.With(c, Enemy(2, 2, 0, 3));
+ held = Simulation.Step(c, held, Array.Empty<Command>()).State;
+ var why = new List<Cause>();
+ EnemyAI.Order(c, held, why);
+ Assert.Equal(Orders.Fortify, held.Units[2].Order);
+ Assert.Empty(why);
+ }
+
+ // R-ENEMY-003: a standing attack is kept. Step 1 on the next turn finds the unit already attacking the same hex and leaves
+ // the order as it is; giving it again would be refused, and the unit would fortify instead.
+ [Fact]
+ public void AStandingAttackIsKeptOnTheNextTurn()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10));
+ var why = new List<Cause>();
+ EnemyAI.Order(c, s, why);
+ Assert.Equal((Combat.Attack, "3,0"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ EnemyAI.Order(c, s, why); // the next turn's step 1, the attack still standing
+ Assert.Equal((Combat.Attack, "3,0"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ }
+
+ // R-ENEMY-001: the enemy orders in ascending unit id. Two scripted units march on the same hex, which has one slot left
+ // (the fixture's two units of a side a hex): unit 0 takes it, and unit 1's move is refused, so it fortifies where it is.
+ [Fact]
+ public void TheLowerIdTakesTheLastStackSlot()
+ {
+ string Row(string id) => "- id: " + id + "\n service: army\n side: egypt\n status: draft\n strength: { value: 30, as_of: '1948-05-15' }\n anchors:\n"
+ + " - { place: Start, lon: 34.29, lat: 33.35, from: '1948-05-15', to: '1948-06-11' }\n"
+ + " - { place: June, lon: 34.29, lat: 33.35, from: '1948-06-11', to: '1948-07-08', sources: [{ url: 'https://example.org/a', checked: '2026-10-13' }, { url: 'https://example.org/b', checked: '2026-10-13' }] }\n";
+ var c = ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>
+ {
+ ["unit_types.yaml"] = Types,
+ ["map/hexes.json"] = MovementTests.Board(),
+ ["corridors.yaml"] = "corridors: []\n",
+ ["map/units_1948.yaml"] = "units:\n" + Row("first") + Row("second"),
+ ["map/unit_anchor_hexes.yaml"] = "anchors:\n - { id: first, anchor: 0, q: 2, r: 2 }\n - { id: first, anchor: 1, q: 3, r: 2 }\n"
+ + " - { id: second, anchor: 0, q: 4, r: 2 }\n - { id: second, anchor: 1, q: 3, r: 2 }\n",
+ })));
+ var s = CombatTests.With(c, Enemy(2, 3, 2, 1)); // units 0 and 1 are the two rows; unit 2 already holds one slot
+ Assert.Equal((2, 2), (s.Units[0].Q, s.Units[0].R));
+ Assert.Equal((4, 2), (s.Units[1].Q, s.Units[1].R));
+ EnemyAI.Order(c, s, new List<Cause>());
+ Assert.Equal((3, 2), (s.Units[0].Q, s.Units[0].R));
+ Assert.Equal((4, 2, Orders.Fortifying), (s.Units[1].Q, s.Units[1].R, s.Units[1].Order));
+ }
+
+ // Q-ENEMY-2: no RNG in the AI. Step 1 leaves RngState as it found it.
+ [Fact]
+ public void NoRngInTheAI()
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10), Enemy(3, 4, 0, 3));
+ var rng = s.RngState;
+ EnemyAI.Order(c, s, new List<Cause>());
+ Assert.Equal(rng, s.RngState);
+ var real = Simulation.NewGame(Real, 1, Mode.Historical);
+ rng = real.RngState;
+ EnemyAI.Order(Real, real, new List<Cause>());
+ Assert.Equal(rng, real.RngState);
+ }
+
+ // R-ENEMY-002: the script never invents a position. Each scripted target is its row's 11 June 1948 anchor that cites at
+ // least two sources that are not Wikipedia or Wikidata, on that anchor's hex (unit_anchor_hexes.yaml); the ALA has none.
+ [Fact]
+ public void TheScriptNeverInventsAPosition()
+ {
+ var scripted = Real.StartingForces.Where(f => f.Script != null)
+ .Select(f => (f.Row, f.Script!.Value.Q, f.Script!.Value.R, f.ScriptUntil)).OrderBy(x => x.Row, StringComparer.Ordinal).ToList();
+ var until = new DateTime(1948, 7, 8); // the four anchors' window end: the first truce's (units_1948.yaml)
+ Assert.Equal(new[]
+ {
+ ("arab_legion", -2, 22, until), // Latrun
+ ("egyptian_expeditionary_force", -7, 24, until), // Isdud, the forward base (the row's first 11 June anchor)
+ ("iraqi_expeditionary_force", 6, 13, until), // Jenin
+ ("syrian_army", 13, 5, until), // the bridgehead at Mishmar HaYarden
+ }, scripted);
+ Assert.All(Real.StartingForces.Where(f => f.Side == Movement.PlayerSide), f => Assert.Null(f.Script));
+ }
+
+ // R-ENEMY-002: an 11 June anchor on one non-Wikipedia source (with Wikipedia beside it) is not scripted; two are.
+ [Theory]
+ [InlineData("['https://example.org/a', 'https://en.wikipedia.org/wiki/Isdud', 'https://www.wikidata.org/wiki/Q1']", false)]
+ [InlineData("['https://example.org/a', 'https://example.org/b']", true)]
+ public void AScriptedAnchorNeedsTwoSources(string urls, bool scripted)
+ {
+ var sources = string.Join(", ", urls.Trim('[', ']').Split(", ").Select(u => "{ url: " + u + ", checked: '2026-10-13' }"));
+ var row = "- id: force\n service: army\n side: egypt\n status: draft\n strength: { value: 30, as_of: '1948-05-15' }\n anchors:\n"
+ + " - { place: Start, lon: 34.29, lat: 33.35, from: '1948-05-15', to: '1948-06-11' }\n"
+ + " - { place: June, lon: 34.29, lat: 33.35, from: '1948-06-11', to: '1948-07-08', sources: [" + sources + "] }\n";
+ var c = ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>
+ {
+ ["unit_types.yaml"] = Types,
+ ["map/hexes.json"] = MovementTests.Board(),
+ ["corridors.yaml"] = "corridors: []\n",
+ ["map/units_1948.yaml"] = "units:\n" + row,
+ ["map/unit_anchor_hexes.yaml"] = "anchors:\n - { id: force, anchor: 0, q: 5, r: 0 }\n - { id: force, anchor: 1, q: 3, r: 2 }\n",
+ })));
+ var f = Assert.Single(c.StartingForces);
+ Assert.Equal(scripted ? (3, 2) : ((int, int)?)null, f.Script);
+ }
+
+ // R-ENEMY-002: in the real game each scripted unit marches on its 11 June hex along its cheapest path (round the player's
+ // units and localities), as far as its movement points reach, on the turns that start before the anchor's window ends
+ // (15 May and 15 June). With 3 points a turn each ends on its hex or next to it. From the turn of 15 July the AI moves
+ // no unit.
+ [Fact]
+ public void AScriptedUnitMarchesToItsJuneAnchorThenFollowsTheGoals()
+ {
+ var c = Real;
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var targets = c.StartingForces.Select((f, i) => (f, i)).Where(x => x.f.Script != null)
+ .ToDictionary(x => x.i, x => x.f.Script!.Value);
+ Assert.Equal(4, targets.Count);
+ var first = Simulation.Step(c, s, Array.Empty<Command>());
+ foreach (var id in targets.Keys)
+ Assert.Contains(first.Why, x => x.Message.StartsWith($"Unit {id} moved to hex", StringComparison.Ordinal));
+ var june = Play(c, first.State, 1);
+ foreach (var (id, hex) in targets.Select(t => (t.Key, t.Value)))
+ Assert.True(Distance(june.Units[id].Q, june.Units[id].R, hex.Q, hex.R) <= 1, $"unit {id} is not at or next to its 11 June hex");
+ Assert.Equal(new DateTime(1948, 7, 15), june.Date);
+ var why = new List<Cause>();
+ var positions = june.Units.Values.Select(u => (u.Id, u.Q, u.R)).ToList();
+ EnemyAI.Order(c, june, why);
+ Assert.Equal(positions, june.Units.Values.Select(u => (u.Id, u.Q, u.R)).ToList());
+ Assert.DoesNotContain(why, x => x.Message.Contains("moved to hex", StringComparison.Ordinal));
+ }
+
+ // R-ENEMY-002: the script is Historical mode's only; another mode's units follow the goals from the first turn.
+ [Fact]
+ public void OnlyAHistoricalGameFollowsTheScript()
+ {
+ var s = Simulation.NewGame(Real, 1, Mode.Historical);
+ s.Mode = Mode.Open;
+ var positions = s.Units.Values.Select(u => (u.Id, u.Q, u.R)).ToList();
+ EnemyAI.Order(Real, s, new List<Cause>());
+ Assert.Equal(positions, s.Units.Values.Select(u => (u.Id, u.Q, u.R)).ToList());
+ }
+
+ // R-ENEMY-002: a scripted move that cannot be made (a player unit holds the 11 June hex) falls back to the goals.
+ [Fact]
+ public void ABlockedScriptFallsBackToTheGoals()
+ {
+ var c = Real;
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var legion = c.StartingForces.FindIndex(f => f.Row == "arab_legion");
+ var latrun = c.StartingForces[legion].Script!.Value;
+ var id = s.NextId++;
+ s.Units.Add(id, new Unit(id, StartingForces.IsraeliBrigadeType, latrun.Q, latrun.R, side: "israel", strength: 1));
+ var before = (s.Units[legion].Q, s.Units[legion].R);
+ var why = new List<Cause>();
+ EnemyAI.Order(c, s, why);
+ var u = s.Units[legion];
+ Assert.Equal(before, (u.Q, u.R));
+ Assert.Contains(u.Order, new[] { Orders.Fortifying, Combat.Attack });
+ }
+
+ // R-ENEMY-006: no enemy attack inside a truce window. In the reference run Harel (unit 5, by Sha'ar HaGai) stands next
+ // to the Arab Legion at Latrun. The turn of 15 July 1948 runs to 15 August, its middle (30 July) inside the second truce,
+ // so the Legion fortifies and Harel is still there on 15 August. Without the guard the Legion destroyed it at 4:1 on
+ // that turn.
+ [Fact]
+ public void TheArabLegionDoesNotAttackHarelAtLatrunInTheSecondTruce()
+ {
+ var s = GoldenRun.Play(Real, new DateTime(1948, 7, 15));
+ Assert.Equal(new DateTime(1948, 7, 15), s.Date);
+ var legion = Real.StartingForces.FindIndex(f => f.Row == "arab_legion");
+ var harel = Real.StartingForces.FindIndex(f => f.Row == "harel");
+ Assert.Equal((-2, 22), (s.Units[legion].Q, s.Units[legion].R)); // the Legion on Latrun
+ Assert.Equal(1, Distance(s.Units[legion].Q, s.Units[legion].R, s.Units[harel].Q, s.Units[harel].R));
+ var strength = s.Units[harel].Strength;
+ var step = Simulation.Step(Real, s, GoldenRun.Orders(Real, s));
+ Assert.Equal(new DateTime(1948, 8, 15), step.State.Date);
+ Assert.DoesNotContain(step.Why, x => x.Message.StartsWith($"Unit {legion} will attack", StringComparison.Ordinal));
+ Assert.True(step.State.Units.ContainsKey(harel), "Harel was destroyed");
+ Assert.Equal(strength, step.State.Units[harel].Strength);
+ Assert.Equal(Orders.Fortify, step.State.Units[legion].Order);
+ }
+
+ // R-ENEMY-006: a standing attack ends on a truce turn even when the unit moves on its script (owner's repro, #1263). On
+ // the 15 May 1948 turn Givati (unit 4) goes to -7,24 and the Negev (unit 6) to -10,27, and Egypt (unit 7) attacks the
+ // Negev. On the 15 June turn Givati goes back to -5,23, Egypt marches on its script to -9,27, and without the guard its
+ // standing attack destroyed the Negev at 900% inside the first truce. The golden seed: the Negev survives the first turn
+ // at 2, its start before #1240 (Ben-Gurion's diary gives it 1, which the first turn's attack destroys), so it is set here.
+ [Fact]
+ public void AScriptedMoveDoesNotCarryAnAttackIntoTheFirstTruce()
+ {
+ var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
+ Assert.Equal(new DateTime(1948, 5, 15), s.Date);
+ Assert.Equal(("givati", "negev", "egyptian_expeditionary_force"),
+ (Real.StartingForces[4].Row, Real.StartingForces[6].Row, Real.StartingForces[7].Row));
+ var negev = s.Units[6];
+ Assert.Equal(1, negev.Strength);
+ s.Units[6] = new Unit(negev.Id, negev.Type, negev.Q, negev.R, negev.Service, negev.Side, 2, negev.Order, negev.OrderTarget);
+ var first = Simulation.Step(Real, s, new Command[] { new Move(4, -7, 24), new Move(6, -10, 27) });
+ Assert.Contains(first.Why, x => x.Message == "Unit 7 will attack hex -10,27");
+ s = first.State;
+ Assert.Equal(new DateTime(1948, 6, 15), s.Date);
+ Assert.True(s.Units.ContainsKey(6), "the Negev fell on the 15 May turn");
+ Assert.Equal((Combat.Attack, "-10,27"), (s.Units[7].Order, s.Units[7].OrderTarget)); // the attack stands
+ var strength = s.Units[6].Strength;
+ var step = Simulation.Step(Real, s, new Command[] { new Move(4, -5, 23) });
+ Assert.Equal((-9, 27), (step.State.Units[7].Q, step.State.Units[7].R)); // Egypt moved on its script
+ Assert.True(step.State.Units.ContainsKey(6), "the Negev was destroyed");
+ Assert.Equal(strength, step.State.Units[6].Strength);
+ }
+
+ // R-ENEMY-006: the truce windows are the sourced ones, half-open: the first truce from 11 June to 8 July 1948 and the
+ // second from 18 July to 15 October 1948. The Ten Days and Operation Yoav's first day are open to attacks. When `next`
+ // is omitted the turn is read on its first day.
+ [Theory]
+ [InlineData(1948, 6, 10, false)]
+ [InlineData(1948, 6, 11, true)]
+ [InlineData(1948, 7, 7, true)]
+ [InlineData(1948, 7, 8, false)]
+ [InlineData(1948, 7, 17, false)]
+ [InlineData(1948, 7, 18, true)]
+ [InlineData(1948, 10, 14, true)]
+ [InlineData(1948, 10, 15, false)]
+ public void TheTruceWindowsAreTheSourcedOnes(int y, int m, int d, bool truce)
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10));
+ s.Date = new DateTime(y, m, d);
+ EnemyAI.Order(c, s, new List<Cause>());
+ Assert.Equal(truce ? (Orders.Fortifying, "2,0") : (Combat.Attack, "3,0"), (s.Units[2].Order, s.Units[2].OrderTarget));
+ }
+
+ // R-ENEMY-006: a turn is in a truce when its middle day is. The 15 May 1948 turn (middle 30 May) is war, though the first
+ // truce starts before it ends; the 15 July turn (30 July) is the second truce, though the Ten Days are on when it starts;
+ // the 15 October turn (30 October) is war again.
+ [Theory]
+ [InlineData(5, 6, false)]
+ [InlineData(6, 7, true)]
+ [InlineData(7, 8, true)]
+ [InlineData(9, 10, true)]
+ [InlineData(10, 11, false)]
+ public void ATurnIsReadAtItsMiddle(int from, int to, bool truce)
+ {
+ var c = Load();
+ var s = CombatTests.With(c, Soldier(1, 3, 0, 5), Enemy(2, 2, 0, 10));
+ s.Date = new DateTime(1948, from, 15);
+ EnemyAI.Order(c, s, new List<Cause>(), new DateTime(1948, to, 15));
+ Assert.Equal(truce ? Orders.Fortifying : Combat.Attack, s.Units[2].Order);
+ }
+
+ // R-ENEMY-004: no reinforcements in Phase 5 (Q-ENEMY-3). No turn adds an enemy unit.
+ [Fact]
+ public void NoReinforcements()
+ {
+ var s = Simulation.NewGame(Real, 1, Mode.Historical);
+ var ids = s.Units.Values.Where(IsEnemy).Select(u => u.Id).ToHashSet();
+ for (int i = 0; i < 8 && s.GameOver == null; i++)
+ {
+ s = Simulation.Step(Real, s, Array.Empty<Command>()).State;
+ Assert.Subset(ids, s.Units.Values.Where(IsEnemy).Select(u => u.Id).ToHashSet());
+ }
+ }
+
+ // R-ENEMY-001, R-ENEMY-002: no enemy move ends on a locality off enemy_held_localities (the mirror of R-MOVE-005), so the
+ // script creates no capture while enemy capture (Q-TGT-2 (c)) is unbuilt. A unit that never moved may still stand where it
+ // started (Syria's start hex is Degania Alef's, R-CTRL-004).
+ [Fact]
+ public void NoEnemyMoveEndsOnAPlayersLocality()
+ {
+ var c = Real;
+ var held = new HashSet<string>(c.Rules.Mvp!.EnemyHeldLocalities, StringComparer.Ordinal);
+ var heldHexes = c.SettlementHexes.Where(h => held.Contains(h.Id)).Select(h => (h.Q, h.R)).ToHashSet();
+ var players = c.SettlementHexes.Select(h => (h.Q, h.R)).Where(h => !heldHexes.Contains(h)).ToHashSet();
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var start = s.Units.Values.ToDictionary(u => u.Id, u => (u.Q, u.R));
+ for (int i = 0; i < 8 && s.GameOver == null; i++)
+ {
+ s = Simulation.Step(c, s, Array.Empty<Command>()).State;
+ foreach (var u in s.Units.Values.Where(IsEnemy))
+ if ((u.Q, u.R) != start[u.Id]) Assert.DoesNotContain((u.Q, u.R), players);
+ }
+ Assert.Empty(s.Settlements);
+ }
+}
diff --git a/sim/Statecraft.Sim.Tests/Fixture.cs b/sim/Statecraft.Sim.Tests/Fixture.cs
index ec3490674..fe3ab7abe 100644
--- a/sim/Statecraft.Sim.Tests/Fixture.cs
+++ b/sim/Statecraft.Sim.Tests/Fixture.cs
@@ -302,6 +302,7 @@ mvp:
combat_table:
- { min_ratio_percent: 0, attacker_loss: [1, 1], defender_loss: [1, 1], defender_retreats: [0, 0] }
- { min_ratio_percent: 200, attacker_loss: [0, 0], defender_loss: [1, 1], defender_retreats: [1, 1] }
+ enemy_attack_ratio_percent: 200
";
public static readonly string GddCities = "\ncities:\n" + City("town_a", 12, 8, 10, 6, "[military_camp, hakhshara]");
diff --git a/sim/Statecraft.Sim.Tests/MovementTests.cs b/sim/Statecraft.Sim.Tests/MovementTests.cs
index 91c2de4d3..bcf46f4d6 100644
--- a/sim/Statecraft.Sim.Tests/MovementTests.cs
+++ b/sim/Statecraft.Sim.Tests/MovementTests.cs
@@ -202,7 +202,8 @@ public class MovementTests
{
var c = Load(Board((2, 1, "hills"), (3, 1, "desert"), (4, 0, "sea"), (0, 2, "lake"), (3, 2, "hills")),
("enemy_held_localities: []", "enemy_held_localities: [town_a]"));
- var s = With(c, Soldier(1, 1, 1), Enemy(2, 3, 0), Soldier(3, 2, 2), Soldier(4, 2, 2));
+ // Strengths so the enemy's AI holds instead of attacking the soldier when it ends next to it (R-ENEMY-003).
+ var s = With(c, new Unit(1, "soldier", 1, 1, side: "israel", strength: 5), new Unit(2, "enemy", 3, 0, side: "egypt", strength: 1), Soldier(3, 2, 2), Soldier(4, 2, 2));
var reach = Movement.Reachable(c, s, 1);
Assert.NotEmpty(reach);
foreach (var h in c.Hexes)
diff --git a/sim/Statecraft.Sim.Tests/OrdersTests.cs b/sim/Statecraft.Sim.Tests/OrdersTests.cs
index 1c276c8a4..ef7c98fdc 100644
--- a/sim/Statecraft.Sim.Tests/OrdersTests.cs
+++ b/sim/Statecraft.Sim.Tests/OrdersTests.cs
@@ -88,20 +88,22 @@ public class OrdersTests
}
// R-ORDER-003: hold acts on the unit itself and nothing around it: an enemy unit next to it and one past it keep their
- // hexes through the turn, and neither they nor a player unit beside it change their defence. Enemy moves come with the
- // enemy AI (Phase 5 PR 10); nothing in hold stops one.
+ // hexes through the turn, and a player unit beside it keeps its defence. The enemy units fortify by their own AI
+ // (R-ENEMY-003, below the attack ratio here), not because of the hold; nothing in hold stops an enemy move.
[Fact]
public void HoldNeverIntercepts()
{
var c = Load();
- var s = With(c, Soldier(1, 2, 1), Soldier(2, 3, 1), Enemy(3, 1, 1), Enemy(4, 0, 1));
+ static Unit Strong(Unit u) => new Unit(u.Id, u.Type, u.Q, u.R, side: u.Side, strength: u.Side == "israel" ? 5 : 1);
+ var s = With(c, Strong(Soldier(1, 2, 1)), Strong(Soldier(2, 3, 1)), Strong(Enemy(3, 1, 1)), Strong(Enemy(4, 0, 1)));
var r = Simulation.Step(c, s, new Command[] { new UnitOrder(1, Orders.Hold) });
Assert.Empty(r.Rejected);
Assert.Equal(120, Defence(c, r.State, 1)); // the fixture's hold percent, on its own hex
foreach (var id in new[] { 2, 3, 4 })
{
- Assert.Equal(100, Defence(c, r.State, id));
- Assert.Equal((s.Units[id].Q, s.Units[id].R, ""), (r.State.Units[id].Q, r.State.Units[id].R, r.State.Units[id].Order));
+ var order = id == 2 ? "" : Orders.Fortify; // the enemy's own fortify, set at the turn's end (R-ENEMY-003)
+ Assert.Equal(id == 2 ? 100 : 150, Defence(c, r.State, id));
+ Assert.Equal((s.Units[id].Q, s.Units[id].R, order), (r.State.Units[id].Q, r.State.Units[id].R, r.State.Units[id].Order));
}
}
diff --git a/sim/Statecraft.Sim.Tests/PlayerTextTests.cs b/sim/Statecraft.Sim.Tests/PlayerTextTests.cs
index 30ae7bc02..6617df9a9 100644
--- a/sim/Statecraft.Sim.Tests/PlayerTextTests.cs
+++ b/sim/Statecraft.Sim.Tests/PlayerTextTests.cs
@@ -286,8 +286,10 @@ public class PlayerTextTests
bad.Add("money without a unit: " + w.Message);
if (w.System == "map") // "Built a housing project: cost IL 6.0m, Housing +3.0%, ...": every number with its unit
{
- // A hex "(q, r)" and a corridor's "segment N" (R-COR-001, Q-SENS-1) are names, not amounts.
- if (Regex.IsMatch(Regex.Replace(Regex.Replace(w.Message, @"\(-?\d+, -?\d+\)|segment \d+", ""), @"(IL |\$)[+-]?\d+\.\dm|[+-]?\d+(\.\d)?%", ""), @"\d"))
+ // A hex "(q, r)" or "hex q,r", a corridor's "segment N" (R-COR-001, Q-SENS-1) and a unit's "unit N" are names, not
+ // amounts; movement points and people are units (the enemy's step 1 lines, R-ENEMY-001, reach the reference run).
+ var named = Regex.Replace(w.Message, @"\(-?\d+, -?\d+\)|segment \d+|hex -?\d+,-?\d+|[Uu]nits? \d+(, \d+)*|\d[\d,]* (movement points|people)", "");
+ if (Regex.IsMatch(Regex.Replace(named, @"(IL |\$)[+-]?\d+\.\dm|[+-]?\d+(\.\d)?%", ""), @"\d"))
bad.Add("map line number without a unit: " + w.Message);
}
else if (indicators.Contains(w.Target) && Regex.IsMatch(Regex.Replace(w.Message, @"[+-]?\d+(\.\d)?%", ""), @"\d"))
diff --git a/sim/Statecraft.Sim.Tests/TrainingTests.cs b/sim/Statecraft.Sim.Tests/TrainingTests.cs
index e01af8fa9..103355857 100644
--- a/sim/Statecraft.Sim.Tests/TrainingTests.cs
+++ b/sim/Statecraft.Sim.Tests/TrainingTests.cs
@@ -251,7 +251,7 @@ public class TrainingTests
}
Assert.Empty(s.Cohorts);
Assert.Equal(3, s.Cities["tel_aviv"].Recruits);
- Assert.Equal(Mvp.StartingForces.Count, s.Units.Count); // the starting forces only (R-UNIT-006): a recruit is a count until Phase 5
+ Assert.DoesNotContain(s.Units.Keys, id => id >= Mvp.StartingForces.Count); // the starting forces only (R-UNIT-006): a recruit is a count until Phase 5; the enemy may have destroyed one (R-ENEMY-003)
}
/// <summary>The timing rule, the plan's reading of D-A6 (Task 4.3) and not part of the owner's ruling: the turn a cohort is ordered in is its first training turn, so a cohort
@@ -273,8 +273,8 @@ public class TrainingTests
var u0 = Step(Mvp, start, new Train("tel_aviv", "halutz", 2)).State; // halutz: 4 turns, done at the end of T+3
var u2 = Step(Mvp, Step(Mvp, u0).State).State;
- Assert.Equal(Mvp.StartingForces.Count, u2.Units.Count); // the starting forces only (R-UNIT-006)
- Assert.Equal(Mvp.StartingForces.Count + 2, Step(Mvp, u2).State.Units.Count);
+ Assert.DoesNotContain(u2.Units.Keys, id => id >= Mvp.StartingForces.Count); // the starting forces only (R-UNIT-006)
+ Assert.Equal(2, Step(Mvp, u2).State.Units.Keys.Count(id => id >= Mvp.StartingForces.Count));
}
// R-TRAIN-003
@@ -287,11 +287,11 @@ public class TrainingTests
var cohort = Assert.Single(s.Cohorts.Keys);
for (int turn = 1; turn < Mvp.UnitTypes.Single(t => t.Id == "halutz").TrainingTurns; turn++)
{
- Assert.Equal(Mvp.StartingForces.Count, s.Units.Count); // the starting forces only (R-UNIT-006)
+ Assert.DoesNotContain(s.Units.Keys, id => id >= Mvp.StartingForces.Count); // the starting forces only (R-UNIT-006)
s = Step(Mvp, s).State;
}
Assert.Empty(s.Cohorts);
- var halutzim = s.Units.Values.Skip(Mvp.StartingForces.Count).ToList(); // after the starting forces (R-UNIT-006)
+ var halutzim = s.Units.Values.Where(u => u.Id >= Mvp.StartingForces.Count).ToList(); // after the starting forces (R-UNIT-006)
Assert.Equal(new[] { cohort + 1, cohort + 2, cohort + 3 }, halutzim.Select(u => u.Id));
Assert.All(halutzim, u => Assert.Equal(("halutz", -3, 19), (u.Type, u.Q, u.R))); // Tel Aviv's settlement hex
Assert.Equal(cohort + 4, s.NextId);
diff --git a/sim/Statecraft.Sim.Tests/golden/hash1950.txt b/sim/Statecraft.Sim.Tests/golden/hash1950.txt
index cd79bb8f5..b04e7307b 100644
--- a/sim/Statecraft.Sim.Tests/golden/hash1950.txt
+++ b/sim/Statecraft.Sim.Tests/golden/hash1950.txt
@@ -1,4 +1,4 @@
-1950-01-15;1948;Historical;20;
+1950-01-15;11400714819323200433;Historical;20;
ind{
food_supply=0.8886718750000001 [3FEC700000000001]
housing=0.106156810303452 [3FBB2D17BC8041AE]
@@ -119,13 +119,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
13:veteran_halutz@-3,19/6
site{
beit_kama=done,1949-06-15
diff --git a/sim/Statecraft.Sim.Tests/golden/hash1952.txt b/sim/Statecraft.Sim.Tests/golden/hash1952.txt
index 2f3688c02..bb766a88c 100644
--- a/sim/Statecraft.Sim.Tests/golden/hash1952.txt
+++ b/sim/Statecraft.Sim.Tests/golden/hash1952.txt
@@ -1,4 +1,4 @@
-1952-01-15;1948;Historical;44;
+1952-01-15;11400714819323200433;Historical;44;
ind{
food_supply=0.8555635490815091 [3FEB60C6CEDE8D7F]
housing=0.08527449992708651 [3FB5D48CB45E9D83]
@@ -140,13 +140,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
13:veteran_halutz@-3,19/6
site{
beit_kama=done,1949-06-15
diff --git a/sim/Statecraft.Sim.Tests/golden/manifest.txt b/sim/Statecraft.Sim.Tests/golden/manifest.txt
index 699a12a0e..938d00183 100644
--- a/sim/Statecraft.Sim.Tests/golden/manifest.txt
+++ b/sim/Statecraft.Sim.Tests/golden/manifest.txt
@@ -2,11 +2,11 @@
# Tests read this file. To accept a deliberate change: STATECRAFT_UPDATE_GOLDEN=1 dotnet test sim/Statecraft.sln
# rewrites this file, the snapshots and the GoldenRun.cs constants; then update docs/determinism.md and tools/web_check.py
# (GoldenManifestTests names any place that is out of date) and add a row to the rebaseline history.
-hash1950 a1ad42117a8523a79cf36cc03f33c63fbe887820d7f6fb89a834f2f952510548
-hash1952 9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40
-smoke5 ed761c3b9c4e9b76520af20080b2a7c8b984a1f44f1d52e5127aae1baa992a8d
-scenario-reckless 574c8a19ef2869168781290b0e9b8333dd0b1b89889fd8473aed76c3ef22f37e
-scenario-cautious 00ec7ec25dc43c30c1836998c39dd4dcdab8ec32a6f8865c0a0d94e4f97436f5
-scenario-mvp-absorb-first ac5f51bb1cbe71f1853c6c85d8c93bf745224017187368f0ec243c39ba013186
-mvp-end 9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40
-scenario-mvp-defeat f2ad11143797be1b3d6a06c21b86b2ecfbfcdbe7a72a1fdeba801491cd59619c
+hash1950 1392f3eb726172c284555b2bcacc0be3f8d9e33d19306dfc3baf0e2c63fe249b
+hash1952 cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5
+smoke5 11c41014a5ed74f783bc2fa0af99ee86830d162d5e71c883318e36f68f47b9b4
+scenario-reckless d6fd9e89518bc31d43816bca87d71665a56643f835749b2d4f093cbdb3870239
+scenario-cautious 230791a17b8085ce7f2f5f80ca52d13b74df4d55432ff573dc0b1cf20c328c84
+scenario-mvp-absorb-first de1769316d1268baab0baad253b2b263bc816e51e7999cea3b3641ab434a0904
+mvp-end cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5
+scenario-mvp-defeat 7404295a585197d155809a6d7b32c3c863152fe9c82c182a5528be86f7a231b2
diff --git a/sim/Statecraft.Sim.Tests/golden/mvp-end.txt b/sim/Statecraft.Sim.Tests/golden/mvp-end.txt
index 2f3688c02..bb766a88c 100644
--- a/sim/Statecraft.Sim.Tests/golden/mvp-end.txt
+++ b/sim/Statecraft.Sim.Tests/golden/mvp-end.txt
@@ -1,4 +1,4 @@
-1952-01-15;1948;Historical;44;
+1952-01-15;11400714819323200433;Historical;44;
ind{
food_supply=0.8555635490815091 [3FEB60C6CEDE8D7F]
housing=0.08527449992708651 [3FB5D48CB45E9D83]
@@ -140,13 +140,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
13:veteran_halutz@-3,19/6
site{
beit_kama=done,1949-06-15
diff --git a/sim/Statecraft.Sim.Tests/golden/scenario-cautious.txt b/sim/Statecraft.Sim.Tests/golden/scenario-cautious.txt
index 593ce0422..408da87f8 100644
--- a/sim/Statecraft.Sim.Tests/golden/scenario-cautious.txt
+++ b/sim/Statecraft.Sim.Tests/golden/scenario-cautious.txt
@@ -1,4 +1,4 @@
-1949-01-15;11400714819323200433;Historical;8;
+1949-01-15;4354685564936847302;Historical;8;
ind{
food_supply=0.47968750000000004 [3FDEB33333333334]
housing=0.41912251055925737 [3FDAD2E738F7A4A9]
@@ -106,13 +106,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
next=12
food=2253,751,0
arms=12
\ No newline at end of file
diff --git a/sim/Statecraft.Sim.Tests/golden/scenario-mvp-absorb-first.txt b/sim/Statecraft.Sim.Tests/golden/scenario-mvp-absorb-first.txt
index 43651adff..60d31bf9b 100644
--- a/sim/Statecraft.Sim.Tests/golden/scenario-mvp-absorb-first.txt
+++ b/sim/Statecraft.Sim.Tests/golden/scenario-mvp-absorb-first.txt
@@ -1,4 +1,4 @@
-1950-01-15;11400714819323200433;Historical;20;
+1950-01-15;4354685564936847302;Historical;20;
ind{
food_supply=0.8886718750000001 [3FEC700000000001]
housing=0.4168186644627783 [3FDAAD28310EB8D1]
@@ -120,13 +120,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
19:halutz@-3,19
20:halutz@-3,19
21:halutz@-3,19
diff --git a/sim/Statecraft.Sim.Tests/golden/scenario-mvp-defeat.txt b/sim/Statecraft.Sim.Tests/golden/scenario-mvp-defeat.txt
index 573cf2b0f..65a641b82 100644
--- a/sim/Statecraft.Sim.Tests/golden/scenario-mvp-defeat.txt
+++ b/sim/Statecraft.Sim.Tests/golden/scenario-mvp-defeat.txt
@@ -1,9 +1,9 @@
-1952-01-15;8709371129873692656;Historical;44;
+1952-01-15;6018027440424184879;Historical;44;
ind{
food_supply=0 [0000000000000000]
- housing=0.08527429756872115 [3FB5D4894F3F3D56]
- intl_standing=0.8100000000000003 [3FE9EB851EB851EE]
- morale=0.6946902205214107 [3FE63AE6FC3FB212]
+ housing=0.12527429756872116 [3FC008FCF98B23CA]
+ intl_standing=0.7900000000000003 [3FE947AE147AE14A]
+ morale=0.6206843611464106 [3FE3DCA57308664A]
security=0.5061322526321603 [3FE0323C441030A6]
pol{
border_settlement=0.2 [3FC999999999999A]
@@ -14,16 +14,16 @@ eff{
border_villages_secure=0.04113225263216018 [3FA50F495FBB5C36]
building_houses=0.08504947742483629 [3FB5C5CD7414E142]
food_lifts_cohesion=0.012063175589914928 [3F88B49405281EDE]
- homes_lift_morale=0.01282220528531898 [3F8A428742E78348]
+ homes_lift_morale=0.018816345910318985 [3F93449799429ABC]
rationing_feeds=0 [0000000000000000]
rationing_grates=0 [0000000000000000]
safety_lifts_morale=0.05054370052651369 [3FA9E0DD298F75A0]
work_lifts_cohesion=0.019261139119662984 [3F93B9312A691F60]
shift{
food_supply=-0.04999999999999999 [BFA9999999999998]
- housing=-2.7755575615628914E-17 [BC80000000000000]
- intl_standing=0.3100000000000003 [3FD3D70A3D70A3DC]
- morale=0.050000000000000044 [3FA99999999999A0]
+ housing=0.039999999999999994 [3FA47AE147AE147A]
+ intl_standing=0.29000000000000026 [3FD28F5C28F5C294]
+ morale=-0.030000000000000027 [BF9EB851EB851EC0]
security=0.16500000000000015 [3FC51EB851EB8524]
bud{
agriculture_minister=0.07 [3FB1EB851EB851EC]
@@ -50,7 +50,7 @@ debt=339.14012071283804 [4075323DEF37721B]
fx=-74.99043132809655 [C052BF633A14C6F2]
rung=2;cf=0;
mob=1 [3FF0000000000000]
-stab=0.4576489406596934 [3FDD4A1EC84BACE6]
+stab=0.4380036636522637 [3FDC084084BA89BB]
att=4 [4010000000000000]
fired[
altalena
@@ -99,7 +99,7 @@ chosen{
forex_crisis_1951=request_aid
frumkin_commission=inquiry
idf_founding=one_army
- iraqi_exodus=airlift
+ iraqi_exodus=pace
israel_bonds=donations_only
jerusalem_ceasefire=keep
jerusalem_military_government=military_government
@@ -139,13 +139,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
next=12
food=0,1390,1390
exhausted=3
diff --git a/sim/Statecraft.Sim.Tests/golden/scenario-reckless.txt b/sim/Statecraft.Sim.Tests/golden/scenario-reckless.txt
index 36eb71c6c..207afebbd 100644
--- a/sim/Statecraft.Sim.Tests/golden/scenario-reckless.txt
+++ b/sim/Statecraft.Sim.Tests/golden/scenario-reckless.txt
@@ -1,4 +1,4 @@
-1949-01-15;11400714819323200433;Historical;8;
+1949-01-15;4354685564936847302;Historical;8;
ind{
food_supply=0.59921875 [3FE32CCCCCCCCCCD]
housing=0.41912251055925737 [3FDAD2E738F7A4A9]
@@ -104,13 +104,12 @@ units[
2:veteran_soldier@0,15|side=israel|strength=4
3:veteran_soldier@-3,19|side=israel|strength=3
4:veteran_soldier@-5,23|side=israel|strength=3
- 5:veteran_soldier@-2,23|side=israel|strength=1
6:veteran_soldier@-9,27|side=israel|strength=1
- 7:egyptian_army@-11,27|side=egypt|strength=10
- 8:arab_legion@0,22|side=jordan|strength=5
- 9:syrian_army@10,10|side=syria|strength=3
- 10:iraqi_army@9,11|side=iraq|strength=3
- 11:arab_liberation_army@12,4|side=ala|strength=4
+ 7:egyptian_army@-8,25|side=egypt|strength=10|order=fortify|target=-8,25
+ 8:arab_legion@-2,22|side=jordan|strength=4|order=fortify|target=-2,22
+ 9:syrian_army@13,6|side=syria|strength=3|order=fortify|target=13,6
+ 10:iraqi_army@6,13|side=iraq|strength=3|order=fortify|target=6,13
+ 11:arab_liberation_army@12,4|side=ala|strength=4|order=fortify|target=12,4
next=12
food=2253,751,0
arms=12
\ No newline at end of file
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index 586b2f9d4..16a0b912f 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -60,12 +60,14 @@ namespace Statecraft.Sim
/// <summary>R-COMBAT-001: gives the player's military unit a standing attack on the adjacent hex (q, r), replacing its
/// order (a fortify is lost). Refused, state untouched, for an unknown unit, a halutz, an enemy unit, no hex, the attack
/// it already has, a hex not next to it or with no enemy unit, or fewer Arms than one attack takes on top of the
- /// player's other attacks already ordered (Q-COMBAT-4). It costs no Attention (D-A6) and resolves in this turn's step 2.</summary>
- internal static string? Give(Content c, GameState s, int unitId, int? q, int? r, List<Cause> why)
+ /// player's other attacks already ordered (Q-COMBAT-4). It costs no Attention (D-A6) and resolves in this turn's step 2.
+ /// With <paramref name="enemy"/> the enemy AI orders its own unit through the same checks, the sides swapped, and pays no
+ /// Arms (R-ENEMY-001; Q-ENEMY-4).</summary>
+ internal static string? Give(Content c, GameState s, int unitId, int? q, int? r, List<Cause> why, bool enemy = false)
{
if (!s.Units.TryGetValue(unitId, out var u)) return "There is no such unit";
if (!Military(u)) return "Only a military unit takes that order";
- if (!Player(u)) return "That unit is not yours";
+ if (Player(u) == enemy) return "That unit is not yours";
if (q == null || r == null) return "An attack names a hex";
var target = Hex(q.Value, r.Value);
if (u.Order == Attack && u.OrderTarget == target) return "That unit already has that order";
@@ -76,7 +78,7 @@ namespace Statecraft.Sim
// Arms are paid at resolution, so the stock must also cover the player's other standing attacks, this turn's
// included; the unit's own attack, which this order replaces, is not counted.
var committed = s.Units.Values.Where(x => x.Id != unitId && x.Order == Attack && Military(x) && Player(x)).Sum(x => Need(c, mvp, x));
- if (s.Arms < need + committed) return Short(need, s.Arms, committed);
+ if (!enemy && s.Arms < need + committed) return Short(need, s.Arms, committed);
s.Units[unitId] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, Attack, target);
why.Add(new Cause("map", unitId.ToString(CultureInfo.InvariantCulture), 0, $"Unit {unitId} will attack hex {target}"));
return null;
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 1b083436b..691713ff7 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -633,6 +633,9 @@ namespace Statecraft.Sim
/// <summary>The combat table (R-COMBAT-002, R-COMBAT-003; D-G3, Q-COMBAT-1): its rows in ascending
/// <see cref="CombatRow.MinRatioPercent"/>, the first at 0. Required in the GDD loop.</summary>
public List<CombatRow> CombatTable { get; set; } = new List<CombatRow>();
+ /// <summary>The least Combat.Preview ratio, in percent, at which an enemy unit on its goals attacks an adjacent player
+ /// unit (R-ENEMY-003; D-G7, Q-ENEMY-2). Required in the GDD loop, at least 1.</summary>
+ public int? EnemyAttackRatioPercent { get; set; }
/// <summary>The share of each numeric effect a delegated option applies when its minister is below the loyalty floor
/// (R-EVT-003; #107). Required in the GDD loop, 0 to 1.</summary>
public double? DisloyalEffectShare { get; set; }
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index e2e69dc84..398f55dc1 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -1254,6 +1254,9 @@ namespace Statecraft.Sim
if (row.AttackerLoss.Concat(row.DefenderLoss).Any(x => x < 0)) throw new ContentException(file, at, "a loss must be >= 0");
if (row.DefenderRetreats.Any(x => x != 0 && x != 1)) throw new ContentException(file, at + ".defender_retreats", "must be 0 or 1");
}
+ // R-ENEMY-003 (Q-ENEMY-2): required, so a missing key is never read as an enemy that attacks at any odds.
+ if (m.EnemyAttackRatioPercent == null) throw new ContentException(file, "mvp.enemy_attack_ratio_percent", "required when eras.yaml systems.loop is gdd");
+ if (m.EnemyAttackRatioPercent < 1) throw new ContentException(file, "mvp.enemy_attack_ratio_percent", "must be >= 1");
// R-MOVE-007 (Q-MOVE-2): required, so a missing key is never read as a free step along a corridor.
foreach (var (key, v) in new[] { ("road_move_cost", m.RoadMoveCost), ("rail_move_cost", m.RailMoveCost) })
{
diff --git a/sim/Statecraft.Sim/EnemyAI.cs b/sim/Statecraft.Sim/EnemyAI.cs
new file mode 100644
index 000000000..15e46dd9c
--- /dev/null
+++ b/sim/Statecraft.Sim/EnemyAI.cs
@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Statecraft.Sim
+{
+ /// <summary>The enemy AI, resolver step 1 (GDD Phase 5 PR 10, part 1; docs/rules/mvp.md R-ENEMY-001 to R-ENEMY-004). Every
+ /// non-player military unit gets its orders from the state alone, through the player's own validation (Movement.Move,
+ /// Combat.Give, Orders.Give with the sides swapped). Q-ENEMY-1: a starting unit marches on its row's sourced 11 June 1948
+ /// position; after that, or when the move cannot be made, it follows the simple goals of Q-ENEMY-2: attack an adjacent player
+ /// unit at mvp.enemy_attack_ratio_percent or better, else hold its hex fortified. No RNG (Q-ENEMY-2), no reinforcements
+ /// (Q-ENEMY-3), no supply (Q-ENEMY-4).</summary>
+ public static class EnemyAI
+ {
+ /// <summary>The script's dated position (Q-ENEMY-1; research_notes/GDD phase 5/arab-forces-1948.md section 10): an
+ /// anchor of map/units_1948.yaml from this day. The other scripted date, 15 May 1948, is the start (R-UNIT-006).</summary>
+ public const string ScriptDate = "1948-06-11";
+
+ /// <summary>R-ENEMY-006: the truce windows, half-open [from, until), in which no enemy unit attacks (owner, 21 October
+ /// 2026, #749). The first truce from 11 June 1948 (content/events/first_truce.yaml: S/834, four weeks from 06:00 GMT on
+ /// 11 June) to 8 July, when Egypt renewed the fighting (first_truce.yaml, ten_days.yaml; the 11 June anchors' `to` in
+ /// map/units_1948.yaml). The second from 18 July 1948 (second_truce.yaml: 19:00 on 18 July) to 15 October, Operation
+ /// Yoav (second_truce.yaml; operation_yoav.yaml `from`). The same windows as the map's display-only truce periods
+ /// (docs/rules/mvp.md R-ENEMY-006). A stopgap by date: #750 replaces it with the full war state (a refused truce, the
+ /// security rung), so a refused truce card does not lift it yet.</summary>
+ static readonly (DateTime From, DateTime Until)[] Truces =
+ {
+ (new DateTime(1948, 6, 11), new DateTime(1948, 7, 8)),
+ (new DateTime(1948, 7, 18), new DateTime(1948, 10, 15)),
+ };
+
+ /// <summary>R-ENEMY-006: whether the turn from <paramref name="start"/> to <paramref name="next"/> is in a truce: its
+ /// middle is inside a window. A monthly turn spans a truce's edge (15 July to 15 August 1948 holds three days of the Ten
+ /// Days and 28 of the second truce), and its middle puts it on the side that holds most of it. Agent default (#1263).</summary>
+ static bool InTruce(DateTime start, DateTime next)
+ {
+ var mid = start.AddTicks((next - start).Ticks / 2);
+ return Truces.Any(t => mid >= t.From && mid < t.Until);
+ }
+
+ static bool IsEnemy(Unit u) => u.Side != "" && u.Side != Movement.PlayerSide;
+
+ /// <summary>Resolver step 1: in ascending unit id, each enemy unit makes its scripted move (R-ENEMY-002) or, failing
+ /// that, takes the goals' order (R-ENEMY-003). A refused order changes nothing, as the player's does.
+ /// <paramref name="next"/> is the turn's end (R-ENEMY-006); without it the turn is read on its first day.</summary>
+ public static void Order(Content c, GameState s, List<Cause> why, DateTime? next = null)
+ {
+ var truce = InTruce(s.Date, next ?? s.Date);
+ var mvp = c.RulesAt(s.Date).Mvp!; // the loader requires it in the GDD loop
+ var spent = new Dictionary<int, int>();
+ foreach (var id in s.Units.Keys.ToList()) // Units is a SortedList: ascending id
+ {
+ if (!s.Units.TryGetValue(id, out var u) || !IsEnemy(u)) continue;
+ // R-ENEMY-006: a truce ends a standing attack whatever the unit does: a scripted move keeps every order but
+ // fortify (Movement.Move), and step 2 would resolve it inside the truce (owner, #1263).
+ 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, "", "");
+ 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"));
+ }
+ 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;
+ Goals(c, s, mvp, s.Units[id], truce, why);
+ }
+ }
+
+ /// <summary>R-ENEMY-002: the hex unit <paramref name="u"/> marches on this turn, or null. Only a starting force has a
+ /// script (the unit with id i of a new game is c.StartingForces[i], R-UNIT-006), and only in Historical mode, on a turn
+ /// that starts before its anchor's window ends.</summary>
+ static (int Q, int R)? Script(Content c, GameState s, Unit u)
+ {
+ if (s.Mode != Mode.Historical || u.Id >= c.StartingForces.Count) return null;
+ var f = c.StartingForces[u.Id];
+ if (f.Side != u.Side || f.Type != u.Type || f.Script == null || s.Date >= f.ScriptUntil) return null;
+ return f.Script;
+ }
+
+ /// <summary>R-ENEMY-003: attack the first neighbour, in the fixed order (R-MOVE-002), with a player military unit whose
+ /// attack preview reaches the threshold (a standing attack on it is kept); else fortify the unit's hex, which keeps a
+ /// fortify it already has. Inside a truce window (R-ENEMY-006) it only fortifies, which also replaces a standing
+ /// attack.</summary>
+ static void Goals(Content c, GameState s, MvpRules mvp, Unit u, bool truce, List<Cause> why)
+ {
+ 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 < mvp.EnemyAttackRatioPercent!.Value) 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;
+ }
+ Orders.Give(c, s, u.Id, Orders.Fortify, why, enemy: true);
+ }
+ }
+}
diff --git a/sim/Statecraft.Sim/GoldenRun.cs b/sim/Statecraft.Sim/GoldenRun.cs
index b5941b50f..7e80c0c73 100644
--- a/sim/Statecraft.Sim/GoldenRun.cs
+++ b/sim/Statecraft.Sim/GoldenRun.cs
@@ -16,13 +16,13 @@ namespace Statecraft.Sim
/// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change: the value
/// is pinned in sim/Statecraft.Sim.Tests/golden/manifest.txt, and STATECRAFT_UPDATE_GOLDEN=1 dotnet test rewrites
/// both (docs/determinism.md). Say why in the commit message.</summary>
- public const string Hash1950 = "a1ad42117a8523a79cf36cc03f33c63fbe887820d7f6fb89a834f2f952510548";
+ public const string Hash1950 = "1392f3eb726172c284555b2bcacc0be3f8d9e33d19306dfc3baf0e2c63fe249b";
public static readonly DateTime Until1950 = new DateTime(1950, 1, 1);
/// <summary>Final hash of Play(content, Until1952): the M3 content range (to 31 December 1951). The GDD game ends on
/// 15 January 1952 (R-LOOP-004), the first turn on or after Until1952, so this is also the mvp-end run.</summary>
- public const string Hash1952 = "9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40";
+ public const string Hash1952 = "cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5";
public static readonly DateTime Until1952 = new DateTime(1952, 1, 1);
diff --git a/sim/Statecraft.Sim/Movement.cs b/sim/Statecraft.Sim/Movement.cs
index 1733adb67..8f256aab2 100644
--- a/sim/Statecraft.Sim/Movement.cs
+++ b/sim/Statecraft.Sim/Movement.cs
@@ -53,17 +53,18 @@ namespace Statecraft.Sim
/// <summary>R-MOVE-006: moves the unit, spending the path's cost from what <paramref name="spent"/> says it has used this
/// turn (R-MOVE-001). Refused, state untouched, for an unknown or enemy unit, one that cannot move or is already there,
/// water or a hex off the map, an enemy unit's hex (entering it is an attack, Q-MOVE-3), an enemy-held locality
- /// (R-MOVE-005), a hex full of the unit's side (R-MOVE-003), or a hex out of reach this turn.</summary>
- internal static string? Move(Content c, GameState s, int unitId, int q, int r, Dictionary<int, int> spent, List<Cause> why)
+ /// (R-MOVE-005), a hex full of the unit's side (R-MOVE-003), or a hex out of reach this turn. With <paramref name="enemy"/>
+ /// the enemy AI orders its own unit through the same checks, the sides swapped (R-ENEMY-001).</summary>
+ internal static string? Move(Content c, GameState s, int unitId, int q, int r, Dictionary<int, int> spent, List<Cause> why, bool enemy = false)
{
if (!s.Units.TryGetValue(unitId, out var u)) return "There is no such unit";
- if (IsEnemy(u)) return "That unit is not yours";
+ if (IsEnemy(u) != enemy) return "That unit is not yours";
var points = Points(c, u);
if (points == 0) return "That unit cannot move";
if (u.Q == q && u.R == r) return "That unit is already there";
if (!(c.HexAt(q, r) is MapHex hex) || !hex.Land) return "Units cannot enter water or leave the map";
- if (s.Units.Values.Any(x => x.Q == q && x.R == r && IsEnemy(x))) return "An enemy unit holds that hex";
- if (NoEntry(c, s).Contains((q, r))) return "Units may not enter an enemy-held locality before its capture is possible";
+ if (s.Units.Values.Any(x => x.Q == q && x.R == r && IsEnemy(x) != enemy)) return "An enemy unit holds that hex";
+ if (NoEntry(c, s, enemy).Contains((q, r))) return "Units may not enter an enemy-held locality before its capture is possible";
if (Full(c, s, u, q, r) is string full) return full;
spent.TryGetValue(unitId, out var used);
var (dist, _) = Search(c, s, u, points - used);
@@ -90,11 +91,25 @@ namespace Statecraft.Sim
return held >= limit ? FormattableString.Invariant($"That hex already holds {held} units of that side, the most one hex can hold") : null;
}
- /// <summary>R-MOVE-005: the hexes of mvp.enemy_held_localities.</summary>
- internal static HashSet<(int Q, int R)> NoEntry(Content c, GameState s)
+ /// <summary>R-MOVE-005: the hexes of mvp.enemy_held_localities. For the enemy (R-ENEMY-001, the rule's mirror) every
+ /// other locality's hex, so no enemy move ends on a player's locality while enemy capture (Q-TGT-2 (c)) is unbuilt.</summary>
+ internal static HashSet<(int Q, int R)> NoEntry(Content c, GameState s, bool enemy = false)
{
var ids = c.RulesAt(s.Date).Mvp!.EnemyHeldLocalities;
- return new HashSet<(int Q, int R)>(c.SettlementHexes.Where(h => ids.Contains(h.Id)).Select(h => (h.Q, h.R)));
+ var held = new HashSet<(int Q, int R)>(c.SettlementHexes.Where(h => ids.Contains(h.Id)).Select(h => (h.Q, h.R)));
+ return enemy ? new HashSet<(int Q, int R)>(c.SettlementHexes.Select(h => (h.Q, h.R)).Where(h => !held.Contains(h))) : held;
+ }
+
+ /// <summary>R-ENEMY-002: the furthest hex the unit can reach this turn on its cheapest path to (<paramref name="q"/>,
+ /// <paramref name="r"/>), by the same search as Move, or null when no path leads there or its first step is out of reach.</summary>
+ internal static (int Q, int R)? Toward(Content c, GameState s, Unit u, int q, int r)
+ {
+ var (dist, prev) = Search(c, s, u, int.MaxValue);
+ if (!dist.ContainsKey((q, r)) || (u.Q, u.R) == (q, r)) return null;
+ var points = Points(c, u);
+ var h = (q, r);
+ while (dist[h] > points) h = prev[h];
+ return h == (u.Q, u.R) ? ((int, int)?)null : h;
}
/// <summary>R-MOVE-007: the hexes of each built corridor segment (a segment not started or being built is no road yet),
@@ -122,9 +137,9 @@ namespace Statecraft.Sim
static (Dictionary<(int Q, int R), int> Dist, Dictionary<(int Q, int R), (int Q, int R)> Prev) Search(Content c, GameState s, Unit u, int budget)
{
var costs = c.RulesAt(s.Date).Mvp!.MoveCost; // the loader requires a cost for every land biome in the GDD loop
- var blocked = NoEntry(c, s);
+ var blocked = NoEntry(c, s, IsEnemy(u));
var built = BuiltSegments(c, s);
- foreach (var x in s.Units.Values) if (IsEnemy(x)) blocked.Add((x.Q, x.R));
+ foreach (var x in s.Units.Values) if (IsEnemy(x) != IsEnemy(u)) blocked.Add((x.Q, x.R));
var dist = new Dictionary<(int Q, int R), int> { [(u.Q, u.R)] = 0 };
var prev = new Dictionary<(int Q, int R), (int Q, int R)>();
var frontier = new SortedSet<(int Cost, int Q, int R)> { (0, u.Q, u.R) };
diff --git a/sim/Statecraft.Sim/Orders.cs b/sim/Statecraft.Sim/Orders.cs
index 0151e3f65..430149745 100644
--- a/sim/Statecraft.Sim/Orders.cs
+++ b/sim/Statecraft.Sim/Orders.cs
@@ -29,13 +29,14 @@ namespace Statecraft.Sim
/// <summary>R-ORDER-001: gives the unit its standing order, replacing any other. Fortify names the unit's hex and starts
/// as Fortifying; hold names nothing, as it acts where the unit is. Refused, state untouched, for an unknown order or
/// unit, a unit with no side (a halutz), an enemy unit, hold for artillery (R-COMBAT-007: it never holds ground,
- /// Q-ARTY-1), or the order it already has.</summary>
- internal static string? Give(Content c, GameState s, int unitId, string order, List<Cause> why)
+ /// Q-ARTY-1), or the order it already has. With <paramref name="enemy"/> the enemy AI orders its own unit through the
+ /// same checks, the sides swapped (R-ENEMY-001).</summary>
+ internal static string? Give(Content c, GameState s, int unitId, string order, List<Cause> why, bool enemy = false)
{
if (order != Fortify && order != Hold) return "That order is not known";
if (!s.Units.TryGetValue(unitId, out var u)) return "There is no such unit";
if (u.Side == "") return "Only a military unit takes that order";
- if (u.Side != PlayerSide) return "That unit is not yours";
+ if ((u.Side != PlayerSide) != enemy) return "That unit is not yours";
if (order == Hold && c.UnitTypes.FirstOrDefault(t => t.Id == u.Type)?.Class == Combat.ArtilleryClass) return "Artillery does not hold ground";
var here = Hex(u);
var already = order == Hold ? u.Order == Hold : (u.Order == Fortify || u.Order == Fortifying) && u.OrderTarget == here;
diff --git a/sim/Statecraft.Sim/StartingForces.cs b/sim/Statecraft.Sim/StartingForces.cs
index b682387a8..e642aa636 100644
--- a/sim/Statecraft.Sim/StartingForces.cs
+++ b/sim/Statecraft.Sim/StartingForces.cs
@@ -21,6 +21,11 @@ namespace Statecraft.Sim
public int R { get; set; }
/// <summary>The row's review status (draft, to_verify, fact-checked); the release gate reads it (Q-START-3).</summary>
public string Status { get; set; } = "";
+ /// <summary>The hex of the row's 11 June 1948 anchor that the enemy AI marches on (R-ENEMY-002; Q-ENEMY-1), or null
+ /// when the row has none on two sources.</summary>
+ public (int Q, int R)? Script { get; set; }
+ /// <summary>The end of that anchor's window (its `to`): from a turn starting on it the unit follows the goals.</summary>
+ public DateTime ScriptUntil { get; set; }
}
/// <summary>The sourced starting forces, both sides (GDD Phase 5 PR 4; docs/rules/mvp.md R-UNIT-006, R-UNIT-007).
@@ -72,15 +77,43 @@ namespace Statecraft.Sim
var hex = c.HexAt(q, r);
if (hex == null) throw new ContentException(FileName, $"{at}.anchors[{k}]", FormattableString.Invariant($"hex q={q} r={r} is not on the map ({ContentLoader.HexesFile})"));
if (!hex.Land) throw new ContentException(FileName, $"{at}.anchors[{k}]", FormattableString.Invariant($"hex q={q} r={r} is not a land hex in {ContentLoader.HexesFile}"));
- list.Add(new StartingForce
+ var force = new StartingForce
{
Row = id, Anchor = Text(anchor, "place"), Type = TypeFor(c, side, at), Side = side,
Strength = units, Q = q, R = r, Status = Text(row, "status"),
- });
+ };
+ if (side != Movement.PlayerSide) Script(c, force, anchors, id, at, hexes);
+ list.Add(force);
}
return list;
}
+ /// <summary>R-ENEMY-002 (Q-ENEMY-1: the 15 May and 11 June 1948 positions two sources agree on, arab-forces-1948.md
+ /// section 10). The 15 May position is the start; the script's target is the row's first anchor from 11 June that cites
+ /// at least two sources other than Wikipedia and Wikidata (which the file uses for coordinates only), on that anchor's
+ /// hex. A row with none has no script and its unit follows the goals.</summary>
+ static void Script(Content c, StartingForce force, List<object> anchors, string id, string at,
+ Dictionary<(string id, int anchor), (int q, int r)> hexes)
+ {
+ for (int j = 0; j < anchors.Count; j++)
+ {
+ if (!(anchors[j] is Dictionary<object, object> a) || Text(a, "from") != EnemyAI.ScriptDate) continue;
+ var sourced = a.TryGetValue("sources", out var so) && so is List<object> sources ? sources.Count(x =>
+ x is Dictionary<object, object> m && Text(m, "url") is var url && url != ""
+ && !url.Contains("wikipedia.org", StringComparison.Ordinal) && !url.Contains("wikidata.org", StringComparison.Ordinal)) : 0;
+ if (sourced < 2) continue;
+ if (!hexes.TryGetValue((id, j), out var qr))
+ throw new ContentException(HexesFileName, "anchors", $"no hex for {at}.anchors[{j}] ({FileName}); rerun tools/unit_anchor_hexes.py");
+ if (!(c.HexAt(qr.q, qr.r) is MapHex hex) || !hex.Land)
+ throw new ContentException(FileName, $"{at}.anchors[{j}]", FormattableString.Invariant($"hex q={qr.q} r={qr.r} is not a land hex in {ContentLoader.HexesFile}"));
+ if (!DateTime.TryParseExact(Text(a, "to"), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var until))
+ throw new ContentException(FileName, $"{at}.anchors[{j}].to", "required, a date 'YYYY-MM-DD'");
+ force.Script = (qr.q, qr.r);
+ force.ScriptUntil = until;
+ return;
+ }
+ }
+
/// <summary>Adds the starting forces to a new game, in the content's order, taking the next ids: the unit with id i
/// of a new game is <c>c.StartingForces[i]</c>.</summary>
public static void Init(Content c, GameState s)
diff --git a/sim/Statecraft.Sim/TurnResolver.cs b/sim/Statecraft.Sim/TurnResolver.cs
index a0859f4f9..16d3ccfab 100644
--- a/sim/Statecraft.Sim/TurnResolver.cs
+++ b/sim/Statecraft.Sim/TurnResolver.cs
@@ -14,7 +14,7 @@ namespace Statecraft.Sim
{
if (!c.IsGdd) return;
var r = c.RulesAt(s.Date).Mvp!; // the loader requires it in the GDD loop
- // 1. Enemy orders: Phase 5 PR 10.
+ EnemyAI.Order(c, s, why, next); // 1. enemy orders, in ascending unit id (R-ENEMY-001)
Combat.Resolve(c, s, why); // 2. combat, in ascending attacker id (R-COMBAT-002)
SettlementControl.Capture(c, s, next, why); // 3. capture, in ordinal settlement id (R-CTRL-004, R-CTRL-005)
Orders.Settle(s, why); // 3b. after combat: fortify given this turn is set, and lost where a unit left its hex (R-ORDER-002)
diff --git a/tools/web_check.py b/tools/web_check.py
index 4aeb9c9b0..c08e72fbe 100644
--- a/tools/web_check.py
+++ b/tools/web_check.py
@@ -8,7 +8,7 @@ fail on a failure line or a timeout. Uses Chrome for Testing from the Playwright
--enable-logging=stderr --v=1 as INFO:CONSOLE... "text", source: ...
Usage (from the repo root):
- uv run tools/web_check.py unity/Builds/Web-disabled --query "golden" --expect "GOLDEN OK hash1950 a1ad42117a8523a79cf36cc03f33c63fbe887820d7f6fb89a834f2f952510548" --expect "GOLDEN OK hash1952 9cbd73eda1440e1ee3e1ee565cf81960f2e7ee5595e55bdb4ed661f1d5e0ab40"
+ uv run tools/web_check.py unity/Builds/Web-disabled --query "golden" --expect "GOLDEN OK hash1950 1392f3eb726172c284555b2bcacc0be3f8d9e33d19306dfc3baf0e2c63fe249b" --expect "GOLDEN OK hash1952 cc96cd0a8d50a7693300ef887673c7ccfc18dac6d4dcaa9f52ed2ed09062eda5"
Options: --query Q (page URL query, without '?'), --expect TEXT (repeatable), --fail REGEX (default: GOLDEN FAIL|SMOKE FAIL|BOOT FAIL),
--timeout SECONDS (default 300), --stall SECONDS (fail when no console message arrives for that long; 0 = off),
--swiftshader (software WebGL when there is no GPU), --dpr N (default 1), --shots DIR (reassemble the player's