BattleHex.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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
  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. si16 hex;
  32. static const si16 INVALID = -1;
  33. enum EDir
  34. {
  35. TOP_LEFT,
  36. TOP_RIGHT,
  37. RIGHT,
  38. BOTTOM_RIGHT,
  39. BOTTOM_LEFT,
  40. LEFT,
  41. NONE
  42. };
  43. BattleHex();
  44. BattleHex(si16 _hex);
  45. BattleHex(si16 x, si16 y);
  46. BattleHex(std::pair<si16, si16> xy);
  47. operator si16() const;
  48. bool isValid() const;
  49. bool isAvailable() const; //valid position not in first or last column
  50. void setX(si16 x);
  51. void setY(si16 y);
  52. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  53. void setXY(std::pair<si16, si16> xy);
  54. si16 getX() const;
  55. si16 getY() const;
  56. std::pair<si16, si16> getXY() const;
  57. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  58. BattleHex& operator+=(EDir dir);
  59. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  60. BattleHex operator+(EDir dir) const;
  61. std::vector<BattleHex> neighbouringTiles() const;
  62. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  63. static char getDistance(BattleHex hex1, BattleHex hex2);
  64. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  65. static BattleHex getClosestTile(ui8 side, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  66. template <typename Handler>
  67. void serialize(Handler &h, const int version)
  68. {
  69. h & hex;
  70. }
  71. using NeighbouringTiles = std::array<BattleHex, 6>;
  72. using NeighbouringTilesCache = std::vector<NeighbouringTiles>;
  73. static const NeighbouringTilesCache neighbouringTilesCache;
  74. };
  75. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);
  76. VCMI_LIB_NAMESPACE_END