CCreatureSet.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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(const CCreatureSet&)
  260. {
  261. assert(0);
  262. }
  263. CCreatureSet::~CCreatureSet()
  264. {
  265. clear();
  266. }
  267. void CCreatureSet::setToArmy(CCreatureSet &src)
  268. {
  269. clear();
  270. while(src)
  271. {
  272. TSlots::iterator i = src.slots.begin();
  273. assert(i->second->type);
  274. assert(i->second->valid(false));
  275. assert(i->second->armyObj == NULL);
  276. putStack(i->first, i->second);
  277. src.slots.erase(i);
  278. }
  279. }
  280. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  281. {
  282. assert(vstd::contains(slots, slot));
  283. CStackInstance *ret = slots[slot];
  284. if(CArmedInstance *armedObj = castToArmyObj())
  285. {
  286. ret->setArmyObj(NULL); //detaches from current armyobj
  287. }
  288. assert(!ret->armyObj); //we failed detaching?
  289. slots.erase(slot);
  290. return ret;
  291. }
  292. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  293. {
  294. assert(vstd::contains(slots, slot));
  295. CStackInstance *s = slots[slot];
  296. s->setType(type->idNumber);
  297. }
  298. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs) const
  299. {
  300. std::set<const CCreature*> cres;
  301. //get types of creatures that need their own slot
  302. for(TSlots::const_iterator i = cs.slots.begin(); i != cs.slots.end(); i++)
  303. cres.insert(i->second->type);
  304. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  305. cres.insert(i->second->type);
  306. return cres.size() <= ARMY_SIZE;
  307. }
  308. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  309. {
  310. return vstd::contains(slots, slot);
  311. }
  312. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  313. {
  314. assert(0);
  315. return cs;
  316. }
  317. CStackInstance::CStackInstance()
  318. : armyObj(_armyObj)
  319. {
  320. init();
  321. }
  322. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  323. : armyObj(_armyObj)
  324. {
  325. init();
  326. setType(id);
  327. count = Count;
  328. }
  329. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  330. : armyObj(_armyObj)
  331. {
  332. init();
  333. type = cre;
  334. count = Count;
  335. }
  336. void CStackInstance::init()
  337. {
  338. experience = 0;
  339. count = 0;
  340. type = NULL;
  341. idRand = -1;
  342. _armyObj = NULL;
  343. nodeType = STACK;
  344. }
  345. int CStackInstance::getQuantityID() const
  346. {
  347. return CCreature::getQuantityID(count);
  348. }
  349. void CStackInstance::setType(int creID)
  350. {
  351. setType(VLC->creh->creatures[creID]);
  352. }
  353. void CStackInstance::setType(const CCreature *c)
  354. {
  355. if(type)
  356. detachFrom(const_cast<CCreature*>(type));
  357. type = c;
  358. attachTo(const_cast<CCreature*>(type));
  359. }
  360. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  361. {
  362. if(_armyObj)
  363. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  364. _armyObj = ArmyObj;
  365. if(ArmyObj)
  366. {
  367. attachTo(const_cast<CArmedInstance*>(_armyObj));
  368. }
  369. }
  370. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  371. // {
  372. // out.insert(type);
  373. //
  374. // if(source && source != this) //we should be root, if not - do not inherit anything
  375. // return;
  376. //
  377. // if(armyObj)
  378. // out.insert(armyObj);
  379. // else
  380. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  381. // }
  382. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  383. {
  384. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  385. }
  386. bool CStackInstance::valid(bool allowUnrandomized) const
  387. {
  388. bool isRand = (idRand != -1);
  389. if(!isRand)
  390. {
  391. return (type && type == VLC->creh->creatures[type->idNumber]);
  392. }
  393. else
  394. return allowUnrandomized;
  395. }
  396. CStackInstance::~CStackInstance()
  397. {
  398. }
  399. CStackBasicDescriptor::CStackBasicDescriptor()
  400. {
  401. type = NULL;
  402. count = -1;
  403. }
  404. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  405. : type (VLC->creh->creatures[id]), count(Count)
  406. {
  407. }
  408. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  409. : type(c), count(Count)
  410. {
  411. }
  412. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  413. {
  414. if(!sth.valid(true))
  415. str << "an invalid stack!";
  416. str << "stack with " << sth.count << " of ";
  417. if(sth.type)
  418. str << sth.type->namePl;
  419. else
  420. str << sth.idRand;
  421. return str;
  422. }