CCreatureSet.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 "spells/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, int mode) const
  203. {
  204. /// Mode represent return string format
  205. /// "Pack" - 0, "A pack of" - 1, "a pack of" - 2
  206. int quantity = CCreature::getQuantityID(getStackCount(slot));
  207. if(quantity)
  208. return VLC->generaltexth->arraytxt[(174 + mode) + 3*CCreature::getQuantityID(getStackCount(slot))];
  209. return "";
  210. }
  211. std::string CCreatureSet::getArmyDescription() const
  212. {
  213. std::string text;
  214. std::vector<std::string> guards;
  215. for(auto & elem : stacks)
  216. {
  217. auto str = boost::str(boost::format("%s %s") % getRoughAmount(elem.first, 2) % getCreature(elem.first)->namePl);
  218. guards.push_back(str);
  219. }
  220. if(guards.size())
  221. {
  222. for(int i = 0; i < guards.size(); i++)
  223. {
  224. text += guards[i];
  225. if(i + 2 < guards.size())
  226. text += ", ";
  227. else if(i + 2 == guards.size())
  228. text += VLC->generaltexth->allTexts[237];
  229. }
  230. }
  231. return text;
  232. }
  233. int CCreatureSet::stacksCount() const
  234. {
  235. return stacks.size();
  236. }
  237. void CCreatureSet::setFormation(bool tight)
  238. {
  239. formation = tight;
  240. }
  241. void CCreatureSet::setStackCount(SlotID slot, TQuantity count)
  242. {
  243. assert(hasStackAtSlot(slot));
  244. assert(stacks[slot]->count + count > 0);
  245. if (VLC->modh->modules.STACK_EXP && count > stacks[slot]->count)
  246. stacks[slot]->experience *= (count / static_cast<double>(stacks[slot]->count));
  247. stacks[slot]->count = count;
  248. armyChanged();
  249. }
  250. void CCreatureSet::giveStackExp(TExpType exp)
  251. {
  252. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  253. i->second->giveStackExp(exp);
  254. }
  255. void CCreatureSet::setStackExp(SlotID slot, TExpType exp)
  256. {
  257. assert(hasStackAtSlot(slot));
  258. stacks[slot]->experience = exp;
  259. }
  260. void CCreatureSet::clear()
  261. {
  262. while(!stacks.empty())
  263. {
  264. eraseStack(stacks.begin()->first);
  265. }
  266. }
  267. const CStackInstance& CCreatureSet::getStack(SlotID slot) const
  268. {
  269. assert(hasStackAtSlot(slot));
  270. return *getStackPtr(slot);
  271. }
  272. const CStackInstance* CCreatureSet::getStackPtr(SlotID slot) const
  273. {
  274. if(hasStackAtSlot(slot))
  275. return stacks.find(slot)->second;
  276. else return nullptr;
  277. }
  278. void CCreatureSet::eraseStack(SlotID slot)
  279. {
  280. assert(hasStackAtSlot(slot));
  281. CStackInstance *toErase = detachStack(slot);
  282. vstd::clear_pointer(toErase);
  283. }
  284. bool CCreatureSet::contains(const CStackInstance *stack) const
  285. {
  286. if(!stack)
  287. return false;
  288. for(auto & elem : stacks)
  289. if(elem.second == stack)
  290. return true;
  291. return false;
  292. }
  293. SlotID CCreatureSet::findStack(const CStackInstance *stack) const
  294. {
  295. auto h = dynamic_cast<const CGHeroInstance *>(this);
  296. if (h && h->commander == stack)
  297. return SlotID::COMMANDER_SLOT_PLACEHOLDER;
  298. if(!stack)
  299. return SlotID();
  300. for(auto & elem : stacks)
  301. if(elem.second == stack)
  302. return elem.first;
  303. return SlotID();
  304. }
  305. CArmedInstance * CCreatureSet::castToArmyObj()
  306. {
  307. return dynamic_cast<CArmedInstance *>(this);
  308. }
  309. void CCreatureSet::putStack(SlotID slot, CStackInstance *stack)
  310. {
  311. assert(slot.getNum() < GameConstants::ARMY_SIZE);
  312. assert(!hasStackAtSlot(slot));
  313. stacks[slot] = stack;
  314. stack->setArmyObj(castToArmyObj());
  315. armyChanged();
  316. }
  317. void CCreatureSet::joinStack(SlotID slot, CStackInstance * stack)
  318. {
  319. const CCreature *c = getCreature(slot);
  320. assert(c == stack->type);
  321. assert(c);
  322. UNUSED(c);
  323. //TODO move stuff
  324. changeStackCount(slot, stack->count);
  325. vstd::clear_pointer(stack);
  326. }
  327. void CCreatureSet::changeStackCount(SlotID slot, TQuantity toAdd)
  328. {
  329. setStackCount(slot, getStackCount(slot) + toAdd);
  330. }
  331. CCreatureSet::CCreatureSet()
  332. {
  333. formation = false;
  334. }
  335. CCreatureSet::CCreatureSet(const CCreatureSet&)
  336. {
  337. assert(0);
  338. }
  339. CCreatureSet::~CCreatureSet()
  340. {
  341. clear();
  342. }
  343. void CCreatureSet::setToArmy(CSimpleArmy &src)
  344. {
  345. clear();
  346. while(src)
  347. {
  348. auto i = src.army.begin();
  349. assert(i->second.type);
  350. assert(i->second.count);
  351. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  352. src.army.erase(i);
  353. }
  354. }
  355. CStackInstance * CCreatureSet::detachStack(SlotID slot)
  356. {
  357. assert(hasStackAtSlot(slot));
  358. CStackInstance *ret = stacks[slot];
  359. //if(CArmedInstance *armedObj = castToArmyObj())
  360. if(ret)
  361. {
  362. ret->setArmyObj(nullptr); //detaches from current armyobj
  363. assert(!ret->armyObj); //we failed detaching?
  364. }
  365. stacks.erase(slot);
  366. armyChanged();
  367. return ret;
  368. }
  369. void CCreatureSet::setStackType(SlotID slot, const CCreature *type)
  370. {
  371. assert(hasStackAtSlot(slot));
  372. CStackInstance *s = stacks[slot];
  373. s->setType(type->idNumber);
  374. armyChanged();
  375. }
  376. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  377. {
  378. if(!allowMergingStacks)
  379. {
  380. int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
  381. std::set<const CCreature*> cresToAdd;
  382. for(auto & elem : cs.stacks)
  383. {
  384. SlotID dest = getSlotFor(elem.second->type);
  385. if(!dest.validSlot() || hasStackAtSlot(dest))
  386. cresToAdd.insert(elem.second->type);
  387. }
  388. return cresToAdd.size() <= freeSlots;
  389. }
  390. else
  391. {
  392. CCreatureSet cres;
  393. SlotID j;
  394. //get types of creatures that need their own slot
  395. for(auto & elem : cs.stacks)
  396. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  397. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  398. //cres.addToSlot(elem.first, elem.second->type->idNumber, 1, true);
  399. for(auto & elem : stacks)
  400. {
  401. if ((j = cres.getSlotFor(elem.second->type)).validSlot())
  402. cres.addToSlot(j, elem.second->type->idNumber, 1, true); //merge if possible
  403. else
  404. return false; //no place found
  405. }
  406. return true; //all stacks found their slots
  407. }
  408. }
  409. bool CCreatureSet::hasStackAtSlot(SlotID slot) const
  410. {
  411. return vstd::contains(stacks, slot);
  412. }
  413. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  414. {
  415. assert(0);
  416. return *this;
  417. }
  418. void CCreatureSet::armyChanged()
  419. {
  420. }
  421. CStackInstance::CStackInstance()
  422. : armyObj(_armyObj)
  423. {
  424. init();
  425. }
  426. CStackInstance::CStackInstance(CreatureID id, TQuantity Count)
  427. : armyObj(_armyObj)
  428. {
  429. init();
  430. setType(id);
  431. count = Count;
  432. }
  433. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  434. : armyObj(_armyObj)
  435. {
  436. init();
  437. setType(cre);
  438. count = Count;
  439. }
  440. void CStackInstance::init()
  441. {
  442. experience = 0;
  443. count = 0;
  444. type = nullptr;
  445. idRand = -1;
  446. _armyObj = nullptr;
  447. setNodeType(STACK_INSTANCE);
  448. }
  449. int CStackInstance::getQuantityID() const
  450. {
  451. return CCreature::getQuantityID(count);
  452. }
  453. int CStackInstance::getExpRank() const
  454. {
  455. if (!VLC->modh->modules.STACK_EXP)
  456. return 0;
  457. int tier = type->level;
  458. if (vstd::iswithin(tier, 1, 7))
  459. {
  460. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  461. { //exp values vary from 1st level to max exp at 11th level
  462. if (experience >= VLC->creh->expRanks[tier][i])
  463. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  464. }
  465. return 0;
  466. }
  467. else //higher tier
  468. {
  469. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  470. {
  471. if (experience >= VLC->creh->expRanks[0][i])
  472. return ++i;
  473. }
  474. return 0;
  475. }
  476. }
  477. int CStackInstance::getLevel() const
  478. {
  479. return std::max (1, (int)type->level);
  480. }
  481. si32 CStackInstance::magicResistance() const
  482. {
  483. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  484. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  485. {
  486. //resistance skill
  487. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::RESISTANCE);
  488. }
  489. vstd::amin (val, 100);
  490. return val;
  491. }
  492. void CStackInstance::giveStackExp(TExpType exp)
  493. {
  494. int level = type->level;
  495. if (!vstd::iswithin(level, 1, 7))
  496. level = 0;
  497. CCreatureHandler * creh = VLC->creh;
  498. ui32 maxExp = creh->expRanks[level].back();
  499. vstd::amin(exp, (TExpType)maxExp); //prevent exp overflow due to different types
  500. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  501. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  502. }
  503. void CStackInstance::setType(CreatureID creID)
  504. {
  505. if(creID >= 0 && creID < VLC->creh->creatures.size())
  506. setType(VLC->creh->creatures[creID]);
  507. else
  508. setType((const CCreature*)nullptr);
  509. }
  510. void CStackInstance::setType(const CCreature *c)
  511. {
  512. if(type)
  513. {
  514. detachFrom(const_cast<CCreature*>(type));
  515. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  516. experience *= VLC->creh->expAfterUpgrade / 100.0;
  517. }
  518. type = c;
  519. if(type)
  520. attachTo(const_cast<CCreature*>(type));
  521. }
  522. std::string CStackInstance::bonusToString(const Bonus *bonus, bool description) const
  523. {
  524. if(Bonus::MAGIC_RESISTANCE == bonus->type)
  525. {
  526. return "";
  527. }
  528. else
  529. {
  530. return VLC->getBth()->bonusToString(bonus, this, description);
  531. }
  532. }
  533. std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
  534. {
  535. return VLC->getBth()->bonusToGraphics(bonus);
  536. }
  537. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  538. {
  539. if(_armyObj)
  540. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  541. _armyObj = ArmyObj;
  542. if(ArmyObj)
  543. {
  544. attachTo(const_cast<CArmedInstance*>(_armyObj));
  545. }
  546. }
  547. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  548. {
  549. int quantity = getQuantityID();
  550. if (quantity)
  551. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  552. else
  553. return "";
  554. }
  555. bool CStackInstance::valid(bool allowUnrandomized) const
  556. {
  557. bool isRand = (idRand != -1);
  558. if(!isRand)
  559. {
  560. return (type && type == VLC->creh->creatures[type->idNumber]);
  561. }
  562. else
  563. return allowUnrandomized;
  564. }
  565. CStackInstance::~CStackInstance()
  566. {
  567. }
  568. std::string CStackInstance::nodeName() const
  569. {
  570. std::ostringstream oss;
  571. oss << "Stack of " << count << " of ";
  572. if(type)
  573. oss << type->namePl;
  574. else if(idRand >= 0)
  575. oss << "[no type, idRand=" << idRand << "]";
  576. else
  577. oss << "[UNDEFINED TYPE]";
  578. return oss.str();
  579. }
  580. void CStackInstance::deserializationFix()
  581. {
  582. const CCreature *backup = type;
  583. type = nullptr;
  584. setType(backup);
  585. const CArmedInstance *armyBackup = _armyObj;
  586. _armyObj = nullptr;
  587. setArmyObj(armyBackup);
  588. artDeserializationFix(this);
  589. }
  590. CreatureID CStackInstance::getCreatureID() const
  591. {
  592. if(type)
  593. return type->idNumber;
  594. else
  595. return CreatureID::NONE;
  596. }
  597. std::string CStackInstance::getName() const
  598. {
  599. return (count > 1) ? type->namePl : type->nameSing;
  600. }
  601. ui64 CStackInstance::getPower() const
  602. {
  603. assert(type);
  604. return type->AIValue * count;
  605. }
  606. ArtBearer::ArtBearer CStackInstance::bearerType() const
  607. {
  608. return ArtBearer::CREATURE;
  609. }
  610. CCommanderInstance::CCommanderInstance()
  611. {
  612. init();
  613. name = "Unnamed";
  614. }
  615. CCommanderInstance::CCommanderInstance (CreatureID id)
  616. {
  617. init();
  618. setType(id);
  619. name = "Commando"; //TODO - parse them
  620. }
  621. void CCommanderInstance::init()
  622. {
  623. alive = true;
  624. experience = 0;
  625. level = 1;
  626. count = 1;
  627. type = nullptr;
  628. idRand = -1;
  629. _armyObj = nullptr;
  630. setNodeType (CBonusSystemNode::COMMANDER);
  631. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  632. }
  633. CCommanderInstance::~CCommanderInstance()
  634. {
  635. }
  636. void CCommanderInstance::setAlive (bool Alive)
  637. {
  638. //TODO: helm of immortality
  639. alive = Alive;
  640. if (!alive)
  641. {
  642. popBonuses(Bonus::UntilCommanderKilled);
  643. }
  644. }
  645. void CCommanderInstance::giveStackExp (TExpType exp)
  646. {
  647. if (alive)
  648. experience += exp;
  649. }
  650. int CCommanderInstance::getExpRank() const
  651. {
  652. return VLC->heroh->level (experience);
  653. }
  654. int CCommanderInstance::getLevel() const
  655. {
  656. return std::max (1, getExpRank());
  657. }
  658. void CCommanderInstance::levelUp ()
  659. {
  660. level++;
  661. for (auto bonus : VLC->creh->commanderLevelPremy)
  662. { //grant all regular level-up bonuses
  663. accumulateBonus (*bonus);
  664. }
  665. }
  666. ArtBearer::ArtBearer CCommanderInstance::bearerType() const
  667. {
  668. return ArtBearer::COMMANDER;
  669. }
  670. bool CCommanderInstance::gainsLevel() const
  671. {
  672. return experience >= VLC->heroh->reqExp(level+1);
  673. }
  674. CStackBasicDescriptor::CStackBasicDescriptor()
  675. {
  676. type = nullptr;
  677. count = -1;
  678. }
  679. CStackBasicDescriptor::CStackBasicDescriptor(CreatureID id, TQuantity Count)
  680. : type (VLC->creh->creatures[id]), count(Count)
  681. {
  682. }
  683. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  684. : type(c), count(Count)
  685. {
  686. }
  687. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  688. {
  689. if(!sth.valid(true))
  690. str << "an invalid stack!";
  691. str << "stack with " << sth.count << " of ";
  692. if(sth.type)
  693. str << sth.type->namePl;
  694. else
  695. str << sth.idRand;
  696. return str;
  697. }
  698. void CSimpleArmy::clear()
  699. {
  700. army.clear();
  701. }
  702. CSimpleArmy::operator bool() const
  703. {
  704. return army.size();
  705. }
  706. bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
  707. {
  708. assert(!vstd::contains(army, slot));
  709. army[slot] = CStackBasicDescriptor(cre, count);
  710. return true;
  711. }