BattleHex.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 xDst = std::abs(hex1 % GameConstants::BFIELD_WIDTH - hex2 % GameConstants::BFIELD_WIDTH),
  74. yDst = std::abs(hex1 / GameConstants::BFIELD_WIDTH - hex2 / GameConstants::BFIELD_WIDTH);
  75. return std::max(xDst, yDst) + std::min(xDst, yDst) - (yDst + 1)/2;
  76. }
  77. void BattleHex::checkAndPush(int tile, std::vector<BattleHex> & ret)
  78. {
  79. if( tile>=0 && tile<GameConstants::BFIELD_SIZE && (tile%GameConstants::BFIELD_WIDTH != (GameConstants::BFIELD_WIDTH - 1))
  80. && (tile%GameConstants::BFIELD_WIDTH != 0) )
  81. ret.push_back(BattleHex(tile));
  82. }
  83. bool BattleHex::isAvailable() const
  84. {
  85. return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
  86. }