int3.h 5.3 KB

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