int3.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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, y, z;
  17. //c-tor: x, y, z initialized to 0
  18. constexpr int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized.
  19. //c-tor: x, y, z initialized to i
  20. explicit constexpr int3(const si32 i) : x(i), y(i), z(i) {}
  21. //c-tor: x, y, z initialized to X, Y, Z
  22. constexpr int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {}
  23. constexpr int3(const int3 & c) = default;
  24. constexpr int3 & operator=(const int3 & c) = default;
  25. constexpr int3 operator-() const { return int3(-x, -y, -z); }
  26. constexpr int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
  27. constexpr int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
  28. //returns int3 with coordinates increased by given number
  29. constexpr int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); }
  30. //returns int3 with coordinates decreased by given number
  31. constexpr int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); }
  32. //returns int3 with coordinates multiplied by given number
  33. constexpr int3 operator*(const double i) const { return int3((int)(x * i), (int)(y * i), (int)(z * i)); }
  34. //returns int3 with coordinates divided 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 multiplied by given number
  37. constexpr int3 operator*(const si32 i) const { return int3(x * i, y * i, z * i); }
  38. //returns int3 with coordinates divided by given number
  39. constexpr int3 operator/(const si32 i) const { return int3(x / i, y / i, z / i); }
  40. constexpr int3 & operator+=(const int3 & i)
  41. {
  42. x += i.x;
  43. y += i.y;
  44. z += i.z;
  45. return *this;
  46. }
  47. constexpr int3 & operator-=(const int3 & i)
  48. {
  49. x -= i.x;
  50. y -= i.y;
  51. z -= i.z;
  52. return *this;
  53. }
  54. //increases all coordinates by given number
  55. constexpr int3 & operator+=(const si32 i)
  56. {
  57. x += i;
  58. y += i;
  59. z += i;
  60. return *this;
  61. }
  62. //decreases all coordinates by given number
  63. constexpr int3 & operator-=(const si32 i)
  64. {
  65. x -= i;
  66. y -= i;
  67. z -= i;
  68. return *this;
  69. }
  70. constexpr bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
  71. constexpr bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
  72. constexpr bool operator<(const int3 & i) const
  73. {
  74. if (z < i.z)
  75. return true;
  76. if (z > i.z)
  77. return false;
  78. if (y < i.y)
  79. return true;
  80. if (y > i.y)
  81. return false;
  82. if (x < i.x)
  83. return true;
  84. if (x > i.x)
  85. return false;
  86. return false;
  87. }
  88. enum EDistanceFormula
  89. {
  90. DIST_2D = 0,
  91. DIST_MANHATTAN, // patrol distance
  92. DIST_CHEBYSHEV, // ambient sound distance
  93. DIST_2DSQ
  94. };
  95. ui32 dist(const int3 & o, EDistanceFormula formula) const
  96. {
  97. switch(formula)
  98. {
  99. case DIST_2D:
  100. return std::round(dist2d(o));
  101. case DIST_MANHATTAN:
  102. return static_cast<ui32>(mandist2d(o));
  103. case DIST_CHEBYSHEV:
  104. return static_cast<ui32>(chebdist2d(o));
  105. case DIST_2DSQ:
  106. return dist2dSQ(o);
  107. default:
  108. return 0;
  109. }
  110. }
  111. //returns squared distance on Oxy plane (z coord is not used)
  112. constexpr ui32 dist2dSQ(const int3 & o) const
  113. {
  114. const si32 dx = (x - o.x);
  115. const si32 dy = (y - o.y);
  116. return (ui32)(dx*dx) + (ui32)(dy*dy);
  117. }
  118. //returns distance on Oxy plane (z coord is not used)
  119. double dist2d(const int3 & o) const
  120. {
  121. return std::sqrt((double)dist2dSQ(o));
  122. }
  123. //manhattan distance used for patrol radius (z coord is not used)
  124. constexpr double mandist2d(const int3 & o) const
  125. {
  126. return vstd::abs(o.x - x) + vstd::abs(o.y - y);
  127. }
  128. //chebyshev distance used for ambient sounds (z coord is not used)
  129. constexpr double chebdist2d(const int3 & o) const
  130. {
  131. return std::max(vstd::abs(o.x - x), vstd::abs(o.y - y));
  132. }
  133. constexpr bool areNeighbours(const int3 & o) const
  134. {
  135. return (dist2dSQ(o) < 4) && (z == o.z);
  136. }
  137. //returns "(x y z)" string
  138. std::string toString() const
  139. {
  140. //Performance is important here
  141. std::string result = "(" +
  142. std::to_string(x) + " " +
  143. std::to_string(y) + " " +
  144. std::to_string(z) + ")";
  145. return result;
  146. }
  147. constexpr bool valid() const //Should be named "isValid"?
  148. {
  149. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  150. }
  151. template <typename Handler>
  152. void serialize(Handler &h, const int version)
  153. {
  154. h & x;
  155. h & y;
  156. h & z;
  157. }
  158. constexpr static std::array<int3, 8> getDirs()
  159. {
  160. return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  161. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };
  162. }
  163. };
  164. template<typename Container>
  165. int3 findClosestTile (Container & container, int3 dest)
  166. {
  167. static_assert(std::is_same<typename Container::value_type, int3>::value,
  168. "findClosestTile requires <int3> container.");
  169. int3 result(-1, -1, -1);
  170. ui32 distance = std::numeric_limits<ui32>::max();
  171. for (const int3& tile : container)
  172. {
  173. const ui32 currentDistance = dest.dist2dSQ(tile);
  174. if (currentDistance < distance)
  175. {
  176. result = tile;
  177. distance = currentDistance;
  178. }
  179. }
  180. return result;
  181. }
  182. VCMI_LIB_NAMESPACE_END
  183. template<>
  184. struct std::hash<VCMI_LIB_WRAP_NAMESPACE(int3)> {
  185. size_t operator()(VCMI_LIB_WRAP_NAMESPACE(int3) const& pos) const
  186. {
  187. size_t ret = std::hash<int>()(pos.x);
  188. VCMI_LIB_WRAP_NAMESPACE(vstd)::hash_combine(ret, pos.y);
  189. VCMI_LIB_WRAP_NAMESPACE(vstd)::hash_combine(ret, pos.z);
  190. return ret;
  191. }
  192. };