BattleHex.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * BattleHex.cpp, 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. #include "StdInc.h"
  11. #include "BattleHex.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. BattleHex::BattleHex() : hex(INVALID) {}
  14. BattleHex::BattleHex(si16 _hex) : hex(_hex) {}
  15. BattleHex::BattleHex(si16 x, si16 y)
  16. {
  17. setXY(x, y);
  18. }
  19. BattleHex::BattleHex(std::pair<si16, si16> xy)
  20. {
  21. setXY(xy);
  22. }
  23. BattleHex::operator si16() const
  24. {
  25. return hex;
  26. }
  27. void BattleHex::setX(si16 x)
  28. {
  29. setXY(x, getY());
  30. }
  31. void BattleHex::setY(si16 y)
  32. {
  33. setXY(getX(), y);
  34. }
  35. void BattleHex::setXY(si16 x, si16 y, bool hasToBeValid)
  36. {
  37. if(hasToBeValid)
  38. {
  39. if(x < 0 || x >= GameConstants::BFIELD_WIDTH || y < 0 || y >= GameConstants::BFIELD_HEIGHT)
  40. throw std::runtime_error("Valid hex required");
  41. }
  42. hex = x + y * GameConstants::BFIELD_WIDTH;
  43. }
  44. void BattleHex::setXY(std::pair<si16, si16> xy)
  45. {
  46. setXY(xy.first, xy.second);
  47. }
  48. si16 BattleHex::getX() const
  49. {
  50. return hex % GameConstants::BFIELD_WIDTH;
  51. }
  52. si16 BattleHex::getY() const
  53. {
  54. return hex / GameConstants::BFIELD_WIDTH;
  55. }
  56. std::pair<si16, si16> BattleHex::getXY() const
  57. {
  58. return std::make_pair(getX(), getY());
  59. }
  60. BattleHex & BattleHex::moveInDirection(EDir dir, bool hasToBeValid)
  61. {
  62. si16 x = getX();
  63. si16 y = getY();
  64. switch(dir)
  65. {
  66. case TOP_LEFT:
  67. setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
  68. break;
  69. case TOP_RIGHT:
  70. setXY((y%2) ? x : x+1, y-1, hasToBeValid);
  71. break;
  72. case RIGHT:
  73. setXY(x+1, y, hasToBeValid);
  74. break;
  75. case BOTTOM_RIGHT:
  76. setXY((y%2) ? x : x+1, y+1, hasToBeValid);
  77. break;
  78. case BOTTOM_LEFT:
  79. setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
  80. break;
  81. case LEFT:
  82. setXY(x-1, y, hasToBeValid);
  83. break;
  84. case NONE:
  85. break;
  86. default:
  87. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  88. break;
  89. }
  90. return *this;
  91. }
  92. BattleHex & BattleHex::operator+=(BattleHex::EDir dir)
  93. {
  94. return moveInDirection(dir);
  95. }
  96. BattleHex BattleHex::cloneInDirection(BattleHex::EDir dir, bool hasToBeValid) const
  97. {
  98. BattleHex result(hex);
  99. result.moveInDirection(dir, hasToBeValid);
  100. return result;
  101. }
  102. BattleHex BattleHex::operator+(BattleHex::EDir dir) const
  103. {
  104. return cloneInDirection(dir);
  105. }
  106. BattleHex::EDir BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  107. {
  108. for(auto dir : hexagonalDirections())
  109. if(hex2 == hex1.cloneInDirection(dir, false))
  110. return dir;
  111. return NONE;
  112. }
  113. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  114. {
  115. return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
  116. }
  117. VCMI_LIB_NAMESPACE_END