BattleHexArray.cpp 3.4 KB

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