CCreatureSet.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. #include "StdInc.h"
  2. #include "CCreatureSet.h"
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include "CModHandler.h"
  6. #include "mapObjects/CGHeroInstance.h"
  7. #include "IGameCallback.h"
  8. #include "CGameState.h"
  9. #include "CGeneralTextHandler.h"
  10. #include "CSpellHandler.h"
  11. #include "CHeroHandler.h"
  12. #include "IBonusTypeHandler.h"
  13. /*
  14. * CCreatureSet.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. const CStackInstance &CCreatureSet::operator[](SlotID slot) const
  23. {
  24. auto i = stacks.find(slot);
  25. if (i != stacks.end())
  26. return *i->second;
  27. else
  28. throw std::runtime_error("That slot is empty!");
  29. }
  30. const CCreature* CCreatureSet::getCreature(SlotID slot) const
  31. {
  32. auto i = stacks.find(slot);
  33. if (i != stacks.end())
  34. return i->second->type;
  35. else
  36. return nullptr;
  37. }
  38. bool CCreatureSet::setCreature(SlotID slot, CreatureID type, TQuantity quantity) /*slots 0 to 6 */
  39. {
  40. if(!slot.validSlot())
  41. {
  42. logGlobal->errorStream() << "Cannot set slot " << slot;
  43. return false;
  44. }
  45. if(!quantity)
  46. {
  47. logGlobal->warnStream() << "Using set creature to delete stack?";
  48. eraseStack(slot);
  49. return true;
  50. }
  51. if(hasStackAtSlot(slot)) //remove old creature
  52. eraseStack(slot);
  53. putStack(slot, new CStackInstance(type, quantity));
  54. return true;
  55. }
  56. SlotID CCreatureSet::getSlotFor(CreatureID creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  57. {
  58. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  59. }
  60. SlotID CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  61. {
  62. assert(c->valid());
  63. for(auto & elem : stacks)
  64. {
  65. assert(elem.second->type->valid());
  66. if(elem.second->type == c)
  67. {
  68. return elem.first; //if there is already such creature we return its slot id
  69. }
  70. }
  71. return getFreeSlot(slotsAmount);
  72. }
  73. SlotID CCreatureSet::getFreeSlot(ui32 slotsAmount/*=ARMY_SIZE*/) const
  74. {
  75. for(ui32 i=0; i<slotsAmount; i++)
  76. {
  77. if(!vstd::contains(stacks, SlotID(i)))
  78. {
  79. return SlotID(i); //return first free slot
  80. }
  81. }
  82. return SlotID(); //no slot available
  83. }
  84. int CCreatureSet::getStackCount(SlotID slot) const
  85. {
  86. auto i = stacks.find(slot);
  87. if (i != stacks.end())
  88. return i->second->count;
  89. else
  90. return 0; //TODO? consider issuing a warning
  91. }
  92. TExpType CCreatureSet::getStackExperience(SlotID slot) const
  93. {
  94. auto i = stacks.find(slot);
  95. if (i != stacks.end())
  96. return i->second->experience;
  97. else
  98. return 0; //TODO? consider issuing a warning
  99. }
  100. bool CCreatureSet::mergableStacks(std::pair<SlotID, SlotID> &out, SlotID preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  101. {
  102. //try to match creature to our preferred stack
  103. if(preferable.validSlot() && vstd::contains(stacks, preferable))
  104. {
  105. const CCreature *cr = stacks.find(preferable)->second->type;
  106. for(auto & elem : stacks)
  107. {
  108. if(cr == elem.second->type && elem.first != preferable)
  109. {
  110. out.first = preferable;
  111. out.second = elem.first;
  112. return true;
  113. }
  114. }
  115. }
  116. for(auto i=stacks.begin(); i!=stacks.end(); ++i)
  117. {
  118. for(auto & elem : stacks)
  119. {
  120. if(i->second->type == elem.second->type && i->first != elem.first)
  121. {
  122. out.first = i->first;
  123. out.second = elem.first;
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. void CCreatureSet::sweep()
  131. {
  132. for(auto i=stacks.begin(); i!=stacks.end(); ++i)
  133. {
  134. if(!i->second->count)
  135. {
  136. stacks.erase(i);
  137. sweep();
  138. break;
  139. }
  140. }
  141. }
  142. void CCreatureSet::addToSlot(SlotID slot, CreatureID cre, TQuantity count, bool allowMerging/* = true*/)
  143. {
  144. const CCreature *c = VLC->creh->creatures[cre];
  145. if(!hasStackAtSlot(slot))
  146. {
  147. setCreature(slot, cre, count);
  148. }
  149. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  150. {
  151. setStackCount(slot, getStackCount(slot) + count);
  152. }
  153. else
  154. {
  155. logGlobal->errorStream() << "Failed adding to slot!";
  156. }
  157. }
  158. void CCreatureSet::addToSlot(SlotID slot, CStackInstance *stack, bool allowMerging/* = true*/)
  159. {
  160. assert(stack->valid(true));
  161. if(!hasStackAtSlot(slot))
  162. {
  163. putStack(slot, stack);
  164. }
  165. else if(allowMerging && stack->type == getCreature(slot))
  166. {
  167. joinStack(slot, stack);
  168. }
  169. else
  170. {
  171. logGlobal->errorStream() << "Cannot add to slot " << slot << " stack " << *stack;
  172. }
  173. }
  174. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  175. {
  176. for(auto & elem : stacks)
  177. {
  178. if(!elem.second->valid(allowUnrandomized))
  179. return false;
  180. }
  181. return true;
  182. }
  183. bool CCreatureSet::slotEmpty(SlotID slot) const
  184. {
  185. return !hasStackAtSlot(slot);
  186. }
  187. bool CCreatureSet::needsLastStack() const
  188. {
  189. return false;
  190. }
  191. ui64 CCreatureSet::getArmyStrength() const
  192. {
  193. ui64 ret = 0;
  194. for(auto & elem : stacks)
  195. ret += elem.second->getPower();
  196. return ret;
  197. }
  198. ui64 CCreatureSet::getPower (SlotID slot) const
  199. {
  200. return getStack(slot).getPower();
  201. }
  202. std::string CCreatureSet::getRoughAmount (SlotID slot) const
  203. {
  204. int quantity = CCreature::getQuantityID(getStackCount(slot));
  205. if (quantity)
  206. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  207. return "";
  208. }
  209. int CCreatureSet::stacksCount() const
  210. {
  211. return stacks.size();
  212. }
  213. void CCreatureSet::setFormation(bool tight)
  214. {
  215. formation = tight;
  216. }
  217. void CCreatureSet::setStackCount(SlotID slot, TQuantity count)
  218. {
  219. assert(hasStackAtSlot(slot));
  220. assert(stacks[slot]->count + count > 0);
  221. if (VLC->modh->modules.STACK_EXP && count > stacks[slot]->count)
  222. stacks[slot]->experience *= (count / static_cast<double>(stacks[slot]->count));
  223. stacks[slot]->count = count;
  224. armyChanged();
  225. }
  226. void CCreatureSet::giveStackExp(TExpType exp)
  227. {
  228. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  229. i->second->giveStackExp(exp);
  230. }
  231. void CCreatureSet::setStackExp(SlotID slot, TExpType exp)
  232. {
  233. assert(hasStackAtSlot(slot));
  234. stacks[slot]->experience = exp;
  235. }
  236. void CCreatureSet::clear()
  237. {
  238. while(!stacks.empty())
  239. {
  240. eraseStack(stacks.begin()->first);
  241. }
  242. }
  243. const CStackInstance& CCreatureSet::getStack(SlotID slot) const
  244. {
  245. assert(hasStackAtSlot(slot));
  246. return *getStackPtr(slot);
  247. }
  248. const CStackInstance* CCreatureSet::getStackPtr(SlotID slot) const
  249. {
  250. if(hasStackAtSlot(slot))
  251. return stacks.find(slot)->second;
  252. else return nullptr;
  253. }
  254. void CCreatureSet::eraseStack(SlotID slot)
  255. {
  256. assert(hasStackAtSlot(slot));
  257. CStackInstance *toErase = detachStack(slot);
  258. vstd::clear_pointer(toErase);
  259. }
  260. bool CCreatureSet::contains(const CStackInstance *stack) const
  261. {
  262. if(!stack)
  263. return false;
  264. for(auto & elem : stacks)
  265. if(elem.second == stack)
  266. return true;
  267. return false;
  268. }
  269. SlotID CCreatureSet::findStack(const CStackInstance *stack) const
  270. {
  271. auto h = dynamic_cast<const CGHeroInstance *>(this);
  272. if (h && h->commander == stack)
  273. return SlotID::COMMANDER_SLOT_PLACEHOLDER;
  274. if(!stack)
  275. return SlotID();
  276. for(auto & elem : stacks)
  277. if(elem.second == stack)
  278. return elem.first;
  279. return SlotID();
  280. }
  281. CArmedInstance * CCreatureSet::castToArmyObj()
  282. {
  283. return dynamic_cast<CArmedInstance *>(this);
  284. }
  285. void CCreatureSet::putStack(SlotID slot, CStackInstance *stack)
  286. {
  287. assert(slot.getNum() < GameConstants::ARMY_SIZE);
  288. assert(!hasStackAtSlot(slot));
  289. stacks[slot] = stack;
  290. stack->setArmyObj(castToArmyObj());
  291. armyChanged();
  292. }
  293. void CCreatureSet::joinStack(SlotID slot, CStackInstance * stack)
  294. {
  295. const CCreature *c = getCreature(slot);
  296. assert(c == stack->type);
  297. assert(c);
  298. UNUSED(c);
  299. //TODO move stuff
  300. changeStackCount(slot, stack->count);
  301. vstd::clear_pointer(stack);
  302. }
  303. void CCreatureSet::changeStackCount(SlotID slot, TQuantity toAdd)
  304. {
  305. setStackCount(slot, getStackCount(slot) + toAdd);
  306. }
  307. CCreatureSet::CCreatureSet()
  308. {
  309. formation = false;
  310. }
  311. CCreatureSet::CCreatureSet(const CCreatureSet&)
  312. {
  313. assert(0);
  314. }
  315. CCreatureSet::~CCreatureSet()
  316. {
  317. clear();
  318. }
  319. void CCreatureSet::setToArmy(CSimpleArmy &src)
  320. {
  321. clear();
  322. while(src)
  323. {
  324. auto i = src.army.begin();
  325. assert(i->second.type);
  326. assert(i->second.count);
  327. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  328. src.army.erase(i);
  329. }
  330. }
  331. CStackInstance * CCreatureSet::detachStack(SlotID slot)
  332. {
  333. assert(hasStackAtSlot(slot));
  334. CStackInstance *ret = stacks[slot];
  335. //if(CArmedInstance *armedObj = castToArmyObj())
  336. if(ret)
  337. {
  338. ret->setArmyObj(nullptr); //detaches from current armyobj
  339. assert(!ret->armyObj); //we failed detaching?
  340. }
  341. stacks.erase(slot);
  342. armyChanged();
  343. return ret;
  344. }
  345. void CCreatureSet::setStackType(SlotID slot, const CCreature *type)
  346. {
  347. assert(hasStackAtSlot(slot));
  348. CStackInstance *s = stacks[slot];
  349. s->setType(type->idNumber);
  350. armyChanged();
  351. }
  352. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  353. {
  354. if(!allowMergingStacks)
  355. {
  356. int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
  357. std::set<const CCreature*> cresToAdd;
  358. for(auto & elem : cs.stacks)
  359. {
  360. SlotID dest = getSlotFor(elem.second->type);
  361. if(!dest.validSlot() || hasStackAtSlot(dest))
  362. cresToAdd.insert(elem.second->type);
  363. }
  364. return cresToAdd.size() <= freeSlots;
  365. }
  366. else
  367. {
  368. CCreatureSet cres;
  369. SlotID j;
  370. //get types of creatures that need their own slot
  371. for(auto & elem : cs.stacks)
  372. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  373. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  374. //cres.addToSlot(elem.first, elem.second->type->idNumber, 1, true);
  375. for(auto & elem : stacks)
  376. {
  377. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  378. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  379. else
  380. return false; //no place found
  381. }
  382. return true; //all stacks found their slots
  383. }
  384. }
  385. bool CCreatureSet::hasStackAtSlot(SlotID slot) const
  386. {
  387. return vstd::contains(stacks, slot);
  388. }
  389. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  390. {
  391. assert(0);
  392. return *this;
  393. }
  394. void CCreatureSet::armyChanged()
  395. {
  396. }
  397. CStackInstance::CStackInstance()
  398. : armyObj(_armyObj)
  399. {
  400. init();
  401. }
  402. CStackInstance::CStackInstance(CreatureID id, TQuantity Count)
  403. : armyObj(_armyObj)
  404. {
  405. init();
  406. setType(id);
  407. count = Count;
  408. }
  409. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  410. : armyObj(_armyObj)
  411. {
  412. init();
  413. setType(cre);
  414. count = Count;
  415. }
  416. void CStackInstance::init()
  417. {
  418. experience = 0;
  419. count = 0;
  420. type = nullptr;
  421. idRand = -1;
  422. _armyObj = nullptr;
  423. setNodeType(STACK_INSTANCE);
  424. }
  425. int CStackInstance::getQuantityID() const
  426. {
  427. return CCreature::getQuantityID(count);
  428. }
  429. int CStackInstance::getExpRank() const
  430. {
  431. if (!VLC->modh->modules.STACK_EXP)
  432. return 0;
  433. int tier = type->level;
  434. if (vstd::iswithin(tier, 1, 7))
  435. {
  436. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  437. { //exp values vary from 1st level to max exp at 11th level
  438. if (experience >= VLC->creh->expRanks[tier][i])
  439. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  440. }
  441. return 0;
  442. }
  443. else //higher tier
  444. {
  445. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  446. {
  447. if (experience >= VLC->creh->expRanks[0][i])
  448. return ++i;
  449. }
  450. return 0;
  451. }
  452. }
  453. int CStackInstance::getLevel() const
  454. {
  455. return std::max (1, (int)type->level);
  456. }
  457. si32 CStackInstance::magicResistance() const
  458. {
  459. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  460. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  461. {
  462. //resistance skill
  463. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::RESISTANCE);
  464. }
  465. vstd::amin (val, 100);
  466. return val;
  467. }
  468. void CStackInstance::giveStackExp(TExpType exp)
  469. {
  470. int level = type->level;
  471. if (!vstd::iswithin(level, 1, 7))
  472. level = 0;
  473. CCreatureHandler * creh = VLC->creh;
  474. ui32 maxExp = creh->expRanks[level].back();
  475. vstd::amin(exp, (TExpType)maxExp); //prevent exp overflow due to different types
  476. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  477. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  478. }
  479. void CStackInstance::setType(CreatureID creID)
  480. {
  481. if(creID >= 0 && creID < VLC->creh->creatures.size())
  482. setType(VLC->creh->creatures[creID]);
  483. else
  484. setType((const CCreature*)nullptr);
  485. }
  486. void CStackInstance::setType(const CCreature *c)
  487. {
  488. if(type)
  489. {
  490. detachFrom(const_cast<CCreature*>(type));
  491. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  492. experience *= VLC->creh->expAfterUpgrade / 100.0;
  493. }
  494. type = c;
  495. if(type)
  496. attachTo(const_cast<CCreature*>(type));
  497. }
  498. std::string CStackInstance::bonusToString(const Bonus *bonus, bool description) const
  499. {
  500. if(Bonus::MAGIC_RESISTANCE == bonus->type)
  501. {
  502. return "";
  503. }
  504. else
  505. {
  506. return VLC->getBth()->bonusToString(bonus, this, description);
  507. }
  508. }
  509. std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
  510. {
  511. return VLC->getBth()->bonusToGraphics(bonus);
  512. }
  513. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  514. {
  515. if(_armyObj)
  516. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  517. _armyObj = ArmyObj;
  518. if(ArmyObj)
  519. {
  520. attachTo(const_cast<CArmedInstance*>(_armyObj));
  521. }
  522. }
  523. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  524. {
  525. int quantity = getQuantityID();
  526. if (quantity)
  527. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  528. else
  529. return "";
  530. }
  531. bool CStackInstance::valid(bool allowUnrandomized) const
  532. {
  533. bool isRand = (idRand != -1);
  534. if(!isRand)
  535. {
  536. return (type && type == VLC->creh->creatures[type->idNumber]);
  537. }
  538. else
  539. return allowUnrandomized;
  540. }
  541. CStackInstance::~CStackInstance()
  542. {
  543. }
  544. std::string CStackInstance::nodeName() const
  545. {
  546. std::ostringstream oss;
  547. oss << "Stack of " << count << " of ";
  548. if(type)
  549. oss << type->namePl;
  550. else if(idRand >= 0)
  551. oss << "[no type, idRand=" << idRand << "]";
  552. else
  553. oss << "[UNDEFINED TYPE]";
  554. return oss.str();
  555. }
  556. void CStackInstance::deserializationFix()
  557. {
  558. const CCreature *backup = type;
  559. type = nullptr;
  560. setType(backup);
  561. const CArmedInstance *armyBackup = _armyObj;
  562. _armyObj = nullptr;
  563. setArmyObj(armyBackup);
  564. artDeserializationFix(this);
  565. }
  566. CreatureID CStackInstance::getCreatureID() const
  567. {
  568. if(type)
  569. return type->idNumber;
  570. else
  571. return CreatureID::NONE;
  572. }
  573. std::string CStackInstance::getName() const
  574. {
  575. return (count > 1) ? type->namePl : type->nameSing;
  576. }
  577. ui64 CStackInstance::getPower() const
  578. {
  579. assert(type);
  580. return type->AIValue * count;
  581. }
  582. ArtBearer::ArtBearer CStackInstance::bearerType() const
  583. {
  584. return ArtBearer::CREATURE;
  585. }
  586. CCommanderInstance::CCommanderInstance()
  587. {
  588. init();
  589. name = "Unnamed";
  590. }
  591. CCommanderInstance::CCommanderInstance (CreatureID id)
  592. {
  593. init();
  594. setType(id);
  595. name = "Commando"; //TODO - parse them
  596. }
  597. void CCommanderInstance::init()
  598. {
  599. alive = true;
  600. experience = 0;
  601. level = 1;
  602. count = 1;
  603. type = nullptr;
  604. idRand = -1;
  605. _armyObj = nullptr;
  606. setNodeType (CBonusSystemNode::COMMANDER);
  607. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  608. }
  609. CCommanderInstance::~CCommanderInstance()
  610. {
  611. }
  612. void CCommanderInstance::setAlive (bool Alive)
  613. {
  614. //TODO: helm of immortality
  615. alive = Alive;
  616. if (!alive)
  617. {
  618. getBonusList().remove_if (Bonus::UntilCommanderKilled);
  619. }
  620. }
  621. void CCommanderInstance::giveStackExp (TExpType exp)
  622. {
  623. if (alive)
  624. experience += exp;
  625. }
  626. int CCommanderInstance::getExpRank() const
  627. {
  628. return VLC->heroh->level (experience);
  629. }
  630. int CCommanderInstance::getLevel() const
  631. {
  632. return std::max (1, getExpRank());
  633. }
  634. void CCommanderInstance::levelUp ()
  635. {
  636. level++;
  637. for (auto bonus : VLC->creh->commanderLevelPremy)
  638. { //grant all regular level-up bonuses
  639. accumulateBonus (*bonus);
  640. }
  641. }
  642. ArtBearer::ArtBearer CCommanderInstance::bearerType() const
  643. {
  644. return ArtBearer::COMMANDER;
  645. }
  646. bool CCommanderInstance::gainsLevel() const
  647. {
  648. return experience >= VLC->heroh->reqExp(level+1);
  649. }
  650. CStackBasicDescriptor::CStackBasicDescriptor()
  651. {
  652. type = nullptr;
  653. count = -1;
  654. }
  655. CStackBasicDescriptor::CStackBasicDescriptor(CreatureID id, TQuantity Count)
  656. : type (VLC->creh->creatures[id]), count(Count)
  657. {
  658. }
  659. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  660. : type(c), count(Count)
  661. {
  662. }
  663. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  664. {
  665. if(!sth.valid(true))
  666. str << "an invalid stack!";
  667. str << "stack with " << sth.count << " of ";
  668. if(sth.type)
  669. str << sth.type->namePl;
  670. else
  671. str << sth.idRand;
  672. return str;
  673. }
  674. void CSimpleArmy::clear()
  675. {
  676. army.clear();
  677. }
  678. CSimpleArmy::operator bool() const
  679. {
  680. return army.size();
  681. }
  682. bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
  683. {
  684. assert(!vstd::contains(army, slot));
  685. army[slot] = CStackBasicDescriptor(cre, count);
  686. return true;
  687. }