BattleHexArray.h 7.7 KB

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