BattleHex.cpp 5.6 KB

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