BattleHexArray.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * BattleHexArray.h, 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. #pragma once
  11. #include "BattleHex.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Class representing an array of unique BattleHex objects
  14. class DLL_LINKAGE BattleHexArray
  15. {
  16. public:
  17. static constexpr uint8_t totalSize = GameConstants::BFIELD_SIZE;
  18. using StorageType = std::vector<BattleHex>;
  19. using value_type = BattleHex;
  20. using size_type = StorageType::size_type;
  21. using reference = value_type &;
  22. using const_reference = const value_type &;
  23. using pointer = value_type *;
  24. using const_pointer = const value_type *;
  25. using difference_type = typename StorageType::difference_type;
  26. using iterator = typename StorageType::iterator;
  27. using const_iterator = typename StorageType::const_iterator;
  28. using reverse_iterator = typename StorageType::reverse_iterator;
  29. using const_reverse_iterator = typename StorageType::const_reverse_iterator;
  30. using NeighbouringTilesCache = std::array<BattleHexArray, GameConstants::BFIELD_SIZE>;
  31. static const NeighbouringTilesCache neighbouringTilesCache;
  32. static const BattleHexArray closestTilesCacheForAttacker;
  33. static const BattleHexArray closestTilesCacheForDefender;
  34. BattleHexArray() noexcept
  35. {
  36. internalStorage.reserve(totalSize);
  37. }
  38. template <typename Container, typename = std::enable_if_t<
  39. std::is_convertible_v<typename Container::value_type, BattleHex>>>
  40. BattleHexArray(const Container & container) noexcept
  41. : BattleHexArray()
  42. {
  43. for(auto value : container)
  44. {
  45. insert(value);
  46. }
  47. }
  48. void resize(size_type size)
  49. {
  50. clear();
  51. internalStorage.resize(size);
  52. }
  53. BattleHexArray(std::initializer_list<BattleHex> initList) noexcept;
  54. /// returns all tiles, unavailable tiles will be set as invalid
  55. /// order of returned tiles matches EDir enum
  56. static BattleHexArray generateAllNeighbouringTiles(BattleHex hex)
  57. {
  58. BattleHexArray ret;
  59. ret.resize(6);
  60. for(auto dir : BattleHex::hexagonalDirections())
  61. ret.set(dir, hex.cloneInDirection(dir, false));
  62. return ret;
  63. }
  64. void checkAndPush(BattleHex tile)
  65. {
  66. if(tile.isAvailable() && !contains(tile))
  67. {
  68. presenceFlags[tile] = 1;
  69. internalStorage.emplace_back(tile);
  70. }
  71. }
  72. void insert(BattleHex hex) noexcept
  73. {
  74. /*if(isNotValidForInsertion(hex))
  75. return;*/
  76. if(contains(hex))
  77. return;
  78. presenceFlags[hex] = 1;
  79. internalStorage.emplace_back(hex);
  80. }
  81. void set(size_type index, BattleHex hex)
  82. {
  83. /*if(isNotValidForInsertion(hex))
  84. return;*/
  85. if(index >= internalStorage.size())
  86. {
  87. logGlobal->error("Invalid BattleHexArray::set index parameter. It is " + std::to_string(index)
  88. + " and current size is " + std::to_string(internalStorage.size()));
  89. throw std::out_of_range("Invalid BattleHexArray::set index parameter. It is " + std::to_string(index)
  90. + " and current size is " + std::to_string(internalStorage.size()));
  91. }
  92. if(contains(hex))
  93. return;
  94. presenceFlags[hex] = 1;
  95. internalStorage[index] = hex;
  96. }
  97. iterator BattleHexArray::insert(iterator pos, BattleHex hex) noexcept
  98. {
  99. /*if(isNotValidForInsertion(hex))
  100. return pos;*/
  101. if(contains(hex))
  102. return pos;
  103. presenceFlags[hex] = 1;
  104. return internalStorage.insert(pos, hex);
  105. }
  106. static BattleHex getClosestTileFromAllPossibleNeighbours(BattleSide side, BattleHex pos);
  107. BattleHex getClosestTile(BattleSide side, BattleHex initialPos) const;
  108. void merge(const BattleHexArray & other) noexcept;
  109. void clear() noexcept;
  110. inline void erase(size_type index) noexcept
  111. {
  112. assert(index < totalSize);
  113. internalStorage[index] = BattleHex::INVALID;
  114. presenceFlags[index] = 0;
  115. }
  116. void erase(iterator first, iterator last) noexcept;
  117. inline void pop_back() noexcept
  118. {
  119. presenceFlags[internalStorage.back()] = 0;
  120. internalStorage.pop_back();
  121. }
  122. inline std::vector<BattleHex> toVector() const noexcept
  123. {
  124. return internalStorage;
  125. }
  126. template <typename Predicate>
  127. iterator findIf(Predicate predicate) noexcept
  128. {
  129. return std::find_if(begin(), end(), predicate);
  130. }
  131. template <typename Predicate>
  132. const_iterator findIf(Predicate predicate) const noexcept
  133. {
  134. return std::find_if(begin(), end(), predicate);
  135. }
  136. template <typename Predicate>
  137. BattleHexArray filterBy(Predicate predicate) const noexcept
  138. {
  139. BattleHexArray filtered;
  140. for(auto hex : internalStorage)
  141. {
  142. if(predicate(hex))
  143. {
  144. filtered.insert(hex);
  145. }
  146. }
  147. return filtered;
  148. }
  149. [[nodiscard]] inline bool BattleHexArray::contains(BattleHex hex) const noexcept
  150. {
  151. if(hex.isValid())
  152. return presenceFlags[hex];
  153. /*
  154. if(!isTower(hex))
  155. logGlobal->warn("BattleHexArray::contains( %d ) - invalid BattleHex!", hex);
  156. */
  157. // return true for invalid hexes
  158. return true;
  159. }
  160. template <typename Serializer>
  161. void serialize(Serializer & s)
  162. {
  163. s & internalStorage;
  164. if(!internalStorage.empty() && presenceFlags[internalStorage.front()] == 0)
  165. {
  166. for(auto hex : internalStorage)
  167. presenceFlags[hex] = 1;
  168. }
  169. }
  170. [[nodiscard]] inline const BattleHex & BattleHexArray::back() const noexcept
  171. {
  172. return internalStorage.back();
  173. }
  174. [[nodiscard]] inline const BattleHex & BattleHexArray::front() const noexcept
  175. {
  176. return internalStorage.front();
  177. }
  178. [[nodiscard]] inline const BattleHex & BattleHexArray::operator[](size_type index) const noexcept
  179. {
  180. return internalStorage[index];
  181. }
  182. [[nodiscard]] inline const BattleHex & BattleHexArray::at(size_type index) const
  183. {
  184. return internalStorage.at(index);
  185. }
  186. [[nodiscard]] inline size_type size() const noexcept
  187. {
  188. return internalStorage.size();
  189. }
  190. [[nodiscard]] inline BattleHexArray::iterator BattleHexArray::begin() noexcept
  191. {
  192. return internalStorage.begin();
  193. }
  194. [[nodiscard]] inline BattleHexArray::const_iterator BattleHexArray::begin() const noexcept
  195. {
  196. return internalStorage.begin();
  197. }
  198. [[nodiscard]] inline bool BattleHexArray::empty() const noexcept
  199. {
  200. return internalStorage.empty();
  201. }
  202. [[nodiscard]] inline BattleHexArray::iterator BattleHexArray::end() noexcept
  203. {
  204. return internalStorage.end();
  205. }
  206. [[nodiscard]] inline BattleHexArray::const_iterator BattleHexArray::end() const noexcept
  207. {
  208. return internalStorage.end();
  209. }
  210. [[nodiscard]] inline BattleHexArray::reverse_iterator BattleHexArray::rbegin() noexcept
  211. {
  212. return reverse_iterator(end());
  213. }
  214. [[nodiscard]] inline BattleHexArray::const_reverse_iterator BattleHexArray::rbegin() const noexcept
  215. {
  216. return const_reverse_iterator(end());
  217. }
  218. [[nodiscard]] inline BattleHexArray::reverse_iterator BattleHexArray::rend() noexcept
  219. {
  220. return reverse_iterator(begin());
  221. }
  222. [[nodiscard]] inline BattleHexArray::const_reverse_iterator BattleHexArray::rend() const noexcept
  223. {
  224. return const_reverse_iterator(begin());
  225. }
  226. private:
  227. StorageType internalStorage;
  228. std::array<uint8_t, totalSize> presenceFlags = {};
  229. [[nodiscard]] inline bool BattleHexArray::isNotValidForInsertion(BattleHex hex) const
  230. {
  231. if(isTower(hex))
  232. return true;
  233. if(!hex.isValid())
  234. {
  235. //logGlobal->warn("BattleHexArray::insert( %d ) - invalid BattleHex!", hex);
  236. return true;
  237. }
  238. return contains(hex) || internalStorage.size() >= totalSize;
  239. }
  240. [[nodiscard]] inline bool isTower(BattleHex hex) const
  241. {
  242. return hex == BattleHex::CASTLE_CENTRAL_TOWER || hex == BattleHex::CASTLE_UPPER_TOWER || hex == BattleHex::CASTLE_BOTTOM_TOWER;
  243. }
  244. /// returns all valid neighbouring tiles
  245. static BattleHexArray::NeighbouringTilesCache calculateNeighbouringTiles();
  246. static BattleHexArray generateNeighbouringTiles(BattleHex hex);
  247. static BattleHexArray generateAttackerClosestTilesCache();
  248. static BattleHexArray generateDefenderClosestTilesCache();
  249. };
  250. VCMI_LIB_NAMESPACE_END