int3.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 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. enum EDistanceFormula
  86. {
  87. DIST_2D = 0,
  88. DIST_MANHATTAN, // patrol distance
  89. DIST_CHEBYSHEV, // ambient sound distance
  90. DIST_2DSQ
  91. };
  92. ui32 dist(const int3 & o, EDistanceFormula formula) const
  93. {
  94. switch(formula)
  95. {
  96. case DIST_2D:
  97. return static_cast<ui32>(dist2d(o));
  98. case DIST_MANHATTAN:
  99. return static_cast<ui32>(mandist2d(o));
  100. case DIST_CHEBYSHEV:
  101. return static_cast<ui32>(chebdist2d(o));
  102. case DIST_2DSQ:
  103. return dist2dSQ(o);
  104. default:
  105. return 0;
  106. }
  107. }
  108. //returns squared distance on Oxy plane (z coord is not used)
  109. ui32 dist2dSQ(const int3 & o) const
  110. {
  111. const si32 dx = (x - o.x);
  112. const si32 dy = (y - o.y);
  113. return (ui32)(dx*dx) + (ui32)(dy*dy);
  114. }
  115. //returns distance on Oxy plane (z coord is not used)
  116. double dist2d(const int3 & o) const
  117. {
  118. return std::sqrt((double)dist2dSQ(o));
  119. }
  120. //manhattan distance used for patrol radius (z coord is not used)
  121. double mandist2d(const int3 & o) const
  122. {
  123. return abs(o.x - x) + abs(o.y - y);
  124. }
  125. //chebyshev distance used for ambient sounds (z coord is not used)
  126. double chebdist2d(const int3 & o) const
  127. {
  128. return std::max(std::abs(o.x - x), std::abs(o.y - y));
  129. }
  130. bool areNeighbours(const int3 & o) const
  131. {
  132. return (dist2dSQ(o) < 4) && (z == o.z);
  133. }
  134. //returns "(x y z)" string
  135. std::string toString() const
  136. {
  137. std::string result("(");
  138. result += boost::lexical_cast<std::string>(x); result += ' ';
  139. result += boost::lexical_cast<std::string>(y); result += ' ';
  140. result += boost::lexical_cast<std::string>(z); result += ')';
  141. return result;
  142. }
  143. bool valid() const //Should be named "isValid"?
  144. {
  145. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  146. }
  147. template <typename Handler>
  148. void serialize(Handler &h, const int version)
  149. {
  150. h & x;
  151. h & y;
  152. h & z;
  153. }
  154. static std::array<int3, 8> getDirs()
  155. {
  156. return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  157. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };
  158. }
  159. };
  160. //Why not normal function?
  161. struct ShashInt3
  162. {
  163. size_t operator()(int3 const& pos) const
  164. {
  165. size_t ret = std::hash<int>()(pos.x);
  166. vstd::hash_combine(ret, pos.y);
  167. vstd::hash_combine(ret, pos.z);
  168. return ret;
  169. }
  170. };
  171. template<typename Container>
  172. int3 findClosestTile (Container & container, int3 dest)
  173. {
  174. static_assert(std::is_same<typename Container::value_type, int3>::value,
  175. "findClosestTile requires <int3> container.");
  176. int3 result(-1, -1, -1);
  177. ui32 distance = std::numeric_limits<ui32>::max();
  178. for (const int3& tile : container)
  179. {
  180. const ui32 currentDistance = dest.dist2dSQ(tile);
  181. if (currentDistance < distance)
  182. {
  183. result = tile;
  184. distance = currentDistance;
  185. }
  186. }
  187. return result;
  188. }