From 075582910a7e9daeaa25b71307615693af3ef243 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 4 Jul 2024 07:53:25 +0000 Subject: [PATCH] Less CPU intensive version of ExplorationHelper::scanMap method --- AI/Nullkiller/Helpers/ExplorationHelper.cpp | 62 ++++++++++----------- AI/Nullkiller/Helpers/ExplorationHelper.h | 1 - 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/AI/Nullkiller/Helpers/ExplorationHelper.cpp b/AI/Nullkiller/Helpers/ExplorationHelper.cpp index 8d25bcd26..0be612ee0 100644 --- a/AI/Nullkiller/Helpers/ExplorationHelper.cpp +++ b/AI/Nullkiller/Helpers/ExplorationHelper.cpp @@ -70,11 +70,8 @@ bool ExplorationHelper::scanMap() int3 mapSize = cbp->getMapSize(); int perimeter = 2 * sightRadius * (mapSize.x + mapSize.y); - std::vector from; - std::vector to; - - from.reserve(perimeter); - to.reserve(perimeter); + std::vector edgeTiles; + edgeTiles.reserve(perimeter); foreach_tile_pos([&](const int3 & pos) { @@ -91,13 +88,13 @@ bool ExplorationHelper::scanMap() }); if(hasInvisibleNeighbor) - from.push_back(pos); + edgeTiles.push_back(pos); } }); logAi->debug("Exploration scan visible area perimeter for hero %s", hero->getNameTranslated()); - for(const int3 & tile : from) + for(const int3 & tile : edgeTiles) { scanTile(tile); } @@ -108,19 +105,36 @@ bool ExplorationHelper::scanMap() } allowDeadEndCancellation = false; - - for(int i = 0; i < sightRadius; i++) - { - getVisibleNeighbours(from, to); - vstd::concatenate(from, to); - vstd::removeDuplicates(from); - } - logAi->debug("Exploration scan all possible tiles for hero %s", hero->getNameTranslated()); - for(const int3 & tile : from) + boost::multi_array potentialTiles = *ts->fogOfWarMap; + std::vector tilesToExploreFrom = edgeTiles; + + // WARNING: POTENTIAL BUG + // AI attempts to move to any tile within sight radius to reveal some new tiles + // however sight radius is circular, while this method assumes square radius + // standing on the edge of a square will NOT reveal tile in opposite corner + for(int i = 0; i < sightRadius; i++) { - scanTile(tile); + std::vector newTilesToExploreFrom; + + for(const int3 & tile : tilesToExploreFrom) + { + foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour) + { + if(potentialTiles[neighbour.z][neighbour.x][neighbour.y]) + { + newTilesToExploreFrom.push_back(neighbour); + potentialTiles[neighbour.z][neighbour.x][neighbour.y] = false; + } + }); + } + for(const int3 & tile : newTilesToExploreFrom) + { + scanTile(tile); + } + + std::swap(tilesToExploreFrom, newTilesToExploreFrom); } return !bestGoal->invalid(); @@ -172,20 +186,6 @@ void ExplorationHelper::scanTile(const int3 & tile) } } -void ExplorationHelper::getVisibleNeighbours(const std::vector & tiles, std::vector & out) const -{ - for(const int3 & tile : tiles) - { - foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour) - { - if((*(ts->fogOfWarMap))[neighbour.z][neighbour.x][neighbour.y]) - { - out.push_back(neighbour); - } - }); - } -} - int ExplorationHelper::howManyTilesWillBeDiscovered(const int3 & pos) const { int ret = 0; diff --git a/AI/Nullkiller/Helpers/ExplorationHelper.h b/AI/Nullkiller/Helpers/ExplorationHelper.h index 8c4ec58f4..994d2809a 100644 --- a/AI/Nullkiller/Helpers/ExplorationHelper.h +++ b/AI/Nullkiller/Helpers/ExplorationHelper.h @@ -46,7 +46,6 @@ public: private: void scanTile(const int3 & tile); bool hasReachableNeighbor(const int3 & pos) const; - void getVisibleNeighbours(const std::vector & tiles, std::vector & out) const; }; }