BattleHex.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. VCMI_LIB_NAMESPACE_BEGIN
  12. //TODO: change to enum class
  13. namespace BattleSide
  14. {
  15. enum Type
  16. {
  17. ATTACKER = 0,
  18. DEFENDER = 1
  19. };
  20. }
  21. namespace GameConstants
  22. {
  23. const int BFIELD_WIDTH = 17;
  24. const int BFIELD_HEIGHT = 11;
  25. const int BFIELD_SIZE = BFIELD_WIDTH * BFIELD_HEIGHT;
  26. }
  27. using BattleSideOpt = std::optional<ui8>;
  28. // for battle stacks' positions
  29. struct DLL_LINKAGE BattleHex //TODO: decide if this should be changed to class for better code design
  30. {
  31. // helpers for siege
  32. static constexpr si16 CASTLE_CENTRAL_TOWER = -2;
  33. static constexpr si16 CASTLE_BOTTOM_TOWER = -3;
  34. static constexpr si16 CASTLE_UPPER_TOWER = -4;
  35. // hexes for interaction with heroes
  36. static constexpr si16 HERO_ATTACKER = 0;
  37. static constexpr si16 HERO_DEFENDER = GameConstants::BFIELD_WIDTH - 1;
  38. // helpers for rendering
  39. static constexpr si16 HEX_BEFORE_ALL = std::numeric_limits<si16>::min();
  40. static constexpr si16 HEX_AFTER_ALL = std::numeric_limits<si16>::max();
  41. si16 hex;
  42. static constexpr si16 INVALID = -1;
  43. enum EDir
  44. {
  45. NONE = -1,
  46. TOP_LEFT,
  47. TOP_RIGHT,
  48. RIGHT,
  49. BOTTOM_RIGHT,
  50. BOTTOM_LEFT,
  51. LEFT,
  52. //Note: unused by BattleHex class, used by other code
  53. TOP,
  54. BOTTOM
  55. };
  56. BattleHex();
  57. BattleHex(si16 _hex);
  58. BattleHex(si16 x, si16 y);
  59. BattleHex(std::pair<si16, si16> xy);
  60. operator si16() const;
  61. bool isValid() const;
  62. bool isAvailable() const; //valid position not in first or last column
  63. void setX(si16 x);
  64. void setY(si16 y);
  65. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  66. void setXY(std::pair<si16, si16> xy);
  67. si16 getX() const;
  68. si16 getY() const;
  69. std::pair<si16, si16> getXY() const;
  70. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  71. BattleHex& operator+=(EDir dir);
  72. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  73. BattleHex operator+(EDir dir) const;
  74. /// returns all valid neighbouring tiles
  75. std::vector<BattleHex> neighbouringTiles() const;
  76. /// returns all tiles, unavailable tiles will be set as invalid
  77. /// order of returned tiles matches EDir enim
  78. std::vector<BattleHex> allNeighbouringTiles() const;
  79. static EDir mutualPosition(BattleHex hex1, BattleHex hex2);
  80. static uint8_t getDistance(BattleHex hex1, BattleHex hex2);
  81. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  82. static BattleHex getClosestTile(ui8 side, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  83. template <typename Handler>
  84. void serialize(Handler &h, const int version)
  85. {
  86. h & hex;
  87. }
  88. using NeighbouringTiles = std::array<BattleHex, 6>;
  89. using NeighbouringTilesCache = std::vector<NeighbouringTiles>;
  90. static const NeighbouringTilesCache neighbouringTilesCache;
  91. private:
  92. //Constexpr defined array with all directions used in battle
  93. static constexpr auto hexagonalDirections() {
  94. return std::array<EDir,6>{BattleHex::TOP_LEFT, BattleHex::TOP_RIGHT, BattleHex::RIGHT, BattleHex::BOTTOM_RIGHT, BattleHex::BOTTOM_LEFT, BattleHex::LEFT};
  95. }
  96. };
  97. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  98. VCMI_LIB_NAMESPACE_END