BattleHexArray.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include <vstd/RNG.h>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. /**
  16. * @brief Class representing a collection of unique, valid BattleHex objects.
  17. * The BattleHexArray is a specialized container designed for storing instances
  18. * of BattleHex. Key notes:
  19. * - Each BattleHex in the array is unique.
  20. * - Invalid BattleHex objects (e.g., those with an out-of-bounds or special
  21. * value) cannot be inserted into the array.
  22. * - Maintains an efficient storage mechanism for fast access and presence tracking system using bitset for quick existence checks.
  23. * - Attempting to insert invalid BattleHex objects will have no effect.
  24. *
  25. */
  26. class DLL_LINKAGE BattleHexArray
  27. {
  28. public:
  29. static constexpr uint8_t totalSize = GameConstants::BFIELD_SIZE;
  30. using StorageType = boost::container::small_vector<BattleHex, 8>;
  31. using ArrayOfBattleHexArrays = std::array<BattleHexArray, totalSize>;
  32. using value_type = BattleHex;
  33. using size_type = StorageType::size_type;
  34. using reference = value_type &;
  35. using const_reference = const value_type &;
  36. using pointer = value_type *;
  37. using const_pointer = const value_type *;
  38. using difference_type = typename StorageType::difference_type;
  39. // using iterator = typename StorageType::iterator;
  40. using const_iterator = typename StorageType::const_iterator;
  41. // using reverse_iterator = typename StorageType::reverse_iterator;
  42. using const_reverse_iterator = typename StorageType::const_reverse_iterator;
  43. BattleHexArray() = default;
  44. template <typename Container, typename = std::enable_if_t<
  45. std::is_convertible_v<typename Container::value_type, BattleHex>>>
  46. BattleHexArray(const Container & container) noexcept
  47. : BattleHexArray()
  48. {
  49. for(auto value : container)
  50. {
  51. insert(value);
  52. }
  53. }
  54. void resize(size_type size)
  55. {
  56. clear();
  57. internalStorage.resize(size);
  58. }
  59. BattleHexArray(std::initializer_list<BattleHex> initList) noexcept;
  60. void checkAndPush(const BattleHex & tile)
  61. {
  62. if(tile.isAvailable() && !contains(tile))
  63. {
  64. presenceFlags.set(tile.toInt());
  65. internalStorage.emplace_back(tile);
  66. }
  67. }
  68. void insert(const BattleHex & hex) noexcept
  69. {
  70. if(contains(hex))
  71. return;
  72. presenceFlags.set(hex.toInt());
  73. internalStorage.emplace_back(hex);
  74. }
  75. void set(size_type index, const BattleHex & hex)
  76. {
  77. if(index >= internalStorage.size())
  78. {
  79. logGlobal->error("Invalid BattleHexArray::set index parameter. It is " + std::to_string(index)
  80. + " and current size is " + std::to_string(internalStorage.size()));
  81. throw std::out_of_range("Invalid BattleHexArray::set index parameter. It is " + std::to_string(index)
  82. + " and current size is " + std::to_string(internalStorage.size()));
  83. }
  84. if(contains(hex))
  85. return;
  86. presenceFlags.set(hex.toInt());
  87. internalStorage[index] = hex;
  88. }
  89. // iterator insert(iterator pos, const BattleHex & hex) noexcept
  90. // {
  91. // if(contains(hex))
  92. // return pos;
  93. //
  94. // presenceFlags.set(hex.toInt());
  95. // return internalStorage.insert(pos, hex);
  96. // }
  97. void insert(const BattleHexArray & other) noexcept;
  98. template <typename Container, typename = std::enable_if_t<
  99. std::is_convertible_v<typename Container::value_type, BattleHex>>>
  100. void insert(const Container & container) noexcept
  101. {
  102. for(auto value : container)
  103. {
  104. insert(value);
  105. }
  106. }
  107. template<typename Predicate>
  108. void sort(Predicate pred)
  109. {
  110. std::sort(internalStorage.begin(), internalStorage.end(), pred);
  111. }
  112. template<typename Predicate>
  113. void erase_if(Predicate pred)
  114. {
  115. vstd::erase_if(internalStorage, pred);
  116. // reinit presence flags
  117. presenceFlags = {};
  118. for(const auto & hex : internalStorage)
  119. presenceFlags.set(hex.toInt()) = true;
  120. }
  121. void shuffle(vstd::RNG & rand)
  122. {
  123. int64_t n = internalStorage.size();
  124. for(int64_t i = n - 1; i > 0; --i)
  125. {
  126. auto randIndex = rand.nextInt64(0, i);
  127. std::swap(internalStorage[i], internalStorage[randIndex]);
  128. }
  129. }
  130. void clear() noexcept;
  131. inline void erase(const BattleHex & target) noexcept
  132. {
  133. assert(contains(target));
  134. vstd::erase(internalStorage, target);
  135. presenceFlags.reset(target.toInt());
  136. }
  137. // void erase(iterator first, iterator last) noexcept;
  138. inline void pop_back() noexcept
  139. {
  140. presenceFlags.reset(internalStorage.back().toInt());
  141. internalStorage.pop_back();
  142. }
  143. inline std::vector<BattleHex> toVector() const noexcept
  144. {
  145. return std::vector<BattleHex>(internalStorage.begin(), internalStorage.end());
  146. }
  147. [[nodiscard]] std::string toString(std::string delimiter = ", ") const noexcept
  148. {
  149. std::string result = "[";
  150. for(auto it = internalStorage.begin(); it != internalStorage.end(); ++it)
  151. {
  152. if(it != internalStorage.begin())
  153. result += delimiter;
  154. result += std::to_string(it->toInt());
  155. }
  156. result += "]";
  157. return result;
  158. }
  159. // template <typename Predicate>
  160. // iterator findIf(Predicate predicate) noexcept
  161. // {
  162. // return std::find_if(begin(), end(), predicate);
  163. // }
  164. template <typename Predicate>
  165. const_iterator findIf(Predicate predicate) const noexcept
  166. {
  167. return std::find_if(begin(), end(), predicate);
  168. }
  169. template <typename Predicate>
  170. BattleHexArray filterBy(Predicate predicate) const noexcept
  171. {
  172. BattleHexArray filtered;
  173. for(const auto & hex : internalStorage)
  174. {
  175. if(predicate(hex))
  176. {
  177. filtered.insert(hex);
  178. }
  179. }
  180. return filtered;
  181. }
  182. /// get (precomputed) all possible surrounding tiles
  183. static const BattleHexArray & getAllNeighbouringTiles(const BattleHex & hex) noexcept
  184. {
  185. static const BattleHexArray invalid;
  186. if (hex.isValid())
  187. return allNeighbouringTiles[hex.toInt()];
  188. else
  189. return invalid;
  190. }
  191. /// get (precomputed) only valid and available surrounding tiles
  192. static const BattleHexArray & getNeighbouringTiles(const BattleHex & hex) noexcept
  193. {
  194. static const BattleHexArray invalid;
  195. if (hex.isValid())
  196. return neighbouringTiles[hex.toInt()];
  197. else
  198. return invalid;
  199. }
  200. /// get (precomputed) only valid and available surrounding tiles for double wide creatures
  201. static const BattleHexArray & getNeighbouringTilesDoubleWide(const BattleHex & hex, BattleSide side) noexcept
  202. {
  203. assert(hex.isValid() && (side == BattleSide::ATTACKER || side == BattleSide::DEFENDER));
  204. return neighbouringTilesDoubleWide.at(side)[hex.toInt()];
  205. }
  206. /// note: returns true when param is ivalid BattleHex
  207. [[nodiscard]] inline bool contains(const BattleHex & hex) const noexcept
  208. {
  209. if(hex.isValid())
  210. return presenceFlags.test(hex.toInt());
  211. return true;
  212. }
  213. template <typename Serializer>
  214. void serialize(Serializer & s)
  215. {
  216. s & internalStorage;
  217. if(!s.saving)
  218. {
  219. for(const auto & hex : internalStorage)
  220. presenceFlags.set(hex.toInt()) = true;
  221. }
  222. }
  223. [[nodiscard]] inline const BattleHex & back() const noexcept
  224. {
  225. return internalStorage.back();
  226. }
  227. [[nodiscard]] inline const BattleHex & front() const noexcept
  228. {
  229. return internalStorage.front();
  230. }
  231. [[nodiscard]] inline const BattleHex & operator[](size_type index) const noexcept
  232. {
  233. return internalStorage[index];
  234. }
  235. [[nodiscard]] inline const BattleHex & at(size_type index) const
  236. {
  237. return internalStorage.at(index);
  238. }
  239. [[nodiscard]] inline size_type size() const noexcept
  240. {
  241. return internalStorage.size();
  242. }
  243. // [[nodiscard]] inline iterator begin() noexcept
  244. // {
  245. // return internalStorage.begin();
  246. // }
  247. [[nodiscard]] inline const_iterator begin() const noexcept
  248. {
  249. return internalStorage.begin();
  250. }
  251. [[nodiscard]] inline bool empty() const noexcept
  252. {
  253. return internalStorage.empty();
  254. }
  255. // [[nodiscard]] inline iterator end() noexcept
  256. // {
  257. // return internalStorage.end();
  258. // }
  259. [[nodiscard]] inline const_iterator end() const noexcept
  260. {
  261. return internalStorage.end();
  262. }
  263. // [[nodiscard]] inline reverse_iterator rbegin() noexcept
  264. // {
  265. // return reverse_iterator(end());
  266. // }
  267. [[nodiscard]] inline const_reverse_iterator rbegin() const noexcept
  268. {
  269. return const_reverse_iterator(end());
  270. }
  271. // [[nodiscard]] inline reverse_iterator rend() noexcept
  272. // {
  273. // return reverse_iterator(begin());
  274. // }
  275. [[nodiscard]] inline const_reverse_iterator rend() const noexcept
  276. {
  277. return const_reverse_iterator(begin());
  278. }
  279. bool operator ==(const BattleHexArray & other) const noexcept
  280. {
  281. if(internalStorage != other.internalStorage || presenceFlags != other.presenceFlags)
  282. return false;
  283. return true;
  284. }
  285. private:
  286. StorageType internalStorage;
  287. std::bitset<totalSize> presenceFlags;
  288. static const ArrayOfBattleHexArrays neighbouringTiles;
  289. static const ArrayOfBattleHexArrays allNeighbouringTiles;
  290. static const std::map<BattleSide, ArrayOfBattleHexArrays> neighbouringTilesDoubleWide;
  291. static ArrayOfBattleHexArrays precalculateNeighbouringTiles();
  292. static ArrayOfBattleHexArrays precalculateAllNeighbouringTiles();
  293. static ArrayOfBattleHexArrays precalculateNeighbouringTilesDoubleWide(BattleSide side);
  294. };
  295. VCMI_LIB_NAMESPACE_END