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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version

### Fixed

- Many-to-many `tcod.path.path2d` queries retain weighted predecessors when reconstructing their route.
- Copying or pickling Perlin and Simplex noise no longer advances a shared random generator while preparing an unused wavelet tile.
- `tcod.image.Image.rotate90` updates the width and height of rectangular images, keeping NumPy array conversions consistent with the rotated pixels.
- Adding a root to an already resolved `tcod.path.Pathfinder` resets its predecessor only when its value improves the current distance. This prevents cyclic paths while preserving routes to cheaper roots.
Expand Down
4 changes: 2 additions & 2 deletions tcod/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,10 +1559,10 @@ def path2d( # noqa: C901, PLR0912, PLR0913
out = out[::-1]
return out

# Crude Dijkstra implementation until issues with Pathfinder are fixed
# Resolve every endpoint before choosing the nearest start.
pf.resolve(None)
best_distance, best_ij = min((pf.distance[ij], ij) for ij in start_points)
if best_distance == np.iinfo(pf.distance.dtype).max:
return np.zeros((0, 2), dtype=np.intc) # All endpoints unreachable

return hillclimb2d(pf.distance, best_ij, cardinal=bool(cardinal), diagonal=bool(diagonal))
return pf.path_from(best_ij)
20 changes: 20 additions & 0 deletions tests/test_path2d_predecessors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Many-to-many paths must retain movement costs during reconstruction."""

import numpy as np
import pytest

import tcod.path


@pytest.mark.parametrize("diagonal", [0, 1, 2, 3])
def test_path2d_diagonal_predecessors(diagonal: int) -> None:
"""An expensive diagonal must not replace two cheaper cardinal moves."""
cost = np.ones((4, 4), dtype=np.int16)
path = tcod.path.path2d(
cost, start_points=[(0, 0), (0, 1)], end_points=[(2, 2), (3, 3)], cardinal=1, diagonal=diagonal
)
total = sum(diagonal if np.all(a != b) else 1 for a, b in zip(path, path[1:]))
assert total == (2 if diagonal == 1 else 3)
assert tuple(path[0]) in [(0, 0), (0, 1)]
assert tuple(path[-1]) in [(2, 2), (3, 3)]
np.testing.assert_array_equal(cost, 1)
Loading