2
0

int3.h 3.0 KB

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