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.

13.0 KB
Raw Blame
using System;
using System.Collections.Generic;
using System.Linq;
using Statecraft.Sim;

/// <summary>ResolveCapture and Raid and withdraw (GDD Phase 6 PR 5, #797). The owner's rulings of 18 October 2026 (#783,
/// batch 3): Q-RAID-1 (a), the player's units leave and the place returns to enemy control at once with its residents
/// unchanged; Q-HIST-1 (a), a capture the player does not answer waits and the units stay. The owner's ruling of 18 October
/// 2026 on #797, "Counts as Raid": a capture still waiting at the MVP's end makes the units leave, changes nothing for the
/// residents and adds nothing to the score. Where the units go is the default proposed on #797: each steps to the first
/// free neighbouring hex in the fixed order. The places are CaptureTests' fixture values, not history: town_a sits on
/// (1, 0) of the 6 x 3 board, whose neighbours on the board are (2, 0), (0, 0), (0, 1) and (1, 1), in that order.</summary>
public class RaidTests
{
    static Content Load() => CaptureTests.Load(CaptureTests.AllTargets);
    static Unit Soldier(int id, int q, int r) => CaptureTests.Soldier(id, q, r);
    static Unit Enemy(int id, int q, int r) => CaptureTests.Enemy(id, q, r);

    /// <summary>A new game with <paramref name="units"/> and town_a captured and awaiting the choice.</summary>
    static GameState Awaiting(Content c, params Unit[] units)
    {
        var s = CombatTests.With(c, units);
        s.Settlements["town_a"] = new SettlementControl(Movement.PlayerSide, SettlementControl.AwaitingChoice, s.Date);
        return s;
    }

    static StepResult Step(Content c, GameState s, params Command[] commands) => Simulation.Step(c, s, commands);

    static ResolveCapture Raid(string id = "town_a") => new ResolveCapture(id, SettlementControl.Raid);

    // R-CTRL-010 (Q-RAID-1 (a)): the player's units leave, the soldier and a halutz alike, each to the first free
    // neighbouring hex, and the place keeps only an ownerless lost marker: it is under enemy control again.
    [Fact]
    public void RaidWithdrawsThePlayersUnitsAndReturnsThePlaceToEnemyControl()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0), new Unit(2, "halutz", 1, 0));
        var result = Step(c, s, Raid());
        Assert.Empty(result.Rejected);
        var lost = Assert.Single(result.State.Settlements).Value;
        Assert.Equal(("", "", s.Date, false), (lost.Owner, lost.State, lost.Since, lost.Fled));
        Assert.Equal((2, 0), (result.State.Units[1].Q, result.State.Units[1].R));
        Assert.Equal((2, 0), (result.State.Units[2].Q, result.State.Units[2].R));
        Assert.Contains(result.Why, x => x.Target == "town_a" && x.Message.Contains("under enemy control again"));
        Assert.Contains(Movement.NoEntry(c, result.State), h => h == (1, 0));   // no withdrawal ends on it (R-CTRL-010)
    }

    // R-CTRL-010 (Q-RAID-1 (a), "residents unchanged"): Raid changes the units' hexes and replaces the capture with the
    // ownerless marker, and nothing else.
    [Fact]
    public void RaidChangesOnlyTheUnitsHexesAndTheRecord()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0));
        var next = Step(c, s, Raid()).State;
        var without = next.Clone(); without.Settlements.Clear();
        var twin = Step(c, CombatTests.With(c, Soldier(1, 2, 0))).State;
        Assert.Equal(twin.Describe(), without.Describe());
    }

    // R-CTRL-010: a neighbour with an enemy unit, or one already full under the stack limit (the fixture's 2), is passed
    // over for the next in the fixed order.
    [Fact]
    public void AUnitSkipsAHexWithAnEnemyOrNoRoom()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0), Soldier(2, 1, 0), Enemy(3, 2, 0), Soldier(4, 0, 0));
        var next = Step(c, s, Raid()).State;
        Assert.Equal((0, 0), (next.Units[1].Q, next.Units[1].R));   // (2, 0) holds an enemy unit
        Assert.Equal((0, 1), (next.Units[2].Q, next.Units[2].R));   // (0, 0) now holds two Israeli units
        Assert.Equal("", next.Settlements["town_a"].Owner);
    }

    // R-CTRL-010, R-CTRL-007: when a unit has no free hex to withdraw to, Raid is refused with a reason, the Attention is
    // refunded and the state is unchanged, a unit that had already found a hex included, so the choice waits. Here unit 1
    // fits on (2, 0) beside unit 7, which leaves no room there for unit 6, and enemy units hold the other three neighbours.
    [Fact]
    public void RaidWithNowhereToGoIsRefused()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0), Soldier(6, 1, 0), Soldier(7, 2, 0), Enemy(3, 0, 0), Enemy(4, 0, 1), Enemy(5, 1, 1));
        var before = s.Clone();
        var why = new List<Cause>();
        Assert.Contains("A unit has no free hex", SettlementControl.Resolve(c, s, "town_a", SettlementControl.Raid, why));
        Assert.Empty(why);
        Assert.Equal(before.Describe(), s.Describe());
        Assert.Equal(before.Hash(), s.Hash());
        Assert.Single(Step(c, before, Raid()).Rejected);
        // #2733 item 1: with a cost the Attention could pay, the refused order's Attention comes back.
        var costly = CaptureTests.Load(f => f["rules.yaml"] = f["rules.yaml"].Replace("resolve_capture: 0,", "resolve_capture: 2,"), CaptureTests.AllTargets);
        before.Attention = 5;
        Assert.InRange(AttentionSystem.Cost(costly, before, SettlementControl.ResolveKind), 0.1, before.Attention);
        var refused = Simulation.Preview(costly, before, new Command[] { Raid() });
        Assert.Contains("A unit has no free hex", Assert.Single(refused.Rejected).Reason);
        Assert.Equal(before.Attention, refused.State.Attention);
    }

    [Fact]
    public void TheLegacyLoopStillRefusesCaptureChoicesWithoutChangingTheirPreview()
    {
        var c = ContentLoader.Load(Fixture.Write());   // legacy content, as EraSystemsTests.WithoutTheSwitchTheLoopIsLegacy
        Assert.False(c.IsGdd);
        var s = Simulation.NewGame(c, 1, Mode.Historical);
        var before = Simulation.Preview(c, s, Array.Empty<Command>());
        var result = Simulation.Preview(c, s, new Command[] { new ResolveCapture("town_a", SettlementControl.Raid) });
        Assert.Equal("That order is not available in this game", Assert.Single(result.Rejected).Reason);
        Assert.Equal(before.State.Hash(), result.State.Hash());
    }

    // R-CTRL-007: ResolveCapture is valid only for a settlement awaiting the choice, and only for an outcome that is built;
    // otherwise it is refused with a reason and the state is unchanged.
    [Theory]
    [InlineData("town_c", "raid", "not awaiting a decision")]   // a target with no record
    [InlineData("nowhere", "raid", "not awaiting a decision")]   // not a settlement
    [InlineData("town_a", "surrender", "not known")]
    [InlineData("town_a", "deport", "not built yet")]
    public void ResolveCaptureIsRefusedWithAReason(string id, string outcome, string reason)
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0));
        var before = s.Clone();
        var why = new List<Cause>();
        Assert.Contains(reason, SettlementControl.Resolve(c, s, id, outcome, why));
        Assert.Empty(why);
        Assert.Equal(before.Describe(), s.Describe());
        Assert.Contains(reason, Assert.Single(Step(c, before, new ResolveCapture(id, outcome)).Rejected).Reason);
    }

    // R-CTRL-007: a record that is not awaiting the choice (a later state, PR 6) is refused too.
    [Fact]
    public void ARecordInAnotherStateIsRefused()
    {
        var c = Load();
        var s = CombatTests.With(c, Soldier(1, 1, 0));
        s.Settlements["town_a"] = new SettlementControl(Movement.PlayerSide, "occupied", s.Date);
        Assert.Contains("not awaiting a decision", SettlementControl.Resolve(c, s, "town_a", SettlementControl.Raid, new List<Cause>()));
    }

    // R-CTRL-008 (Q-HIST-1 (a)): before the MVP's end, a capture the player does not answer waits and the units stay.
    [Fact]
    public void AnUnansweredCaptureWaits()
    {
        var c = Load();
        var next = Step(c, Awaiting(c, Soldier(1, 1, 0))).State;
        Assert.Equal(SettlementControl.AwaitingChoice, next.Settlements["town_a"].State);
        Assert.Equal((1, 0), (next.Units[1].Q, next.Units[1].R));
    }

    // R-CTRL-008 (#797, "Counts as Raid"): on the turn that reaches the game's end, a capture still waiting resolves as
    // Raid: the units leave and the ownerless marker remains.
    [Fact]
    public void ACaptureStillWaitingAtTheEndCountsAsRaid()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0));
        var twin = CombatTests.With(c, Soldier(1, 2, 0));
        s.Date = twin.Date = c.End.AddDays(-10);   // the next turn ends on the game's end
        var result = Step(c, s);
        Assert.NotNull(result.State.GameOver);
        Assert.Equal("", result.State.Settlements["town_a"].Owner);
        Assert.Equal((2, 0), (result.State.Units[1].Q, result.State.Units[1].R));
        var raid = Assert.Single(result.Why, x => x.Target == "town_a" && x.Key == CauseKeys.PendingBecameRaid);
        Assert.Equal(c.SettlementNames["town_a"] + ": no decision was made before the game ended, so it counts as a raid", raid.Message);
        var other = Step(c, twin).State;
        Assert.Equal(other.Score, result.State.Score);
        var without = result.State.Clone(); without.Settlements.Clear();
        Assert.Equal(other.Describe(), without.Describe());
    }

    // R-CTRL-008: at the end, a unit with nowhere to go stays while the others leave, and the record becomes ownerless (the game
    // is over). The lost marker still remains. Unit 1 fits on (2, 0) beside unit 7; unit 6 then has no room, and enemy units hold the other neighbours.
    [Fact]
    public void AtTheEndTheLostMarkerRemainsEvenWithNowhereToWithdraw()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0), Soldier(6, 1, 0), Soldier(7, 2, 0), Enemy(3, 0, 0), Enemy(4, 0, 1), Enemy(5, 1, 1));
        s.Date = c.End.AddDays(-10);   // the next turn ends on the game's end
        var next = Step(c, s).State;
        Assert.NotNull(next.GameOver);
        Assert.Equal("", next.Settlements["town_a"].Owner);
        Assert.Equal((2, 0), (next.Units[1].Q, next.Units[1].R));
        Assert.Equal((1, 0), (next.Units[6].Q, next.Units[6].R));
    }

    // R-CTRL-008 (#2733 item 3): the captures still waiting at the end resolve in ordinal id order, whatever order they were
    // captured in: town_c (taken first) resolves after town_a.
    [Fact]
    public void PendingCapturesResolveAtTheEndInOrdinalOrder()
    {
        var c = Load();
        var s = CombatTests.With(c, Soldier(1, 1, 0), Soldier(2, 4, 0));
        s.Settlements["town_c"] = new SettlementControl(Movement.PlayerSide, SettlementControl.AwaitingChoice, s.Date);
        s.Settlements["town_a"] = new SettlementControl(Movement.PlayerSide, SettlementControl.AwaitingChoice, s.Date);
        s.Date = c.End.AddDays(-10);   // the next turn ends on the game's end
        var why = Step(c, s).Why;
        Assert.Equal(new[] { "town_a", "town_c" }, why.Where(x => x.Key == CauseKeys.PendingBecameRaid).Select(x => x.Target));
    }

    // R-CTRL-010: a unit keeps its orders when it withdraws, as after a Move; a fortify names the place's hex, so the
    // turn's order step drops it (R-ORDER-002), and hold goes with the unit.
    [Fact]
    public void AFortifyIsLostAndHoldKeptOnWithdrawal()
    {
        var c = Load();
        var fortified = new Unit(1, "soldier", 1, 0, side: "israel", strength: 2, order: Orders.Fortify, orderTarget: "1,0");
        var holding = new Unit(2, "soldier", 1, 0, side: "israel", strength: 2, order: Orders.Hold);
        var next = Step(c, Awaiting(c, fortified, holding), Raid()).State;
        Assert.Equal("", next.Units[1].Order);
        Assert.Equal(Orders.Hold, next.Units[2].Order);
    }

    // R-CTRL-010 with R-HOUSE-003 (Q-HOUSE-5, #1130): a withdrawing formation keeps its home city, so it still counts in
    // that city's housing after it steps out.
    [Fact]
    public void AWithdrawingFormationKeepsItsHomeCity()
    {
        var c = Load();
        var formation = new Unit(1, "soldier", 1, 0, side: "israel", strength: 2, homeCity: "town_a");
        var next = Step(c, Awaiting(c, formation), Raid()).State;
        Assert.Equal(((2, 0), "town_a"), ((next.Units[1].Q, next.Units[1].R), next.Units[1].HomeCity));
    }

    // R-CTRL-009: HistoricalAI leaves out a Raid the sim would refuse, so its orders are never rejected and the capture waits.
    [Fact]
    public void HistoricalAIWaitsWhenRaidIsRefused()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0), Enemy(2, 2, 0), Enemy(3, 0, 0), Enemy(4, 0, 1), Enemy(5, 1, 1));
        Assert.DoesNotContain(HistoricalAI.Commands(c, s), x => x is ResolveCapture);
    }

    // R-CTRL-010, determinism: Raid draws nothing from the RNG.
    [Fact]
    public void RaidDrawsNothing()
    {
        var c = Load();
        var s = Awaiting(c, Soldier(1, 1, 0));
        var rng = s.RngState;
        Assert.Null(SettlementControl.Resolve(c, s, "town_a", SettlementControl.Raid, new List<Cause>()));
        Assert.Equal(rng, s.RngState);
    }
}