BattleHex.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // for battle stacks' positions
  21. struct DLL_LINKAGE BattleHex //TODO: decide if this should be changed to class for better code design
  22. {
  23. // helpers for siege
  24. static constexpr si16 CASTLE_CENTRAL_TOWER = -2;
  25. static constexpr si16 CASTLE_BOTTOM_TOWER = -3;
  26. static constexpr si16 CASTLE_UPPER_TOWER = -4;
  27. // hexes for interaction with heroes
  28. static constexpr si16 HERO_ATTACKER = 0;
  29. static constexpr si16 HERO_DEFENDER = GameConstants::BFIELD_WIDTH - 1;
  30. // helpers for rendering
  31. static constexpr si16 HEX_BEFORE_ALL = std::numeric_limits<si16>::min();
  32. static constexpr si16 HEX_AFTER_ALL = std::numeric_limits<si16>::max();
  33. static constexpr si16 DESTRUCTIBLE_WALL_1 = 29;
  34. static constexpr si16 DESTRUCTIBLE_WALL_2 = 78;
  35. static constexpr si16 DESTRUCTIBLE_WALL_3 = 130;
  36. static constexpr si16 DESTRUCTIBLE_WALL_4 = 182;
  37. static constexpr si16 GATE_BRIDGE = 94;
  38. static constexpr si16 GATE_OUTER = 95;
  39. static constexpr si16 GATE_INNER = 96;
  40. si16 hex;
  41. static constexpr si16 INVALID = -1;
  42. enum EDir
  43. {
  44. NONE = -1,
  45. TOP_LEFT,
  46. TOP_RIGHT,
  47. RIGHT,
  48. BOTTOM_RIGHT,
  49. BOTTOM_LEFT,
  50. LEFT,
  51. //Note: unused by BattleHex class, used by other code
  52. TOP,
  53. BOTTOM
  54. };
  55. BattleHex();
  56. BattleHex(si16 _hex);
  57. BattleHex(si16 x, si16 y);
  58. BattleHex(std::pair<si16, si16> xy);
  59. operator si16() const;
  60. inline bool isValid() const
  61. {
  62. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  63. }
  64. bool isAvailable() const //valid position not in first or last column
  65. {
  66. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH - 1;
  67. }
  68. void setX(si16 x);
  69. void setY(si16 y);
  70. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  71. void setXY(std::pair<si16, si16> xy);
  72. si16 getX() const;
  73. si16 getY() const;
  74. std::pair<si16, si16> getXY() const;
  75. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  76. BattleHex& operator+=(EDir dir);
  77. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  78. BattleHex operator+(EDir dir) const;
  79. static EDir mutualPosition(BattleHex hex1, BattleHex hex2);
  80. static uint8_t getDistance(BattleHex hex1, BattleHex hex2)
  81. {
  82. int y1 = hex1.getY();
  83. int y2 = hex2.getY();
  84. int x1 = hex1.getX() + y1 / 2;
  85. int x2 = hex2.getX() + y2 / 2;
  86. int xDst = x2 - x1;
  87. int yDst = y2 - y1;
  88. if((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  89. return std::max(std::abs(xDst), std::abs(yDst));
  90. return std::abs(xDst) + std::abs(yDst);
  91. }
  92. template <typename Handler>
  93. void serialize(Handler &h)
  94. {
  95. h & hex;
  96. }
  97. //Constexpr defined array with all directions used in battle
  98. static constexpr auto hexagonalDirections() {
  99. return std::array<EDir,6>{BattleHex::TOP_LEFT, BattleHex::TOP_RIGHT, BattleHex::RIGHT, BattleHex::BOTTOM_RIGHT, BattleHex::BOTTOM_LEFT, BattleHex::LEFT};
  100. }
  101. };
  102. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  103. VCMI_LIB_NAMESPACE_END