CCreatureSet.cpp 17 KB

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