int3.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * int3.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. /// Class which consists of three integer values. Represents position on adventure map.
  13. class int3
  14. {
  15. public:
  16. si32 x;
  17. si32 y;
  18. si32 z;
  19. //c-tor: x, y, z initialized to 0
  20. constexpr int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized.
  21. //c-tor: x, y, z initialized to i
  22. explicit constexpr int3(const si32 i) : x(i), y(i), z(i) {}
  23. //c-tor: x, y, z initialized to X, Y, Z
  24. constexpr int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {}
  25. constexpr int3(const int3 & c) = default;
  26. constexpr int3 & operator=(const int3 & c) = default;
  27. constexpr int3 operator-() const { return int3(-x, -y, -z); }
  28. constexpr int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
  29. constexpr int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
  30. //returns int3 with coordinates increased by given number
  31. constexpr int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); }
  32. //returns int3 with coordinates decreased by given number
  33. constexpr int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); }
  34. //returns int3 with coordinates multiplied by given number
  35. constexpr int3 operator*(const double i) const { return int3((int)(x * i), (int)(y * i), (int)(z * i)); }
  36. //returns int3 with coordinates divided by given number
  37. constexpr int3 operator/(const double i) const { return int3((int)(x / i), (int)(y / i), (int)(z / i)); }
  38. //returns int3 with coordinates multiplied by given number
  39. constexpr int3 operator*(const si32 i) const { return int3(x * i, y * i, z * i); }
  40. //returns int3 with coordinates divided by given number
  41. constexpr int3 operator/(const si32 i) const { return int3(x / i, y / i, z / i); }
  42. constexpr int3 & operator+=(const int3 & i)
  43. {
  44. x += i.x;
  45. y += i.y;
  46. z += i.z;
  47. return *this;
  48. }
  49. constexpr int3 & operator-=(const int3 & i)
  50. {
  51. x -= i.x;
  52. y -= i.y;
  53. z -= i.z;
  54. return *this;
  55. }
  56. //increases all coordinates by given number
  57. constexpr int3 & operator+=(const si32 i)
  58. {
  59. x += i;
  60. y += i;
  61. z += i;
  62. return *this;
  63. }
  64. //decreases all coordinates by given number
  65. constexpr int3 & operator-=(const si32 i)
  66. {
  67. x -= i;
  68. y -= i;
  69. z -= i;
  70. return *this;
  71. }
  72. constexpr bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
  73. constexpr bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
  74. constexpr bool operator<(const int3 & i) const
  75. {
  76. if (z != i.z)
  77. return z < i.z;
  78. if (y != i.y)
  79. return y < i.y;
  80. return x < i.x;
  81. }
  82. enum EDistanceFormula
  83. {
  84. DIST_2D = 0,
  85. DIST_MANHATTAN, // patrol distance
  86. DIST_CHEBYSHEV, // ambient sound distance
  87. DIST_2DSQ
  88. };
  89. ui32 dist(const int3 & o, EDistanceFormula formula) const
  90. {
  91. switch(formula)
  92. {
  93. case DIST_2D:
  94. return std::round(dist2d(o));
  95. case DIST_MANHATTAN:
  96. return static_cast<ui32>(mandist2d(o));
  97. case DIST_CHEBYSHEV:
  98. return static_cast<ui32>(chebdist2d(o));
  99. case DIST_2DSQ:
  100. return dist2dSQ(o);
  101. default:
  102. return 0;
  103. }
  104. }
  105. //returns squared distance on Oxy plane (z coord is not used)
  106. constexpr ui32 dist2dSQ(const int3 & o) const
  107. {
  108. const si32 dx = (x - o.x);
  109. const si32 dy = (y - o.y);
  110. return (ui32)(dx*dx) + (ui32)(dy*dy);
  111. }
  112. //returns distance on Oxy plane (z coord is not used)
  113. double dist2d(const int3 & o) const
  114. {
  115. return std::sqrt((double)dist2dSQ(o));
  116. }
  117. //manhattan distance used for patrol radius (z coord is not used)
  118. constexpr double mandist2d(const int3 & o) const
  119. {
  120. return vstd::abs(o.x - x) + vstd::abs(o.y - y);
  121. }
  122. //chebyshev distance used for ambient sounds (z coord is not used)
  123. constexpr double chebdist2d(const int3 & o) const
  124. {
  125. return std::max(vstd::abs(o.x - x), vstd::abs(o.y - y));
  126. }
  127. constexpr bool areNeighbours(const int3 & o) const
  128. {
  129. return (dist2dSQ(o) < 4) && (z == o.z);
  130. }
  131. //returns "(x y z)" string
  132. std::string toString() const
  133. {
  134. //Performance is important here
  135. std::string result = "(" +
  136. std::to_string(x) + " " +
  137. std::to_string(y) + " " +
  138. std::to_string(z) + ")";
  139. return result;
  140. }
  141. constexpr bool isValid() const //Should be named "isValid"?
  142. {
  143. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  144. }
  145. template <typename Handler>
  146. void serialize(Handler &h)
  147. {
  148. h & x;
  149. h & y;
  150. h & z;
  151. }
  152. constexpr static std::array<int3, 8> getDirs()
  153. {
  154. return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  155. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };
  156. }
  157. // Solution by ChatGPT
  158. // Assume values up to +- 1000
  159. friend std::size_t hash_value(const int3& v) {
  160. // Since the range is [-1000, 1000], offsetting by 1000 maps it to [0, 2000]
  161. std::size_t hx = v.x + 1000;
  162. std::size_t hy = v.y + 1000;
  163. std::size_t hz = v.z + 1000;
  164. // Combine the hash values, multiplying them by prime numbers
  165. return ((hx * 4000037u) ^ (hy * 2003u)) + hz;
  166. }
  167. };
  168. template<typename Container>
  169. int3 findClosestTile (Container & container, int3 dest)
  170. {
  171. static_assert(std::is_same_v<typename Container::value_type, int3>,
  172. "findClosestTile requires <int3> container.");
  173. int3 result(-1, -1, -1);
  174. ui32 distance = std::numeric_limits<ui32>::max();
  175. for (const int3& tile : container)
  176. {
  177. const ui32 currentDistance = dest.dist2dSQ(tile);
  178. if (currentDistance < distance)
  179. {
  180. result = tile;
  181. distance = currentDistance;
  182. }
  183. }
  184. return result;
  185. }
  186. VCMI_LIB_NAMESPACE_END
  187. template<>
  188. struct std::hash<VCMI_LIB_WRAP_NAMESPACE(int3)> {
  189. std::size_t operator()(VCMI_LIB_WRAP_NAMESPACE(int3) const& pos) const noexcept {
  190. return hash_value(pos);
  191. }
  192. };