BattleHex.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. bool BattleHex::isValid() const
  28. {
  29. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  30. }
  31. bool BattleHex::isAvailable() const
  32. {
  33. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  34. }
  35. void BattleHex::setX(si16 x)
  36. {
  37. setXY(x, getY());
  38. }
  39. void BattleHex::setY(si16 y)
  40. {
  41. setXY(getX(), y);
  42. }
  43. void BattleHex::setXY(si16 x, si16 y, bool hasToBeValid)
  44. {
  45. if(hasToBeValid)
  46. {
  47. if(x < 0 || x >= GameConstants::BFIELD_WIDTH || y < 0 || y >= GameConstants::BFIELD_HEIGHT)
  48. throw std::runtime_error("Valid hex required");
  49. }
  50. hex = x + y * GameConstants::BFIELD_WIDTH;
  51. }
  52. void BattleHex::setXY(std::pair<si16, si16> xy)
  53. {
  54. setXY(xy.first, xy.second);
  55. }
  56. si16 BattleHex::getX() const
  57. {
  58. return hex % GameConstants::BFIELD_WIDTH;
  59. }
  60. si16 BattleHex::getY() const
  61. {
  62. return hex / GameConstants::BFIELD_WIDTH;
  63. }
  64. std::pair<si16, si16> BattleHex::getXY() const
  65. {
  66. return std::make_pair(getX(), getY());
  67. }
  68. BattleHex& BattleHex::moveInDirection(EDir dir, bool hasToBeValid)
  69. {
  70. si16 x = getX();
  71. si16 y = getY();
  72. switch(dir)
  73. {
  74. case TOP_LEFT:
  75. setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
  76. break;
  77. case TOP_RIGHT:
  78. setXY((y%2) ? x : x+1, y-1, hasToBeValid);
  79. break;
  80. case RIGHT:
  81. setXY(x+1, y, hasToBeValid);
  82. break;
  83. case BOTTOM_RIGHT:
  84. setXY((y%2) ? x : x+1, y+1, hasToBeValid);
  85. break;
  86. case BOTTOM_LEFT:
  87. setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
  88. break;
  89. case LEFT:
  90. setXY(x-1, y, hasToBeValid);
  91. break;
  92. case NONE:
  93. break;
  94. default:
  95. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  96. break;
  97. }
  98. return *this;
  99. }
  100. BattleHex &BattleHex::operator+=(BattleHex::EDir dir)
  101. {
  102. return moveInDirection(dir);
  103. }
  104. BattleHex BattleHex::cloneInDirection(BattleHex::EDir dir, bool hasToBeValid) const
  105. {
  106. BattleHex result(hex);
  107. result.moveInDirection(dir, hasToBeValid);
  108. return result;
  109. }
  110. BattleHex BattleHex::operator+(BattleHex::EDir dir) const
  111. {
  112. return cloneInDirection(dir);
  113. }
  114. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  115. {
  116. std::vector<BattleHex> ret;
  117. ret.reserve(6);
  118. for(auto dir : hexagonalDirections())
  119. checkAndPush(cloneInDirection(dir, false), ret);
  120. return ret;
  121. }
  122. std::vector<BattleHex> BattleHex::allNeighbouringTiles() const
  123. {
  124. std::vector<BattleHex> ret;
  125. ret.resize(6);
  126. for(auto dir : hexagonalDirections())
  127. ret[dir] = cloneInDirection(dir, false);
  128. return ret;
  129. }
  130. BattleHex::EDir BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  131. {
  132. for(auto dir : hexagonalDirections())
  133. if(hex2 == hex1.cloneInDirection(dir, false))
  134. return dir;
  135. return NONE;
  136. }
  137. uint8_t BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  138. {
  139. int y1 = hex1.getY();
  140. int y2 = hex2.getY();
  141. // FIXME: why there was * 0.5 instead of / 2?
  142. int x1 = static_cast<int>(hex1.getX() + y1 / 2);
  143. int x2 = static_cast<int>(hex2.getX() + y2 / 2);
  144. int xDst = x2 - x1;
  145. int yDst = y2 - y1;
  146. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  147. return std::max(std::abs(xDst), std::abs(yDst));
  148. return std::abs(xDst) + std::abs(yDst);
  149. }
  150. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  151. {
  152. if(tile.isAvailable())
  153. ret.push_back(tile);
  154. }
  155. BattleHex BattleHex::getClosestTile(BattleSide side, BattleHex initialPos, std::set<BattleHex> & possibilities)
  156. {
  157. std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  158. BattleHex initialHex = BattleHex(initialPos);
  159. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  160. {
  161. return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
  162. };
  163. boost::sort (sortedTiles, compareDistance); //closest tiles at front
  164. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  165. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  166. {
  167. return closestDistance < here.getDistance (initialPos, here);
  168. };
  169. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  170. auto compareHorizontal = [side, initialPos](const BattleHex left, const BattleHex right) -> bool
  171. {
  172. if(left.getX() != right.getX())
  173. {
  174. if(side == BattleSide::ATTACKER)
  175. return left.getX() > right.getX(); //find furthest right
  176. else
  177. return left.getX() < right.getX(); //find furthest left
  178. }
  179. else
  180. {
  181. //Prefer tiles in the same row.
  182. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  183. }
  184. };
  185. boost::sort (sortedTiles, compareHorizontal);
  186. return sortedTiles.front();
  187. }
  188. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  189. {
  190. return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
  191. }
  192. static BattleHex::NeighbouringTilesCache calculateNeighbouringTiles()
  193. {
  194. BattleHex::NeighbouringTilesCache ret;
  195. ret.resize(GameConstants::BFIELD_SIZE);
  196. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  197. {
  198. auto hexes = BattleHex(hex).neighbouringTiles();
  199. size_t index = 0;
  200. for(auto neighbour : hexes)
  201. ret[hex].at(index++) = neighbour;
  202. }
  203. return ret;
  204. }
  205. const BattleHex::NeighbouringTilesCache BattleHex::neighbouringTilesCache = calculateNeighbouringTiles();
  206. VCMI_LIB_NAMESPACE_END