BattleHex.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret);
  57. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret);
  58. checkAndPush(hex - 1, ret);
  59. checkAndPush(hex + 1, ret);
  60. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret);
  61. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret);
  62. return ret;
  63. }
  64. signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
  65. {
  66. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  67. return 0;
  68. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  69. return 1;
  70. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  71. return 5;
  72. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  73. return 2;
  74. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  75. return 4;
  76. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  77. return 3;
  78. return -1;
  79. }
  80. char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
  81. {
  82. int y1 = hex1.getY(),
  83. y2 = hex2.getY();
  84. int x1 = hex1.getX() + y1 / 2.0,
  85. x2 = hex2.getX() + y2 / 2.0;
  86. int xDst = x2 - x1,
  87. yDst = y2 - y1;
  88. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  89. return std::max(std::abs(xDst), std::abs(yDst));
  90. else
  91. return std::abs(xDst) + std::abs(yDst);
  92. }
  93. void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
  94. {
  95. if(tile.isAvailable())
  96. ret.push_back(tile);
  97. }
  98. bool BattleHex::isAvailable() const
  99. {
  100. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  101. }
  102. BattleHex BattleHex::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities)
  103. {
  104. std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
  105. BattleHex initialHex = BattleHex(initialPos);
  106. auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
  107. {
  108. return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
  109. };
  110. boost::sort (sortedTiles, compareDistance); //closest tiles at front
  111. int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
  112. auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
  113. {
  114. return closestDistance < here.getDistance (initialPos, here);
  115. };
  116. vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
  117. auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
  118. {
  119. if(left.getX() != right.getX())
  120. {
  121. if (attackerOwned)
  122. return left.getX() > right.getX(); //find furthest right
  123. else
  124. return left.getX() < right.getX(); //find furthest left
  125. }
  126. else
  127. {
  128. //Prefer tiles in the same row.
  129. return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
  130. }
  131. };
  132. boost::sort (sortedTiles, compareHorizontal);
  133. return sortedTiles.front();
  134. }