BattleHexArray.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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)
  22. {
  23. BattleHex initialHex = BattleHex(initialPos);
  24. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  25. {
  26. return initialHex.getDistance(initialHex, left) < initialHex.getDistance(initialHex, right);
  27. };
  28. BattleHexArray sortedTiles(*this);
  29. boost::sort(sortedTiles, compareDistance); //closest tiles at front
  30. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  31. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  32. {
  33. return closestDistance < here.getDistance(initialPos, here);
  34. };
  35. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  36. auto compareHorizontal = [side, initialPos](const BattleHex left, const BattleHex right) -> bool
  37. {
  38. if(left.getX() != right.getX())
  39. {
  40. if(side == BattleSide::ATTACKER)
  41. return left.getX() > right.getX(); //find furthest right
  42. else
  43. return left.getX() < right.getX(); //find furthest left
  44. }
  45. else
  46. {
  47. //Prefer tiles in the same row.
  48. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  49. }
  50. };
  51. boost::sort(sortedTiles, compareHorizontal);
  52. return sortedTiles.front();
  53. }
  54. void BattleHexArray::merge(const BattleHexArray & other) noexcept
  55. {
  56. for(auto hex : other)
  57. {
  58. insert(hex);
  59. }
  60. }
  61. void BattleHexArray::erase(iterator first, iterator last) noexcept
  62. {
  63. for(auto it = first; it != last && it != internalStorage.end(); ++it)
  64. {
  65. presenceFlags[*it] = 0;
  66. }
  67. internalStorage.erase(first, last);
  68. }
  69. void BattleHexArray::clear() noexcept
  70. {
  71. for(auto hex : internalStorage)
  72. presenceFlags[hex] = 0;
  73. internalStorage.clear();
  74. }
  75. static BattleHexArray::NeighbouringTilesCache calculateNeighbouringTiles()
  76. {
  77. BattleHexArray::NeighbouringTilesCache ret;
  78. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  79. {
  80. auto hexes = BattleHexArray::generateNeighbouringTiles(hex);
  81. size_t index = 0;
  82. for(auto neighbour : hexes)
  83. ret[hex].at(index++) = neighbour;
  84. }
  85. return ret;
  86. }
  87. const BattleHexArray::NeighbouringTilesCache BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles();
  88. VCMI_LIB_NAMESPACE_END