Skip to content

refactor(pathfinder): Simplify implementation of spiral cell search in Pathfinder::adjustDestination()#2790

Open
stephanmeesters wants to merge 3 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/pathfinding-adjust-destination
Open

refactor(pathfinder): Simplify implementation of spiral cell search in Pathfinder::adjustDestination()#2790
stephanmeesters wants to merge 3 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/pathfinding-adjust-destination

Conversation

@stephanmeesters

@stephanmeesters stephanmeesters commented Jun 14, 2026

Copy link
Copy Markdown

This change simplifies code in Pathfinder::adjustDestination and adds information about the way that path finding cells are searched.

As noted in linked item MAX_ADJUSTMENT_CELL_COUNT = 400 is the current limit for the amount of checkForAdjust calls, which can get expensive. And actually there is a minor bug because limit is only checked in the outer while loop it can overshoot that target, so at most we will see around 420 checks.

@greptile-apps

greptile-apps Bot commented Jun 14, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors the spiral destination-search loop in Pathfinder::adjustDestination to replace four hand-rolled, copy-paste inner for loops with a single unified loop driven by a directions[4] array and a segmentLength counter. A clear ASCII diagram is also added to document the spiral traversal order.

  • The spiral cell-visitation order (right → up → left → down, expanding every two segments) is preserved exactly — verified by tracing both the old and the new code up through ring 3.
  • The pre-existing overshoot of MAX_ADJUSTMENT_CELL_COUNT (up to ~4·segmentLength extra checks per outer loop iteration) is intentionally carried over, as documented in the PR description.

Confidence Score: 5/5

Safe to merge — the change is a pure structural simplification of one function; no logic paths, limit semantics, or spiral traversal order are altered.

The new loop was manually traced cell-by-cell against the old code through three full rings and produces the same sequence. ICoord2D is a POD struct {Int x, y;} so the aggregate initializer {1,0} is well-formed. The dir & 1 increment gate correctly replicates the two delta++ bumps from the original. No functional regressions were identified.

No files require special attention.

Important Files Changed

Filename Overview
Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp Spiral search in adjustDestination refactored to a single unified direction-array loop; traversal order and limit semantics are preserved identically.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A([adjustDestination called]) --> B[Compute cell x/y from dest]
    B --> C{checkForAdjust center cell}
    C -- true --> Z([return true])
    C -- false --> D[segmentLength = 1, limit = MAX_ADJUSTMENT_CELL_COUNT]
    D --> E{limit > 0?}
    E -- false --> F([return false / fallback])
    E -- true --> G[dir = 0..3 loop]
    G --> H[Walk segmentLength steps in directions dir, decrement limit each step]
    H --> I{checkForAdjust hit?}
    I -- true --> Z
    I -- false --> J{dir is odd? dir AND 1}
    J -- yes --> K[segmentLength++]
    J -- no --> L[next dir]
    K --> L
    L --> M{dir < 4?}
    M -- yes --> H
    M -- no --> E
Loading

Reviews (4): Last reviewed commit: "Fix indentation" | Re-trigger Greptile

@stephanmeesters

Copy link
Copy Markdown
Author

@greptileai Spaces were used intentionally in the comment lines so they render correctly in every editor

@greptile-apps

greptile-apps Bot commented Jun 14, 2026

Copy link
Copy Markdown

That makes complete sense — the spaces in the ASCII art grid ensure the diagram aligns correctly regardless of tab width settings. Acknowledged, no issue there.

@stephanmeesters stephanmeesters changed the title refactor(pathfinding): Refactor spiral search in adjustDestination refactor(pathfinder): Refactor spiral search in adjustDestination Jun 14, 2026
Comment thread Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp Outdated
Comment thread Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp Outdated
@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing labels Jun 14, 2026
@stephanmeesters stephanmeesters force-pushed the refactor/pathfinding-adjust-destination branch from 4b5ac68 to 475cc93 Compare June 14, 2026 11:27
@stephanmeesters stephanmeesters force-pushed the refactor/pathfinding-adjust-destination branch from 475cc93 to de9c3d9 Compare June 14, 2026 11:28
@xezon xezon changed the title refactor(pathfinder): Refactor spiral search in adjustDestination refactor(pathfinder): Simplify implementation of spiral cell search in Pathfinder::adjustDestination() Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants