int3.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. operator bool() const
  85. {
  86. return slots.size() > 0;
  87. }
  88. void sweep()
  89. {
  90. for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
  91. {
  92. if(!i->second.second)
  93. {
  94. slots.erase(i);
  95. sweep();
  96. break;
  97. }
  98. }
  99. }
  100. };
  101. class int3
  102. {
  103. public:
  104. si32 x,y,z;
  105. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  106. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  107. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  108. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  109. ~int3() {} // d-tor - does nothing
  110. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased 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 increased by given numer
  113. {return int3(x+i,y+i,z+i);}
  114. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  115. {return int3(x-i.x,y-i.y,z-i.z);}
  116. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  117. {return int3(x-i,y-i,z-i);}
  118. inline int3 operator-() const //returns opposite position
  119. {return int3(-x,-y,-z);}
  120. inline double dist2d(const int3 other) const //distance (z coord is not used)
  121. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  122. inline void operator+=(const int3 & i)
  123. {
  124. x+=i.x;
  125. y+=i.y;
  126. z+=i.z;
  127. }
  128. inline void operator+=(const si32 & i)
  129. {
  130. x+=i;
  131. y+=i;
  132. z+=i;
  133. }
  134. inline void operator-=(const int3 & i)
  135. {
  136. x-=i.x;
  137. y-=i.y;
  138. z-=i.z;
  139. }
  140. inline void operator-=(const si32 & i)
  141. {
  142. x+=i;
  143. y+=i;
  144. z+=i;
  145. }
  146. inline bool operator==(const int3 & i) const
  147. {return (x==i.x) && (y==i.y) && (z==i.z);}
  148. inline bool operator!=(const int3 & i) const
  149. {return !(*this==i);}
  150. inline bool operator<(const int3 & i) const
  151. {
  152. if (z<i.z)
  153. return true;
  154. if (z>i.z)
  155. return false;
  156. if (y<i.y)
  157. return true;
  158. if (y>i.y)
  159. return false;
  160. if (x<i.x)
  161. return true;
  162. if (x>i.x)
  163. return false;
  164. return false;
  165. }
  166. template <typename Handler> void serialize(Handler &h, const int version)
  167. {
  168. h & x & y & z;
  169. }
  170. };
  171. inline std::istream & operator>>(std::istream & str, int3 & dest)
  172. {
  173. str>>dest.x>>dest.y>>dest.z;
  174. return str;
  175. }
  176. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  177. {
  178. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  179. }
  180. #endif // __INT3_H__