114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import heapq
|
|
import random
|
|
|
|
from engine.map_gen import GameMap
|
|
|
|
Position = tuple[int, int]
|
|
|
|
_NEIGHBOURS: tuple[Position, ...] = ((0, -1), (0, 1), (-1, 0), (1, 0))
|
|
|
|
|
|
def _heuristic(a: Position, b: Position) -> int:
|
|
return abs(a[0] - b[0]) + abs(a[1] - b[1])
|
|
|
|
|
|
def _walkable(game_map: GameMap, pos: Position, blocked: set[Position]) -> bool:
|
|
return game_map.is_walkable(*pos) and pos not in blocked
|
|
|
|
|
|
def astar(
|
|
game_map: GameMap,
|
|
start: Position,
|
|
goal: Position,
|
|
blocked: set[Position] | None = None,
|
|
) -> list[Position]:
|
|
"""A* path from start to goal (exclusive of start, inclusive of goal).
|
|
|
|
blocked: tiles occupied by entities, treated as impassable. The goal is
|
|
always enterable so an enemy adjacent-to/standing-on an occupied target
|
|
still paths to it.
|
|
Returns [] when no path exists.
|
|
"""
|
|
blocked = blocked or set()
|
|
if not _walkable(game_map, start, blocked) and not game_map.is_walkable(*start):
|
|
return []
|
|
if not game_map.is_walkable(*goal):
|
|
return []
|
|
|
|
came_from: dict[Position, Position] = {}
|
|
g_score: dict[Position, int] = {start: 0}
|
|
open_heap: list[tuple[int, int, Position]] = [
|
|
(_heuristic(start, goal), 0, start)
|
|
]
|
|
closed: set[Position] = set()
|
|
|
|
while open_heap:
|
|
_, _, current = heapq.heappop(open_heap)
|
|
if current == goal:
|
|
path: list[Position] = [current]
|
|
while current in came_from:
|
|
current = came_from[current]
|
|
path.append(current)
|
|
path.reverse()
|
|
return path[1:]
|
|
if current in closed:
|
|
continue
|
|
closed.add(current)
|
|
|
|
for dx, dy in _NEIGHBOURS:
|
|
nxt: Position = (current[0] + dx, current[1] + dy)
|
|
if nxt in closed or not game_map.in_bounds(*nxt):
|
|
continue
|
|
enterable = (
|
|
nxt == goal or _walkable(game_map, nxt, blocked)
|
|
)
|
|
if not enterable:
|
|
continue
|
|
tentative = g_score[current] + 1
|
|
if tentative < g_score.get(nxt, _INF):
|
|
g_score[nxt] = tentative
|
|
came_from[nxt] = current
|
|
heapq.heappush(
|
|
open_heap,
|
|
(tentative + _heuristic(nxt, goal), tentative, nxt),
|
|
)
|
|
return []
|
|
|
|
|
|
_INF: int = 1 << 30
|
|
|
|
|
|
def random_step(game_map: GameMap, start: Position) -> Position | None:
|
|
"""No-path fallback: one random walkable adjacent tile, or None (wait)."""
|
|
options = [
|
|
(start[0] + dx, start[1] + dy)
|
|
for dx, dy in _NEIGHBOURS
|
|
if game_map.is_walkable(start[0] + dx, start[1] + dy)
|
|
]
|
|
return random.choice(options) if options else None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from data.constants import MAP_HEIGHT, MAP_WIDTH, TILE_FLOOR
|
|
from engine.map_gen import generate_map
|
|
|
|
game_map = generate_map(0, seed=42)
|
|
floors = [
|
|
(x, y)
|
|
for y in range(MAP_HEIGHT)
|
|
for x in range(MAP_WIDTH)
|
|
if game_map.tiles[y][x] == TILE_FLOOR
|
|
]
|
|
for _ in range(50):
|
|
a, b = random.sample(floors, 2)
|
|
path = astar(game_map, a, b)
|
|
assert path and path[-1] == b, (a, b)
|
|
assert game_map.is_walkable(*a)
|
|
assert all(
|
|
game_map.is_walkable(*p) for p in path
|
|
), "path crosses a wall"
|
|
assert isinstance(random_step(game_map, a), (tuple, type(None)))
|
|
print("pathfinding ok")
|