int3.h 5.3 KB

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