int3.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /// 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. //returns int3 with coordinates multiplied by given number
  38. int3 operator*(const double i) const { return int3((int)(x * i), (int)(y * i), (int)(z * i)); }
  39. //returns int3 with coordinates divided by given number
  40. int3 operator/(const double i) const { return int3((int)(x / i), (int)(y / i), (int)(z / i)); }
  41. //returns int3 with coordinates multiplied by given number
  42. int3 operator*(const si32 i) const { return int3(x * i, y * i, z * i); }
  43. //returns int3 with coordinates divided by given number
  44. int3 operator/(const si32 i) const { return int3(x / i, y / i, z / i); }
  45. int3 & operator+=(const int3 & i)
  46. {
  47. x += i.x;
  48. y += i.y;
  49. z += i.z;
  50. return *this;
  51. }
  52. int3 & operator-=(const int3 & i)
  53. {
  54. x -= i.x;
  55. y -= i.y;
  56. z -= i.z;
  57. return *this;
  58. }
  59. //increases 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. //decreases all coordinates by given number
  68. int3 & operator-=(const si32 i)
  69. {
  70. x -= i;
  71. y -= i;
  72. z -= i;
  73. return *this;
  74. }
  75. bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
  76. bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
  77. bool operator<(const int3 & i) const
  78. {
  79. if (z < i.z)
  80. return true;
  81. if (z > i.z)
  82. return false;
  83. if (y < i.y)
  84. return true;
  85. if (y > i.y)
  86. return false;
  87. if (x < i.x)
  88. return true;
  89. if (x > i.x)
  90. return false;
  91. return false;
  92. }
  93. enum EDistanceFormula
  94. {
  95. DIST_2D = 0,
  96. DIST_MANHATTAN, // patrol distance
  97. DIST_CHEBYSHEV, // ambient sound distance
  98. DIST_2DSQ
  99. };
  100. ui32 dist(const int3 & o, EDistanceFormula formula) const
  101. {
  102. switch(formula)
  103. {
  104. case DIST_2D:
  105. return static_cast<ui32>(dist2d(o));
  106. case DIST_MANHATTAN:
  107. return static_cast<ui32>(mandist2d(o));
  108. case DIST_CHEBYSHEV:
  109. return static_cast<ui32>(chebdist2d(o));
  110. case DIST_2DSQ:
  111. return dist2dSQ(o);
  112. default:
  113. return 0;
  114. }
  115. }
  116. //returns squared distance on Oxy plane (z coord is not used)
  117. ui32 dist2dSQ(const int3 & o) const
  118. {
  119. const si32 dx = (x - o.x);
  120. const si32 dy = (y - o.y);
  121. return (ui32)(dx*dx) + (ui32)(dy*dy);
  122. }
  123. //returns distance on Oxy plane (z coord is not used)
  124. double dist2d(const int3 & o) const
  125. {
  126. return std::sqrt((double)dist2dSQ(o));
  127. }
  128. //manhattan distance used for patrol radius (z coord is not used)
  129. double mandist2d(const int3 & o) const
  130. {
  131. return abs(o.x - x) + abs(o.y - y);
  132. }
  133. //chebyshev distance used for ambient sounds (z coord is not used)
  134. double chebdist2d(const int3 & o) const
  135. {
  136. return std::max(std::abs(o.x - x), std::abs(o.y - y));
  137. }
  138. bool areNeighbours(const int3 & o) const
  139. {
  140. return (dist2dSQ(o) < 4) && (z == o.z);
  141. }
  142. //returns "(x y z)" string
  143. std::string toString() const
  144. {
  145. std::string result("(");
  146. result += boost::lexical_cast<std::string>(x); result += ' ';
  147. result += boost::lexical_cast<std::string>(y); result += ' ';
  148. result += boost::lexical_cast<std::string>(z); result += ')';
  149. return result;
  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. }