Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version

## [Unreleased]

### Fixed

- Lazy pathfinder queries reconsider previously visited goals after adding a root or lowering a root's distance.

## [21.2.1] - 2026-06-04

### Deployment
Expand Down
8 changes: 7 additions & 1 deletion tcod/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,13 @@ def resolve(self, goal: tuple[int, ...] | None = None) -> None:
goal = goal[::-1]
if self._distance[goal] != np.iinfo(self._distance.dtype).max: # noqa: SIM102
if not lib.frontier_has_index(self._frontier_p, goal):
return
if not lib.TCOD_frontier_size(self._frontier_p):
return
# A new root may improve an already visited goal. Queue its
# current distance so the search still stops at this goal
# when the remaining frontier cannot improve it.
distance = int(self._distance[goal])
lib.TCOD_frontier_push(self._frontier_p, goal, distance, distance)
self._update_heuristic(goal)
self._graph._resolve(self)

Expand Down
59 changes: 59 additions & 0 deletions tests/test_path_lazy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Regression tests for resolving an existing distance after adding roots."""

from typing import Literal

import numpy as np
import pytest

import tcod.path


@pytest.mark.parametrize("order", ["C", "F"])
@pytest.mark.parametrize("heuristic", [False, True])
@pytest.mark.parametrize("full_resolve", [False, True])
def test_resolve_after_lowering_root(order: Literal["C", "F"], heuristic: bool, full_resolve: bool) -> None:
cost = np.ones((9, 13), dtype=np.int8, order=order)
graph = tcod.path.SimpleGraph(cost=cost, cardinal=1, diagonal=0)
if not heuristic:
graph.set_heuristic(cardinal=0, diagonal=0)
pathfinder = tcod.path.Pathfinder(graph)
pathfinder.add_root((0, 0), value=7)
pathfinder.resolve(None if full_resolve else (8, 12))
pathfinder.add_root((0, 0), value=-3)
for goal in ((8, 12), (0, 1), (4, 6)):
path = pathfinder.path_from(goal)
assert pathfinder.distance[goal] == sum(goal) - 3
assert len(path) == sum(goal) + 1
np.testing.assert_array_equal(path[-1], (0, 0))


@pytest.mark.parametrize("order", ["C", "F"])
@pytest.mark.parametrize("heuristic", [False, True])
def test_resolve_after_adding_root(order: Literal["C", "F"], heuristic: bool) -> None:
cost = np.ones((9, 13), dtype=np.int8, order=order)
graph = tcod.path.SimpleGraph(cost=cost, cardinal=1, diagonal=0)
if not heuristic:
graph.set_heuristic(cardinal=0, diagonal=0)
pathfinder = tcod.path.Pathfinder(graph)
pathfinder.add_root((0, 0))
pathfinder.resolve()
pathfinder.add_root((8, 12))
for goal in ((8, 11), (4, 6), (7, 12)):
pathfinder.resolve(goal)
assert pathfinder.distance[goal] == min(sum(goal), 20 - sum(goal))


@pytest.mark.parametrize("heuristic", [False, True])
def test_cached_goal_does_not_resolve_entire_frontier(heuristic: bool) -> None:
graph = tcod.path.SimpleGraph(cost=np.ones((101, 101), dtype=np.int8), cardinal=1, diagonal=0)
if not heuristic:
graph.set_heuristic(cardinal=0, diagonal=0)
pathfinder = tcod.path.Pathfinder(graph)
pathfinder.add_root((0, 0))
pathfinder.resolve()
pathfinder.add_root((100, 100))
before = pathfinder.distance.copy()
pathfinder.resolve((0, 1))
assert pathfinder.distance[0, 1] == 1
assert np.count_nonzero(pathfinder.distance != before) <= 4
assert pathfinder.distance[100, 98] == before[100, 98]
Loading