2017-06-24 15:51:07 +02:00
|
|
|
/*
|
|
|
|
* ReachabilityInfo.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-07-01 20:05:10 +02:00
|
|
|
#include "StdInc.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
#include "ReachabilityInfo.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "Unit.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-06-24 15:51:07 +02:00
|
|
|
|
|
|
|
ReachabilityInfo::Parameters::Parameters()
|
|
|
|
{
|
|
|
|
perspective = BattlePerspective::ALL_KNOWING;
|
2017-07-01 10:34:00 +02:00
|
|
|
side = 0;
|
|
|
|
doubleWide = flying = false;
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ReachabilityInfo::Parameters::Parameters(const battle::Unit * Stack, BattleHex StartPosition)
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
perspective = (BattlePerspective::BattlePerspective)(Stack->unitSide());
|
|
|
|
startPosition = StartPosition;
|
|
|
|
doubleWide = Stack->doubleWide();
|
|
|
|
side = Stack->unitSide();
|
|
|
|
flying = Stack->hasBonusOfType(Bonus::FLYING);
|
|
|
|
knownAccessible = battle::Unit::getHexes(startPosition, doubleWide, side);
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReachabilityInfo::ReachabilityInfo()
|
|
|
|
{
|
|
|
|
distances.fill(INFINITE_DIST);
|
|
|
|
predecessors.fill(BattleHex::INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachabilityInfo::isReachable(BattleHex hex) const
|
|
|
|
{
|
|
|
|
return distances[hex] < INFINITE_DIST;
|
|
|
|
}
|
2020-11-23 08:40:36 +02:00
|
|
|
|
|
|
|
int ReachabilityInfo::distToNearestNeighbour(
|
2020-11-28 17:11:33 +02:00
|
|
|
const std::vector<BattleHex> & targetHexes,
|
2020-11-23 08:40:36 +02:00
|
|
|
BattleHex * chosenHex) const
|
|
|
|
{
|
|
|
|
int ret = 1000000;
|
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
for(auto targetHex : targetHexes)
|
2020-11-23 08:40:36 +02:00
|
|
|
{
|
2020-11-28 17:11:33 +02:00
|
|
|
for(auto & n : targetHex.neighbouringTiles())
|
2020-11-23 08:40:36 +02:00
|
|
|
{
|
|
|
|
if(distances[n] >= 0 && distances[n] < ret)
|
|
|
|
{
|
|
|
|
ret = distances[n];
|
|
|
|
if(chosenHex)
|
|
|
|
*chosenHex = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
|
|
|
|
int ReachabilityInfo::distToNearestNeighbour(
|
|
|
|
const battle::Unit * attacker,
|
|
|
|
const battle::Unit * defender,
|
|
|
|
BattleHex * chosenHex) const
|
|
|
|
{
|
|
|
|
auto attackableHexes = defender->getAttackableHexes(attacker);
|
|
|
|
|
|
|
|
return distToNearestNeighbour(attackableHexes, chosenHex);
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|