CCreatureSet.cpp 20 KB

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