BattleHex.cpp 5.1 KB

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