BattleHex.cpp 5.1 KB

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