int3.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. //a few typedefs for CCreatureSet
  17. typedef si32 TSlot, TQuantity;
  18. typedef ui32 TCreature;
  19. typedef std::pair<TCreature, TQuantity> TStack;
  20. typedef std::map<TSlot, TStack> TSlots;
  21. class CCreatureSet //seven combined creatures
  22. {
  23. public:
  24. TSlots slots; //slots[slot_id]=> pair(creature_id,creature_quantity)
  25. ui8 formation; //false - wide, true - tight
  26. bool setCreature (TSlot slot, TCreature type, TQuantity quantity) //slots 0 to 6
  27. {
  28. slots[slot] = TStack(type, quantity); //brutal force
  29. if (slots.size() > 7) return false;
  30. else return true;
  31. }
  32. TSlot getSlotFor(TCreature creature, ui32 slotsAmount=7) const //returns -1 if no slot available
  33. {
  34. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  35. {
  36. if(i->second.first == creature)
  37. {
  38. return i->first; //if there is already such creature we return its slot id
  39. }
  40. }
  41. for(ui32 i=0; i<slotsAmount; i++)
  42. {
  43. if(slots.find(i) == slots.end())
  44. {
  45. return i; //return first free slot
  46. }
  47. }
  48. return -1; //no slot available
  49. }
  50. bool mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable = -1) //looks for two same stacks, returns slot positions
  51. {
  52. //try to match creature to our preferred stack
  53. if(preferable >= 0 && slots.find(preferable) != slots.end())
  54. {
  55. TCreature id = slots[preferable].first;
  56. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  57. {
  58. if(id == j->second.first)
  59. {
  60. out.first = preferable;
  61. out.second = j->first;
  62. return true;
  63. }
  64. }
  65. }
  66. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  67. {
  68. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  69. {
  70. if(i->second.first == j->second.first)
  71. {
  72. out.first = i->first;
  73. out.second = j->first;
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. template <typename Handler> void serialize(Handler &h, const int version)
  81. {
  82. h & slots & formation;
  83. }
  84. void sweep()
  85. {
  86. for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
  87. {
  88. if(!i->second.second)
  89. {
  90. slots.erase(i);
  91. sweep();
  92. break;
  93. }
  94. }
  95. }
  96. };
  97. class int3
  98. {
  99. public:
  100. si32 x,y,z;
  101. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  102. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  103. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  104. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  105. ~int3() {} // d-tor - does nothing
  106. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  107. {return int3(x+i.x,y+i.y,z+i.z);}
  108. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  109. {return int3(x+i,y+i,z+i);}
  110. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  111. {return int3(x-i.x,y-i.y,z-i.z);}
  112. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  113. {return int3(x-i,y-i,z-i);}
  114. inline int3 operator-() const //returns opposite position
  115. {return int3(-x,-y,-z);}
  116. inline double dist2d(const int3 other) const //distance (z coord is not used)
  117. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  118. inline void operator+=(const int3 & i)
  119. {
  120. x+=i.x;
  121. y+=i.y;
  122. z+=i.z;
  123. }
  124. inline void operator+=(const si32 & i)
  125. {
  126. x+=i;
  127. y+=i;
  128. z+=i;
  129. }
  130. inline void operator-=(const int3 & i)
  131. {
  132. x-=i.x;
  133. y-=i.y;
  134. z-=i.z;
  135. }
  136. inline void operator-=(const si32 & i)
  137. {
  138. x+=i;
  139. y+=i;
  140. z+=i;
  141. }
  142. inline bool operator==(const int3 & i) const
  143. {return (x==i.x) && (y==i.y) && (z==i.z);}
  144. inline bool operator!=(const int3 & i) const
  145. {return !(*this==i);}
  146. inline bool operator<(const int3 & i) const
  147. {
  148. if (z<i.z)
  149. return true;
  150. if (z>i.z)
  151. return false;
  152. if (y<i.y)
  153. return true;
  154. if (y>i.y)
  155. return false;
  156. if (x<i.x)
  157. return true;
  158. if (x>i.x)
  159. return false;
  160. return false;
  161. }
  162. template <typename Handler> void serialize(Handler &h, const int version)
  163. {
  164. h & x & y & z;
  165. }
  166. };
  167. inline std::istream & operator>>(std::istream & str, int3 & dest)
  168. {
  169. str>>dest.x>>dest.y>>dest.z;
  170. return str;
  171. }
  172. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  173. {
  174. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  175. }
  176. #endif // __INT3_H__