← 输入框|github.com|刷新
Skip to content

Keep root costs and traversal consistent when adding destinations - #191

Merged
HexDecimal merged 5 commits into
libtcod:mainfrom
antonsoo:fix/reset-added-root-traversal
Oct 12, 2026
Merged

HexDecimal merged 5 commits into
libtcod:mainfrom
antonsoo:fix/reset-added-root-traversal

Conversation

@antonsoo

@antonsoo antonsoo commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Adding a root after resolving a Pathfinder updates its distance but leaves its old predecessor, so routes through it can loop after resolving again. An added destination with a higher cost also overwrites a cheaper route.

Update the distance and reset the predecessor only when the new root improves the current distance. Equal or worse roots retain the existing field, path and frontier. This keeps destination weights and traversal consistent.

import numpy as np
import tcod.path

graph = tcod.path.SimpleGraph(cost=np.ones((4, 7), np.int8), cardinal=1, diagonal=0)
pf = tcod.path.Pathfinder(graph)
pf.add_root((0, 0))
pf.resolve()
pf.add_root((3, 6))
pf.resolve()
print(pf.path_from((3, 6)))

Untouched main raises Possible cyclic loop detected; the fix returns a valid route. For the weighted case, start with root (0, 0), resolve, then add (0, 2) with value 20. Previously its distance changes from 2 to 20 and routes terminate at this expensive root. Now its distance remains 2 and the cheaper route is preserved.

The original 12 regressions cover 2D/3D grids, C/F order and root weights. The additional 64 cases cover partial/full resolution, negative/positive initial weights and lower/equal/higher new costs: 48 fail on the previous PR head, and all 64 pass after this update.

On three published Dragon Age maps, an independent multi-source Dijkstra reference verifies all 2,304 agent routes with competing destination costs. This corrects 44 wrong distance entries and 319 invalid-cost routes. The original 1,920 destination routes and all twelve ordinary control reports/hashes stay identical.

Validation after integrating current main: complete native pytest --no-window suite passes 281 tests with 95 existing skips, using SDL's dummy audio backend on this host. All 76 root regressions and 14 merged lazy-goal cases pass. The preservation test takes its snapshot after the lazy path query, which can advance a partially resolved search. Changed-file project hooks and Mypy pass. Without the dummy backend the existing audio-device fixture reports no available device. Security-specific hooks were excluded from this non-security task. The prior graphical screenshot hang on unchanged main remains an existing local limitation; Windows/macOS and a complete graphical-suite pass are not claimed.

This correction explicitly covers adding roots followed by resolution. Reconsidering an already settled lazy goal is handled separately in #192, which has now merged and is included in this branch. Native weighted-root and lazy-new-root game-map checks independently verify 4,608 routes on the combined source.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants