feat(rules): 3D flight over ground terrain - airborne creatures (fly >= land, V4 budget from fly speed) move on Grid.airborne_view: ground difficult terrain costs 1 and cells impassable to walkers (pits, water, rubble, low obstacles) are crossable at cost 1, EXCEPT blocks_los cells (solid barriers, assumed full height) which still stop flight; run/charge/5-foot-step gates and diagonal corner rule follow the same view (flying past a pit is legal, past a wall corner is not); elevation climb cost still applies (step heights, not wall heights - documented); line of effect, cover and high ground use the unchanged real grid so combat geometry is unaffected by flight (SRD Movement, Position and Distance: flying creatures not hampered by difficult terrain, able to avoid most obstacles), 11 tests
This commit is contained in:
+283
-7
@@ -1,14 +1,18 @@
|
||||
"""V4: movement uses the best of land and fly speed.
|
||||
"""V4: movement uses the best of land and fly speed + 3D flight over terrain.
|
||||
|
||||
The engine budgets every movement action (move, 5-foot step toward a target,
|
||||
charge at 2x, withdraw at 2x, run at 4x) from ``_speed_ft(state)`` — the
|
||||
maximum of the creature's ``speed_land_ft`` and ``speed_fly_ft`` (a missing
|
||||
fly speed counts as 0, so a land-only creature is unchanged). Simplification:
|
||||
a creature that has a fly speed is assumed to use it, but flying OVER
|
||||
obstacles or difficult terrain is NOT modeled — the grid's passability and
|
||||
terrain costs still apply to the whole budget. The observation snapshot adds
|
||||
an additive ``speed_effective_ft`` key; ``speed_land_ft``/``speed_fly_ft`` are
|
||||
kept as-is.
|
||||
fly speed counts as 0, so a land-only creature is unchanged). A creature
|
||||
whose budget comes from its fly speed (fly >= land) is AIRBORNE
|
||||
(``_is_airborne``): it moves on ``Grid.airborne_view``, where ground difficult
|
||||
terrain costs 1 and cells impassable to walkers (pits, water, rubble) are
|
||||
crossable — SRD "Movement, Position, and Distance": "Flying and incorporeal
|
||||
creatures are not hampered by difficult terrain"; "Flying and incorporeal
|
||||
creatures are able to avoid most obstacles". Cells that block line of effect
|
||||
(``blocks_los`` — solid barriers, assumed full height) still stop flight and
|
||||
still block attacks. The observation snapshot adds an additive
|
||||
``speed_effective_ft`` key; ``speed_land_ft``/``speed_fly_ft`` are kept as-is.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -77,6 +81,45 @@ def _corridor(cols: int) -> Grid:
|
||||
return Grid.from_spec(spec)
|
||||
|
||||
|
||||
_LEGEND = {
|
||||
".": TerrainType(type="floor", move_cost=1),
|
||||
"T": TerrainType(type="rubble", move_cost=2),
|
||||
"P": TerrainType(type="pit", move_cost=None),
|
||||
"#": TerrainType(type="wall", move_cost=None, blocks_los=True),
|
||||
}
|
||||
|
||||
|
||||
def _terrain_grid(rows: tuple[str, ...]) -> Grid:
|
||||
"""A grid from ASCII rows over the shared ``_LEGEND`` terrain types."""
|
||||
spec = MapSpec(name="terrain", terrain=rows, legend=_LEGEND)
|
||||
return Grid.from_spec(spec)
|
||||
|
||||
|
||||
def _run_move_on(
|
||||
*, land: int, fly: int | None, destination: tuple[int, int], grid: Grid,
|
||||
) -> CombatantState:
|
||||
"""Hero acts first and takes one ``move`` to ``destination`` on ``grid``.
|
||||
|
||||
The foe idles far to the right (never threatens). Returns the hero state.
|
||||
"""
|
||||
cols = grid.width
|
||||
hero = make_state(
|
||||
_combatant("hero", land=land, fly=fly, initiative_mod=2), "players", (0, 0),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (0, cols - 1))
|
||||
|
||||
def policy(_engine: CombatEngine, state: CombatantState) -> tuple[Action, ...]:
|
||||
if state.combatant.id == "hero":
|
||||
return (Action(kind="move", destination=destination),)
|
||||
return _WAIT
|
||||
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), grid, [hero, foe], policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
return eng._states[0]
|
||||
|
||||
|
||||
def _run_move(
|
||||
*, land: int, fly: int | None, destination: tuple[int, int], cols: int,
|
||||
) -> CombatantState:
|
||||
@@ -220,3 +263,236 @@ def test_fly_lower_than_land_uses_land() -> None:
|
||||
assert mover.pos == (0, 6)
|
||||
mover = _run_move(land=30, fly=10, destination=(0, 7), cols=12)
|
||||
assert mover.pos == (0, 0)
|
||||
|
||||
|
||||
# ── 3D flight over ground terrain (airborne creatures) ────────────────────────
|
||||
#
|
||||
# SRD "Movement, Position, and Distance" (Terrain and Obstacles): "Flying and
|
||||
# incorporeal creatures are not hampered by difficult terrain" and "Flying and
|
||||
# incorporeal creatures are able to avoid most obstacles." An airborne creature
|
||||
# (fly >= land, ``_is_airborne``) moves on ``Grid.airborne_view``: ground
|
||||
# difficult terrain costs 1 and cells impassable to walkers (pits) are
|
||||
# crossable, EXCEPT cells that block line of effect (solid barriers, assumed
|
||||
# full height) which still stop flight. Walkers are bit-identical. The foe sits
|
||||
# two rows below the hero's lane so it never threatens the row being crossed.
|
||||
|
||||
_LEGEND = {
|
||||
".": TerrainType(type="floor", move_cost=1),
|
||||
"T": TerrainType(type="rubble", move_cost=2),
|
||||
"P": TerrainType(type="pit", move_cost=None),
|
||||
"#": TerrainType(type="wall", move_cost=None, blocks_los=True),
|
||||
}
|
||||
|
||||
|
||||
def _lane_grid(lane: str) -> Grid:
|
||||
"""A 3-row grid: the terrain ``lane`` on row 0, floor rows below."""
|
||||
width = len(lane)
|
||||
rows = (lane, "." * width, "." * width)
|
||||
spec = MapSpec(name="lane", terrain=rows, legend=_LEGEND)
|
||||
return Grid.from_spec(spec)
|
||||
|
||||
|
||||
def _move_on_lane(*, land: int, fly: int | None, lane: str,
|
||||
destination: tuple[int, int]) -> tuple[int, int]:
|
||||
"""Hero (row 0, col 0) acts first and takes one ``move`` to ``destination``.
|
||||
|
||||
The foe idles at (2, 0), two rows below and never threatening. Returns the
|
||||
hero's final position.
|
||||
"""
|
||||
hero = make_state(
|
||||
_combatant("hero", land=land, fly=fly, initiative_mod=2), "players", (0, 0),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (2, 0))
|
||||
|
||||
def policy(_engine: CombatEngine, state: CombatantState) -> tuple[Action, ...]:
|
||||
if state.combatant.id == "hero":
|
||||
return (Action(kind="move", destination=destination),)
|
||||
return _WAIT
|
||||
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), _lane_grid(lane), [hero, foe],
|
||||
policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
return eng._states[0].pos
|
||||
|
||||
|
||||
def test_flyer_crosses_rubble_at_normal_cost() -> None:
|
||||
# Rubble at cols 4-5: the flyer prices every cell at 1, so (0, 9) costs
|
||||
# 9 cells and is within the 16-cell (fly 80) budget.
|
||||
assert _move_on_lane(land=30, fly=80, lane="....TT....", destination=(0, 9)) == (0, 9)
|
||||
|
||||
|
||||
def test_walker_still_pays_for_rubble() -> None:
|
||||
# Same lane, same 10-cell span: a land-30 walker pays 3 + 2*2 + 4 = 11
|
||||
# cells for (0, 9), beyond its 6-cell budget, so it is rejected in place.
|
||||
assert _move_on_lane(land=30, fly=None, lane="....TT....", destination=(0, 9)) == (0, 0)
|
||||
|
||||
|
||||
def test_flyer_crosses_pit_impassable_to_walkers() -> None:
|
||||
# A pit (move_cost None, no blocks_los) at col 3: the flyer avoids the
|
||||
# obstacle and reaches (0, 7); the walker cannot enter the pit cell.
|
||||
assert _move_on_lane(land=30, fly=80, lane="...P....", destination=(0, 7)) == (0, 7)
|
||||
assert _move_on_lane(land=30, fly=None, lane="...P....", destination=(0, 7)) == (0, 0)
|
||||
|
||||
|
||||
def test_flyer_still_stopped_by_wall() -> None:
|
||||
# A wall column (blocks_los) is a solid barrier assumed full height: it
|
||||
# stops flight just like walking — no crossing and no detour.
|
||||
hero = make_state(
|
||||
_combatant("hero", land=30, fly=80, initiative_mod=2), "players", (0, 0),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (2, 7))
|
||||
|
||||
def policy(_engine: CombatEngine, state: CombatantState) -> tuple[Action, ...]:
|
||||
if state.combatant.id == "hero":
|
||||
return (Action(kind="move", destination=(0, 7)),)
|
||||
return _WAIT
|
||||
|
||||
spec = MapSpec(
|
||||
name="wall", terrain=("...#....", "...#....", "...#...."), legend=_LEGEND,
|
||||
)
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), Grid.from_spec(spec), [hero, foe],
|
||||
policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
assert eng._states[0].pos == (0, 0)
|
||||
|
||||
|
||||
def test_flyer_runs_over_rubble_walker_run_stops() -> None:
|
||||
# Run is a straight line: an airborne runner is not hampered by the
|
||||
# rubble; a walker's run stops in front of it (CRB "Difficult Terrain":
|
||||
# "You can't run or charge across difficult terrain").
|
||||
def _run(land: int, fly: int | None) -> tuple[int, int]:
|
||||
hero = make_state(
|
||||
_combatant("hero", land=land, fly=fly, initiative_mod=2), "players", (0, 0),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (2, 0))
|
||||
|
||||
def policy(_e: CombatEngine, s: CombatantState) -> tuple[Action, ...]:
|
||||
if s.combatant.id == "hero":
|
||||
return (Action(kind="run", destination=(0, 9)),)
|
||||
return _WAIT
|
||||
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), _lane_grid("....TT...."),
|
||||
[hero, foe], policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
return eng._states[0].pos
|
||||
|
||||
assert _run(30, 80) == (0, 9)
|
||||
# The walker's run destination lies beyond difficult terrain: the
|
||||
# reachability verdict and the walked line agree, so the run is rejected
|
||||
# outright (unreachable_destination) and the walker never starts.
|
||||
assert _run(30, None) == (0, 0)
|
||||
|
||||
|
||||
def test_flyer_5foot_step_into_rubble() -> None:
|
||||
# SRD "Take 5-Foot Step": the step is legal only if movement isn't
|
||||
# hampered by difficult terrain — a flyer's isn't, a walker's is.
|
||||
def _step(land: int, fly: int | None) -> tuple[int, int]:
|
||||
hero = make_state(
|
||||
_combatant("hero", land=land, fly=fly, initiative_mod=2), "players", (0, 3),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (2, 0))
|
||||
|
||||
def policy(_e: CombatEngine, s: CombatantState) -> tuple[Action, ...]:
|
||||
if s.combatant.id == "hero":
|
||||
return (Action(kind="5foot_step", destination=(0, 4)),)
|
||||
return _WAIT
|
||||
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), _lane_grid("...T...."),
|
||||
[hero, foe], policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
return eng._states[0].pos
|
||||
|
||||
assert _step(30, 80) == (0, 4)
|
||||
assert _step(30, None) == (0, 3)
|
||||
|
||||
|
||||
def test_movement_cost_ft_uses_the_air_view() -> None:
|
||||
# Cost from the hero's cell to the target's anchor: rubble prices at 1 for
|
||||
# the flyer (8 cells = 40 ft) and at 2 for the walker (13 cells = 65 ft,
|
||||
# beyond its 30 ft speed so None).
|
||||
grid = _lane_grid("..TTTTT..")
|
||||
flyer = make_state(_combatant("flyer", land=30, fly=80), "players", (0, 0))
|
||||
walker = make_state(_combatant("walker", land=30), "players", (0, 0))
|
||||
target_foe = make_state(_combatant("target", land=30), "monsters", (0, 8))
|
||||
eng = CombatEngine(ScriptedRng([]), grid, [flyer, target_foe])
|
||||
assert eng.movement_cost_ft(flyer, target_foe) == 40
|
||||
target_walker = make_state(_combatant("target", land=30), "monsters", (0, 8))
|
||||
eng2 = CombatEngine(ScriptedRng([]), grid, [walker, target_walker])
|
||||
assert eng2.movement_cost_ft(walker, target_walker) is None
|
||||
|
||||
|
||||
def test_fly_equal_to_land_is_airborne() -> None:
|
||||
# fly >= land: the budget comes from the fly speed, so the creature flies
|
||||
# (6-cell budget, rubble priced at 1, (0, 6) reachable).
|
||||
assert _move_on_lane(land=30, fly=30, lane="....TT....", destination=(0, 6)) == (0, 6)
|
||||
|
||||
|
||||
def test_fly_slower_than_land_walks_and_pays() -> None:
|
||||
# fly < land: the creature walks (budget = land) and pays the rubble cost
|
||||
# like any walker — (0, 9) costs 11 cells > the 6-cell budget.
|
||||
assert _move_on_lane(land=30, fly=10, lane="....TT....", destination=(0, 9)) == (0, 0)
|
||||
|
||||
|
||||
def test_flyer_still_pays_elevation_climb() -> None:
|
||||
# The elevation layer stores STEP heights, not wall heights, so the
|
||||
# climbing cost still applies to flight (documented simplification): with
|
||||
# a 2-cell budget (fly 10 = land 10) the step up onto the higher ledge
|
||||
# costs 2 and only the flat neighbour is reachable.
|
||||
legend = {".": TerrainType(type="floor", move_cost=1)}
|
||||
spec = MapSpec(
|
||||
name="ledge", terrain=("....", "...."), legend=legend,
|
||||
elevation=("..1.", "...."),
|
||||
)
|
||||
grid = Grid.from_spec(spec)
|
||||
|
||||
def _go(destination: tuple[int, int]) -> tuple[int, int]:
|
||||
hero = make_state(
|
||||
_combatant("hero", land=10, fly=10, initiative_mod=2), "players", (0, 0),
|
||||
)
|
||||
foe = make_state(_combatant("foe", land=30), "monsters", (1, 3))
|
||||
|
||||
def policy(_e: CombatEngine, s: CombatantState) -> tuple[Action, ...]:
|
||||
if s.combatant.id == "hero":
|
||||
return (Action(kind="move", destination=destination),)
|
||||
return _WAIT
|
||||
|
||||
eng = CombatEngine(
|
||||
ScriptedRng([10, 5]), grid, [hero, foe], policy=policy, round_cap=1,
|
||||
)
|
||||
eng.run()
|
||||
return eng._states[0].pos
|
||||
|
||||
assert _go((0, 2)) == (0, 0)
|
||||
assert _go((0, 1)) == (0, 1)
|
||||
|
||||
|
||||
def test_airborne_view_preserves_combat_flags() -> None:
|
||||
# The air view normalizes only the walking cost: solid barriers stay
|
||||
# impassable (so LoE/cover geometry is untouched by flight), cover flags
|
||||
# and the elevation layer are preserved.
|
||||
legend = dict(_LEGEND)
|
||||
legend["C"] = TerrainType(type="pillar", move_cost=None, cover=True)
|
||||
spec = MapSpec(
|
||||
name="flags", terrain=(".TP#C",), legend=legend, elevation=(".1...",),
|
||||
)
|
||||
grid = Grid.from_spec(spec)
|
||||
view = grid.airborne_view()
|
||||
wall = view.terrain_at((0, 3))
|
||||
assert wall.blocks_los
|
||||
assert wall.move_cost is None
|
||||
pit = view.terrain_at((0, 2))
|
||||
assert pit.move_cost == 1
|
||||
assert not pit.blocks_los
|
||||
assert view.terrain_at((0, 1)).move_cost == 1
|
||||
pillar = view.terrain_at((0, 4))
|
||||
assert pillar.move_cost == 1
|
||||
assert pillar.cover
|
||||
assert view.elevation_at((0, 1)) == grid.elevation_at((0, 1)) == 1
|
||||
|
||||
Reference in New Issue
Block a user