BattleHexArray.cpp 3.3 KB

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