BattleHex.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. //TODO: change to enum class
  12. namespace BattleSide
  13. {
  14. enum
  15. {
  16. ATTACKER = 0,
  17. DEFENDER = 1
  18. };
  19. }
  20. namespace GameConstants
  21. {
  22. const int BFIELD_WIDTH = 17;
  23. const int BFIELD_HEIGHT = 11;
  24. const int BFIELD_SIZE = BFIELD_WIDTH * BFIELD_HEIGHT;
  25. }
  26. typedef boost::optional<ui8> BattleSideOpt;
  27. // for battle stacks' positions
  28. struct DLL_LINKAGE BattleHex //TODO: decide if this should be changed to class for better code design
  29. {
  30. si16 hex;
  31. static const si16 INVALID = -1;
  32. enum EDir
  33. {
  34. TOP_LEFT,
  35. TOP_RIGHT,
  36. RIGHT,
  37. BOTTOM_RIGHT,
  38. BOTTOM_LEFT,
  39. LEFT,
  40. NONE
  41. };
  42. BattleHex();
  43. BattleHex(si16 _hex);
  44. BattleHex(si16 x, si16 y);
  45. BattleHex(std::pair<si16, si16> xy);
  46. operator si16() const;
  47. bool isValid() const;
  48. bool isAvailable() const; //valid position not in first or last column
  49. void setX(si16 x);
  50. void setY(si16 y);
  51. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  52. void setXY(std::pair<si16, si16> xy);
  53. si16 getX() const;
  54. si16 getY() const;
  55. std::pair<si16, si16> getXY() const;
  56. BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
  57. BattleHex& operator+=(EDir dir);
  58. BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
  59. BattleHex operator+(EDir dir) const;
  60. std::vector<BattleHex> neighbouringTiles() const;
  61. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  62. static char getDistance(BattleHex hex1, BattleHex hex2);
  63. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  64. static BattleHex getClosestTile(ui8 side, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  65. template <typename Handler>
  66. void serialize(Handler &h, const int version)
  67. {
  68. h & hex;
  69. }
  70. using NeighbouringTiles = std::array<BattleHex, 6>;
  71. using NeighbouringTilesCache = std::vector<NeighbouringTiles>;
  72. static const NeighbouringTilesCache neighbouringTilesCache;
  73. };
  74. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);