BattleHexArray.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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(BattleHex target) noexcept
  97. {
  98. assert(contains(target));
  99. vstd::erase(internalStorage, target);
  100. presenceFlags[target.toInt()] = 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. static const BattleHexArray invalid;
  139. if (hex.isValid())
  140. return allNeighbouringTiles[hex.toInt()];
  141. else
  142. return invalid;
  143. }
  144. /// get (precomputed) only valid and available surrounding tiles
  145. static const BattleHexArray & getNeighbouringTiles(BattleHex hex) noexcept
  146. {
  147. static const BattleHexArray invalid;
  148. if (hex.isValid())
  149. return neighbouringTiles[hex.toInt()];
  150. else
  151. return invalid;
  152. }
  153. /// get (precomputed) only valid and available surrounding tiles for double wide creatures
  154. static const BattleHexArray & getNeighbouringTilesDoubleWide(BattleHex hex, BattleSide side) noexcept
  155. {
  156. assert(hex.isValid() && (side == BattleSide::ATTACKER || side == BattleSide::DEFENDER));
  157. return neighbouringTilesDoubleWide.at(side)[hex.toInt()];
  158. }
  159. [[nodiscard]] inline bool contains(BattleHex hex) const noexcept
  160. {
  161. if(hex.isValid())
  162. return presenceFlags[hex.toInt()];
  163. /*
  164. if(!isTower(hex))
  165. logGlobal->warn("BattleHexArray::contains( %d ) - invalid BattleHex!", hex);
  166. */
  167. // returns true also for invalid hexes
  168. return true;
  169. }
  170. template <typename Serializer>
  171. void serialize(Serializer & s)
  172. {
  173. s & internalStorage;
  174. if(!s.saving)
  175. {
  176. for(auto hex : internalStorage)
  177. presenceFlags[hex.toInt()] = true;
  178. }
  179. }
  180. [[nodiscard]] inline const BattleHex & back() const noexcept
  181. {
  182. return internalStorage.back();
  183. }
  184. [[nodiscard]] inline const BattleHex & front() const noexcept
  185. {
  186. return internalStorage.front();
  187. }
  188. [[nodiscard]] inline const BattleHex & operator[](size_type index) const noexcept
  189. {
  190. return internalStorage[index];
  191. }
  192. [[nodiscard]] inline const BattleHex & at(size_type index) const
  193. {
  194. return internalStorage.at(index);
  195. }
  196. [[nodiscard]] inline size_type size() const noexcept
  197. {
  198. return internalStorage.size();
  199. }
  200. [[nodiscard]] inline iterator begin() noexcept
  201. {
  202. return internalStorage.begin();
  203. }
  204. [[nodiscard]] inline const_iterator begin() const noexcept
  205. {
  206. return internalStorage.begin();
  207. }
  208. [[nodiscard]] inline bool empty() const noexcept
  209. {
  210. return internalStorage.empty();
  211. }
  212. [[nodiscard]] inline iterator end() noexcept
  213. {
  214. return internalStorage.end();
  215. }
  216. [[nodiscard]] inline const_iterator end() const noexcept
  217. {
  218. return internalStorage.end();
  219. }
  220. [[nodiscard]] inline reverse_iterator rbegin() noexcept
  221. {
  222. return reverse_iterator(end());
  223. }
  224. [[nodiscard]] inline const_reverse_iterator rbegin() const noexcept
  225. {
  226. return const_reverse_iterator(end());
  227. }
  228. [[nodiscard]] inline reverse_iterator rend() noexcept
  229. {
  230. return reverse_iterator(begin());
  231. }
  232. [[nodiscard]] inline const_reverse_iterator rend() const noexcept
  233. {
  234. return const_reverse_iterator(begin());
  235. }
  236. bool operator ==(const BattleHexArray & other) const noexcept
  237. {
  238. if(internalStorage != other.internalStorage || presenceFlags != other.presenceFlags)
  239. return false;
  240. return true;
  241. }
  242. private:
  243. StorageType internalStorage;
  244. std::bitset<totalSize> presenceFlags;
  245. [[nodiscard]] inline bool isNotValidForInsertion(BattleHex hex) const
  246. {
  247. if(isTower(hex))
  248. return true;
  249. if(!hex.isValid())
  250. {
  251. //logGlobal->warn("BattleHexArray::insert( %d ) - invalid BattleHex!", hex);
  252. return true;
  253. }
  254. return contains(hex) || internalStorage.size() >= totalSize;
  255. }
  256. [[nodiscard]] inline bool isTower(BattleHex hex) const
  257. {
  258. return hex == BattleHex::CASTLE_CENTRAL_TOWER || hex == BattleHex::CASTLE_UPPER_TOWER || hex == BattleHex::CASTLE_BOTTOM_TOWER;
  259. }
  260. static const ArrayOfBattleHexArrays neighbouringTiles;
  261. static const ArrayOfBattleHexArrays allNeighbouringTiles;
  262. static const std::map<BattleSide, ArrayOfBattleHexArrays> neighbouringTilesDoubleWide;
  263. static ArrayOfBattleHexArrays precalculateNeighbouringTiles();
  264. static ArrayOfBattleHexArrays precalculateAllNeighbouringTiles();
  265. static ArrayOfBattleHexArrays precalculateNeighbouringTilesDoubleWide(BattleSide side);
  266. };
  267. VCMI_LIB_NAMESPACE_END