CCreatureSet.cpp 17 KB

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