CCreatureSet.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. {
  337. ret->setArmyObj(nullptr); //detaches from current armyobj
  338. }
  339. assert(!ret->armyObj); //we failed detaching?
  340. stacks.erase(slot);
  341. armyChanged();
  342. return ret;
  343. }
  344. void CCreatureSet::setStackType(SlotID slot, const CCreature *type)
  345. {
  346. assert(hasStackAtSlot(slot));
  347. CStackInstance *s = stacks[slot];
  348. s->setType(type->idNumber);
  349. armyChanged();
  350. }
  351. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  352. {
  353. if(!allowMergingStacks)
  354. {
  355. int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
  356. std::set<const CCreature*> cresToAdd;
  357. for(auto & elem : cs.stacks)
  358. {
  359. SlotID dest = getSlotFor(elem.second->type);
  360. if(!dest.validSlot() || hasStackAtSlot(dest))
  361. cresToAdd.insert(elem.second->type);
  362. }
  363. return cresToAdd.size() <= freeSlots;
  364. }
  365. else
  366. {
  367. CCreatureSet cres;
  368. SlotID j;
  369. //get types of creatures that need their own slot
  370. for(auto & elem : cs.stacks)
  371. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  372. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  373. //cres.addToSlot(elem.first, elem.second->type->idNumber, 1, true);
  374. for(auto & elem : stacks)
  375. {
  376. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  377. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  378. else
  379. return false; //no place found
  380. }
  381. return true; //all stacks found their slots
  382. }
  383. }
  384. bool CCreatureSet::hasStackAtSlot(SlotID slot) const
  385. {
  386. return vstd::contains(stacks, slot);
  387. }
  388. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  389. {
  390. assert(0);
  391. return *this;
  392. }
  393. void CCreatureSet::armyChanged()
  394. {
  395. }
  396. CStackInstance::CStackInstance()
  397. : armyObj(_armyObj)
  398. {
  399. init();
  400. }
  401. CStackInstance::CStackInstance(CreatureID id, TQuantity Count)
  402. : armyObj(_armyObj)
  403. {
  404. init();
  405. setType(id);
  406. count = Count;
  407. }
  408. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  409. : armyObj(_armyObj)
  410. {
  411. init();
  412. setType(cre);
  413. count = Count;
  414. }
  415. void CStackInstance::init()
  416. {
  417. experience = 0;
  418. count = 0;
  419. type = nullptr;
  420. idRand = -1;
  421. _armyObj = nullptr;
  422. setNodeType(STACK_INSTANCE);
  423. }
  424. int CStackInstance::getQuantityID() const
  425. {
  426. return CCreature::getQuantityID(count);
  427. }
  428. int CStackInstance::getExpRank() const
  429. {
  430. if (!VLC->modh->modules.STACK_EXP)
  431. return 0;
  432. int tier = type->level;
  433. if (vstd::iswithin(tier, 1, 7))
  434. {
  435. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  436. { //exp values vary from 1st level to max exp at 11th level
  437. if (experience >= VLC->creh->expRanks[tier][i])
  438. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  439. }
  440. return 0;
  441. }
  442. else //higher tier
  443. {
  444. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  445. {
  446. if (experience >= VLC->creh->expRanks[0][i])
  447. return ++i;
  448. }
  449. return 0;
  450. }
  451. }
  452. int CStackInstance::getLevel() const
  453. {
  454. return std::max (1, (int)type->level);
  455. }
  456. si32 CStackInstance::magicResistance() const
  457. {
  458. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  459. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  460. {
  461. //resistance skill
  462. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::RESISTANCE);
  463. }
  464. vstd::amin (val, 100);
  465. return val;
  466. }
  467. void CStackInstance::giveStackExp(TExpType exp)
  468. {
  469. int level = type->level;
  470. if (!vstd::iswithin(level, 1, 7))
  471. level = 0;
  472. CCreatureHandler * creh = VLC->creh;
  473. ui32 maxExp = creh->expRanks[level].back();
  474. vstd::amin(exp, (TExpType)maxExp); //prevent exp overflow due to different types
  475. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  476. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  477. }
  478. void CStackInstance::setType(CreatureID creID)
  479. {
  480. if(creID >= 0 && creID < VLC->creh->creatures.size())
  481. setType(VLC->creh->creatures[creID]);
  482. else
  483. setType((const CCreature*)nullptr);
  484. }
  485. void CStackInstance::setType(const CCreature *c)
  486. {
  487. if(type)
  488. {
  489. detachFrom(const_cast<CCreature*>(type));
  490. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  491. experience *= VLC->creh->expAfterUpgrade / 100.0;
  492. }
  493. type = c;
  494. if(type)
  495. attachTo(const_cast<CCreature*>(type));
  496. }
  497. std::string CStackInstance::bonusToString(const Bonus *bonus, bool description) const
  498. {
  499. if(Bonus::MAGIC_RESISTANCE == bonus->type)
  500. {
  501. return "";
  502. }
  503. else
  504. {
  505. return VLC->getBth()->bonusToString(bonus, this, description);
  506. }
  507. }
  508. std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
  509. {
  510. return VLC->getBth()->bonusToGraphics(bonus);
  511. }
  512. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  513. {
  514. if(_armyObj)
  515. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  516. _armyObj = ArmyObj;
  517. if(ArmyObj)
  518. {
  519. attachTo(const_cast<CArmedInstance*>(_armyObj));
  520. }
  521. }
  522. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  523. {
  524. int quantity = getQuantityID();
  525. if (quantity)
  526. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  527. else
  528. return "";
  529. }
  530. bool CStackInstance::valid(bool allowUnrandomized) const
  531. {
  532. bool isRand = (idRand != -1);
  533. if(!isRand)
  534. {
  535. return (type && type == VLC->creh->creatures[type->idNumber]);
  536. }
  537. else
  538. return allowUnrandomized;
  539. }
  540. CStackInstance::~CStackInstance()
  541. {
  542. }
  543. std::string CStackInstance::nodeName() const
  544. {
  545. std::ostringstream oss;
  546. oss << "Stack of " << count << " of ";
  547. if(type)
  548. oss << type->namePl;
  549. else if(idRand >= 0)
  550. oss << "[no type, idRand=" << idRand << "]";
  551. else
  552. oss << "[UNDEFINED TYPE]";
  553. return oss.str();
  554. }
  555. void CStackInstance::deserializationFix()
  556. {
  557. const CCreature *backup = type;
  558. type = nullptr;
  559. setType(backup);
  560. const CArmedInstance *armyBackup = _armyObj;
  561. _armyObj = nullptr;
  562. setArmyObj(armyBackup);
  563. artDeserializationFix(this);
  564. }
  565. CreatureID CStackInstance::getCreatureID() const
  566. {
  567. if(type)
  568. return type->idNumber;
  569. else
  570. return CreatureID::NONE;
  571. }
  572. std::string CStackInstance::getName() const
  573. {
  574. return (count > 1) ? type->namePl : type->nameSing;
  575. }
  576. ui64 CStackInstance::getPower() const
  577. {
  578. assert(type);
  579. return type->AIValue * count;
  580. }
  581. ArtBearer::ArtBearer CStackInstance::bearerType() const
  582. {
  583. return ArtBearer::CREATURE;
  584. }
  585. CCommanderInstance::CCommanderInstance()
  586. {
  587. init();
  588. name = "Unnamed";
  589. }
  590. CCommanderInstance::CCommanderInstance (CreatureID id)
  591. {
  592. init();
  593. setType(id);
  594. name = "Commando"; //TODO - parse them
  595. }
  596. void CCommanderInstance::init()
  597. {
  598. alive = true;
  599. experience = 0;
  600. level = 1;
  601. count = 1;
  602. type = nullptr;
  603. idRand = -1;
  604. _armyObj = nullptr;
  605. setNodeType (CBonusSystemNode::COMMANDER);
  606. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  607. }
  608. CCommanderInstance::~CCommanderInstance()
  609. {
  610. }
  611. void CCommanderInstance::setAlive (bool Alive)
  612. {
  613. //TODO: helm of immortality
  614. alive = Alive;
  615. if (!alive)
  616. {
  617. getBonusList().remove_if (Bonus::UntilCommanderKilled);
  618. }
  619. }
  620. void CCommanderInstance::giveStackExp (TExpType exp)
  621. {
  622. if (alive)
  623. experience += exp;
  624. }
  625. int CCommanderInstance::getExpRank() const
  626. {
  627. return VLC->heroh->level (experience);
  628. }
  629. int CCommanderInstance::getLevel() const
  630. {
  631. return std::max (1, getExpRank());
  632. }
  633. void CCommanderInstance::levelUp ()
  634. {
  635. level++;
  636. for (auto bonus : VLC->creh->commanderLevelPremy)
  637. { //grant all regular level-up bonuses
  638. accumulateBonus (*bonus);
  639. }
  640. }
  641. ArtBearer::ArtBearer CCommanderInstance::bearerType() const
  642. {
  643. return ArtBearer::COMMANDER;
  644. }
  645. bool CCommanderInstance::gainsLevel() const
  646. {
  647. return experience >= VLC->heroh->reqExp(level+1);
  648. }
  649. CStackBasicDescriptor::CStackBasicDescriptor()
  650. {
  651. type = nullptr;
  652. count = -1;
  653. }
  654. CStackBasicDescriptor::CStackBasicDescriptor(CreatureID id, TQuantity Count)
  655. : type (VLC->creh->creatures[id]), count(Count)
  656. {
  657. }
  658. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  659. : type(c), count(Count)
  660. {
  661. }
  662. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  663. {
  664. if(!sth.valid(true))
  665. str << "an invalid stack!";
  666. str << "stack with " << sth.count << " of ";
  667. if(sth.type)
  668. str << sth.type->namePl;
  669. else
  670. str << sth.idRand;
  671. return str;
  672. }
  673. void CSimpleArmy::clear()
  674. {
  675. army.clear();
  676. }
  677. CSimpleArmy::operator bool() const
  678. {
  679. return army.size();
  680. }
  681. bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
  682. {
  683. assert(!vstd::contains(army, slot));
  684. army[slot] = CStackBasicDescriptor(cre, count);
  685. return true;
  686. }