BattleHex.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * BattleHex.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 "BattleSide.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. //TODO: change to enum class
  14. namespace GameConstants
  15. {
  16. const int BFIELD_WIDTH = 17;
  17. const int BFIELD_HEIGHT = 11;
  18. const int BFIELD_SIZE = BFIELD_WIDTH * BFIELD_HEIGHT;
  19. }
  20. class BattleHexArray;
  21. /**
  22. * @brief Represents a battlefield hexagonal tile.
  23. *
  24. * Valid hexes are within the range 0 to 186, excluding some invalid values, ex. castle towers (-2, -3, -4).
  25. * Available hexes are those valid ones but NOT in the first or last column.
  26. */
  27. class DLL_LINKAGE BattleHex
  28. {
  29. public:
  30. // helpers for siege
  31. static constexpr si16 CASTLE_CENTRAL_TOWER = -2;
  32. static constexpr si16 CASTLE_BOTTOM_TOWER = -3;
  33. static constexpr si16 CASTLE_UPPER_TOWER = -4;
  34. // hexes for interaction with heroes
  35. static constexpr si16 HERO_ATTACKER = 0;
  36. static constexpr si16 HERO_DEFENDER = GameConstants::BFIELD_WIDTH - 1;
  37. // helpers for rendering
  38. static constexpr si16 HEX_BEFORE_ALL = std::numeric_limits<si16>::min();
  39. static constexpr si16 HEX_AFTER_ALL = std::numeric_limits<si16>::max();
  40. static constexpr si16 DESTRUCTIBLE_WALL_1 = 29;
  41. static constexpr si16 DESTRUCTIBLE_WALL_2 = 78;
  42. static constexpr si16 DESTRUCTIBLE_WALL_3 = 130;
  43. static constexpr si16 DESTRUCTIBLE_WALL_4 = 182;
  44. static constexpr si16 GATE_BRIDGE = 94;
  45. static constexpr si16 GATE_OUTER = 95;
  46. static constexpr si16 GATE_INNER = 96;
  47. static constexpr si16 INVALID = -1;
  48. enum EDir
  49. {
  50. NONE = -1,
  51. TOP_LEFT,
  52. TOP_RIGHT,
  53. RIGHT,
  54. BOTTOM_RIGHT,
  55. BOTTOM_LEFT,
  56. LEFT,
  57. //Note: unused by BattleHex class, used by other code
  58. TOP,
  59. BOTTOM
  60. };
  61. BattleHex() noexcept
  62. : hex(INVALID)
  63. {}
  64. BattleHex(si16 _hex) noexcept
  65. : hex(_hex)
  66. {}
  67. BattleHex(si16 x, si16 y)
  68. {
  69. setXY(x, y);
  70. }
  71. BattleHex(std::pair<si16, si16> xy)
  72. {
  73. setXY(xy);
  74. }
  75. [[nodiscard]] bool isValid() const noexcept
  76. {
  77. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  78. }
  79. [[nodiscard]] bool isAvailable() const noexcept //valid position not in first or last column
  80. {
  81. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH - 1;
  82. }
  83. [[nodiscard]] inline bool isTower() const noexcept
  84. {
  85. return hex == BattleHex::CASTLE_CENTRAL_TOWER || hex == BattleHex::CASTLE_UPPER_TOWER || hex == BattleHex::CASTLE_BOTTOM_TOWER;
  86. }
  87. void setX(si16 x)
  88. {
  89. setXY(x, getY());
  90. }
  91. void setY(si16 y)
  92. {
  93. setXY(getX(), y);
  94. }
  95. void setXY(si16 x, si16 y, bool hasToBeValid = true)
  96. {
  97. if(hasToBeValid)
  98. {
  99. if(x < 0 || x >= GameConstants::BFIELD_WIDTH || y < 0 || y >= GameConstants::BFIELD_HEIGHT)
  100. throw std::out_of_range("Hex at (" + std::to_string(x) + ", " + std::to_string(y) + ") is not valid!");
  101. }
  102. hex = x + y * GameConstants::BFIELD_WIDTH;
  103. }
  104. void setXY(std::pair<si16, si16> xy)
  105. {
  106. setXY(xy.first, xy.second);
  107. }
  108. [[nodiscard]] si16 getX() const noexcept
  109. {
  110. return hex % GameConstants::BFIELD_WIDTH;
  111. }
  112. [[nodiscard]] si16 getY() const noexcept
  113. {
  114. return hex / GameConstants::BFIELD_WIDTH;
  115. }
  116. [[nodiscard]] std::pair<si16, si16> getXY() const noexcept
  117. {
  118. return std::make_pair(getX(), getY());
  119. }
  120. BattleHex & moveInDirection(EDir dir, bool hasToBeValid = true)
  121. {
  122. si16 x = getX();
  123. si16 y = getY();
  124. switch(dir)
  125. {
  126. case TOP_LEFT:
  127. setXY((y % 2) ? x - 1 : x, y - 1, hasToBeValid);
  128. break;
  129. case TOP_RIGHT:
  130. setXY((y % 2) ? x : x + 1, y - 1, hasToBeValid);
  131. break;
  132. case RIGHT:
  133. setXY(x + 1, y, hasToBeValid);
  134. break;
  135. case BOTTOM_RIGHT:
  136. setXY((y % 2) ? x : x + 1, y + 1, hasToBeValid);
  137. break;
  138. case BOTTOM_LEFT:
  139. setXY((y % 2) ? x - 1 : x, y + 1, hasToBeValid);
  140. break;
  141. case LEFT:
  142. setXY(x - 1, y, hasToBeValid);
  143. break;
  144. case NONE:
  145. break;
  146. default:
  147. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  148. break;
  149. }
  150. return *this;
  151. }
  152. [[nodiscard]] BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const
  153. {
  154. BattleHex result(hex);
  155. result.moveInDirection(dir, hasToBeValid);
  156. return result;
  157. }
  158. [[nodiscard]] static uint8_t getDistance(const BattleHex & hex1, const BattleHex & hex2) noexcept
  159. {
  160. int y1 = hex1.getY();
  161. int y2 = hex2.getY();
  162. int x1 = hex1.getX() + y1 / 2;
  163. int x2 = hex2.getX() + y2 / 2;
  164. int xDst = x2 - x1;
  165. int yDst = y2 - y1;
  166. if((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  167. return std::max(std::abs(xDst), std::abs(yDst));
  168. return std::abs(xDst) + std::abs(yDst);
  169. }
  170. [[nodiscard]] static BattleHex getClosestTile(BattleSide side, const BattleHex & initialPos, const BattleHexArray & hexes);
  171. //Constexpr defined array with all directions used in battle
  172. [[nodiscard]] static constexpr auto hexagonalDirections() noexcept
  173. {
  174. return std::array<EDir,6>{TOP_LEFT, TOP_RIGHT, RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT};
  175. }
  176. [[nodiscard]] static EDir mutualPosition(const BattleHex & hex1, const BattleHex & hex2)
  177. {
  178. for(auto dir : hexagonalDirections())
  179. {
  180. try
  181. {
  182. if(hex2 == hex1.cloneInDirection(dir, true))
  183. return dir;
  184. }
  185. catch (const std::out_of_range &)
  186. {
  187. continue;
  188. }
  189. }
  190. return NONE;
  191. }
  192. /// get (precomputed) all possible surrounding tiles
  193. [[nodiscard]] const BattleHexArray & getAllNeighbouringTiles() const noexcept;
  194. /// get (precomputed) only valid and available surrounding tiles
  195. [[nodiscard]] const BattleHexArray & getNeighbouringTiles() const noexcept;
  196. /// get (precomputed) only valid and available surrounding tiles for double wide creatures
  197. [[nodiscard]] const BattleHexArray & getNeighbouringTilesDoubleWide(BattleSide side) const noexcept;
  198. /// get integer hex value
  199. [[nodiscard]] si16 toInt() const noexcept
  200. {
  201. return hex;
  202. }
  203. BattleHex & operator+=(EDir dir)
  204. {
  205. return moveInDirection(dir);
  206. }
  207. [[nodiscard]] BattleHex operator+(EDir dir) const
  208. {
  209. return cloneInDirection(dir);
  210. }
  211. // Prefix increment
  212. BattleHex & operator++() noexcept
  213. {
  214. ++hex;
  215. return *this;
  216. }
  217. // Postfix increment is deleted; use prefix increment as it has better performance
  218. BattleHex operator++(int) = delete;
  219. [[nodiscard]] bool operator ==(const BattleHex & other) const noexcept
  220. {
  221. return hex == other.hex;
  222. }
  223. [[nodiscard]] bool operator !=(const BattleHex & other) const noexcept
  224. {
  225. return hex != other.hex;
  226. }
  227. [[nodiscard]] bool operator <(const BattleHex & other) const noexcept
  228. {
  229. return hex < other.hex;
  230. }
  231. [[nodiscard]] bool operator <=(const BattleHex & other) const noexcept
  232. {
  233. return hex <= other.hex;
  234. }
  235. template <typename Handler>
  236. void serialize(Handler & h)
  237. {
  238. h & hex;
  239. }
  240. private:
  241. si16 hex;
  242. };
  243. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  244. VCMI_LIB_NAMESPACE_END