int3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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) const //returns -1 if no slot available
  13. {
  14. for(std::map<si32,std::pair<ui32,si32> >::const_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()>0;
  37. }
  38. void sweep()
  39. {
  40. for(std::map<si32,std::pair<ui32,si32> >::iterator i=slots.begin(); i!=slots.end(); ++i)
  41. {
  42. if(!i->second.second)
  43. {
  44. slots.erase(i);
  45. sweep();
  46. break;
  47. }
  48. }
  49. }
  50. };
  51. class int3
  52. {
  53. public:
  54. si32 x,y,z;
  55. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  56. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  57. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  58. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  59. ~int3() {} // d-tor - does nothing
  60. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  61. {return int3(x+i.x,y+i.y,z+i.z);}
  62. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  63. {return int3(x+i,y+i,z+i);}
  64. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  65. {return int3(x-i.x,y-i.y,z-i.z);}
  66. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  67. {return int3(x-i,y-i,z-i);}
  68. inline int3 operator-() const //returns opposite position
  69. {return int3(-x,-y,-z);}
  70. inline double dist2d(const int3 other) const //distance (z coord is not used)
  71. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  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 void operator-=(const int3 & i)
  85. {
  86. x-=i.x;
  87. y-=i.y;
  88. z-=i.z;
  89. }
  90. inline void operator-=(const si32 & i)
  91. {
  92. x+=i;
  93. y+=i;
  94. z+=i;
  95. }
  96. inline bool operator==(const int3 & i) const
  97. {return (x==i.x) && (y==i.y) && (z==i.z);}
  98. inline bool operator!=(const int3 & i) const
  99. {return !(*this==i);}
  100. inline bool operator<(const int3 & i) const
  101. {
  102. if (z<i.z)
  103. return true;
  104. if (z>i.z)
  105. return false;
  106. if (y<i.y)
  107. return true;
  108. if (y>i.y)
  109. return false;
  110. if (x<i.x)
  111. return true;
  112. if (x>i.x)
  113. return false;
  114. return false;
  115. }
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & x & y & z;
  119. }
  120. };
  121. inline std::istream & operator>>(std::istream & str, int3 & dest)
  122. {
  123. str>>dest.x>>dest.y>>dest.z;
  124. return str;
  125. }
  126. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  127. {
  128. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  129. }
  130. #endif // __INT3_H__