BattleHexArray.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * BattleHexArray.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 "BattleHexArray.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. BattleHexArray::BattleHexArray(std::initializer_list<BattleHex> initList) noexcept
  14. : BattleHexArray()
  15. {
  16. for(auto hex : initList)
  17. {
  18. insert(hex);
  19. }
  20. }
  21. BattleHex BattleHexArray::getClosestTile(BattleSide side, BattleHex initialPos) const
  22. {
  23. if(this->empty())
  24. return BattleHex();
  25. BattleHex initialHex = BattleHex(initialPos);
  26. int closestDistance = std::numeric_limits<int>::max();
  27. BattleHexArray closestTiles;
  28. for(auto hex : internalStorage)
  29. {
  30. int distance = initialHex.getDistance(initialHex, hex);
  31. if(distance < closestDistance)
  32. {
  33. closestDistance = distance;
  34. closestTiles.clear();
  35. closestTiles.insert(hex);
  36. }
  37. else if(distance == closestDistance)
  38. closestTiles.insert(hex);
  39. }
  40. auto compareHorizontal = [side, initialPos](const BattleHex & left, const BattleHex & right)
  41. {
  42. if(left.getX() != right.getX())
  43. {
  44. return (side == BattleSide::ATTACKER) ? (left.getX() > right.getX()) : (left.getX() < right.getX());
  45. }
  46. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  47. };
  48. auto bestTile = std::min_element(closestTiles.begin(), closestTiles.end(), compareHorizontal);
  49. return (bestTile != closestTiles.end()) ? *bestTile : BattleHex();
  50. }
  51. BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTiles()
  52. {
  53. BattleHexArray::ArrayOfBattleHexArrays ret;
  54. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  55. {
  56. BattleHexArray hexes = BattleHexArray::generateNeighbouringTiles(hex);
  57. size_t index = 0;
  58. ret[hex].resize(hexes.size());
  59. for(auto neighbour : hexes)
  60. ret[hex].set(index++, neighbour);
  61. }
  62. return ret;
  63. }
  64. BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTilesDblWide(BattleSide side)
  65. {
  66. ArrayOfBattleHexArrays ret;
  67. for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
  68. {
  69. BattleHexArray hexes;
  70. if(side == BattleSide::ATTACKER)
  71. {
  72. const BattleHex otherHex = hex - 1;
  73. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  74. hexes.checkAndPush(hex.cloneInDirection(dir, false));
  75. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  76. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false));
  77. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  78. }
  79. else if(side == BattleSide::DEFENDER)
  80. {
  81. const BattleHex otherHex = hex + 1;
  82. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  83. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  84. hexes.checkAndPush(otherHex.cloneInDirection(dir, false));
  85. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  86. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
  87. }
  88. ret[hex.hex] = std::move(hexes);
  89. }
  90. return ret;
  91. }
  92. BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
  93. {
  94. BattleHexArray ret;
  95. for(auto dir : BattleHex::hexagonalDirections())
  96. ret.checkAndPush(hex.cloneInDirection(dir, false));
  97. return ret;
  98. }
  99. void BattleHexArray::insert(const BattleHexArray & other) noexcept
  100. {
  101. for(auto hex : other)
  102. {
  103. insert(hex);
  104. }
  105. }
  106. void BattleHexArray::erase(iterator first, iterator last) noexcept
  107. {
  108. for(auto it = first; it != last && it != internalStorage.end(); ++it)
  109. {
  110. presenceFlags[*it] = 0;
  111. }
  112. internalStorage.erase(first, last);
  113. }
  114. void BattleHexArray::clear() noexcept
  115. {
  116. for(auto hex : internalStorage)
  117. presenceFlags[hex] = 0;
  118. internalStorage.clear();
  119. }
  120. const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles();
  121. const std::map<BattleSide, BattleHexArray::ArrayOfBattleHexArrays> BattleHexArray::neighbouringTilesDblWide =
  122. { { BattleSide::ATTACKER, calculateNeighbouringTilesDblWide(BattleSide::ATTACKER) },
  123. { BattleSide::DEFENDER, calculateNeighbouringTilesDblWide(BattleSide::DEFENDER) } };
  124. VCMI_LIB_NAMESPACE_END