ReachabilityInfo.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * ReachabilityInfo.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ReachabilityInfo.h"
  12. #include "Unit.h"
  13. ReachabilityInfo::Parameters::Parameters()
  14. {
  15. perspective = BattlePerspective::ALL_KNOWING;
  16. side = 0;
  17. doubleWide = flying = false;
  18. }
  19. ReachabilityInfo::Parameters::Parameters(const battle::Unit * Stack, BattleHex StartPosition)
  20. {
  21. perspective = (BattlePerspective::BattlePerspective)(Stack->unitSide());
  22. startPosition = StartPosition;
  23. doubleWide = Stack->doubleWide();
  24. side = Stack->unitSide();
  25. flying = Stack->hasBonusOfType(Bonus::FLYING);
  26. knownAccessible = battle::Unit::getHexes(startPosition, doubleWide, side);
  27. }
  28. ReachabilityInfo::ReachabilityInfo()
  29. {
  30. distances.fill(INFINITE_DIST);
  31. predecessors.fill(BattleHex::INVALID);
  32. }
  33. bool ReachabilityInfo::isReachable(BattleHex hex) const
  34. {
  35. return distances[hex] < INFINITE_DIST;
  36. }
  37. int ReachabilityInfo::distToNearestNeighbour(
  38. const std::vector<BattleHex> & targetHexes,
  39. BattleHex * chosenHex) const
  40. {
  41. int ret = 1000000;
  42. for(auto targetHex : targetHexes)
  43. {
  44. for(auto & n : targetHex.neighbouringTiles())
  45. {
  46. if(distances[n] >= 0 && distances[n] < ret)
  47. {
  48. ret = distances[n];
  49. if(chosenHex)
  50. *chosenHex = n;
  51. }
  52. }
  53. }
  54. return ret;
  55. }
  56. int ReachabilityInfo::distToNearestNeighbour(
  57. const battle::Unit * attacker,
  58. const battle::Unit * defender,
  59. BattleHex * chosenHex) const
  60. {
  61. auto attackableHexes = defender->getAttackableHexes(attacker);
  62. return distToNearestNeighbour(attackableHexes, chosenHex);
  63. }