int3.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef INT3_H
  2. #define INT3_H
  3. #include <map>
  4. #include <vector>
  5. #include <cmath>
  6. class CCreature;
  7. class CCreatureSet //seven combined creatures
  8. {
  9. public:
  10. std::map<si32,std::pair<ui32,si32> > slots; //slots[slot_id]=> pair(creature_id,creature_quantity)
  11. bool formation; //false - wide, true - tight
  12. si32 getSlotFor(ui32 creature, ui32 slotsAmount=7) //returns -1 if no slot available
  13. {
  14. for(std::map<si32,std::pair<ui32,si32> >::iterator i=slots.begin(); i!=slots.end(); i++)
  15. if(i->second.first == creature)
  16. return i->first; //if there is already such creature we return its slot id
  17. for(si32 i=0; i<slotsAmount; i++)
  18. if(slots.find(i) == slots.end())
  19. return i; //return first free slot
  20. return -1; //no slot available
  21. }
  22. template <typename Handler> void serialize(Handler &h, const int version)
  23. {
  24. h & slots & formation;
  25. }
  26. };
  27. class int3
  28. {
  29. public:
  30. si32 x,y,z;
  31. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  32. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  33. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  34. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  35. inline ~int3(){} // d-tor - does nothing
  36. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  37. {return int3(x+i.x,y+i.y,z+i.z);}
  38. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  39. {return int3(x+i,y+i,z+i);}
  40. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  41. {return int3(x-i.x,y-i.y,z-i.z);}
  42. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  43. {return int3(x-i,y-i,z-i);}
  44. inline int3 operator-() const //returns opposite position
  45. {return int3(-x,-y,-z);}
  46. inline double dist2d(const int3 other) const //distance (z coord is not used)
  47. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  48. inline void operator+=(const int3 & i)
  49. {
  50. x+=i.x;
  51. y+=i.y;
  52. z+=i.z;
  53. }
  54. inline void operator+=(const si32 & i)
  55. {
  56. x+=i;
  57. y+=i;
  58. z+=i;
  59. }
  60. inline void operator-=(const int3 & i)
  61. {
  62. x-=i.x;
  63. y-=i.y;
  64. z-=i.z;
  65. }
  66. inline void operator-=(const si32 & i)
  67. {
  68. x+=i;
  69. y+=i;
  70. z+=i;
  71. }
  72. inline bool operator==(const int3 & i) const
  73. {return (x==i.x) && (y==i.y) && (z==i.z);}
  74. inline bool operator!=(const int3 & i) const
  75. {return !(*this==i);}
  76. inline bool operator<(const int3 & i) const
  77. {
  78. if (z<i.z)
  79. return true;
  80. if (z>i.z)
  81. return false;
  82. if (y<i.y)
  83. return true;
  84. if (y>i.y)
  85. return false;
  86. if (x<i.x)
  87. return true;
  88. if (x>i.x)
  89. return false;
  90. return false;
  91. }
  92. template <typename Handler> void serialize(Handler &h, const int version)
  93. {
  94. h & x & y & z;
  95. }
  96. };
  97. inline std::istream & operator>>(std::istream & str, int3 & dest)
  98. {
  99. str>>dest.x>>dest.y>>dest.z;
  100. return str;
  101. }
  102. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  103. {
  104. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  105. }
  106. #endif //INT3_H