BattleHex.h 6.1 KB

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