int3.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. bool setCreature (si32 slot, ui32 type, si32 quantity) //slots 1 to 7
  22. {
  23. slots.find(slot)->second = std::pair<ui32,si32>(type, quantity); //brutal force
  24. if (slots.size() > 7) return false;
  25. else return true;
  26. }
  27. si32 getSlotFor(ui32 creature, ui32 slotsAmount=7) const //returns -1 if no slot available
  28. {
  29. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  30. {
  31. if(i->second.first == creature)
  32. {
  33. return i->first; //if there is already such creature we return its slot id
  34. }
  35. }
  36. for(ui32 i=0; i<slotsAmount; i++)
  37. {
  38. if(slots.find(i) == slots.end())
  39. {
  40. return i; //return first free slot
  41. }
  42. }
  43. return -1; //no slot available
  44. }
  45. template <typename Handler> void serialize(Handler &h, const int version)
  46. {
  47. h & slots & formation;
  48. }
  49. operator bool() const
  50. {
  51. return slots.size()>0;
  52. }
  53. void sweep()
  54. {
  55. for(std::map<si32,std::pair<ui32,si32> >::iterator i=slots.begin(); i!=slots.end(); ++i)
  56. {
  57. if(!i->second.second)
  58. {
  59. slots.erase(i);
  60. sweep();
  61. break;
  62. }
  63. }
  64. }
  65. };
  66. class int3
  67. {
  68. public:
  69. si32 x,y,z;
  70. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  71. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  72. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  73. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  74. ~int3() {} // d-tor - does nothing
  75. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  76. {return int3(x+i.x,y+i.y,z+i.z);}
  77. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  78. {return int3(x+i,y+i,z+i);}
  79. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  80. {return int3(x-i.x,y-i.y,z-i.z);}
  81. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  82. {return int3(x-i,y-i,z-i);}
  83. inline int3 operator-() const //returns opposite position
  84. {return int3(-x,-y,-z);}
  85. inline double dist2d(const int3 other) const //distance (z coord is not used)
  86. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  87. inline void operator+=(const int3 & i)
  88. {
  89. x+=i.x;
  90. y+=i.y;
  91. z+=i.z;
  92. }
  93. inline void operator+=(const si32 & i)
  94. {
  95. x+=i;
  96. y+=i;
  97. z+=i;
  98. }
  99. inline void operator-=(const int3 & i)
  100. {
  101. x-=i.x;
  102. y-=i.y;
  103. z-=i.z;
  104. }
  105. inline void operator-=(const si32 & i)
  106. {
  107. x+=i;
  108. y+=i;
  109. z+=i;
  110. }
  111. inline bool operator==(const int3 & i) const
  112. {return (x==i.x) && (y==i.y) && (z==i.z);}
  113. inline bool operator!=(const int3 & i) const
  114. {return !(*this==i);}
  115. inline bool operator<(const int3 & i) const
  116. {
  117. if (z<i.z)
  118. return true;
  119. if (z>i.z)
  120. return false;
  121. if (y<i.y)
  122. return true;
  123. if (y>i.y)
  124. return false;
  125. if (x<i.x)
  126. return true;
  127. if (x>i.x)
  128. return false;
  129. return false;
  130. }
  131. template <typename Handler> void serialize(Handler &h, const int version)
  132. {
  133. h & x & y & z;
  134. }
  135. };
  136. inline std::istream & operator>>(std::istream & str, int3 & dest)
  137. {
  138. str>>dest.x>>dest.y>>dest.z;
  139. return str;
  140. }
  141. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  142. {
  143. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  144. }
  145. #endif // __INT3_H__