int3.h 2.6 KB

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