BattleHex.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. BattleHex::BattleHex() : hex(INVALID) {}
  13. BattleHex::BattleHex(si16 _hex) : hex(_hex) {}
  14. BattleHex::BattleHex(si16 x, si16 y)
  15. {
  16. setXY(x, y);
  17. }
  18. BattleHex::BattleHex(std::pair<si16, si16> xy)
  19. {
  20. setXY(xy);
  21. }
  22. BattleHex::operator si16() const
  23. {
  24. return hex;
  25. }
  26. bool BattleHex::isValid() const
  27. {
  28. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  29. }
  30. bool BattleHex::isAvailable() const
  31. {
  32. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  33. }
  34. void BattleHex::setX(si16 x)
  35. {
  36. setXY(x, getY());
  37. }
  38. void BattleHex::setY(si16 y)
  39. {
  40. setXY(getX(), y);
  41. }
  42. void BattleHex::setXY(si16 x, si16 y, bool hasToBeValid)
  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. void BattleHex::setXY(std::pair<si16, si16> xy)
  49. {
  50. setXY(xy.first, xy.second);
  51. }
  52. si16 BattleHex::getX() const
  53. {
  54. return hex % GameConstants::BFIELD_WIDTH;
  55. }
  56. si16 BattleHex::getY() const
  57. {
  58. return hex / GameConstants::BFIELD_WIDTH;
  59. }
  60. std::pair<si16, si16> BattleHex::getXY() const
  61. {
  62. return std::make_pair(getX(), getY());
  63. }
  64. BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid)
  65. {
  66. si16 x(getX()), y(getY());
  67. switch(dir)
  68. {
  69. case TOP_LEFT:
  70. setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
  71. break;
  72. case TOP_RIGHT:
  73. setXY((y%2) ? x : x+1, y-1, hasToBeValid);
  74. break;
  75. case RIGHT:
  76. setXY(x+1, y, hasToBeValid);
  77. break;
  78. case BOTTOM_RIGHT:
  79. setXY((y%2) ? x : x+1, y+1, hasToBeValid);
  80. break;
  81. case BOTTOM_LEFT:
  82. setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
  83. break;
  84. case LEFT:
  85. setXY(x-1, y, hasToBeValid);
  86. break;
  87. default:
  88. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  89. break;
  90. }
  91. return *this;
  92. }
  93. BattleHex &BattleHex::operator+=(BattleHex::EDir dir)
  94. {
  95. return moveInDir(dir);
  96. }
  97. BattleHex BattleHex::movedInDir(BattleHex::EDir dir, bool hasToBeValid) const
  98. {
  99. BattleHex result(*this);
  100. result.moveInDir(dir, hasToBeValid);
  101. return result;
  102. }
  103. BattleHex BattleHex::operator+(BattleHex::EDir dir) const
  104. {
  105. return movedInDir(dir);
  106. }
  107. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  108. {
  109. std::vector<BattleHex> ret;
  110. const int WN = GameConstants::BFIELD_WIDTH;
  111. // H3 order : TR, R, BR, BL, L, TL (T = top, B = bottom ...)
  112. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret); // 1
  113. checkAndPush(hex + 1, ret); // 2
  114. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret); // 3
  115. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret); // 4
  116. checkAndPush(hex - 1, ret); // 5
  117. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret); // 6
  118. return ret;
  119. }
  120. signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  121. {
  122. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  123. return 0;
  124. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  125. return 1;
  126. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  127. return 2;
  128. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  129. return 3;
  130. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  131. return 4;
  132. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  133. return 5;
  134. return -1;
  135. }
  136. char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  137. {
  138. int y1 = hex1.getY(), y2 = hex2.getY();
  139. // FIXME: Omit floating point arithmetics
  140. int x1 = (hex1.getX() + y1 * 0.5), x2 = (hex2.getX() + y2 * 0.5);
  141. int xDst = x2 - x1, yDst = y2 - y1;
  142. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  143. return std::max(std::abs(xDst), std::abs(yDst));
  144. return std::abs(xDst) + std::abs(yDst);
  145. }
  146. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  147. {
  148. if(tile.isAvailable())
  149. ret.push_back(tile);
  150. }
  151. BattleHex BattleHex::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities)
  152. {
  153. std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  154. BattleHex initialHex = BattleHex(initialPos);
  155. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  156. {
  157. return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
  158. };
  159. boost::sort (sortedTiles, compareDistance); //closest tiles at front
  160. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  161. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  162. {
  163. return closestDistance < here.getDistance (initialPos, here);
  164. };
  165. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  166. auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
  167. {
  168. if(left.getX() != right.getX())
  169. {
  170. if (attackerOwned)
  171. return left.getX() > right.getX(); //find furthest right
  172. else
  173. return left.getX() < right.getX(); //find furthest left
  174. }
  175. else
  176. {
  177. //Prefer tiles in the same row.
  178. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  179. }
  180. };
  181. boost::sort (sortedTiles, compareHorizontal);
  182. return sortedTiles.front();
  183. }
  184. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  185. {
  186. return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
  187. }