CCreatureSet.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. if(slot > 6 || slot < 0)
  29. {
  30. tlog1 << "Cannot set slot " << slot << std::endl;
  31. return false;
  32. }
  33. if(!quantity)
  34. {
  35. tlog2 << "Using set creature to delete stack?\n";
  36. eraseStack(slot);
  37. }
  38. if(vstd::contains(slots, slot)) //remove old creature
  39. {
  40. eraseStack(slot);
  41. }
  42. CStackInstance *stack = new CStackInstance(type, quantity);
  43. stack->armyObj = castToArmyObj(); //brutal force
  44. slots[slot] = stack;
  45. }
  46. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  47. {
  48. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  49. {
  50. if(i->second->type->idNumber == creature)
  51. {
  52. return i->first; //if there is already such creature we return its slot id
  53. }
  54. }
  55. for(ui32 i=0; i<slotsAmount; i++)
  56. {
  57. if(slots.find(i) == slots.end())
  58. {
  59. return i; //return first free slot
  60. }
  61. }
  62. return -1; //no slot available
  63. }
  64. int CCreatureSet::getAmount(TSlot slot) const
  65. {
  66. TSlots::const_iterator i = slots.find(slot);
  67. if (i != slots.end())
  68. return i->second->count;
  69. else
  70. return 0; //TODO? consider issuing a warning
  71. }
  72. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  73. {
  74. //try to match creature to our preferred stack
  75. if(preferable >= 0 && vstd::contains(slots, preferable))
  76. {
  77. const CCreature *cr = slots.find(preferable)->second->type;
  78. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  79. {
  80. if(cr == j->second->type && j->first != preferable)
  81. {
  82. out.first = preferable;
  83. out.second = j->first;
  84. return true;
  85. }
  86. }
  87. }
  88. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  89. {
  90. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  91. {
  92. if(i->second->type == j->second->type && i->first != j->first)
  93. {
  94. out.first = i->first;
  95. out.second = j->first;
  96. return true;
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. void CCreatureSet::sweep()
  103. {
  104. for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
  105. {
  106. if(!i->second->count)
  107. {
  108. slots.erase(i);
  109. sweep();
  110. break;
  111. }
  112. }
  113. }
  114. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  115. {
  116. const CCreature *c = VLC->creh->creatures[cre];
  117. if(!vstd::contains(slots, slot))
  118. {
  119. setCreature(slot, cre, count);
  120. }
  121. else if(slots[slot]->type == c && allowMerging); //that slot was empty or contained same type creature
  122. {
  123. setStackCount(slot, slots[slot]->count + count);
  124. }
  125. }
  126. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  127. {
  128. assert(stack->type == VLC->creh->creatures[stack->type->idNumber]);
  129. if(!vstd::contains(slots, slot))
  130. {
  131. slots[slot] = stack;
  132. stack->setArmyObj(castToArmyObj());
  133. }
  134. else
  135. {
  136. addToSlot(slot, stack->type->idNumber, stack->count, allowMerging);
  137. }
  138. }
  139. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  140. {
  141. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  142. {
  143. bool isRand = (i->second->idRand != -1);
  144. if(!isRand)
  145. {
  146. assert(i->second->type);
  147. assert(i->second->type == VLC->creh->creatures[i->second->type->idNumber]);
  148. }
  149. else
  150. assert(allowUnrandomized);
  151. }
  152. return true;
  153. }
  154. bool CCreatureSet::slotEmpty(TSlot slot) const
  155. {
  156. return !vstd::contains(slots, slot);
  157. }
  158. bool CCreatureSet::needsLastStack() const
  159. {
  160. return false;
  161. }
  162. int CCreatureSet::getArmyStrength() const
  163. {
  164. int ret = 0;
  165. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  166. ret += i->second->type->AIValue * i->second->count;
  167. return ret;
  168. }
  169. ui64 CCreatureSet::getPower (TSlot slot) const
  170. {
  171. return getCreature(slot)->AIValue * getAmount(slot);
  172. }
  173. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  174. {
  175. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getAmount(slot))];
  176. }
  177. int CCreatureSet::stacksCount() const
  178. {
  179. return slots.size();
  180. }
  181. void CCreatureSet::addStack(TSlot slot, CStackInstance *stack)
  182. {
  183. addToSlot(slot, stack, false);
  184. }
  185. void CCreatureSet::setFormation(bool tight)
  186. {
  187. formation = tight;
  188. }
  189. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  190. {
  191. assert(vstd::contains(slots, slot));
  192. assert(count > 0);
  193. slots[slot]->count = count;
  194. }
  195. void CCreatureSet::clear()
  196. {
  197. slots.clear();
  198. }
  199. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  200. {
  201. assert(vstd::contains(slots, slot));
  202. return *slots.find(slot)->second;
  203. }
  204. void CCreatureSet::eraseStack(TSlot slot)
  205. {
  206. assert(vstd::contains(slots, slot));
  207. slots.erase(slot);
  208. }
  209. bool CCreatureSet::contains(const CStackInstance *stack) const
  210. {
  211. if(!stack)
  212. return false;
  213. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  214. if(i->second == stack)
  215. return true;
  216. return false;
  217. }
  218. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  219. {
  220. if(!stack)
  221. return -1;
  222. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  223. if(i->second == stack)
  224. return i->first;
  225. return -1;
  226. }
  227. CArmedInstance * CCreatureSet::castToArmyObj()
  228. {
  229. return dynamic_cast<CArmedInstance *>(this);
  230. }
  231. CStackInstance::CStackInstance()
  232. {
  233. init();
  234. }
  235. CStackInstance::CStackInstance(TCreature id, TQuantity Count, const CArmedInstance *ArmyObj)
  236. {
  237. init();
  238. setType(id);
  239. setArmyObj(ArmyObj);
  240. count = Count;
  241. }
  242. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  243. {
  244. init();
  245. type = cre;
  246. count = Count;
  247. }
  248. void CStackInstance::init()
  249. {
  250. experience = 0;
  251. count = 0;
  252. type = NULL;
  253. idRand = -1;
  254. armyObj = NULL;
  255. nodeType = STACK;
  256. }
  257. int CStackInstance::getQuantityID() const
  258. {
  259. return CCreature::getQuantityID(count);
  260. }
  261. void CStackInstance::setType(int creID)
  262. {
  263. if(type)
  264. detachFrom(const_cast<CCreature*>(type));
  265. type = VLC->creh->creatures[creID];
  266. attachTo(const_cast<CCreature*>(type));
  267. }
  268. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  269. {
  270. if(armyObj)
  271. detachFrom(const_cast<CArmedInstance*>(armyObj));
  272. armyObj = ArmyObj;
  273. attachTo(const_cast<CArmedInstance*>(armyObj));
  274. }
  275. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  276. // {
  277. // out.insert(type);
  278. //
  279. // if(source && source != this) //we should be root, if not - do not inherit anything
  280. // return;
  281. //
  282. // if(armyObj)
  283. // out.insert(armyObj);
  284. // else
  285. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  286. // }
  287. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  288. {
  289. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  290. }
  291. CStackBasicDescriptor::CStackBasicDescriptor()
  292. {
  293. type = NULL;
  294. count = -1;
  295. }
  296. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  297. : type (VLC->creh->creatures[id]), count(Count)
  298. {
  299. }