BattleHex.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  43. {
  44. std::vector<BattleHex> ret;
  45. const int WN = GameConstants::BFIELD_WIDTH;
  46. // H3 order : TR, R, BR, BL, L, TL (T = top, B = bottom ...)
  47. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret); // 1
  48. checkAndPush(hex + 1, ret); // 2
  49. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret); // 3
  50. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret); // 4
  51. checkAndPush(hex - 1, ret); // 5
  52. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret); // 6
  53. return ret;
  54. }
  55. signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  56. {
  57. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  58. return 0;
  59. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  60. return 1;
  61. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  62. return 5;
  63. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  64. return 2;
  65. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  66. return 4;
  67. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  68. return 3;
  69. return -1;
  70. }
  71. char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  72. {
  73. int y1 = hex1.getY(),
  74. y2 = hex2.getY();
  75. // FIXME: Omit floating point arithmetics
  76. int x1 = (int)(hex1.getX() + y1 * 0.5),
  77. x2 = (int)(hex2.getX() + y2 * 0.5);
  78. int xDst = x2 - x1,
  79. yDst = y2 - y1;
  80. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  81. return std::max(std::abs(xDst), std::abs(yDst));
  82. return std::abs(xDst) + std::abs(yDst);
  83. }
  84. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  85. {
  86. if(tile.isAvailable())
  87. ret.push_back(tile);
  88. }
  89. bool BattleHex::isAvailable() const
  90. {
  91. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  92. }
  93. BattleHex BattleHex::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities)
  94. {
  95. std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  96. BattleHex initialHex = BattleHex(initialPos);
  97. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  98. {
  99. return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
  100. };
  101. boost::sort (sortedTiles, compareDistance); //closest tiles at front
  102. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  103. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  104. {
  105. return closestDistance < here.getDistance (initialPos, here);
  106. };
  107. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  108. auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
  109. {
  110. if(left.getX() != right.getX())
  111. {
  112. if (attackerOwned)
  113. return left.getX() > right.getX(); //find furthest right
  114. else
  115. return left.getX() < right.getX(); //find furthest left
  116. }
  117. else
  118. {
  119. //Prefer tiles in the same row.
  120. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  121. }
  122. };
  123. boost::sort (sortedTiles, compareHorizontal);
  124. return sortedTiles.front();
  125. }
  126. std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
  127. {
  128. return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
  129. }