74f5d64c43
SRD Sunder + CRB 'Smashing an Object' / 'Conditions - Broken': weapons carry Table 7-12 object stats (AttackSpec.hardness/hit_points/enhancement, defaults by weapon class); a successful sunder deals the attacker's actual physical weapon damage minus the target weapon's hardness, subtracted from its hp. At <= half hp the weapon gains the broken condition - still usable at -2 attack/damage, crits only on a natural 20 at x2. At <= 0 it is destroyed (weapon_destroyed, unusable). The old flat str_mod+2 / durability 10 / synthetic 'disarmed' condition is gone. Firearm misfire carve-out kept (broken firearms stay unusable until Quick Clear - misfire escalation not modeled, documented).
211 lines
7.3 KiB
Python
211 lines
7.3 KiB
Python
"""Tests for the ``repair_weapon`` action (repairing a broken weapon).
|
|
|
|
L8 of the ``llm-policy-contract`` plan: ``Action`` gains a ``repair_weapon``
|
|
kind (reusing the existing ``weapon_id`` field) so a policy can restore a
|
|
weapon that a sunder maneuver broke. The weapon is removed from
|
|
``state.weapon_broken``, its accumulated damage is reset, and the weapon is
|
|
fully usable again via ``usable_attacks()``. A weapon DESTROYED by a sunder
|
|
(hp <= 0) cannot be repaired.
|
|
|
|
No default policy emits ``repair_weapon``; it is only accepted by ``_execute``
|
|
(usage via custom policy / LLM).
|
|
"""
|
|
|
|
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 _soft_sword_combatant(cid: str, *, attack_bonus: int = 2) -> Combatant:
|
|
"""Combatant with a authored soft sword (hardness 0, 10 hp) so one
|
|
scripted 1d4+3 sunder breaks it."""
|
|
attack = AttackSpec(
|
|
id=f"{cid}-w",
|
|
name="sword",
|
|
kind="melee",
|
|
attack_bonus=attack_bonus,
|
|
damage=[DamageComponent(formula="1d4", types=["slashing"])],
|
|
hardness=0,
|
|
hit_points=10,
|
|
)
|
|
return make_combatant(cid, attack_bonus=attack_bonus).model_copy(
|
|
update={"attacks": [attack]}
|
|
)
|
|
|
|
|
|
def _heavy_striker(cid: str) -> Combatant:
|
|
attack = AttackSpec(
|
|
id=f"{cid}-w",
|
|
name="sword",
|
|
kind="melee",
|
|
attack_bonus=2,
|
|
damage=[DamageComponent(formula="1d4", types=["slashing"])],
|
|
damage_bonus=3,
|
|
)
|
|
return make_combatant(cid, attack_bonus=2).model_copy(
|
|
update={"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 _weapon_ids(state: CombatantState) -> list[str]:
|
|
return [w.id for w in state.usable_attacks()]
|
|
|
|
|
|
# --- Test 1: repair restores a broken weapon ---------------------------------
|
|
|
|
def test_repair_restores_broken_weapon() -> None:
|
|
a_spec = make_combatant("a", attack_bonus=2)
|
|
b_spec = make_combatant("b")
|
|
a = make_state(a_spec, "players", (1, 1))
|
|
b = make_state(b_spec, "monsters", (1, 2))
|
|
b.weapon_damage = {"b-w": 10}
|
|
b.weapon_broken = {"b-w"}
|
|
engine = make_engine([], [a, b])
|
|
engine._execute(b, Action(kind="repair_weapon", weapon_id="b-w"))
|
|
assert not b.weapon_broken
|
|
assert not b.weapon_damage
|
|
assert "b-w" in _weapon_ids(b)
|
|
assert "repair b-w (restored)" in engine._transcript[-1]
|
|
|
|
|
|
# --- Test 2: weapon_id=None -> nothing to repair, state unchanged ------------
|
|
|
|
def test_repair_none_logs_nothing_to_repair() -> None:
|
|
a_spec = make_combatant("a", attack_bonus=2)
|
|
b_spec = make_combatant("b")
|
|
a = make_state(a_spec, "players", (1, 1))
|
|
b = make_state(b_spec, "monsters", (1, 2))
|
|
b.weapon_damage = {"b-w": 10}
|
|
b.weapon_broken = {"b-w"}
|
|
engine = make_engine([], [a, b])
|
|
engine._execute(b, Action(kind="repair_weapon"))
|
|
assert "nothing to repair" in engine._transcript[-1]
|
|
assert b.weapon_broken == {"b-w"}
|
|
assert b.weapon_damage == {"b-w": 10}
|
|
|
|
|
|
# --- Test 3: id never damaged -> log, state unchanged ------------------------
|
|
|
|
def test_repair_unknown_id_logs_nothing_to_repair() -> None:
|
|
a_spec = make_combatant("a", attack_bonus=2)
|
|
b_spec = make_combatant("b")
|
|
a = make_state(a_spec, "players", (1, 1))
|
|
b = make_state(b_spec, "monsters", (1, 2))
|
|
engine = make_engine([], [a, b])
|
|
engine._execute(b, Action(kind="repair_weapon", weapon_id="b-w"))
|
|
assert "nothing to repair" in engine._transcript[-1]
|
|
assert not b.weapon_broken
|
|
assert not b.weapon_damage
|
|
|
|
|
|
# --- Test 4: real sunder then repair restores the weapon ---------------------
|
|
|
|
def test_sunder_then_repair_restores_weapon() -> None:
|
|
# a's 1d4+3 (scripted 4+3=7) vs b's soft sword (hardness 0, 10 hp):
|
|
# 3 hp left <= half -> broken (still usable at -2, CRB "Broken"); the
|
|
# repair action resets the damage and clears the condition.
|
|
a = make_state(_heavy_striker("a"), "players", (1, 1))
|
|
b = make_state(_soft_sword_combatant("b"), "monsters", (1, 2))
|
|
engine = make_engine([1, 12, 4], [a, b])
|
|
engine._execute(a, Action(kind="maneuver", target_id="b", maneuver="sunder"))
|
|
assert "b-w" in b.weapon_broken
|
|
assert "b-w" in _weapon_ids(b)
|
|
engine._execute(b, Action(kind="repair_weapon", weapon_id="b-w"))
|
|
assert not b.weapon_broken
|
|
assert not b.weapon_damage
|
|
assert "b-w" in _weapon_ids(b)
|
|
assert "repair b-w (restored)" in engine._transcript[-1]
|
|
|
|
|
|
# --- Test 4b: a destroyed weapon cannot be repaired --------------------------
|
|
|
|
def test_repair_destroyed_weapon_is_refused() -> None:
|
|
# CRB "Smashing an Object": hp <= 0 is "ruined" — repair_weapon is a
|
|
# no-op on destroyed weapons.
|
|
a_spec = make_combatant("a", attack_bonus=2)
|
|
b_spec = make_combatant("b")
|
|
a = make_state(a_spec, "players", (1, 1))
|
|
b = make_state(b_spec, "monsters", (1, 2))
|
|
b.weapon_damage = {"b-w": 10}
|
|
b.weapon_destroyed.add("b-w")
|
|
engine = make_engine([], [a, b])
|
|
engine._execute(b, Action(kind="repair_weapon", weapon_id="b-w"))
|
|
assert "b-w" in b.weapon_destroyed
|
|
assert b.weapon_damage == {"b-w": 10}
|
|
assert "weapon destroyed" in engine._transcript[-1]
|
|
|
|
|
|
# --- Test 5: determinism -----------------------------------------------------
|
|
|
|
def _run_with_repair() -> 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))
|
|
b.weapon_damage = {"b-w": 10}
|
|
b.weapon_broken = {"b-w"}
|
|
|
|
def policy(engine: CombatEngine, state: CombatantState) -> tuple[Action, ...]:
|
|
if state.combatant.id == "b":
|
|
return (Action(kind="repair_weapon", weapon_id="b-w"),)
|
|
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_repair() == _run_with_repair()
|