CCreatureSet.cpp 19 KB

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