浏览代码

- fixed #1714
- Performance improvements, refactoring

DjWarmonger 11 年之前
父节点
当前提交
b368e565ab
共有 5 个文件被更改,包括 23 次插入18 次删除
  1. 3 6
      AI/VCAI/Goals.cpp
  2. 12 12
      AI/VCAI/VCAI.cpp
  3. 1 0
      AI/VCAI/VCAI.h
  4. 6 0
      CCallback.cpp
  5. 1 0
      CCallback.h

+ 3 - 6
AI/VCAI/Goals.cpp

@@ -406,8 +406,7 @@ TGoalVec ClearWayTo::getAllPossibleSubgoals()
 			h->visitablePos() == tile) //we are already on that tile! what does it mean?
 			continue;
 
-		cb->setSelection(h);
-		SectorMap sm;
+		SectorMap sm(h);
 
 		int3 tileToHit = sm.firstTileToGet(hero ? hero : h, tile);
 		//if our hero is trapped, make sure we request clearing the way from OUR perspective
@@ -509,10 +508,9 @@ TGoalVec Explore::getAllPossibleSubgoals()
 	}
 	for (auto h : heroes)
 	{
+		SectorMap sm(h);
 		for (auto obj : objs) //double loop, performance risk?
 		{
-			cb->setSelection(h);
-			SectorMap sm; //seems to depend on hero
 			auto t = sm.firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
 			if (t.valid())
 			{
@@ -803,10 +801,9 @@ TGoalVec Conquer::getAllPossibleSubgoals()
 
 	for (auto h : cb->getHeroesInfo())
 	{
+		SectorMap sm(h);
 		for (auto obj : objs) //double loop, performance risk?
 		{
-			cb->setSelection(h);
-			SectorMap sm; //seems to depend on hero
 			auto t = sm.firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
 			if (t.valid())
 			{

+ 12 - 12
AI/VCAI/VCAI.cpp

@@ -2537,17 +2537,16 @@ void AIStatus::setMove(bool ongoing)
 
 SectorMap::SectorMap()
 {
-// 	int3 sizes = cb->getMapSize();
-// 	sector.resize(sizes.x);
-// 	for(auto &i : sector)
-// 		i.resize(sizes.y);
-//
-// 	for(auto &i : sector)
-// 		for(auto &j : i)
-// 			j.resize(sizes.z, 0);
 	update();
 }
 
+SectorMap::SectorMap(HeroPtr h)
+{
+	cb->setSelection(h.h);
+	update();
+	makeParentBFS(h->visitablePos());
+}
+
 bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
 {
 	if(t->blocked && !t->visitable)
@@ -2776,7 +2775,6 @@ For ship construction etc, another function (goal?) is needed
 */
 {
 	int3 ret(-1,-1,-1);
-	cb->setSelection(h.h);
 
 	int sourceSector = retreiveTile(h->visitablePos()),
 		destinationSector = retreiveTile(dst);
@@ -2929,7 +2927,6 @@ For ship construction etc, another function (goal?) is needed
 	}
 	else
 	{
-		makeParentBFS(h->visitablePos());
 		int3 curtile = dst;
 		while(curtile != h->visitablePos())
 		{
@@ -2985,8 +2982,11 @@ void SectorMap::makeParentBFS(crint3 source)
 		{
 			if(retreiveTile(neighPos) == mySector && !vstd::contains(parent, neighPos))
 			{
-				toVisit.push(neighPos);
-				parent[neighPos] = curPos;
+				if (cb->canMoveBetween(curPos, neighPos))
+				{
+					toVisit.push(neighPos);
+					parent[neighPos] = curPos;
+				}
 			}
 		});
 	}

+ 1 - 0
AI/VCAI/VCAI.h

@@ -98,6 +98,7 @@ struct SectorMap
 	std::map<int, Sector> infoOnSectors;
 
 	SectorMap();
+	SectorMap(HeroPtr h);
 	void update();
 	void clear();
 	void exploreNewSector(crint3 pos, int num);

+ 6 - 0
CCallback.cpp

@@ -321,6 +321,12 @@ int CCallback::getDistance( int3 tile )
 		return 255;
 }
 
+bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
+{
+	//TODO: merge with Pathfinder::canMoveBetween
+	return gs->checkForVisitableDir(a, b) && gs->checkForVisitableDir(b, a);
+}
+
 bool CCallback::getPath2( int3 dest, CGPath &ret )
 {
 	if (!gs->map->isInTheMap(dest))

+ 1 - 0
CCallback.h

@@ -110,6 +110,7 @@ public:
 	virtual const CGPathNode *getPathInfo(int3 tile); //uses main, client pathfinder info
 	virtual int getDistance(int3 tile);
 	virtual bool getPath2(int3 dest, CGPath &ret); //uses main, client pathfinder info
+	virtual bool canMoveBetween(const int3 &a, const int3 &b);
 	virtual int getMovementCost(const CGHeroInstance * hero, int3 dest);
 
 	virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1);