BattleHex.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "StdInc.h"
  2. #include "BattleHex.h"
  3. /*
  4. * BattleHex.cpp, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid)
  13. {
  14. si16 x = getX(),
  15. y = getY();
  16. switch(dir)
  17. {
  18. case TOP_LEFT:
  19. setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
  20. break;
  21. case TOP_RIGHT:
  22. setXY((y%2) ? x : x+1, y-1, hasToBeValid);
  23. break;
  24. case RIGHT:
  25. setXY(x+1, y, hasToBeValid);
  26. break;
  27. case BOTTOM_RIGHT:
  28. setXY((y%2) ? x : x+1, y+1, hasToBeValid);
  29. break;
  30. case BOTTOM_LEFT:
  31. setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
  32. break;
  33. case LEFT:
  34. setXY(x-1, y, hasToBeValid);
  35. break;
  36. default:
  37. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  38. break;
  39. }
  40. return *this;
  41. }
  42. void BattleHex::operator+=(EDir dir)
  43. {
  44. moveInDir(dir);
  45. }
  46. BattleHex BattleHex::operator+(EDir dir) const
  47. {
  48. BattleHex ret(*this);
  49. ret += dir;
  50. return ret;
  51. }
  52. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  53. {
  54. std::vector<BattleHex> ret;
  55. const int WN = GameConstants::BFIELD_WIDTH;
  56. // H3 order : TR, R, BR, BL, L, TL (T = top, B = bottom ...)
  57. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret); // 1
  58. checkAndPush(hex + 1, ret); // 2
  59. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret); // 3
  60. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret); // 4
  61. checkAndPush(hex - 1, ret); // 5
  62. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret); // 6
  63. return ret;
  64. }
  65. signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  66. {
  67. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  68. return 0;
  69. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  70. return 1;
  71. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  72. return 5;
  73. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  74. return 2;
  75. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  76. return 4;
  77. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  78. return 3;
  79. return -1;
  80. }
  81. char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  82. {
  83. int y1 = hex1.getY(),
  84. y2 = hex2.getY();
  85. int x1 = hex1.getX() + y1 / 2.0,
  86. x2 = hex2.getX() + y2 / 2.0;
  87. int xDst = x2 - x1,
  88. yDst = y2 - y1;
  89. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  90. return std::max(std::abs(xDst), std::abs(yDst));
  91. else
  92. return std::abs(xDst) + std::abs(yDst);
  93. }
  94. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  95. {
  96. if(tile.isAvailable())
  97. ret.push_back(tile);
  98. }
  99. bool BattleHex::isAvailable() const
  100. {
  101. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  102. }
  103. BattleHex BattleHex::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities)
  104. {
  105. std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  106. BattleHex initialHex = BattleHex(initialPos);
  107. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  108. {
  109. return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
  110. };
  111. boost::sort (sortedTiles, compareDistance); //closest tiles at front
  112. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  113. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  114. {
  115. return closestDistance < here.getDistance (initialPos, here);
  116. };
  117. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  118. auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
  119. {
  120. if(left.getX() != right.getX())
  121. {
  122. if (attackerOwned)
  123. return left.getX() > right.getX(); //find furthest right
  124. else
  125. return left.getX() < right.getX(); //find furthest left
  126. }
  127. else
  128. {
  129. //Prefer tiles in the same row.
  130. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  131. }
  132. };
  133. boost::sort (sortedTiles, compareHorizontal);
  134. return sortedTiles.front();
  135. }
  136. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  137. {
  138. return os << boost::str(boost::format("[BattleHex: x '%d', y '%d', hex '%d']") % hex.getX() % hex.getY() % hex.hex);
  139. }