Browse Source

Patrol: use manhattan distance for getting tiles in radius

ArseniyShestakov 10 years ago
parent
commit
bdc369ffba
4 changed files with 16 additions and 5 deletions
  1. 1 1
      lib/CPathfinder.cpp
  2. 8 2
      lib/IGameCallback.cpp
  3. 1 1
      lib/IGameCallback.h
  4. 6 1
      lib/int3.h

+ 1 - 1
lib/CPathfinder.cpp

@@ -596,7 +596,7 @@ void CPathfinder::initializePatrol()
 		if(hero->patrol.patrolRadious)
 		if(hero->patrol.patrolRadious)
 		{
 		{
 			state = PATROL_RADIUS;
 			state = PATROL_RADIUS;
-			gs->getTilesInRange(patrolTiles, hero->patrol.initialPos, hero->patrol.patrolRadious);
+			gs->getTilesInRange(patrolTiles, hero->patrol.initialPos, hero->patrol.patrolRadious, hero->tempOwner, 0, true);
 		}
 		}
 		else
 		else
 			state = PATROL_LOCKED;
 			state = PATROL_LOCKED;

+ 8 - 2
lib/IGameCallback.cpp

@@ -45,7 +45,7 @@ void CPrivilagedInfoCallback::getFreeTiles (std::vector<int3> &tiles) const
 	}
 	}
 }
 }
 
 
-void CPrivilagedInfoCallback::getTilesInRange( std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player/*=uninit*/, int mode/*=0*/ ) const
+void CPrivilagedInfoCallback::getTilesInRange( std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player/*=uninit*/, int mode/*=0*/, bool patrolDistance/*=false*/) const
 {
 {
 	if(!!player && *player >= PlayerColor::PLAYER_LIMIT)
 	if(!!player && *player >= PlayerColor::PLAYER_LIMIT)
 	{
 	{
@@ -61,7 +61,13 @@ void CPrivilagedInfoCallback::getTilesInRange( std::unordered_set<int3, ShashInt
 		{
 		{
 			for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
 			for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
 			{
 			{
-				double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
+				int3 tilePos(xd,yd,pos.z);
+				double distance;
+				if(patrolDistance)
+					distance = pos.mandist2d(tilePos);
+				else
+					distance = pos.dist2d(tilePos) - 0.5;
+
 				if(distance <= radious)
 				if(distance <= radious)
 				{
 				{
 					if(!player
 					if(!player

+ 1 - 1
lib/IGameCallback.h

@@ -30,7 +30,7 @@ class DLL_LINKAGE CPrivilagedInfoCallback : public CGameInfoCallback
 public:
 public:
 	CGameState * gameState();
 	CGameState * gameState();
 	void getFreeTiles (std::vector<int3> &tiles) const; //used for random spawns
 	void getFreeTiles (std::vector<int3> &tiles) const; //used for random spawns
-	void getTilesInRange(std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int mode=0) const;  //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 -  only unrevealed
+	void getTilesInRange(std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int mode = 0, bool patrolDistance = false) const;  //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 -  only unrevealed
 	void getAllTiles (std::unordered_set<int3, ShashInt3> &tiles, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int level=-1, int surface=0) const; //returns all tiles on given level (-1 - both levels, otherwise number of level); surface: 0 - land and water, 1 - only land, 2 - only water
 	void getAllTiles (std::unordered_set<int3, ShashInt3> &tiles, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int level=-1, int surface=0) const; //returns all tiles on given level (-1 - both levels, otherwise number of level); surface: 0 - land and water, 1 - only land, 2 - only water
 	void pickAllowedArtsSet(std::vector<const CArtifact*> &out); //gives 3 treasures, 3 minors, 1 major -> used by Black Market and Artifact Merchant
 	void pickAllowedArtsSet(std::vector<const CArtifact*> &out); //gives 3 treasures, 3 minors, 1 major -> used by Black Market and Artifact Merchant
 	void getAllowedSpells(std::vector<SpellID> &out, ui16 level);
 	void getAllowedSpells(std::vector<SpellID> &out, ui16 level);

+ 6 - 1
lib/int3.h

@@ -105,6 +105,11 @@ public:
 	{
 	{
 		return std::sqrt((double)dist2dSQ(o));
 		return std::sqrt((double)dist2dSQ(o));
 	}
 	}
+	//manhattan distance used for patrol radius (z coord is not used)
+	double mandist2d(const int3 & o) const
+	{
+		return abs(o.x - x) + abs(o.y - y);
+	}
 
 
 	bool areNeighbours(const int3 & o) const
 	bool areNeighbours(const int3 & o) const
 	{
 	{
@@ -175,4 +180,4 @@ int3 findClosestTile (Container & container, int3 dest)
 		}
 		}
 	}
 	}
 	return result;
 	return result;
-}
+}