From 179707910ef3c28091a4d39cdd6d0218288a206f Mon Sep 17 00:00:00 2001 From: stm <14291421+stephanmeesters@users.noreply.github.com> Date: Sun, 14 Jun 2026 11:08:16 +0200 Subject: [PATCH 1/3] refactor(pathfinding): Refactor spiral search in adjustDestination --- .../Source/GameLogic/AI/AIPathfind.cpp | 64 ++++++++----------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp index 0d8003df21a..93952ff7ecd 100644 --- a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp +++ b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp @@ -5543,48 +5543,40 @@ Bool Pathfinder::adjustDestination(Object *obj, const LocomotorSet& locomotorSet layer = TheTerrainLogic->getLayerForDestination(groupDest); } - Int limit = MAX_ADJUSTMENT_CELL_COUNT; - Int i, j; - i = cell.x; - j = cell.y; + Int i = cell.x; + Int j = cell.y; + // Check the center cell if (checkForAdjust(obj, locomotorSet, isHuman, i,j, layer, iRadius, center, dest, groupDest)) { return true; } - Int delta=1; - Int count; + // TheSuperHackers @info Expanding clockwise spiral search around center cell C. Each full lap walks right→down→left→up. + // After every pair of directions (right+down, then left+up) length of the segment grows by 1. + // + // 6 7 8 9 + // 5 C 1 10 + // 4 3 2 11 + // <------ 12 + // + Int limit = MAX_ADJUSTMENT_CELL_COUNT; + Int segmentLength = 1; + const ICoord2D directions[4] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; while (limit>0) { - for (count = delta; count>0; count--) { - i++; - limit--; - if (checkForAdjust(obj, locomotorSet, isHuman, i,j, layer, iRadius, center, dest, groupDest)) { - return true; - } - } - for (count = delta; count>0; count--) { - j++; - limit--; - if (checkForAdjust(obj, locomotorSet, isHuman, i,j, layer, iRadius, center, dest, groupDest)) { - return true; - } - } - delta++; - for (count = delta; count>0; count--) { - i--; - limit--; - if (checkForAdjust(obj, locomotorSet, isHuman, i,j, layer, iRadius, center, dest, groupDest)) { - return true; - } - } - for (count = delta; count>0; count--) { - j--; - limit--; - if (checkForAdjust(obj, locomotorSet, isHuman, i,j, layer, iRadius, center, dest, groupDest)) { - return true; - } - } - delta++; + for (Int dir = 0; dir < 4; dir++) { + for (Int count = segmentLength; count>0; count--) { + i+=directions[dir].x; + j+=directions[dir].y; + limit--; + if (checkForAdjust(obj, locomotorSet, isHuman, i, j, layer, iRadius, center, dest, groupDest)) { + return true; + } + } + if (dir & 1) { + segmentLength++; + } + } } + if (groupDest) { // Didn't work, so just do simple adjust. return(adjustDestination(obj, locomotorSet, dest, nullptr)); From de9c3d9d3f4f0bfa9f1b45c193bf1869f63092c9 Mon Sep 17 00:00:00 2001 From: stm <14291421+stephanmeesters@users.noreply.github.com> Date: Sun, 14 Jun 2026 13:25:17 +0200 Subject: [PATCH 2/3] Process review comments --- Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp index 93952ff7ecd..7a756833797 100644 --- a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp +++ b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp @@ -5550,13 +5550,13 @@ Bool Pathfinder::adjustDestination(Object *obj, const LocomotorSet& locomotorSet return true; } - // TheSuperHackers @info Expanding clockwise spiral search around center cell C. Each full lap walks right→down→left→up. - // After every pair of directions (right+down, then left+up) length of the segment grows by 1. + // TheSuperHackers @info Expanding counter-clockwise spiral search around center cell C. Each full lap walks right->up->left->down. + // After every pair of directions (right+up, then left+down) length of the segment grows by 1. // - // 6 7 8 9 - // 5 C 1 10 - // 4 3 2 11 // <------ 12 + // 4 3 2 11 + // 5 C 1 10 + // 6 7 8 9 // Int limit = MAX_ADJUSTMENT_CELL_COUNT; Int segmentLength = 1; From ffcffa9f3d0d8e4464128b3220e735fe1b3231e6 Mon Sep 17 00:00:00 2001 From: stm <14291421+stephanmeesters@users.noreply.github.com> Date: Sun, 14 Jun 2026 14:17:40 +0200 Subject: [PATCH 3/3] Fix indentation --- .../Source/GameLogic/AI/AIPathfind.cpp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp index 7a756833797..29664ee8ee3 100644 --- a/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp +++ b/Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp @@ -5562,19 +5562,19 @@ Bool Pathfinder::adjustDestination(Object *obj, const LocomotorSet& locomotorSet Int segmentLength = 1; const ICoord2D directions[4] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; while (limit>0) { - for (Int dir = 0; dir < 4; dir++) { - for (Int count = segmentLength; count>0; count--) { - i+=directions[dir].x; - j+=directions[dir].y; - limit--; - if (checkForAdjust(obj, locomotorSet, isHuman, i, j, layer, iRadius, center, dest, groupDest)) { - return true; - } - } - if (dir & 1) { - segmentLength++; - } - } + for (Int dir = 0; dir < 4; dir++) { + for (Int count = segmentLength; count>0; count--) { + i+=directions[dir].x; + j+=directions[dir].y; + limit--; + if (checkForAdjust(obj, locomotorSet, isHuman, i, j, layer, iRadius, center, dest, groupDest)) { + return true; + } + } + if (dir & 1) { + segmentLength++; + } + } } if (groupDest) {