BattleHex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. typedef boost::optional<ui8> BattleSideOpt;
  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 const si16 CASTLE_CENTRAL_TOWER = -2;
  33. static const si16 CASTLE_BOTTOM_TOWER = -3;
  34. static const si16 CASTLE_UPPER_TOWER = -4;
  35. // helpers for rendering
  36. static const si16 HEX_BEFORE_ALL = std::numeric_limits<si16>::min();
  37. static const si16 HEX_AFTER_ALL = std::numeric_limits<si16>::max();
  38. si16 hex;
  39. static const si16 INVALID = -1;
  40. enum EDir
  41. {
  42. NONE = -1,
  43. TOP_LEFT,
  44. TOP_RIGHT,
  45. RIGHT,
  46. BOTTOM_RIGHT,
  47. BOTTOM_LEFT,
  48. LEFT,
  49. //Note: unused by BattleHex class, used by other code
  50. TOP,
  51. BOTTOM
  52. };
  53. BattleHex();
  54. BattleHex(si16 _hex);
  55. BattleHex(si16 x, si16 y);
  56. BattleHex(std::pair<si16, si16> xy);
  57. operator si16() const;
  58. bool isValid() const;
  59. bool isAvailable() const; //valid position not in first or last column
  60. void setX(si16 x);
  61. void setY(si16 y);
  62. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  63. void setXY(std::pair<si16, si16> xy);
  64. si16 getX() const;
  65. si16 getY() const;
  66. std::pair<si16, si16> getXY() const;
  67. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  68. BattleHex& operator+=(EDir dir);
  69. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  70. BattleHex operator+(EDir dir) const;
  71. /// returns all valid neighbouring tiles
  72. std::vector<BattleHex> neighbouringTiles() const;
  73. /// returns all tiles, unavailable tiles will be set as invalid
  74. /// order of returned tiles matches EDir enim
  75. std::vector<BattleHex> allNeighbouringTiles() const;
  76. static EDir mutualPosition(BattleHex hex1, BattleHex hex2);
  77. static uint8_t getDistance(BattleHex hex1, BattleHex hex2);
  78. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  79. static BattleHex getClosestTile(ui8 side, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  80. template <typename Handler>
  81. void serialize(Handler &h, const int version)
  82. {
  83. h & hex;
  84. }
  85. using NeighbouringTiles = std::array<BattleHex, 6>;
  86. using NeighbouringTilesCache = std::vector<NeighbouringTiles>;
  87. static const NeighbouringTilesCache neighbouringTilesCache;
  88. };
  89. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  90. VCMI_LIB_NAMESPACE_END