BattleHex.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. bool isValid() const;
  61. bool isAvailable() const; //valid position not in first or last column
  62. void setX(si16 x);
  63. void setY(si16 y);
  64. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  65. void setXY(std::pair<si16, si16> xy);
  66. si16 getX() const;
  67. si16 getY() const;
  68. std::pair<si16, si16> getXY() const;
  69. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  70. BattleHex& operator+=(EDir dir);
  71. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  72. BattleHex operator+(EDir dir) const;
  73. /// returns all valid neighbouring tiles
  74. std::vector<BattleHex> neighbouringTiles() const;
  75. /// returns all tiles, unavailable tiles will be set as invalid
  76. /// order of returned tiles matches EDir enim
  77. std::vector<BattleHex> allNeighbouringTiles() const;
  78. static EDir mutualPosition(BattleHex hex1, BattleHex hex2);
  79. static uint8_t getDistance(BattleHex hex1, BattleHex hex2);
  80. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  81. static BattleHex getClosestTile(BattleSide side, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  82. template <typename Handler>
  83. void serialize(Handler &h)
  84. {
  85. h & hex;
  86. }
  87. using NeighbouringTiles = std::array<BattleHex, 6>;
  88. using NeighbouringTilesCache = std::vector<NeighbouringTiles>;
  89. static const NeighbouringTilesCache neighbouringTilesCache;
  90. private:
  91. //Constexpr defined array with all directions used in battle
  92. static constexpr auto hexagonalDirections() {
  93. return std::array<EDir,6>{BattleHex::TOP_LEFT, BattleHex::TOP_RIGHT, BattleHex::RIGHT, BattleHex::BOTTOM_RIGHT, BattleHex::BOTTOM_LEFT, BattleHex::LEFT};
  94. }
  95. };
  96. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  97. VCMI_LIB_NAMESPACE_END