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"
|
2025-01-02 23:56:04 +01:00
|
|
|
#include "BattleHexArray.h"
|
2024-11-12 07:11:18 +01:00
|
|
|
|
2025-01-03 00:11:41 +01:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2025-01-13 14:12:00 +01:00
|
|
|
BattleHex BattleHex::getClosestTile(BattleSide side, const BattleHex & initialPos, const BattleHexArray & hexes)
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-02 23:56:04 +01:00
|
|
|
if(hexes.empty())
|
|
|
|
return BattleHex();
|
2024-11-12 07:11:18 +01:00
|
|
|
|
2025-01-02 23:56:04 +01:00
|
|
|
BattleHex initialHex = BattleHex(initialPos);
|
|
|
|
int closestDistance = std::numeric_limits<int>::max();
|
|
|
|
BattleHexArray closestTiles;
|
2024-11-12 07:11:18 +01:00
|
|
|
|
2025-01-13 14:12:00 +01:00
|
|
|
for(const auto & hex : hexes)
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-02 23:56:04 +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
|
|
|
}
|
|
|
|
|
2025-01-02 23:56:04 +01:00
|
|
|
auto compareHorizontal = [side, initialPos](const BattleHex & left, const BattleHex & right)
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-02 23:56:04 +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
|
|
|
|
2025-01-02 23:56:04 +01:00
|
|
|
auto bestTile = std::min_element(closestTiles.begin(), closestTiles.end(), compareHorizontal);
|
|
|
|
return (bestTile != closestTiles.end()) ? *bestTile : BattleHex();
|
2025-01-03 00:11:41 +01:00
|
|
|
}
|
2024-11-12 07:11:18 +01:00
|
|
|
|
2025-01-06 23:05:45 +01:00
|
|
|
const BattleHexArray & BattleHex::getAllNeighbouringTiles() const noexcept
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-02 23:56:04 +01:00
|
|
|
return BattleHexArray::getAllNeighbouringTiles(*this);
|
2024-11-12 07:11:18 +01:00
|
|
|
}
|
|
|
|
|
2025-01-06 23:05:45 +01:00
|
|
|
const BattleHexArray & BattleHex::getNeighbouringTiles() const noexcept
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-02 23:56:04 +01:00
|
|
|
return BattleHexArray::getNeighbouringTiles(*this);
|
2024-11-12 07:11:18 +01:00
|
|
|
}
|
|
|
|
|
2025-01-06 23:05:45 +01:00
|
|
|
const BattleHexArray & BattleHex::getNeighbouringTilesDoubleWide(BattleSide side) const noexcept
|
2024-11-12 07:11:18 +01:00
|
|
|
{
|
2025-01-06 23:05:45 +01:00
|
|
|
return BattleHexArray::getNeighbouringTilesDoubleWide(*this, side);
|
2025-01-03 00:11:41 +01:00
|
|
|
}
|
2024-11-12 07:11:18 +01:00
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
|
|
|
|
{
|
2025-01-12 17:15:25 +01:00
|
|
|
return os << hex.toInt();
|
2024-11-12 07:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|