CCreatureSet.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #define VCMI_DLL
  2. #include "CCreatureSet.h"
  3. #include "../hch/CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include <assert.h>
  6. #include "../hch/CObjectHandler.h"
  7. #include "IGameCallback.h"
  8. #include "CGameState.h"
  9. #include "../hch/CGeneralTextHandler.h"
  10. const CStackInstance CCreatureSet::operator[](TSlot slot) const
  11. {
  12. TSlots::const_iterator i = slots.find(slot);
  13. if (i != slots.end())
  14. return i->second;
  15. else
  16. throw std::string("That slot is empty!");
  17. }
  18. const CCreature* CCreatureSet::getCreature(TSlot slot) const /*workaround of map issue */
  19. {
  20. TSlots::const_iterator i = slots.find(slot);
  21. if (i != slots.end())
  22. return i->second.type;
  23. else
  24. return NULL;
  25. }
  26. bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
  27. {
  28. slots[slot] = CStackInstance(type, quantity); //brutal force
  29. if (quantity == 0)
  30. slots.erase(slot);
  31. if (slots.size() > ARMY_SIZE)
  32. return false;
  33. else
  34. return true;
  35. }
  36. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  37. {
  38. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  39. {
  40. if(i->second.type->idNumber == creature)
  41. {
  42. return i->first; //if there is already such creature we return its slot id
  43. }
  44. }
  45. for(ui32 i=0; i<slotsAmount; i++)
  46. {
  47. if(slots.find(i) == slots.end())
  48. {
  49. return i; //return first free slot
  50. }
  51. }
  52. return -1; //no slot available
  53. }
  54. int CCreatureSet::getAmount(TSlot slot) const
  55. {
  56. TSlots::const_iterator i = slots.find(slot);
  57. if (i != slots.end())
  58. return i->second.count;
  59. else
  60. return 0; //TODO? consider issuing a warning
  61. }
  62. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  63. {
  64. //try to match creature to our preferred stack
  65. if(preferable >= 0 && vstd::contains(slots, preferable))
  66. {
  67. const CCreature *cr = slots.find(preferable)->second.type;
  68. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  69. {
  70. if(cr == j->second.type && j->first != preferable)
  71. {
  72. out.first = preferable;
  73. out.second = j->first;
  74. return true;
  75. }
  76. }
  77. }
  78. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  79. {
  80. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  81. {
  82. if(i->second.type == j->second.type && i->first != j->first)
  83. {
  84. out.first = i->first;
  85. out.second = j->first;
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. void CCreatureSet::sweep()
  93. {
  94. for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
  95. {
  96. if(!i->second.count)
  97. {
  98. slots.erase(i);
  99. sweep();
  100. break;
  101. }
  102. }
  103. }
  104. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  105. {
  106. assert(slot >= 0);
  107. const CCreature *c = VLC->creh->creatures[cre];
  108. assert(!vstd::contains(slots, slot) || slots[slot].type == c && allowMerging); //that slot was empty or contained same type creature
  109. slots[slot].type = c;
  110. slots[slot].count += count;
  111. //TODO
  112. const CArmedInstance *armedObj = dynamic_cast<const CArmedInstance *>(this);
  113. if(armedObj && !slots[slot].armyObj)
  114. slots[slot].armyObj = armedObj;
  115. }
  116. void CCreatureSet::addToSlot(TSlot slot, const CStackInstance &stack, bool allowMerging/* = true*/)
  117. {
  118. assert(stack.type == VLC->creh->creatures[stack.type->idNumber]);
  119. addToSlot(slot, stack.type->idNumber, stack.count, allowMerging );
  120. }
  121. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  122. {
  123. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  124. {
  125. bool isRand = (i->second.idRand != -1);
  126. if(!isRand)
  127. {
  128. assert(i->second.type);
  129. assert(i->second.type == VLC->creh->creatures[i->second.type->idNumber]);
  130. }
  131. else
  132. assert(allowUnrandomized);
  133. }
  134. return true;
  135. }
  136. bool CCreatureSet::slotEmpty(TSlot slot) const
  137. {
  138. return !vstd::contains(slots, slot);
  139. }
  140. bool CCreatureSet::needsLastStack() const
  141. {
  142. return false;
  143. }
  144. int CCreatureSet::getArmyStrength() const
  145. {
  146. int ret = 0;
  147. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  148. ret += i->second.type->AIValue * i->second.count;
  149. return ret;
  150. }
  151. ui64 CCreatureSet::getPower (TSlot slot) const
  152. {
  153. return getCreature(slot)->AIValue * getAmount(slot);
  154. }
  155. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  156. {
  157. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getAmount(slot))];
  158. }
  159. int CCreatureSet::stacksCount() const
  160. {
  161. return slots.size();
  162. }
  163. void CCreatureSet::addStack(TSlot slot, const CStackInstance &stack)
  164. {
  165. addToSlot(slot, stack, false);
  166. }
  167. void CCreatureSet::setFormation(bool tight)
  168. {
  169. formation = tight;
  170. }
  171. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  172. {
  173. assert(vstd::contains(slots, slot));
  174. assert(count > 0);
  175. slots[slot].count = count;
  176. }
  177. void CCreatureSet::clear()
  178. {
  179. slots.clear();
  180. }
  181. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  182. {
  183. assert(vstd::contains(slots, slot));
  184. return slots.find(slot)->second;
  185. }
  186. void CCreatureSet::eraseStack(TSlot slot)
  187. {
  188. assert(vstd::contains(slots, slot));
  189. slots.erase(slot);
  190. }
  191. bool CCreatureSet::contains(const CStackInstance *stack) const
  192. {
  193. if(!stack)
  194. return false;
  195. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  196. if(&i->second == stack)
  197. return true;
  198. return false;
  199. }
  200. CStackInstance::CStackInstance()
  201. {
  202. init();
  203. }
  204. CStackInstance::CStackInstance(TCreature id, TQuantity Count, const CArmedInstance *ArmyObj)
  205. {
  206. init();
  207. setType(id);
  208. count = Count;
  209. armyObj = ArmyObj;
  210. }
  211. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  212. {
  213. init();
  214. type = cre;
  215. count = Count;
  216. }
  217. void CStackInstance::init()
  218. {
  219. experience = 0;
  220. count = 0;
  221. type = NULL;
  222. idRand = -1;
  223. armyObj = NULL;
  224. nodeType = STACK;
  225. }
  226. int CStackInstance::getQuantityID() const
  227. {
  228. return CCreature::getQuantityID(count);
  229. }
  230. void CStackInstance::setType(int creID)
  231. {
  232. type = VLC->creh->creatures[creID];
  233. }
  234. void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  235. {
  236. out.insert(type);
  237. if(source && source != this) //we should be root, if not - do not inherit anything
  238. return;
  239. if(armyObj)
  240. out.insert(armyObj);
  241. else
  242. out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  243. }