BattleHexArray.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. void BattleHexArray::insert(const BattleHexArray & other) noexcept
  22. {
  23. for(auto hex : other)
  24. {
  25. insert(hex);
  26. }
  27. }
  28. void BattleHexArray::erase(iterator first, iterator last) noexcept
  29. {
  30. for(auto it = first; it != last && it != internalStorage.end(); ++it)
  31. {
  32. presenceFlags[it->toInt()] = 0;
  33. }
  34. internalStorage.erase(first, last);
  35. }
  36. void BattleHexArray::clear() noexcept
  37. {
  38. for(auto hex : internalStorage)
  39. presenceFlags[hex.toInt()] = 0;
  40. internalStorage.clear();
  41. }
  42. BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::precalculateNeighbouringTiles()
  43. {
  44. BattleHexArray::ArrayOfBattleHexArrays ret;
  45. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  46. {
  47. BattleHexArray hexes;
  48. for(auto dir : BattleHex::hexagonalDirections())
  49. hexes.checkAndPush(BattleHex(hex).cloneInDirection(dir, false));
  50. size_t index = 0;
  51. ret[hex].resize(hexes.size());
  52. for(auto neighbour : hexes)
  53. ret[hex].set(index++, neighbour);
  54. }
  55. return ret;
  56. }
  57. BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::precalculateAllNeighbouringTiles()
  58. {
  59. ArrayOfBattleHexArrays ret;
  60. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  61. {
  62. ret[hex].resize(6);
  63. for(auto dir : BattleHex::hexagonalDirections())
  64. ret[hex].set(dir, BattleHex(hex).cloneInDirection(dir, false));
  65. }
  66. return ret;
  67. }
  68. BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::precalculateNeighbouringTilesDoubleWide(BattleSide side)
  69. {
  70. ArrayOfBattleHexArrays ret;
  71. for(si16 h = 0; h < GameConstants::BFIELD_SIZE; h++)
  72. {
  73. BattleHexArray hexes;
  74. BattleHex hex(h);
  75. if(side == BattleSide::ATTACKER)
  76. {
  77. const BattleHex otherHex = h - 1;
  78. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  79. hexes.checkAndPush(hex.cloneInDirection(dir, false));
  80. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  81. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false));
  82. hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  83. }
  84. else if(side == BattleSide::DEFENDER)
  85. {
  86. const BattleHex otherHex = h + 1;
  87. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
  88. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  89. hexes.checkAndPush(otherHex.cloneInDirection(dir, false));
  90. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
  91. hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
  92. }
  93. ret[h] = std::move(hexes);
  94. }
  95. return ret;
  96. }
  97. const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::neighbouringTiles = precalculateNeighbouringTiles();
  98. const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::allNeighbouringTiles = precalculateAllNeighbouringTiles();
  99. const std::map<BattleSide, BattleHexArray::ArrayOfBattleHexArrays> BattleHexArray::neighbouringTilesDoubleWide =
  100. {
  101. { BattleSide::ATTACKER, precalculateNeighbouringTilesDoubleWide(BattleSide::ATTACKER) },
  102. { BattleSide::DEFENDER, precalculateNeighbouringTilesDoubleWide(BattleSide::DEFENDER) }
  103. };
  104. VCMI_LIB_NAMESPACE_END