int3.h 5.5 KB

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