int3.h 2.6 KB

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