Files
pf1e-simulator/tests/test_area_shapes.py
T
ctan 4659fae9f6 feat(rules): support cone and line spell areas
AreaEffect gains shape: burst|cone|line (CRB Magic - Aiming a Spell). Cones (90 deg, length = radius_ft) and lines (5 ft wide, Bresenham ray) originate at the caster's cell and are aimed at the resolved target or the U7b point-cast center; bursts are unchanged. Shared per-creature save + SR application; the observation area dict already exposes the shape.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-13 10:32:37 +02:00

287 lines
11 KiB
Python

"""Cone and line spell areas (Package S1a).
PF1e (CRB, Magic - Aiming a Spell): a cone "shoots away from you in a
quarter-circle in the direction you designate"; a line "shoots away from you
in a line in the direction you designate" and "affects all creatures in
squares through which the line passes". Both originate at the CASTER and are
aimed via ``cast_spell``'s existing ``target_id`` / ``target_pos``.
Factories are reused from ``tests/test_observation_enriched.py``; every roll
is scripted. The cone spell is save-less (save="none", save_for_half=False)
so each affected creature consumes exactly its damage dice — the scripted
queues also pin the EXACT set of creatures caught in each template.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from pf1e_simulator.combat import Action, CombatantState, CombatEngine
from pf1e_simulator.dice import parse_dice
from pf1e_simulator.grid import Grid
from pf1e_simulator.map import MapSpec, TerrainType
from pf1e_simulator.rng import ScriptedRng
from pf1e_simulator.spells import (
AreaEffect,
DamageEffect,
SpellSpec,
load_spell,
)
from tests.test_observation_enriched import make_combatant, make_state
if TYPE_CHECKING:
from pathlib import Path
_CONE_SPARKS = SpellSpec(
name="Cone Sparks",
level=1,
school="evocation",
range="close",
save="none",
base_dc=10,
effects=(
AreaEffect(
shape="cone",
radius_ft=15,
effects=(
DamageEffect(formula=parse_dice("1d6"), types=("fire",)),
),
save_for_half=False,
),
),
)
_LINE_SPARKS = SpellSpec(
name="Line Sparks",
level=2,
school="evocation",
range="long",
save="none",
base_dc=10,
effects=(
AreaEffect(
shape="line",
radius_ft=120,
effects=(
DamageEffect(formula=parse_dice("1d6"), types=("electricity",)),
),
save_for_half=False,
),
),
)
_BURST_SPARKS = SpellSpec(
name="Burst Sparks",
level=3,
school="evocation",
range="long",
save="none",
base_dc=10,
effects=(
AreaEffect(
radius_ft=20,
effects=(
DamageEffect(formula=parse_dice("1d6"), types=("fire",)),
),
save_for_half=False,
),
),
)
_REGISTRY = {
"Cone Sparks": _CONE_SPARKS,
"Line Sparks": _LINE_SPARKS,
"Burst Sparks": _BURST_SPARKS,
}
def _open_grid(side: int = 12) -> Grid:
legend = {".": TerrainType(type="floor", move_cost=1)}
spec = MapSpec(name="open", terrain=tuple(["." * side] * side), legend=legend)
return Grid.from_spec(spec)
def _engine(states: list[CombatantState], rolls: list[int]) -> CombatEngine:
return CombatEngine(
ScriptedRng(rolls), _open_grid(), states, spell_registry=_REGISTRY
)
def _caster(hp: int = 100) -> CombatantState:
return make_state(
make_combatant(
"caster",
hp=hp,
spells=["Cone Sparks", "Line Sparks", "Burst Sparks"],
spell_slots={1: 9, 2: 9, 3: 9},
),
"players",
(6, 2),
)
# ---------------------------------------------------------------------------
# cone: 15 ft in front of the caster, ±45° wide, origin cell excluded
# ---------------------------------------------------------------------------
def test_cone_hits_cells_in_front_and_not_behind() -> None:
"""Given: caster (6,2); foe_front (6,4) east, foe_behind (6,0) west,
foe_side (2,2) north, foe_far (6,7) east beyond the 15-ft reach
When: cast a 15-ft cone aimed at foe_front (target_id)
Then: only foe_front (in front, within 3 cells) is hit; the behind/side
foes fall outside the ±45° quarter circle and foe_far exceeds the length.
The caster's own cell (the origin) is excluded by the cone template."""
caster = _caster()
foe_front = make_state(make_combatant("foe_front", hp=100), "monsters", (6, 4))
foe_behind = make_state(make_combatant("foe_behind", hp=100), "monsters", (6, 0))
foe_side = make_state(make_combatant("foe_side", hp=100), "monsters", (2, 2))
foe_far = make_state(make_combatant("foe_far", hp=100), "monsters", (6, 7))
eng = _engine([caster, foe_front, foe_behind, foe_side, foe_far], [4])
result = eng._execute(
caster, Action(kind="cast_spell", target_id="foe_front", spell_name="Cone Sparks")
)
assert result.ok
assert foe_front.hp == 100 - 4
assert foe_behind.hp == 100
assert foe_side.hp == 100
assert foe_far.hp == 100
assert caster.hp == 100 # origin cell excluded — the caster is never caught
def test_cone_covers_the_full_90_degree_arc() -> None:
"""Given: caster (6,2) aiming east; foes on the straight axis (6,4), the
45° edge (4,4) and just off the edge (3,4)
When: the 15-ft cone resolves
Then: the axis and the 45° edge cell are inside the quarter circle; the
off-edge cell (beyond ±45°) is untouched."""
caster = _caster()
foe_axis = make_state(make_combatant("foe_axis", hp=100), "monsters", (6, 4))
foe_edge = make_state(make_combatant("foe_edge", hp=100), "monsters", (4, 4))
foe_outside = make_state(make_combatant("foe_outside", hp=100), "monsters", (3, 4))
eng = _engine([caster, foe_axis, foe_edge, foe_outside], [4, 4, 4])
eng._execute(
caster, Action(kind="cast_spell", target_id="foe_axis", spell_name="Cone Sparks")
)
assert foe_axis.hp == 100 - 4
assert foe_edge.hp == 100 - 4
assert foe_outside.hp == 100
def test_cone_directed_by_target_pos_point_cast() -> None:
"""Given: the same scene, no target_id — the cone is aimed at an explicit
cell (6,5) via target_pos (U7b point cast)
When: the point cast resolves
Then: the direction is caster -> point; the foe at (6,4) on that bearing
is caught, the north foe is not."""
caster = _caster()
foe_in = make_state(make_combatant("foe_in", hp=100), "monsters", (6, 4))
foe_out = make_state(make_combatant("foe_out", hp=100), "monsters", (2, 2))
eng = _engine([caster, foe_in, foe_out], [4])
result = eng._execute(
caster, Action(kind="cast_spell", spell_name="Cone Sparks", target_pos=(6, 5))
)
assert result.ok
assert foe_in.hp == 100 - 4
assert foe_out.hp == 100
# ---------------------------------------------------------------------------
# line: 120 ft straight ray, 5 ft wide — only the cells it passes through
# ---------------------------------------------------------------------------
def test_line_hits_the_straight_ray_only() -> None:
"""Given: caster (6,2) aiming east at foe_ray (6,5); foe_off (5,5) sits a
row away, foe_west (6,0) is behind the caster
When: cast the 120-ft line aimed at foe_ray
Then: every creature ON the ray (the caster's own cell — the line starts
at your fingertips — and foe_ray) takes damage; the off-row and the
behind-caster foes are outside the 5-ft-wide template."""
caster = _caster()
foe_ray = make_state(make_combatant("foe_ray", hp=100), "monsters", (6, 5))
foe_off = make_state(make_combatant("foe_off", hp=100), "monsters", (5, 5))
foe_west = make_state(make_combatant("foe_west", hp=100), "monsters", (6, 0))
eng = _engine([caster, foe_ray, foe_off, foe_west], [4, 4])
result = eng._execute(
caster, Action(kind="cast_spell", target_id="foe_ray", spell_name="Line Sparks")
)
assert result.ok
assert foe_ray.hp == 100 - 4
assert caster.hp == 100 - 4 # the line's first square is the caster's own
assert foe_off.hp == 100
assert foe_west.hp == 100
def test_line_follows_a_diagonal_bresenham_ray() -> None:
"""Given: caster (6,2) aiming diagonally at foe_diag (2,6)
When: the line resolves
Then: the diagonal ray cells are affected (foe_diag is hit) while a cell
beside the ray (foe_off at (2,5)) is not."""
caster = _caster()
foe_diag = make_state(make_combatant("foe_diag", hp=100), "monsters", (2, 6))
foe_off = make_state(make_combatant("foe_off", hp=100), "monsters", (2, 5))
eng = _engine([caster, foe_diag, foe_off], [4, 4])
eng._execute(
caster, Action(kind="cast_spell", target_id="foe_diag", spell_name="Line Sparks")
)
assert foe_diag.hp == 100 - 4
assert foe_off.hp == 100
# ---------------------------------------------------------------------------
# burst: unchanged circle around the resolved center
# ---------------------------------------------------------------------------
def test_burst_still_centers_on_the_target_and_ignores_the_caster_side() -> None:
"""Given: caster (6,2); foe_in (4,4) and foe_out (2,0) around a burst
centered on target_id=foe_in (radius 20 ft = 4 cells)
When: the burst cast resolves
Then: the circle around the TARGET cell decides — foe_in (distance 0)
and the caster (distance 3) are inside, foe_out (distance 5) is not."""
caster = _caster()
foe_in = make_state(make_combatant("foe_in", hp=100), "monsters", (4, 4))
foe_out = make_state(make_combatant("foe_out", hp=100), "monsters", (2, 0))
eng = _engine([caster, foe_in, foe_out], [4, 4, 4])
eng._execute(
caster, Action(kind="cast_spell", target_id="foe_in", spell_name="Burst Sparks")
)
assert foe_in.hp == 100 - 4
assert caster.hp == 100 - 4
assert foe_out.hp == 100
# ---------------------------------------------------------------------------
# loader: shape parsing
# ---------------------------------------------------------------------------
def test_loader_parses_cone_and_line_shapes(tmp_path: Path) -> None:
"""Given: area effects declaring "shape": "cone" / "line" / (absent)
When: load_spell parses them
Then: the shapes are carried on the AreaEffect; an absent shape defaults
to "burst"; an unknown shape is a data error."""
spell_file = tmp_path / "shapes.json"
spell_file.write_text(
'{"name": "Shapes", "level": 1, "school": "evocation",'
' "range": "close", "save": "ref", "effects": ['
' {"type": "area", "shape": "cone", "radius_ft": 15, "effects": []},'
' {"type": "area", "shape": "line", "radius_ft": 120, "effects": []},'
' {"type": "area", "radius_ft": 20, "effects": []}]}'
)
spell = load_spell(spell_file)
areas = [e for e in spell.effects if isinstance(e, AreaEffect)]
assert [a.shape for a in areas] == ["cone", "line", "burst"]
bad_file = tmp_path / "bad_shape.json"
bad_file.write_text(
'{"name": "Bad", "level": 1, "school": "evocation",'
' "range": "close", "save": "none", "effects": ['
' {"type": "area", "shape": "spiral", "radius_ft": 10, "effects": []}]}'
)
with pytest.raises(ValueError, match="unknown area shape"):
load_spell(bad_file)