int3.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef __INT3_H__
  2. #define __INT3_H__
  3. #include <map>
  4. #include <vector>
  5. #include <cmath>
  6. /*
  7. * int3.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CCreature;
  16. class CCreatureSet //seven combined creatures
  17. {
  18. public:
  19. std::map<si32,std::pair<ui32,si32> > slots; //slots[slot_id]=> pair(creature_id,creature_quantity)
  20. bool formation; //false - wide, true - tight
  21. si32 getSlotFor(ui32 creature, ui32 slotsAmount=7) const //returns -1 if no slot available
  22. {
  23. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  24. {
  25. if(i->second.first == creature)
  26. {
  27. return i->first; //if there is already such creature we return its slot id
  28. }
  29. }
  30. for(ui32 i=0; i<slotsAmount; i++)
  31. {
  32. if(slots.find(i) == slots.end())
  33. {
  34. return i; //return first free slot
  35. }
  36. }
  37. return -1; //no slot available
  38. }
  39. template <typename Handler> void serialize(Handler &h, const int version)
  40. {
  41. h & slots & formation;
  42. }
  43. operator bool() const
  44. {
  45. return slots.size()>0;
  46. }
  47. void sweep()
  48. {
  49. for(std::map<si32,std::pair<ui32,si32> >::iterator i=slots.begin(); i!=slots.end(); ++i)
  50. {
  51. if(!i->second.second)
  52. {
  53. slots.erase(i);
  54. sweep();
  55. break;
  56. }
  57. }
  58. }
  59. };
  60. class int3
  61. {
  62. public:
  63. si32 x,y,z;
  64. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  65. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  66. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  67. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  68. ~int3() {} // d-tor - does nothing
  69. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  70. {return int3(x+i.x,y+i.y,z+i.z);}
  71. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  72. {return int3(x+i,y+i,z+i);}
  73. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  74. {return int3(x-i.x,y-i.y,z-i.z);}
  75. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  76. {return int3(x-i,y-i,z-i);}
  77. inline int3 operator-() const //returns opposite position
  78. {return int3(-x,-y,-z);}
  79. inline double dist2d(const int3 other) const //distance (z coord is not used)
  80. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  81. inline void operator+=(const int3 & i)
  82. {
  83. x+=i.x;
  84. y+=i.y;
  85. z+=i.z;
  86. }
  87. inline void operator+=(const si32 & i)
  88. {
  89. x+=i;
  90. y+=i;
  91. z+=i;
  92. }
  93. inline void operator-=(const int3 & i)
  94. {
  95. x-=i.x;
  96. y-=i.y;
  97. z-=i.z;
  98. }
  99. inline void operator-=(const si32 & i)
  100. {
  101. x+=i;
  102. y+=i;
  103. z+=i;
  104. }
  105. inline bool operator==(const int3 & i) const
  106. {return (x==i.x) && (y==i.y) && (z==i.z);}
  107. inline bool operator!=(const int3 & i) const
  108. {return !(*this==i);}
  109. inline bool operator<(const int3 & i) const
  110. {
  111. if (z<i.z)
  112. return true;
  113. if (z>i.z)
  114. return false;
  115. if (y<i.y)
  116. return true;
  117. if (y>i.y)
  118. return false;
  119. if (x<i.x)
  120. return true;
  121. if (x>i.x)
  122. return false;
  123. return false;
  124. }
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & x & y & z;
  128. }
  129. };
  130. inline std::istream & operator>>(std::istream & str, int3 & dest)
  131. {
  132. str>>dest.x>>dest.y>>dest.z;
  133. return str;
  134. }
  135. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  136. {
  137. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  138. }
  139. #endif // __INT3_H__