BattleHexArray.h 7.9 KB

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