BattleHex.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 <bits/stl_pair.h>
  12. #include "GameConstants.h"
  13. // for battle stacks' positions
  14. class DLL_LINKAGE BattleHex
  15. {
  16. public:
  17. si16 hex;
  18. static const si16 INVALID = -1;
  19. enum EDir { RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT };
  20. BattleHex();
  21. BattleHex(si16 _hex);
  22. BattleHex(si16 x, si16 y);
  23. BattleHex(std::pair<si16, si16> xy);
  24. operator si16() const;
  25. bool isValid() const;
  26. bool isAvailable() const; //valid position not in first or last column
  27. void setX(si16 x);
  28. void setY(si16 y);
  29. void setXY(si16 x, si16 y, bool hasToBeValid = true);
  30. void setXY(std::pair<si16, si16> xy);
  31. si16 getX() const;
  32. si16 getY() const;
  33. std::pair<si16, si16> getXY() const;
  34. //moving to direction
  35. BattleHex& moveInDir(EDir dir, bool hasToBeValid = true);
  36. BattleHex& operator+=(EDir dir); //sugar for above
  37. //generates new BattleHex moved by given dir
  38. BattleHex movedInDir(EDir dir, bool hasToBeValid = true) const;
  39. BattleHex operator+(EDir dir) const;
  40. std::vector<BattleHex> neighbouringTiles() const;
  41. //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
  42. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  43. //returns distance between given hexes
  44. static char getDistance(BattleHex hex1, BattleHex hex2);
  45. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  46. static BattleHex getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  47. template <typename Handler>
  48. void serialize(Handler &h, const int version)
  49. {
  50. h & hex;
  51. }
  52. };
  53. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);