CCreatureSet.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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
  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. return true;
  38. }
  39. if(vstd::contains(slots, slot)) //remove old creature
  40. eraseStack(slot);
  41. putStack(slot, new CStackInstance(type, quantity));
  42. return true;
  43. }
  44. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  45. {
  46. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  47. }
  48. TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  49. {
  50. assert(c->valid());
  51. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  52. {
  53. assert(i->second->type->valid());
  54. if(i->second->type == c)
  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. int CCreatureSet::getStackCount(TSlot slot) const
  69. {
  70. TSlots::const_iterator i = slots.find(slot);
  71. if (i != slots.end())
  72. return i->second->count;
  73. else
  74. return 0; //TODO? consider issuing a warning
  75. }
  76. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  77. {
  78. //try to match creature to our preferred stack
  79. if(preferable >= 0 && vstd::contains(slots, preferable))
  80. {
  81. const CCreature *cr = slots.find(preferable)->second->type;
  82. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  83. {
  84. if(cr == j->second->type && j->first != preferable)
  85. {
  86. out.first = preferable;
  87. out.second = j->first;
  88. return true;
  89. }
  90. }
  91. }
  92. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  93. {
  94. for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
  95. {
  96. if(i->second->type == j->second->type && i->first != j->first)
  97. {
  98. out.first = i->first;
  99. out.second = j->first;
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. void CCreatureSet::sweep()
  107. {
  108. for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
  109. {
  110. if(!i->second->count)
  111. {
  112. slots.erase(i);
  113. sweep();
  114. break;
  115. }
  116. }
  117. }
  118. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  119. {
  120. const CCreature *c = VLC->creh->creatures[cre];
  121. if(!vstd::contains(slots, slot))
  122. {
  123. setCreature(slot, cre, count);
  124. }
  125. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  126. {
  127. setStackCount(slot, getStackCount(slot) + count);
  128. }
  129. else
  130. {
  131. tlog1 << "Failed adding to slot!\n";
  132. }
  133. }
  134. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  135. {
  136. assert(stack->valid(true));
  137. if(!vstd::contains(slots, slot))
  138. {
  139. putStack(slot, stack);
  140. }
  141. else if(allowMerging && stack->type == getCreature(slot))
  142. {
  143. joinStack(slot, stack);
  144. }
  145. else
  146. {
  147. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  148. }
  149. }
  150. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  151. {
  152. for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
  153. {
  154. if(!i->second->valid(allowUnrandomized))
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool CCreatureSet::slotEmpty(TSlot slot) const
  160. {
  161. return !vstd::contains(slots, slot);
  162. }
  163. bool CCreatureSet::needsLastStack() const
  164. {
  165. return false;
  166. }
  167. int CCreatureSet::getArmyStrength() const
  168. {
  169. int ret = 0;
  170. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  171. ret += i->second->type->AIValue * i->second->count;
  172. return ret;
  173. }
  174. ui64 CCreatureSet::getPower (TSlot slot) const
  175. {
  176. return getCreature(slot)->AIValue * getStackCount(slot);
  177. }
  178. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  179. {
  180. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  181. }
  182. int CCreatureSet::stacksCount() const
  183. {
  184. return slots.size();
  185. }
  186. void CCreatureSet::setFormation(bool tight)
  187. {
  188. formation = tight;
  189. }
  190. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  191. {
  192. assert(vstd::contains(slots, slot));
  193. assert(count > 0);
  194. slots[slot]->count = count;
  195. }
  196. void CCreatureSet::clear()
  197. {
  198. while(!slots.empty())
  199. {
  200. eraseStack(slots.begin()->first);
  201. }
  202. }
  203. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  204. {
  205. assert(vstd::contains(slots, slot));
  206. return *slots.find(slot)->second;
  207. }
  208. void CCreatureSet::eraseStack(TSlot slot)
  209. {
  210. assert(vstd::contains(slots, slot));
  211. delNull(slots[slot]);
  212. slots.erase(slot);
  213. }
  214. bool CCreatureSet::contains(const CStackInstance *stack) const
  215. {
  216. if(!stack)
  217. return false;
  218. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  219. if(i->second == stack)
  220. return true;
  221. return false;
  222. }
  223. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  224. {
  225. if(!stack)
  226. return -1;
  227. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  228. if(i->second == stack)
  229. return i->first;
  230. return -1;
  231. }
  232. CArmedInstance * CCreatureSet::castToArmyObj()
  233. {
  234. return dynamic_cast<CArmedInstance *>(this);
  235. }
  236. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  237. {
  238. assert(!vstd::contains(slots, slot));
  239. slots[slot] = stack;
  240. stack->setArmyObj(castToArmyObj());
  241. }
  242. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  243. {
  244. const CCreature *c = getCreature(slot);
  245. assert(c == stack->type);
  246. assert(c);
  247. //TODO move stuff
  248. changeStackCount(slot, stack->count);
  249. delNull(stack);
  250. }
  251. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  252. {
  253. setStackCount(slot, getStackCount(slot) + toAdd);
  254. }
  255. CCreatureSet::CCreatureSet()
  256. {
  257. formation = false;
  258. }
  259. CCreatureSet::~CCreatureSet()
  260. {
  261. clear();
  262. }
  263. void CCreatureSet::setToArmy(CCreatureSet &src)
  264. {
  265. clear();
  266. while(src)
  267. {
  268. TSlots::iterator i = src.slots.begin();
  269. assert(i->second->type);
  270. assert(i->second->valid(false));
  271. assert(i->second->armyObj == NULL);
  272. putStack(i->first, i->second);
  273. src.slots.erase(i);
  274. }
  275. }
  276. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  277. {
  278. assert(vstd::contains(slots, slot));
  279. CStackInstance *ret = slots[slot];
  280. if(CArmedInstance *armedObj = castToArmyObj())
  281. ret->detachFrom(armedObj);
  282. slots.erase(slot);
  283. return ret;
  284. }
  285. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  286. {
  287. assert(vstd::contains(slots, slot));
  288. CStackInstance *s = slots[slot];
  289. s->setType(type->idNumber);
  290. }
  291. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs) const
  292. {
  293. std::set<const CCreature*> cres;
  294. //get types of creatures that need their own slot
  295. for(TSlots::const_iterator i = cs.slots.begin(); i != cs.slots.end(); i++)
  296. cres.insert(i->second->type);
  297. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  298. cres.insert(i->second->type);
  299. return cres.size() <= ARMY_SIZE;
  300. }
  301. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  302. {
  303. return vstd::contains(slots, slot);
  304. }
  305. CStackInstance::CStackInstance()
  306. : armyObj(_armyObj)
  307. {
  308. init();
  309. }
  310. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  311. : armyObj(_armyObj)
  312. {
  313. init();
  314. setType(id);
  315. count = Count;
  316. }
  317. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  318. : armyObj(_armyObj)
  319. {
  320. init();
  321. type = cre;
  322. count = Count;
  323. }
  324. void CStackInstance::init()
  325. {
  326. experience = 0;
  327. count = 0;
  328. type = NULL;
  329. idRand = -1;
  330. _armyObj = NULL;
  331. nodeType = STACK;
  332. }
  333. int CStackInstance::getQuantityID() const
  334. {
  335. return CCreature::getQuantityID(count);
  336. }
  337. void CStackInstance::setType(int creID)
  338. {
  339. setType(VLC->creh->creatures[creID]);
  340. }
  341. void CStackInstance::setType(const CCreature *c)
  342. {
  343. if(type)
  344. detachFrom(const_cast<CCreature*>(type));
  345. type = c;
  346. attachTo(const_cast<CCreature*>(type));
  347. }
  348. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  349. {
  350. if(_armyObj)
  351. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  352. if(ArmyObj)
  353. {
  354. _armyObj = ArmyObj;
  355. attachTo(const_cast<CArmedInstance*>(_armyObj));
  356. }
  357. }
  358. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  359. // {
  360. // out.insert(type);
  361. //
  362. // if(source && source != this) //we should be root, if not - do not inherit anything
  363. // return;
  364. //
  365. // if(armyObj)
  366. // out.insert(armyObj);
  367. // else
  368. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  369. // }
  370. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  371. {
  372. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  373. }
  374. bool CStackInstance::valid(bool allowUnrandomized) const
  375. {
  376. bool isRand = (idRand != -1);
  377. if(!isRand)
  378. {
  379. return (type && type == VLC->creh->creatures[type->idNumber]);
  380. }
  381. else
  382. return allowUnrandomized;
  383. }
  384. CStackBasicDescriptor::CStackBasicDescriptor()
  385. {
  386. type = NULL;
  387. count = -1;
  388. }
  389. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  390. : type (VLC->creh->creatures[id]), count(Count)
  391. {
  392. }
  393. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  394. {
  395. if(!sth.valid(true))
  396. str << "an invalid stack!";
  397. str << "stack with " << sth.count << " of ";
  398. if(sth.type)
  399. str << sth.type->namePl;
  400. else
  401. str << sth.idRand;
  402. return str;
  403. }