int3.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. /*
  3. * int3.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  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. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  17. inline int3(const si32 X, const si32 Y, const si32 Z):x(X),y(Y),z(Z){}; //c-tor
  18. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  19. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  20. ~int3() {} // d-tor - does nothing
  21. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  22. {return int3(x+i.x,y+i.y,z+i.z);}
  23. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  24. {return int3(x+i,y+i,z+i);}
  25. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  26. {return int3(x-i.x,y-i.y,z-i.z);}
  27. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  28. {return int3(x-i,y-i,z-i);}
  29. inline int3 operator-() const //returns opposite position
  30. {return int3(-x,-y,-z);}
  31. inline double dist2d(const int3 &other) const //distance (z coord is not used)
  32. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  33. inline bool areNeighbours(const int3 &other) const
  34. {return dist2d(other) < 2. && z == other.z;}
  35. inline void operator+=(const int3 & i)
  36. {
  37. x+=i.x;
  38. y+=i.y;
  39. z+=i.z;
  40. }
  41. inline void operator+=(const si32 & i)
  42. {
  43. x+=i;
  44. y+=i;
  45. z+=i;
  46. }
  47. inline void operator-=(const int3 & i)
  48. {
  49. x-=i.x;
  50. y-=i.y;
  51. z-=i.z;
  52. }
  53. inline void operator-=(const si32 & i)
  54. {
  55. x+=i;
  56. y+=i;
  57. z+=i;
  58. }
  59. inline bool operator==(const int3 & i) const
  60. {return (x==i.x) && (y==i.y) && (z==i.z);}
  61. inline bool operator!=(const int3 & i) const
  62. {return !(*this==i);}
  63. inline bool operator<(const int3 & i) const
  64. {
  65. if (z<i.z)
  66. return true;
  67. if (z>i.z)
  68. return false;
  69. if (y<i.y)
  70. return true;
  71. if (y>i.y)
  72. return false;
  73. if (x<i.x)
  74. return true;
  75. if (x>i.x)
  76. return false;
  77. return false;
  78. }
  79. inline std::string operator ()() const
  80. {
  81. return "(" + boost::lexical_cast<std::string>(x) +
  82. " " + boost::lexical_cast<std::string>(y) +
  83. " " + boost::lexical_cast<std::string>(z) + ")";
  84. }
  85. template <typename Handler> void serialize(Handler &h, const int version)
  86. {
  87. h & x & y & z;
  88. }
  89. };
  90. inline std::istream & operator>>(std::istream & str, int3 & dest)
  91. {
  92. str>>dest.x>>dest.y>>dest.z;
  93. return str;
  94. }
  95. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  96. {
  97. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  98. }
  99. struct ShashInt3
  100. {
  101. size_t operator()(int3 const& pos) const
  102. {
  103. size_t ret = ::boost::hash<int>()(pos.x);
  104. boost::hash_combine(ret, pos.y);
  105. boost::hash_combine(ret, pos.z);
  106. return ret;
  107. }
  108. };