2
0

int3.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. /*
  3. * int3.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. /// Class which consists of three integer values. Represents position on adventure map.
  12. class int3
  13. {
  14. public:
  15. si32 x, y, z;
  16. //c-tor: x, y, z initialized to 0
  17. int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized.
  18. //c-tor: x, y, z initialized to i
  19. explicit int3(const si32 i) : x(i), y(i), z(i) {}
  20. //c-tor: x, y, z initialized to X, Y, Z
  21. int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {}
  22. int3(const int3 & c) : x(c.x), y(c.y), z(c.z) {} // Should be set to default (C++11)?
  23. int3 & operator=(const int3 & c) // Should be set to default (C++11)?
  24. {
  25. x = c.x;
  26. y = c.y;
  27. z = c.z;
  28. return *this;
  29. }
  30. int3 operator-() const { return int3(-x, -y, -z); }
  31. int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
  32. int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
  33. //returns int3 with coordinates increased by given number
  34. int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); }
  35. //returns int3 with coordinates decreased by given number
  36. int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); }
  37. int3 & operator+=(const int3 & i)
  38. {
  39. x += i.x;
  40. y += i.y;
  41. z += i.z;
  42. return *this;
  43. }
  44. int3 & operator-=(const int3 & i)
  45. {
  46. x -= i.x;
  47. y -= i.y;
  48. z -= i.z;
  49. return *this;
  50. }
  51. //increases all coordinates by given number
  52. int3 & operator+=(const si32 i)
  53. {
  54. x += i;
  55. y += i;
  56. z += i;
  57. return *this;
  58. }
  59. //decreases all coordinates by given number
  60. int3 & operator-=(const si32 i)
  61. {
  62. x -= i;
  63. y -= i;
  64. z -= i;
  65. return *this;
  66. }
  67. bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
  68. bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
  69. bool operator<(const int3 & i) const
  70. {
  71. if (z < i.z)
  72. return true;
  73. if (z > i.z)
  74. return false;
  75. if (y < i.y)
  76. return true;
  77. if (y > i.y)
  78. return false;
  79. if (x < i.x)
  80. return true;
  81. if (x > i.x)
  82. return false;
  83. return false;
  84. }
  85. //returns squared distance on Oxy plane (z coord is not used)
  86. ui32 dist2dSQ(const int3 & o) const
  87. {
  88. const si32 dx = (x - o.x);
  89. const si32 dy = (y - o.y);
  90. return (ui32)(dx*dx) + (ui32)(dy*dy);
  91. }
  92. //returns distance on Oxy plane (z coord is not used)
  93. double dist2d(const int3 & o) const
  94. {
  95. return std::sqrt((double)dist2dSQ(o));
  96. }
  97. //manhattan distance used for patrol radius (z coord is not used)
  98. double mandist2d(const int3 & o) const
  99. {
  100. return abs(o.x - x) + abs(o.y - y);
  101. }
  102. bool areNeighbours(const int3 & o) const
  103. {
  104. return (dist2dSQ(o) < 4) && (z == o.z);
  105. }
  106. //returns "(x y z)" string
  107. std::string operator ()() const //Change to int3::toString()?
  108. {
  109. std::string result("(");
  110. result += boost::lexical_cast<std::string>(x); result += ' ';
  111. result += boost::lexical_cast<std::string>(y); result += ' ';
  112. result += boost::lexical_cast<std::string>(z); result += ')';
  113. return result;
  114. }
  115. bool valid() const //Should be named "isValid"?
  116. {
  117. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  118. }
  119. template <typename Handler>
  120. void serialize(Handler &h, const int version)
  121. {
  122. h & x & y & z;
  123. }
  124. static std::array<int3, 8> getDirs()
  125. {
  126. return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  127. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };
  128. }
  129. };
  130. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  131. {
  132. return str << sth.x << ' ' << sth.y << ' ' << sth.z;
  133. }
  134. inline std::istream & operator>>(std::istream & str, int3 & dest)
  135. {
  136. return str >> dest.x >> dest.y >> dest.z;
  137. }
  138. //Why not normal function?
  139. struct ShashInt3
  140. {
  141. size_t operator()(int3 const& pos) const
  142. {
  143. size_t ret = std::hash<int>()(pos.x);
  144. vstd::hash_combine(ret, pos.y);
  145. vstd::hash_combine(ret, pos.z);
  146. return ret;
  147. }
  148. };
  149. template<typename Container>
  150. int3 findClosestTile (Container & container, int3 dest)
  151. {
  152. static_assert(std::is_same<typename Container::value_type, int3>::value,
  153. "findClosestTile requires <int3> container.");
  154. int3 result(-1, -1, -1);
  155. ui32 distance = std::numeric_limits<ui32>::max();
  156. for (const int3& tile : container)
  157. {
  158. const ui32 currentDistance = dest.dist2dSQ(tile);
  159. if (currentDistance < distance)
  160. {
  161. result = tile;
  162. distance = currentDistance;
  163. }
  164. }
  165. return result;
  166. }