BattleHex.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "GameConstants.h"
  3. /*
  4. * BattleHex.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. // for battle stacks' positions
  13. struct DLL_LINKAGE BattleHex
  14. {
  15. static const si16 INVALID = -1;
  16. enum EDir { RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT };
  17. si16 hex;
  18. BattleHex() : hex(INVALID) {}
  19. BattleHex(si16 _hex) : hex(_hex) {}
  20. operator si16() const { return hex; }
  21. bool isValid() const { return hex >= 0 && hex < GameConstants::BFIELD_SIZE; }
  22. template<typename inttype>
  23. BattleHex(inttype x, inttype y)
  24. {
  25. setXY(x, y);
  26. }
  27. template<typename inttype>
  28. BattleHex(std::pair<inttype, inttype> xy)
  29. {
  30. setXY(xy);
  31. }
  32. template<typename inttype>
  33. void setX(inttype x)
  34. {
  35. setXY(x, getY());
  36. }
  37. template<typename inttype>
  38. void setY(inttype y)
  39. {
  40. setXY(getX(), y);
  41. }
  42. void setXY(si16 x, si16 y, bool hasToBeValid = true)
  43. {
  44. if(hasToBeValid)
  45. assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
  46. hex = x + y * GameConstants::BFIELD_WIDTH;
  47. }
  48. template<typename inttype>
  49. void setXY(std::pair<inttype, inttype> xy)
  50. {
  51. setXY(xy.first, xy.second);
  52. }
  53. si16 getY() const { return hex / GameConstants::BFIELD_WIDTH; }
  54. si16 getX() const { return hex % GameConstants::BFIELD_WIDTH; }
  55. std::pair<si16, si16> getXY() const { return std::make_pair(getX(), getY()); }
  56. //moving to direction
  57. BattleHex& moveInDir(EDir dir, bool hasToBeValid = true);
  58. BattleHex& operator+=(EDir dir) { return moveInDir(dir); } //sugar for above
  59. //generates new BattleHex moved by given dir
  60. BattleHex movedInDir(EDir dir, bool hasToBeValid = true) const
  61. {
  62. BattleHex result(*this);
  63. result.moveInDir(dir, hasToBeValid);
  64. return result;
  65. }
  66. BattleHex operator+(EDir dir) const { return movedInDir(dir); }
  67. std::vector<BattleHex> neighbouringTiles() const;
  68. //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)
  69. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  70. //returns distance between given hexes
  71. static char getDistance(BattleHex hex1, BattleHex hex2);
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & hex;
  75. }
  76. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  77. bool isAvailable() const; //valid position not in first or last column
  78. static BattleHex getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  79. };
  80. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);