GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Keep root costs and traversal consistent when adding destinations by antonsoo · Pull Request #191 · libtcod/python-tcod

Conversation

@antonsoo

@antonsoo antonsoo commented Oct 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@antonsoo
Reset traversal links when adding pathfinder roots
2501da3
@antonsoo antonsoo mentioned this pull request Oct 11, 2026
Reconsider settled pathfinder goals after adding roots #192
Merged
@codecov

codecov Bot commented Oct 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.08%. Comparing base (50d805b) to head (fa431ad).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #191      +/-   ##
==========================================
+ Coverage   86.05%   86.08%   +0.03%     
==========================================
  Files          29       29              
  Lines        7586     7589       +3     
==========================================
+ Hits         6528     6533       +5     
+ Misses       1058     1056       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@HexDecimal

HexDecimal commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

That's my bad for not considering the obvious, of course the index has to be reset with a new root on a partially resolved graph. It's the new root, the root's position must be the landing spot.

To be honest calling add_root after pathfinding has been more speculation than practice. After wrapping my head around the concept I have another case to consider:
I think it would it better if distance and index are only reset if the new distance is less than the current distance. The current tests are for uniform root distances where none are preferred over the others. Adding a new root with higher value and having that override the path to a better root seems incorrect.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@antonsoo
Keep cheaper root distances and traversal when adding destinations
12910cf
@antonsoo antonsoo changed the title Reset traversal links when adding pathfinder roots Keep root costs and traversal consistent when adding destinations Oct 12, 2026
@antonsoo

antonsoo commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

You are right: a more expensive root can replace the predecessor even though a cheaper destination is already reachable. The update now ignores a root unless its value is lower than the current distance, so equal values also preserve the existing route. Both distance and traversal remain unchanged for ignored roots, including partially resolved graphs.

The added cases cover lower, equal and higher costs in C/F order and 2D/3D. On the three recorded game maps, all 2,304 routes now match an independent weighted Dijkstra reference, correcting 319 invalid-cost routes. The original 1,920-route results and control hashes remain identical. The full native no-window suite, changed-file hooks and Mypy pass.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

HexDecimal
HexDecimal requested changes Oct 12, 2026
View reviewed changes

@HexDecimal HexDecimal left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad for commenting early before finishing my suggestions. I'd like some minor tweaks. Then I think this should be good.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tcod/path.py Outdated
Comment on lines +1223 to +1224
if value >= self._distance[index]:
return

@HexDecimal HexDecimal Oct 12, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like the early termination reason to be documented.

Suggested change
if value >= self._distance[index]:
return
if value >= self._distance[index]:
return # More valuable root takes precedence over this new root

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@antonsoo antonsoo Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an inline comment explaining that an existing route of equal or lower cost takes precedence.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tcod/path.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

antonsoo added 3 commits October 12, 2026 00:46
@antonsoo
Explain weighted root preference and ignored additions
f125528
@antonsoo
Merge current upstream pathfinding fix and retain both change notes
ec60553
@antonsoo
Snapshot root state after lazy path queries
fa431ad
@antonsoo

antonsoo commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main after #192 landed and kept both changelog entries. The preservation test now takes its snapshot after the lazy path query, which can advance a partial search. All 90 path cases, the native no-window suite (281 passed, 95 skipped), and 4,608 independently checked game-map routes pass.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

HexDecimal
HexDecimal approved these changes Oct 12, 2026
View reviewed changes
@HexDecimal
HexDecimal merged commit 432932a into libtcod:main Oct 12, 2026
31 checks passed

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@HexDecimal HexDecimal HexDecimal approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@antonsoo @HexDecimal
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.