BattleHex.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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()), y(getY());
  71. switch(dir)
  72. {
  73. case TOP_LEFT:
  74. setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
  75. break;
  76. case TOP_RIGHT:
  77. setXY((y%2) ? x : x+1, y-1, hasToBeValid);
  78. break;
  79. case RIGHT:
  80. setXY(x+1, y, hasToBeValid);
  81. break;
  82. case BOTTOM_RIGHT:
  83. setXY((y%2) ? x : x+1, y+1, hasToBeValid);
  84. break;
  85. case BOTTOM_LEFT:
  86. setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
  87. break;
  88. case LEFT:
  89. setXY(x-1, y, hasToBeValid);
  90. break;
  91. case NONE:
  92. break;
  93. default:
  94. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  95. break;
  96. }
  97. return *this;
  98. }
  99. BattleHex &BattleHex::operator+=(BattleHex::EDir dir)
  100. {
  101. return moveInDirection(dir);
  102. }
  103. BattleHex BattleHex::cloneInDirection(BattleHex::EDir dir, bool hasToBeValid) const
  104. {
  105. BattleHex result(hex);
  106. result.moveInDirection(dir, hasToBeValid);
  107. return result;
  108. }
  109. BattleHex BattleHex::operator+(BattleHex::EDir dir) const
  110. {
  111. return cloneInDirection(dir);
  112. }
  113. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  114. {
  115. std::vector<BattleHex> ret;
  116. ret.reserve(6);
  117. for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
  118. checkAndPush(cloneInDirection(dir, false), ret);
  119. return ret;
  120. }
  121. std::vector<BattleHex> BattleHex::allNeighbouringTiles() const
  122. {
  123. std::vector<BattleHex> ret;
  124. ret.resize(6);
  125. for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
  126. ret[dir] = cloneInDirection(dir, false);
  127. return ret;
  128. }
  129. BattleHex::EDir BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  130. {
  131. for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
  132. if(hex2 == hex1.cloneInDirection(dir,false))
  133. return dir;
  134. return NONE;
  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 = (int)(hex1.getX() + y1 * 0.5), x2 = (int)(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(ui8 side, 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 = [side, initialPos](const BattleHex left, const BattleHex right) -> bool
  167. {
  168. if(left.getX() != right.getX())
  169. {
  170. if(side == BattleSide::ATTACKER)
  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. }
  188. static BattleHex::NeighbouringTilesCache calculateNeighbouringTiles()
  189. {
  190. BattleHex::NeighbouringTilesCache ret;
  191. ret.resize(GameConstants::BFIELD_SIZE);
  192. for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  193. {
  194. auto hexes = BattleHex(hex).neighbouringTiles();
  195. size_t index = 0;
  196. for(auto neighbour : hexes)
  197. ret[hex].at(index++) = neighbour;
  198. }
  199. return ret;
  200. }
  201. const BattleHex::NeighbouringTilesCache BattleHex::neighbouringTilesCache = calculateNeighbouringTiles();
  202. VCMI_LIB_NAMESPACE_END