BattleHex.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)
  49. {
  50. assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
  51. hex = x + y * GameConstants::BFIELD_WIDTH;
  52. }
  53. template<typename inttype>
  54. void setXY(std::pair<inttype, inttype> xy)
  55. {
  56. setXY(xy.first, xy.second);
  57. }
  58. si16 getY() const
  59. {
  60. return hex / GameConstants::BFIELD_WIDTH;
  61. }
  62. si16 getX() const
  63. {
  64. int pos = hex - getY() * GameConstants::BFIELD_WIDTH;
  65. return pos;
  66. }
  67. std::pair<si16, si16> getXY() const
  68. {
  69. return std::make_pair(getX(), getY());
  70. }
  71. //moving to direction
  72. void operator+=(EDir dir);
  73. //generates new BattleHex moved by given dir
  74. BattleHex operator+(EDir dir) const;
  75. std::vector<BattleHex> neighbouringTiles() const;
  76. //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)
  77. static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
  78. //returns distance between given hexes
  79. static char getDistance(BattleHex hex1, BattleHex hex2);
  80. template <typename Handler> void serialize(Handler &h, const int version)
  81. {
  82. h & hex;
  83. }
  84. static void checkAndPush(int tile, std::vector<BattleHex> & ret);
  85. };