BattleHexArray.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 BattleHexArray::generateNeighbouringTiles(BattleHex hex)
  65. {
  66. BattleHexArray ret;
  67. for(auto dir : BattleHex::hexagonalDirections())
  68. ret.checkAndPush(hex.cloneInDirection(dir, false));
  69. return ret;
  70. }
  71. const BattleHexArray & BattleHexArray::getNeighbouringTilesDblWide(BattleHex pos, BattleSide side)
  72. {
  73. static std::array<ArrayOfBattleHexArrays, 2> ret; // 2 -> only two valid sides: ATTACKER and DEFENDER
  74. size_t sideIdx = static_cast<size_t>(side);
  75. static bool initialized[2] = { false, false };
  76. if(!initialized[sideIdx])
  77. {
  78. // first run, need to initialize
  79. for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
  80. {
  81. BattleHexArray hexes;
  82. if(side == BattleSide::ATTACKER)
  83. {
  84. const BattleHex otherHex = hex - 1;
  85. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  86. hexes.checkAndPush(hex.cloneInDirection(dir, false));
  87. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  88. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false));
  89. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  90. }
  91. else if(side == BattleSide::DEFENDER)
  92. {
  93. const BattleHex otherHex = hex + 1;
  94. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  95. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  96. hexes.checkAndPush(otherHex.cloneInDirection(dir, false));
  97. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  98. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
  99. }
  100. ret[sideIdx][hex.hex] = std::move(hexes);
  101. }
  102. initialized[sideIdx] = true;
  103. }
  104. return ret[sideIdx][pos.hex];
  105. }
  106. const BattleHexArray & BattleHexArray::getClosestTilesCache(BattleHex pos, BattleSide side)
  107. {
  108. assert(!neighbouringTilesCache.empty());
  109. static std::array<BattleHexArray, 2> ret;
  110. static bool initialized = false;
  111. size_t sideIdx = static_cast<size_t>(side);
  112. if(!initialized)
  113. {
  114. ret[sideIdx].resize(GameConstants::BFIELD_SIZE);
  115. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  116. {
  117. ret[sideIdx].set(hex, neighbouringTilesCache[hex].getClosestTile(BattleSide::ATTACKER, hex));
  118. }
  119. initialized = true;
  120. }
  121. return ret[sideIdx];
  122. }
  123. void BattleHexArray::merge(const BattleHexArray & other) noexcept
  124. {
  125. for(auto hex : other)
  126. {
  127. insert(hex);
  128. }
  129. }
  130. void BattleHexArray::erase(iterator first, iterator last) noexcept
  131. {
  132. for(auto it = first; it != last && it != internalStorage.end(); ++it)
  133. {
  134. presenceFlags[*it] = 0;
  135. }
  136. internalStorage.erase(first, last);
  137. }
  138. void BattleHexArray::clear() noexcept
  139. {
  140. for(auto hex : internalStorage)
  141. presenceFlags[hex] = 0;
  142. internalStorage.clear();
  143. }
  144. const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles();
  145. VCMI_LIB_NAMESPACE_END