int3.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. };
  35. class int3
  36. {
  37. public:
  38. si32 x,y,z;
  39. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  40. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  41. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  42. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  43. ~int3() {} // d-tor - does nothing
  44. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  45. {return int3(x+i.x,y+i.y,z+i.z);}
  46. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  47. {return int3(x+i,y+i,z+i);}
  48. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased 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 decreased by given numer
  51. {return int3(x-i,y-i,z-i);}
  52. inline int3 operator-() const //returns opposite position
  53. {return int3(-x,-y,-z);}
  54. inline double dist2d(const int3 other) const //distance (z coord is not used)
  55. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  56. inline void operator+=(const int3 & i)
  57. {
  58. x+=i.x;
  59. y+=i.y;
  60. z+=i.z;
  61. }
  62. inline void operator+=(const si32 & i)
  63. {
  64. x+=i;
  65. y+=i;
  66. z+=i;
  67. }
  68. inline void operator-=(const int3 & i)
  69. {
  70. x-=i.x;
  71. y-=i.y;
  72. z-=i.z;
  73. }
  74. inline void operator-=(const si32 & i)
  75. {
  76. x+=i;
  77. y+=i;
  78. z+=i;
  79. }
  80. inline bool operator==(const int3 & i) const
  81. {return (x==i.x) && (y==i.y) && (z==i.z);}
  82. inline bool operator!=(const int3 & i) const
  83. {return !(*this==i);}
  84. inline bool operator<(const int3 & i) const
  85. {
  86. if (z<i.z)
  87. return true;
  88. if (z>i.z)
  89. return false;
  90. if (y<i.y)
  91. return true;
  92. if (y>i.y)
  93. return false;
  94. if (x<i.x)
  95. return true;
  96. if (x>i.x)
  97. return false;
  98. return false;
  99. }
  100. template <typename Handler> void serialize(Handler &h, const int version)
  101. {
  102. h & x & y & z;
  103. }
  104. };
  105. inline std::istream & operator>>(std::istream & str, int3 & dest)
  106. {
  107. str>>dest.x>>dest.y>>dest.z;
  108. return str;
  109. }
  110. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  111. {
  112. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  113. }
  114. #endif //INT3_H