From b640e29a91cda899f7110835b5f38ba4bcd7e4c2 Mon Sep 17 00:00:00 2001 From: ctan Date: Sat, 5 Sep 2026 18:49:56 +0200 Subject: [PATCH] feat(combat): add trick_condition field to Action for dirty trick --- src/pf1e_simulator/combat.py | 26 ++++-- tests/test_dirty_trick.py | 169 +++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 tests/test_dirty_trick.py diff --git a/src/pf1e_simulator/combat.py b/src/pf1e_simulator/combat.py index dd21d14..585641f 100644 --- a/src/pf1e_simulator/combat.py +++ b/src/pf1e_simulator/combat.py @@ -111,6 +111,8 @@ if TYPE_CHECKING: from pf1e_simulator.rng import Rng from pf1e_simulator.spells import SpellSpec + TrickCondition = Literal["dazzled", "blinded", "deafened", "entangled", "nauseated"] + from pf1e_simulator.los import ( has_cover_cells, has_line_of_effect_cells, @@ -367,6 +369,7 @@ class Action: class_ability: str | None = None # "deed"|"spell_combat"|"inspire_courage"|"hex"|"channel" channel_mode: Literal["heal", "harm"] | None = None # channel energy polarity for this use direction: tuple[int, int] | None = None # steering for directional maneuvers + trick_condition: TrickCondition | None = None # dirty trick condition choice @dataclass(frozen=True) @@ -1468,7 +1471,10 @@ class CombatEngine: if kind == "cast_spell": self._execute_cast_spell(state, target, action.spell_name) elif kind == "maneuver": - self._execute_maneuver(state, target, action.maneuver, direction=action.direction) + self._execute_maneuver( + state, target, action.maneuver, + direction=action.direction, trick_condition=action.trick_condition, + ) elif kind in ("channel_energy", "hex", "deed", "spell_combat", "inspire_courage"): self._execute_class_ability(state, target, action) elif kind == "charge": @@ -1572,6 +1578,7 @@ class CombatEngine: maneuver: str | None, *, direction: tuple[int, int] | None = None, + trick_condition: TrickCondition | None = None, ) -> None: """Execute a combat maneuver (trip, disarm, bull_rush, sunder, grapple, escape, overrun, dirty_trick, steal, reposition, drag).""" @@ -1605,7 +1612,9 @@ class CombatEngine: "sunder": lambda: self._maneuver_sunder(state, target, m), "grapple": lambda: self._maneuver_grapple(state, target, m), "overrun": lambda: self._maneuver_overrun(state, target, m), - "dirty_trick": lambda: self._maneuver_dirty_trick(state, target, m), + "dirty_trick": lambda: self._maneuver_dirty_trick( + state, target, m, trick_condition=trick_condition + ), "steal": lambda: self._maneuver_steal(state, target, m), "reposition": lambda: self._maneuver_reposition(state, target, m, direction=direction), "drag": lambda: self._maneuver_drag(state, target, m, direction=direction), @@ -1785,14 +1794,21 @@ class CombatEngine: ) def _maneuver_dirty_trick( - self, state: CombatantState, target: CombatantState, m: _ManeuverRoll + self, + state: CombatantState, + target: CombatantState, + m: _ManeuverRoll, + *, + trick_condition: TrickCondition | None = None, ) -> None: if not m.success: self._maneuver_fail(state, target, "dirty_trick", m) return + condition_name = trick_condition or "dazzled" + condition = CONDITIONS_BY_NAME[condition_name] target.active_conditions.append( ActiveCondition( - condition=CONDITIONS_BY_NAME["dazzled"], + condition=condition, rounds_left=1, source="maneuver: dirty trick", ) @@ -1800,7 +1816,7 @@ class CombatEngine: self._log( f"round {self._current_round} {state.combatant.id}: dirty trick vs " f"{target.combatant.id} d20={m.roll}+{m.attacker_cmb}={m.total} " - f"CMD {m.target_cmd} -> SUCCESS (dazzled)" + f"CMD {m.target_cmd} -> SUCCESS ({condition_name})" ) def _maneuver_steal( diff --git a/tests/test_dirty_trick.py b/tests/test_dirty_trick.py new file mode 100644 index 0000000..c91c915 --- /dev/null +++ b/tests/test_dirty_trick.py @@ -0,0 +1,169 @@ +"""Tests for the ``trick_condition`` field on the dirty trick maneuver. + +L6 of the ``llm-policy-contract`` plan: ``Action`` gains a +``trick_condition`` field that lets a policy choose which condition a +successful dirty trick applies (blinded, deafened, entangled, nauseated) +instead of always applying ``dazzled``. + +When ``trick_condition`` is ``None`` the previous behavior (``dazzled``) is +kept bit-identical. +""" + +from __future__ import annotations + +from pf1e_simulator.combat import Action, CombatantState, CombatEngine +from pf1e_simulator.grid import Grid +from pf1e_simulator.map import MapSpec, TerrainType +from pf1e_simulator.models import ( + AbilityScores, + ACProfile, + AttackSpec, + Combatant, + DamageComponent, + Saves, +) +from pf1e_simulator.rng import ScriptedRng, SeededRng + + +def make_combatant(cid: str, *, attack_bonus: int = 2) -> Combatant: + attack = AttackSpec( + id=f"{cid}-w", + name="sword", + kind="melee", + attack_bonus=attack_bonus, + damage=[DamageComponent(formula="1d4", types=["slashing"])], + ) + return Combatant( + id=cid, + name=cid, + level=1, + size="Medium", + abilities=AbilityScores( + str_score=10, dex_score=10, con_score=10, + int_score=10, wis_score=10, cha_score=10, + ), + hp_max=6, + ac=ACProfile(total=13, touch=13, flat_footed=13), + bab=attack_bonus, + initiative_mod=0, + speed_land_ft=30, + saves=Saves(fort=0, ref=0, will=0), + attacks=[attack], + ) + + +def make_state(combatant: Combatant, side: str, pos: tuple[int, int]) -> CombatantState: + return CombatantState(combatant=combatant, side=side, pos=pos, hp=combatant.hp_max) + + +def make_grid() -> Grid: + legend = {".": TerrainType(type="floor", move_cost=1)} + spec = MapSpec(name="test", terrain=tuple(["." * 8] * 8), legend=legend) + return Grid.from_spec(spec) + + +def make_engine(queue: list[int], states: list[CombatantState]) -> CombatEngine: + return CombatEngine(ScriptedRng(queue), make_grid(), states) + + +def _condition_names(state: CombatantState) -> list[str]: + return [ac.condition.name for ac in state.active_conditions] + + +# --- Test 1: explicit trick_condition applies that condition ----------------- + +def test_dirty_trick_blinded() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([12], [a, b]) + engine._execute_maneuver(a, b, "dirty_trick", trick_condition="blinded") + assert _condition_names(b) == ["blinded"] + + +# --- Test 2: trick_condition=None keeps historical dazzled ------------------- + +def test_dirty_trick_default_dazzled() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([12], [a, b]) + engine._execute_maneuver(a, b, "dirty_trick") + assert _condition_names(b) == ["dazzled"] + + +# --- Test 3: each other valid condition -------------------------------------- + +def test_dirty_trick_deafened() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([12], [a, b]) + engine._execute_maneuver(a, b, "dirty_trick", trick_condition="deafened") + assert _condition_names(b) == ["deafened"] + + +def test_dirty_trick_entangled() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([12], [a, b]) + engine._execute_maneuver(a, b, "dirty_trick", trick_condition="entangled") + assert _condition_names(b) == ["entangled"] + + +def test_dirty_trick_nauseated() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([12], [a, b]) + engine._execute_maneuver(a, b, "dirty_trick", trick_condition="nauseated") + assert _condition_names(b) == ["nauseated"] + + +# --- Test 4: failed maneuver applies no condition ---------------------------- + +def test_dirty_trick_failure_no_condition() -> None: + a_spec = make_combatant("a") + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + engine = make_engine([1], [a, b]) # roll 1 -> automatic failure + engine._execute_maneuver(a, b, "dirty_trick", trick_condition="blinded") + assert _condition_names(b) == [] + + +# --- Test 5: determinism ----------------------------------------------------- + +def _run_with_trick_condition(trick_condition: str | None) -> tuple[str, ...]: + a_spec = make_combatant("a", attack_bonus=5) + b_spec = make_combatant("b") + a = make_state(a_spec, "players", (1, 1)) + b = make_state(b_spec, "monsters", (1, 2)) + + def policy(engine: CombatEngine, state: CombatantState) -> tuple[Action, ...]: + if state.combatant.id == "a": + return ( + Action( + kind="maneuver", target_id="b", maneuver="dirty_trick", + trick_condition=trick_condition, + ), + ) + return (Action(kind="wait"),) + + engine = CombatEngine(SeededRng(42), make_grid(), [a, b], policy=policy) + return engine.run().transcript + + +def test_determinism_same_seed_same_transcript() -> None: + assert _run_with_trick_condition("blinded") == _run_with_trick_condition("blinded") + assert _run_with_trick_condition(None) == _run_with_trick_condition(None) + + +def test_trick_condition_changes_transcript() -> None: + assert _run_with_trick_condition("blinded") != _run_with_trick_condition(None)