You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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
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
changed the title
refactor(pathfinding): Refactor spiral search in adjustDestination
refactor(pathfinder): Refactor spiral search in adjustDestination
Jun 14, 2026
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GenRelates to GeneralsMinorSeverity: Minor < Major < Critical < BlockerRefactorEdits the code with insignificant behavior changes, is never user facingZHRelates to Zero Hour
2 participants
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.
This change simplifies code in
Pathfinder::adjustDestinationand adds information about the way that path finding cells are searched.As noted in linked item
MAX_ADJUSTMENT_CELL_COUNT = 400is the current limit for the amount ofcheckForAdjustcalls, 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.