浏览代码

Merge pull request #455 from nullkiller/deadend-exploration-improvement

After actually checking this branch, I absolutely approve it. AI advances quickly and steadily, and that's the point.
DjWarmonger 7 年之前
父节点
当前提交
f8a9a0af2b
共有 3 个文件被更改,包括 26 次插入44 次删除
  1. 15 30
      AI/VCAI/AIUtility.cpp
  2. 2 3
      AI/VCAI/AIUtility.h
  3. 9 11
      AI/VCAI/VCAI.cpp

+ 15 - 30
AI/VCAI/AIUtility.cpp

@@ -18,7 +18,6 @@
 #include "../../lib/mapObjects/CBank.h"
 #include "../../lib/mapObjects/CGTownInstance.h"
 #include "../../lib/mapObjects/CQuest.h"
-#include "../../lib/CPathfinder.h"
 #include "../../lib/mapping/CMapDefines.h"
 
 extern boost::thread_specific_ptr<CCallback> cb;
@@ -431,51 +430,37 @@ bool isBlockVisitObj(const int3 & pos)
 	return false;
 }
 
-int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp)
+bool hasReachableNeighbor(const int3 &pos, HeroPtr hero, CCallback * cbp)
 {
-	//TODO: do not explore dead-end boundaries
-	int ret = 0;
-	for(int x = pos.x - radious; x <= pos.x + radious; x++)
+	for(crint3 dir : int3::getDirs())
 	{
-		for(int y = pos.y - radious; y <= pos.y + radious; y++)
+		int3 tile = pos + dir;
+		if(cbp->isInTheMap(tile) && cbp->getPathsInfo(hero.get())->getPathInfo(tile)->reachable())
 		{
-			int3 npos = int3(x, y, pos.z);
-			if(cbp->isInTheMap(npos) && pos.dist2d(npos) - 0.5 < radious && !cbp->isVisible(npos))
-			{
-				if(!boundaryBetweenTwoPoints(pos, npos, cbp))
-					ret++;
-			}
+			return true;
 		}
 	}
 
-	return ret;
+	return false;
 }
 
-bool boundaryBetweenTwoPoints(int3 pos1, int3 pos2, CCallback * cbp) //determines if two points are separated by known barrier
+int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp, HeroPtr hero)
 {
-	int xMin = std::min(pos1.x, pos2.x);
-	int xMax = std::max(pos1.x, pos2.x);
-	int yMin = std::min(pos1.y, pos2.y);
-	int yMax = std::max(pos1.y, pos2.y);
-
-	for(int x = xMin; x <= xMax; ++x)
+	int ret = 0;
+	for(int x = pos.x - radious; x <= pos.x + radious; x++)
 	{
-		for(int y = yMin; y <= yMax; ++y)
+		for(int y = pos.y - radious; y <= pos.y + radious; y++)
 		{
-			int3 tile = int3(x, y, pos1.z); //use only on same level, ofc
-			if(std::abs(pos1.dist2d(tile) - pos2.dist2d(tile)) < 1.5)
+			int3 npos = int3(x, y, pos.z);
+			if(cbp->isInTheMap(npos) && pos.dist2d(npos) - 0.5 < radious && !cbp->isVisible(npos))
 			{
-				if(!(cbp->isVisible(tile) && cbp->getTile(tile)->blocked)) //if there's invisible or unblocked tile between, it's good
-					return false;
+				if(hasReachableNeighbor(npos, hero, cbp))
+					ret++;
 			}
 		}
 	}
-	return true; //if all are visible and blocked, we're at dead end
-}
 
-int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir)
-{
-	return howManyTilesWillBeDiscovered(pos + dir, radious, cb.get());
+	return ret;
 }
 
 void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out)

+ 2 - 3
AI/VCAI/AIUtility.h

@@ -17,6 +17,7 @@
 #include "../../lib/CStopWatch.h"
 #include "../../lib/mapObjects/CObjectHandler.h"
 #include "../../lib/mapObjects/CGHeroInstance.h"
+#include "../../lib/CPathfinder.h"
 
 class CCallback;
 
@@ -141,8 +142,7 @@ void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const
 void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
 void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
 
-int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp);
-int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
+int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp, HeroPtr hero);
 void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out);
 
 bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
@@ -155,7 +155,6 @@ bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
 ui64 evaluateDanger(const CGObjectInstance * obj);
 ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor);
 bool isSafeToVisit(HeroPtr h, crint3 tile);
-bool boundaryBetweenTwoPoints(int3 pos1, int3 pos2, CCallback * cbp);
 
 bool compareMovement(HeroPtr lhs, HeroPtr rhs);
 bool compareHeroStrength(HeroPtr h1, HeroPtr h2);

+ 9 - 11
AI/VCAI/VCAI.cpp

@@ -2625,22 +2625,20 @@ void VCAI::buildArmyIn(const CGTownInstance * t)
 
 int3 VCAI::explorationBestNeighbour(int3 hpos, int radius, HeroPtr h)
 {
-	int3 ourPos = h->convertPosition(h->pos, false);
 	std::map<int3, int> dstToRevealedTiles;
+
 	for(crint3 dir : int3::getDirs())
 	{
 		int3 tile = hpos + dir;
 		if(cb->isInTheMap(tile))
 		{
-			if(ourPos != dir) //don't stand in place
+			if(isBlockVisitObj(tile))
+				continue;
+
+			if(isSafeToVisit(h, tile) && isAccessibleForHero(tile, h))
 			{
-				if(isSafeToVisit(h, tile) && isAccessibleForHero(tile, h))
-				{
-					if(isBlockVisitObj(tile))
-						continue;
-					else
-						dstToRevealedTiles[tile] = howManyTilesWillBeDiscovered(radius, hpos, dir);
-				}
+				auto distance = hpos.dist2d(tile); // diagonal movement opens more tiles but spends more mp
+				dstToRevealedTiles[tile] = howManyTilesWillBeDiscovered(tile, radius, cb.get(), h) / distance;
 			}
 		}
 	}
@@ -2696,7 +2694,7 @@ int3 VCAI::explorationNewPoint(HeroPtr h)
 
 			CGPath path;
 			cb->getPathsInfo(hero)->getPath(path, tile);
-			float ourValue = (float)howManyTilesWillBeDiscovered(tile, radius, cbp) / (path.nodes.size() + 1); //+1 prevents erratic jumps
+			float ourValue = (float)howManyTilesWillBeDiscovered(tile, radius, cbp, h) / (path.nodes.size() + 1); //+1 prevents erratic jumps
 
 			if(ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
 			{
@@ -2741,7 +2739,7 @@ int3 VCAI::explorationDesperate(HeroPtr h)
 		{
 			if(cbp->getTile(tile)->blocked) //does it shorten the time?
 				continue;
-			if(!howManyTilesWillBeDiscovered(tile, radius, cbp)) //avoid costly checks of tiles that don't reveal much
+			if(!howManyTilesWillBeDiscovered(tile, radius, cbp, h)) //avoid costly checks of tiles that don't reveal much
 				continue;
 
 			auto t = sm->firstTileToGet(h, tile);