BattleHex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. enum ESiegeHexes : si16
  16. {
  17. GATE_BRIDGE = 94,
  18. GATE_OUTER = 95,
  19. GATE_INNER = 96
  20. };
  21. static const si16 INVALID = -1;
  22. enum EDir { RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT };
  23. si16 hex;
  24. BattleHex() : hex(INVALID) {}
  25. BattleHex(si16 _hex) : hex(_hex) {}
  26. operator si16() const { return hex; }
  27. bool isValid() const { return hex >= 0 && hex < GameConstants::BFIELD_SIZE; }
  28. template<typename inttype>
  29. BattleHex(inttype x, inttype y)
  30. {
  31. setXY(x, y);
  32. }
  33. template<typename inttype>
  34. BattleHex(std::pair<inttype, inttype> xy)
  35. {
  36. setXY(xy);
  37. }
  38. template<typename inttype>
  39. void setX(inttype x)
  40. {
  41. setXY(x, getY());
  42. }
  43. template<typename inttype>
  44. void setY(inttype y)
  45. {
  46. setXY(getX(), y);
  47. }
  48. void setXY(si16 x, si16 y, bool hasToBeValid = true)
  49. {
  50. if(hasToBeValid)
  51. assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
  52. hex = x + y * GameConstants::BFIELD_WIDTH;
  53. }
  54. template<typename inttype>
  55. void setXY(std::pair<inttype, inttype> xy)
  56. {
  57. setXY(xy.first, xy.second);
  58. }
  59. si16 getY() const { return hex / GameConstants::BFIELD_WIDTH; }
  60. si16 getX() const { return hex % GameConstants::BFIELD_WIDTH; }
  61. std::pair<si16, si16> getXY() const { return std::make_pair(getX(), getY()); }
  62. //moving to direction
  63. BattleHex& moveInDir(EDir dir, bool hasToBeValid = true);
  64. BattleHex& operator+=(EDir dir) { return moveInDir(dir); } //sugar for above
  65. //generates new BattleHex moved by given dir
  66. BattleHex movedInDir(EDir dir, bool hasToBeValid = true) const
  67. {
  68. BattleHex result(*this);
  69. result.moveInDir(dir, hasToBeValid);
  70. return result;
  71. }
  72. BattleHex operator+(EDir dir) const { return movedInDir(dir); }
  73. std::vector<BattleHex> neighbouringTiles() const;
  74. //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)
  75. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  76. //returns distance between given hexes
  77. static char getDistance(BattleHex hex1, BattleHex hex2);
  78. template <typename Handler> void serialize(Handler &h, const int version)
  79. {
  80. h & hex;
  81. }
  82. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  83. bool isAvailable() const; //valid position not in first or last column
  84. static BattleHex getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  85. };
  86. DLL_EXPORT std::ostream & operator<<(std::ostream & os, const BattleHex & hex);