int3.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. {
  16. if(i->second.first == creature)
  17. {
  18. return i->first; //if there is already such creature we return its slot id
  19. }
  20. }
  21. for(ui32 i=0; i<slotsAmount; i++)
  22. {
  23. if(slots.find(i) == slots.end())
  24. {
  25. return i; //return first free slot
  26. }
  27. }
  28. return -1; //no slot available
  29. }
  30. template <typename Handler> void serialize(Handler &h, const int version)
  31. {
  32. h & slots & formation;
  33. }
  34. operator bool() const
  35. {
  36. return slots.size();
  37. }
  38. };
  39. class int3
  40. {
  41. public:
  42. si32 x,y,z;
  43. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  44. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  45. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  46. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  47. ~int3() {} // d-tor - does nothing
  48. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  49. {return int3(x+i.x,y+i.y,z+i.z);}
  50. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  51. {return int3(x+i,y+i,z+i);}
  52. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  53. {return int3(x-i.x,y-i.y,z-i.z);}
  54. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  55. {return int3(x-i,y-i,z-i);}
  56. inline int3 operator-() const //returns opposite position
  57. {return int3(-x,-y,-z);}
  58. inline double dist2d(const int3 other) const //distance (z coord is not used)
  59. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  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 void operator-=(const int3 & i)
  73. {
  74. x-=i.x;
  75. y-=i.y;
  76. z-=i.z;
  77. }
  78. inline void operator-=(const si32 & i)
  79. {
  80. x+=i;
  81. y+=i;
  82. z+=i;
  83. }
  84. inline bool operator==(const int3 & i) const
  85. {return (x==i.x) && (y==i.y) && (z==i.z);}
  86. inline bool operator!=(const int3 & i) const
  87. {return !(*this==i);}
  88. inline bool operator<(const int3 & i) const
  89. {
  90. if (z<i.z)
  91. return true;
  92. if (z>i.z)
  93. return false;
  94. if (y<i.y)
  95. return true;
  96. if (y>i.y)
  97. return false;
  98. if (x<i.x)
  99. return true;
  100. if (x>i.x)
  101. return false;
  102. return false;
  103. }
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & x & y & z;
  107. }
  108. };
  109. inline std::istream & operator>>(std::istream & str, int3 & dest)
  110. {
  111. str>>dest.x>>dest.y>>dest.z;
  112. return str;
  113. }
  114. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  115. {
  116. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  117. }
  118. #endif // __INT3_H__