1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-21 21:17:49 +02:00
vcmi/lib/battle/BattleHex.cpp

72 lines
2.0 KiB
C++
Raw Normal View History

2024-11-12 07:11:18 +01:00
/*
* BattleHex.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "BattleHex.h"
#include "BattleHexArray.h"
2024-11-12 07:11:18 +01:00
VCMI_LIB_NAMESPACE_BEGIN
BattleHex BattleHex::getClosestTile(const BattleHexArray & hexes, BattleSide side, BattleHex initialPos)
2024-11-12 07:11:18 +01:00
{
if(hexes.empty())
return BattleHex();
2024-11-12 07:11:18 +01:00
BattleHex initialHex = BattleHex(initialPos);
int closestDistance = std::numeric_limits<int>::max();
BattleHexArray closestTiles;
2024-11-12 07:11:18 +01:00
for(auto hex : hexes)
2024-11-12 07:11:18 +01:00
{
int distance = initialHex.getDistance(initialHex, hex);
if(distance < closestDistance)
{
closestDistance = distance;
closestTiles.clear();
closestTiles.insert(hex);
}
else if(distance == closestDistance)
closestTiles.insert(hex);
2024-11-12 07:11:18 +01:00
}
auto compareHorizontal = [side, initialPos](const BattleHex & left, const BattleHex & right)
2024-11-12 07:11:18 +01:00
{
if(left.getX() != right.getX())
{
return (side == BattleSide::ATTACKER) ? (left.getX() > right.getX()) : (left.getX() < right.getX());
}
return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
};
2024-11-12 07:11:18 +01:00
auto bestTile = std::min_element(closestTiles.begin(), closestTiles.end(), compareHorizontal);
return (bestTile != closestTiles.end()) ? *bestTile : BattleHex();
}
2024-11-12 07:11:18 +01:00
const BattleHexArray & BattleHex::getAllNeighbouringTiles() const
2024-11-12 07:11:18 +01:00
{
return BattleHexArray::getAllNeighbouringTiles(*this);
2024-11-12 07:11:18 +01:00
}
const BattleHexArray & BattleHex::getNeighbouringTiles() const
2024-11-12 07:11:18 +01:00
{
return BattleHexArray::getNeighbouringTiles(*this);
2024-11-12 07:11:18 +01:00
}
const BattleHexArray & BattleHex::getNeighbouringTilesDblWide(BattleSide side) const
2024-11-12 07:11:18 +01:00
{
return BattleHexArray::getNeighbouringTilesDblWide(*this, side);
}
2024-11-12 07:11:18 +01:00
std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
{
return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % static_cast<si16>(hex));
2024-11-12 07:11:18 +01:00
}
VCMI_LIB_NAMESPACE_END