BattleHexArray.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //BattleHex initialHex = BattleHex(initialPos);
  51. //auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  52. //{
  53. // return initialHex.getDistance(initialHex, left) < initialHex.getDistance(initialHex, right);
  54. //};
  55. //BattleHexArray sortedTiles(*this);
  56. //boost::sort(sortedTiles, compareDistance); //closest tiles at front
  57. //int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  58. //auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  59. //{
  60. // return closestDistance < here.getDistance(initialPos, here);
  61. //};
  62. //vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  63. //auto compareHorizontal = [side, initialPos](const BattleHex left, const BattleHex right) -> bool
  64. //{
  65. // if(left.getX() != right.getX())
  66. // {
  67. // if(side == BattleSide::ATTACKER)
  68. // return left.getX() > right.getX(); //find furthest right
  69. // else
  70. // return left.getX() < right.getX(); //find furthest left
  71. // }
  72. // else
  73. // {
  74. // //Prefer tiles in the same row.
  75. // return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  76. // }
  77. //};
  78. //boost::sort(sortedTiles, compareHorizontal);
  79. //return sortedTiles.front();
  80. }
  81. BattleHexArray::NeighbouringTilesCache BattleHexArray::calculateNeighbouringTiles()
  82. {
  83. BattleHexArray::NeighbouringTilesCache ret;
  84. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  85. {
  86. BattleHexArray hexes = BattleHexArray::generateNeighbouringTiles(hex);
  87. size_t index = 0;
  88. ret[hex].resize(hexes.size());
  89. for(auto neighbour : hexes)
  90. ret[hex].set(index++, neighbour);
  91. }
  92. return ret;
  93. }
  94. BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
  95. {
  96. BattleHexArray ret;
  97. for(auto dir : BattleHex::hexagonalDirections())
  98. ret.checkAndPush(hex.cloneInDirection(dir, false));
  99. return ret;
  100. }
  101. BattleHexArray BattleHexArray::generateAttackerClosestTilesCache()
  102. {
  103. assert(!neighbouringTilesCache.empty());
  104. BattleHexArray ret;
  105. ret.resize(GameConstants::BFIELD_SIZE);
  106. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  107. {
  108. ret.set(hex, neighbouringTilesCache[hex].getClosestTile(BattleSide::ATTACKER, hex));
  109. }
  110. return ret;
  111. }
  112. BattleHexArray BattleHexArray::generateDefenderClosestTilesCache()
  113. {
  114. assert(!neighbouringTilesCache.empty());
  115. BattleHexArray ret;
  116. ret.resize(GameConstants::BFIELD_SIZE);
  117. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  118. {
  119. ret.set(hex, neighbouringTilesCache[hex].getClosestTile(BattleSide::DEFENDER, hex));
  120. }
  121. return ret;
  122. }
  123. BattleHex BattleHexArray::getClosestTileFromAllPossibleNeighbours(BattleSide side, BattleHex pos)
  124. {
  125. if(side == BattleSide::ATTACKER)
  126. return closestTilesCacheForAttacker[pos.hex];
  127. else if(side == BattleSide::DEFENDER)
  128. return closestTilesCacheForDefender[pos.hex];
  129. else
  130. assert(false); // we should never be here
  131. }
  132. void BattleHexArray::merge(const BattleHexArray & other) noexcept
  133. {
  134. for(auto hex : other)
  135. {
  136. insert(hex);
  137. }
  138. }
  139. void BattleHexArray::erase(iterator first, iterator last) noexcept
  140. {
  141. for(auto it = first; it != last && it != internalStorage.end(); ++it)
  142. {
  143. presenceFlags[*it] = 0;
  144. }
  145. internalStorage.erase(first, last);
  146. }
  147. void BattleHexArray::clear() noexcept
  148. {
  149. for(auto hex : internalStorage)
  150. presenceFlags[hex] = 0;
  151. internalStorage.clear();
  152. }
  153. const BattleHexArray::NeighbouringTilesCache BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles();
  154. const BattleHexArray BattleHexArray::closestTilesCacheForAttacker = generateAttackerClosestTilesCache();
  155. const BattleHexArray BattleHexArray::closestTilesCacheForDefender = generateDefenderClosestTilesCache();
  156. VCMI_LIB_NAMESPACE_END