BattleHex.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "../GameConstants.h"
  13. BattleHex::BattleHex()
  14. : hex(INVALID) {}
  15. BattleHex::BattleHex(si16 _hex)
  16. : hex(_hex) {}
  17. BattleHex::BattleHex(si16 x, si16 y)
  18. {
  19. setXY(x, y);
  20. }
  21. BattleHex::BattleHex(std::pair<si16, si16> xy)
  22. {
  23. setXY(xy);
  24. }
  25. BattleHex::operator si16() const
  26. {
  27. return hex;
  28. }
  29. bool BattleHex::isValid() const
  30. {
  31. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  32. }
  33. bool BattleHex::isAvailable() const
  34. {
  35. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH - 1;
  36. }
  37. void BattleHex::setX(si16 x)
  38. {
  39. setXY(x, getY());
  40. }
  41. void BattleHex::setY(si16 y)
  42. {
  43. setXY(getX(), y);
  44. }
  45. void BattleHex::setXY(si16 x, si16 y, bool hasToBeValid)
  46. {
  47. if(hasToBeValid)
  48. assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
  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. for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir + 1))
  116. checkAndPush(cloneInDirection(dir, false), ret);
  117. return ret;
  118. }
  119. signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  120. {
  121. for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir + 1))
  122. if(hex2 == hex1.cloneInDirection(dir, false))
  123. return dir;
  124. return INVALID;
  125. }
  126. char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  127. {
  128. int y1 = hex1.getY(), y2 = hex2.getY();
  129. // FIXME: Omit floating point arithmetics
  130. int x1 = (hex1.getX() + y1 * 0.5), x2 = (hex2.getX() + y2 * 0.5);
  131. int xDst = x2 - x1, yDst = y2 - y1;
  132. if((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  133. return std::max(std::abs(xDst), std::abs(yDst));
  134. return std::abs(xDst) + std::abs(yDst);
  135. }
  136. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  137. {
  138. if(tile.isAvailable())
  139. ret.push_back(tile);
  140. }
  141. BattleHex BattleHex::getClosestTile(ui8 side, BattleHex initialPos, std::set<BattleHex> & possibilities)
  142. {
  143. std::vector<BattleHex> sortedTiles(possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  144. BattleHex initialHex = BattleHex(initialPos);
  145. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  146. {
  147. return initialHex.getDistance(initialHex, left) < initialHex.getDistance(initialHex, right);
  148. };
  149. boost::sort(sortedTiles, compareDistance); //closest tiles at front
  150. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  151. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  152. {
  153. return closestDistance < here.getDistance(initialPos, here);
  154. };
  155. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  156. auto compareHorizontal = [side, initialPos](const BattleHex left, const BattleHex right) -> bool
  157. {
  158. if(left.getX() != right.getX())
  159. {
  160. if(side == BattleSide::ATTACKER)
  161. return left.getX() > right.getX(); //find furthest right
  162. else
  163. return left.getX() < right.getX(); //find furthest left
  164. }
  165. else
  166. {
  167. //Prefer tiles in the same row.
  168. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  169. }
  170. };
  171. boost::sort(sortedTiles, compareHorizontal);
  172. return sortedTiles.front();
  173. }
  174. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  175. {
  176. return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
  177. }