BattleHex.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "StdInc.h"
  2. #include "BattleHex.h"
  3. BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid)
  4. {
  5. si16 x = getX(),
  6. y = getY();
  7. switch(dir)
  8. {
  9. case TOP_LEFT:
  10. setXY(y%2 ? x-1 : x, y-1, hasToBeValid);
  11. break;
  12. case TOP_RIGHT:
  13. setXY(y%2 ? x : x+1, y-1, hasToBeValid);
  14. break;
  15. case RIGHT:
  16. setXY(x+1, y, hasToBeValid);
  17. break;
  18. case BOTTOM_RIGHT:
  19. setXY(y%2 ? x : x+1, y+1, hasToBeValid);
  20. break;
  21. case BOTTOM_LEFT:
  22. setXY(y%2 ? x-1 : x, y+1, hasToBeValid);
  23. break;
  24. case LEFT:
  25. setXY(x-1, y, hasToBeValid);
  26. break;
  27. default:
  28. throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
  29. break;
  30. }
  31. return *this;
  32. }
  33. void BattleHex::operator+=(EDir dir)
  34. {
  35. moveInDir(dir);
  36. }
  37. BattleHex BattleHex::operator+(EDir dir) const
  38. {
  39. BattleHex ret(*this);
  40. ret += dir;
  41. return ret;
  42. }
  43. std::vector<BattleHex> BattleHex::neighbouringTiles() const
  44. {
  45. std::vector<BattleHex> ret;
  46. const int WN = GameConstants::BFIELD_WIDTH;
  47. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret);
  48. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret);
  49. checkAndPush(hex - 1, ret);
  50. checkAndPush(hex + 1, ret);
  51. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret);
  52. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret);
  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. int x1 = hex1.getX() + y1 / 2.0,
  76. x2 = hex2.getX() + y2 / 2.0;
  77. int xDst = x2 - x1,
  78. yDst = y2 - y1;
  79. if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
  80. return std::max(std::abs(xDst), std::abs(yDst));
  81. else
  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. }