2
0

BattleHex.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include "GameConstants.h"
  3. /*
  4. * BattleHex.h, 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. // for battle stacks' positions
  13. struct DLL_LINKAGE BattleHex
  14. {
  15. static const si16 INVALID = -1;
  16. enum EDir{RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT};
  17. si16 hex;
  18. BattleHex() : hex(INVALID) {}
  19. BattleHex(si16 _hex) : hex(_hex) {}
  20. operator si16() const
  21. {
  22. return hex;
  23. }
  24. bool isValid() const
  25. {
  26. return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
  27. }
  28. template<typename inttype>
  29. BattleHex(inttype x, inttype y)
  30. {
  31. setXY(x, y);
  32. }
  33. template<typename inttype>
  34. BattleHex(std::pair<inttype, inttype> xy)
  35. {
  36. setXY(xy);
  37. }
  38. template<typename inttype>
  39. void setX(inttype x)
  40. {
  41. setXY(x, getY());
  42. }
  43. template<typename inttype>
  44. void setY(inttype y)
  45. {
  46. setXY(getX(), y);
  47. }
  48. void setXY(si16 x, si16 y, bool hasToBeValid = true)
  49. {
  50. if(hasToBeValid)
  51. {
  52. assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
  53. }
  54. hex = x + y * GameConstants::BFIELD_WIDTH;
  55. }
  56. template<typename inttype>
  57. void setXY(std::pair<inttype, inttype> xy)
  58. {
  59. setXY(xy.first, xy.second);
  60. }
  61. si16 getY() const
  62. {
  63. return hex / GameConstants::BFIELD_WIDTH;
  64. }
  65. si16 getX() const
  66. {
  67. int pos = hex - getY() * GameConstants::BFIELD_WIDTH;
  68. return pos;
  69. }
  70. std::pair<si16, si16> getXY() const
  71. {
  72. return std::make_pair(getX(), getY());
  73. }
  74. //moving to direction
  75. BattleHex& moveInDir(EDir dir, bool hasToBeValid = true);
  76. void operator+=(EDir dir); //sugar for above
  77. //generates new BattleHex moved by given dir
  78. BattleHex operator+(EDir dir) const;
  79. std::vector<BattleHex> neighbouringTiles() const;
  80. //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
  81. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  82. //returns distance between given hexes
  83. static char getDistance(BattleHex hex1, BattleHex hex2);
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & hex;
  87. }
  88. static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
  89. bool isAvailable() const; //valid position not in first or last column
  90. static BattleHex getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad
  91. };