CCreatureSet.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. TQuantity 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 static_cast<int>(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 = static_cast<TExpType>(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. putStack(i->first, new CStackInstance(i->second.first, i->second.second));
  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, CreatureID type)
  370. {
  371. assert(hasStackAtSlot(slot));
  372. CStackInstance *s = stacks[slot];
  373. s->setType(type);
  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. void CCreatureSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName, const boost::optional<int> fixedSize)
  422. {
  423. if(handler.saving && stacks.empty())
  424. return;
  425. auto a = handler.enterArray(fieldName);
  426. if(handler.saving)
  427. {
  428. size_t sz = 0;
  429. for(const auto & p : stacks)
  430. vstd::amax(sz, p.first.getNum()+1);
  431. if(fixedSize)
  432. vstd::amax(sz, fixedSize.get());
  433. a.resize(sz, JsonNode::JsonType::DATA_STRUCT);
  434. for(const auto & p : stacks)
  435. {
  436. auto s = a.enterStruct(p.first.getNum());
  437. p.second->serializeJson(handler);
  438. }
  439. }
  440. else
  441. {
  442. for(size_t idx = 0; idx < a.size(); idx++)
  443. {
  444. auto s = a.enterStruct(idx);
  445. TQuantity amount = 0;
  446. handler.serializeInt("amount", amount);
  447. if(amount > 0)
  448. {
  449. CStackInstance * new_stack = new CStackInstance();
  450. new_stack->serializeJson(handler);
  451. putStack(SlotID((si32)idx), new_stack);
  452. }
  453. }
  454. }
  455. }
  456. CStackInstance::CStackInstance()
  457. : armyObj(_armyObj)
  458. {
  459. init();
  460. }
  461. CStackInstance::CStackInstance(CreatureID id, TQuantity Count)
  462. : armyObj(_armyObj)
  463. {
  464. init();
  465. setType(id);
  466. count = Count;
  467. }
  468. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  469. : armyObj(_armyObj)
  470. {
  471. init();
  472. setType(cre);
  473. count = Count;
  474. }
  475. void CStackInstance::init()
  476. {
  477. experience = 0;
  478. count = 0;
  479. type = nullptr;
  480. idRand = -1;
  481. _armyObj = nullptr;
  482. setNodeType(STACK_INSTANCE);
  483. }
  484. int CStackInstance::getQuantityID() const
  485. {
  486. return CCreature::getQuantityID(count);
  487. }
  488. int CStackInstance::getExpRank() const
  489. {
  490. if (!VLC->modh->modules.STACK_EXP)
  491. return 0;
  492. int tier = type->level;
  493. if (vstd::iswithin(tier, 1, 7))
  494. {
  495. for (int i = (int)VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  496. { //exp values vary from 1st level to max exp at 11th level
  497. if (experience >= VLC->creh->expRanks[tier][i])
  498. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  499. }
  500. return 0;
  501. }
  502. else //higher tier
  503. {
  504. for (int i = (int)VLC->creh->expRanks[0].size()-2; i >-1; --i)
  505. {
  506. if (experience >= VLC->creh->expRanks[0][i])
  507. return ++i;
  508. }
  509. return 0;
  510. }
  511. }
  512. int CStackInstance::getLevel() const
  513. {
  514. return std::max (1, (int)type->level);
  515. }
  516. si32 CStackInstance::magicResistance() const
  517. {
  518. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  519. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  520. {
  521. //resistance skill
  522. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::RESISTANCE);
  523. }
  524. vstd::amin (val, 100);
  525. return val;
  526. }
  527. void CStackInstance::giveStackExp(TExpType exp)
  528. {
  529. int level = type->level;
  530. if (!vstd::iswithin(level, 1, 7))
  531. level = 0;
  532. CCreatureHandler * creh = VLC->creh;
  533. ui32 maxExp = creh->expRanks[level].back();
  534. vstd::amin(exp, (TExpType)maxExp); //prevent exp overflow due to different types
  535. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  536. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  537. }
  538. void CStackInstance::setType(CreatureID creID)
  539. {
  540. if(creID >= 0 && creID < VLC->creh->creatures.size())
  541. setType(VLC->creh->creatures[creID]);
  542. else
  543. setType((const CCreature*)nullptr);
  544. }
  545. void CStackInstance::setType(const CCreature *c)
  546. {
  547. if(type)
  548. {
  549. detachFrom(const_cast<CCreature*>(type));
  550. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  551. experience = static_cast<TExpType>(experience * VLC->creh->expAfterUpgrade / 100.0);
  552. }
  553. CStackBasicDescriptor::setType(c);
  554. if(type)
  555. attachTo(const_cast<CCreature*>(type));
  556. }
  557. std::string CStackInstance::bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const
  558. {
  559. if(Bonus::MAGIC_RESISTANCE == bonus->type)
  560. {
  561. return "";
  562. }
  563. else
  564. {
  565. return VLC->getBth()->bonusToString(bonus, this, description);
  566. }
  567. }
  568. std::string CStackInstance::bonusToGraphics(const std::shared_ptr<Bonus>& bonus) const
  569. {
  570. return VLC->getBth()->bonusToGraphics(bonus);
  571. }
  572. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  573. {
  574. if(_armyObj)
  575. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  576. _armyObj = ArmyObj;
  577. if(ArmyObj)
  578. {
  579. attachTo(const_cast<CArmedInstance*>(_armyObj));
  580. }
  581. }
  582. std::string CStackInstance::getQuantityTXT(bool capitalized) const
  583. {
  584. int quantity = getQuantityID();
  585. if (quantity)
  586. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  587. else
  588. return "";
  589. }
  590. bool CStackInstance::valid(bool allowUnrandomized) const
  591. {
  592. bool isRand = (idRand != -1);
  593. if(!isRand)
  594. {
  595. return (type && type == VLC->creh->creatures[type->idNumber]);
  596. }
  597. else
  598. return allowUnrandomized;
  599. }
  600. CStackInstance::~CStackInstance()
  601. {
  602. }
  603. std::string CStackInstance::nodeName() const
  604. {
  605. std::ostringstream oss;
  606. oss << "Stack of " << count << " of ";
  607. if(type)
  608. oss << type->namePl;
  609. else if(idRand >= 0)
  610. oss << "[no type, idRand=" << idRand << "]";
  611. else
  612. oss << "[UNDEFINED TYPE]";
  613. return oss.str();
  614. }
  615. void CStackInstance::deserializationFix()
  616. {
  617. const CCreature *backup = type;
  618. type = nullptr;
  619. setType(backup);
  620. const CArmedInstance *armyBackup = _armyObj;
  621. _armyObj = nullptr;
  622. setArmyObj(armyBackup);
  623. artDeserializationFix(this);
  624. }
  625. CreatureID CStackInstance::getCreatureID() const
  626. {
  627. if(type)
  628. return type->idNumber;
  629. else
  630. return CreatureID::NONE;
  631. }
  632. std::string CStackInstance::getName() const
  633. {
  634. return (count > 1) ? type->namePl : type->nameSing;
  635. }
  636. ui64 CStackInstance::getPower() const
  637. {
  638. assert(type);
  639. return type->AIValue * count;
  640. }
  641. ArtBearer::ArtBearer CStackInstance::bearerType() const
  642. {
  643. return ArtBearer::CREATURE;
  644. }
  645. void CStackInstance::putArtifact(ArtifactPosition pos, CArtifactInstance * art)
  646. {
  647. assert(!getArt(pos));
  648. art->putAt(ArtifactLocation(this, pos));
  649. }
  650. void CStackInstance::serializeJson(JsonSerializeFormat & handler)
  651. {
  652. //todo: artifacts
  653. CStackBasicDescriptor::serializeJson(handler);//must be first
  654. if(handler.saving)
  655. {
  656. if(idRand > -1)
  657. {
  658. int level = (int)idRand / 2;
  659. boost::logic::tribool upgraded = (idRand % 2) > 0;
  660. handler.serializeInt("level", level, 0);
  661. handler.serializeBool("upgraded", upgraded);
  662. }
  663. }
  664. else
  665. {
  666. //type set by CStackBasicDescriptor::serializeJson
  667. if(type == nullptr)
  668. {
  669. int level = 0;
  670. bool upgraded = false;
  671. handler.serializeInt("level", level, 0);
  672. handler.serializeBool("upgraded", upgraded);
  673. idRand = level * 2 + (int)(bool)upgraded;
  674. }
  675. }
  676. }
  677. CCommanderInstance::CCommanderInstance()
  678. {
  679. init();
  680. name = "Unnamed";
  681. }
  682. CCommanderInstance::CCommanderInstance (CreatureID id)
  683. {
  684. init();
  685. setType(id);
  686. name = "Commando"; //TODO - parse them
  687. }
  688. void CCommanderInstance::init()
  689. {
  690. alive = true;
  691. experience = 0;
  692. level = 1;
  693. count = 1;
  694. type = nullptr;
  695. idRand = -1;
  696. _armyObj = nullptr;
  697. setNodeType (CBonusSystemNode::COMMANDER);
  698. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  699. }
  700. CCommanderInstance::~CCommanderInstance()
  701. {
  702. }
  703. void CCommanderInstance::setAlive (bool Alive)
  704. {
  705. //TODO: helm of immortality
  706. alive = Alive;
  707. if (!alive)
  708. {
  709. removeBonusesRecursive(Bonus::UntilCommanderKilled);
  710. }
  711. }
  712. void CCommanderInstance::giveStackExp (TExpType exp)
  713. {
  714. if (alive)
  715. experience += exp;
  716. }
  717. int CCommanderInstance::getExpRank() const
  718. {
  719. return VLC->heroh->level (experience);
  720. }
  721. int CCommanderInstance::getLevel() const
  722. {
  723. return std::max (1, getExpRank());
  724. }
  725. void CCommanderInstance::levelUp ()
  726. {
  727. level++;
  728. for (auto bonus : VLC->creh->commanderLevelPremy)
  729. { //grant all regular level-up bonuses
  730. accumulateBonus(bonus);
  731. }
  732. }
  733. ArtBearer::ArtBearer CCommanderInstance::bearerType() const
  734. {
  735. return ArtBearer::COMMANDER;
  736. }
  737. bool CCommanderInstance::gainsLevel() const
  738. {
  739. return experience >= (TExpType)VLC->heroh->reqExp(level+1);
  740. }
  741. CStackBasicDescriptor::CStackBasicDescriptor()
  742. {
  743. type = nullptr;
  744. count = -1;
  745. }
  746. CStackBasicDescriptor::CStackBasicDescriptor(CreatureID id, TQuantity Count)
  747. : type (VLC->creh->creatures[id]), count(Count)
  748. {
  749. }
  750. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  751. : type(c), count(Count)
  752. {
  753. }
  754. void CStackBasicDescriptor::setType(const CCreature * c)
  755. {
  756. type = c;
  757. }
  758. void CStackBasicDescriptor::serializeJson(JsonSerializeFormat & handler)
  759. {
  760. handler.serializeInt("amount", count);
  761. if(handler.saving)
  762. {
  763. if(type)
  764. {
  765. std::string typeName = type->identifier;
  766. handler.serializeString("type", typeName);
  767. }
  768. }
  769. else
  770. {
  771. std::string typeName("");
  772. handler.serializeString("type", typeName);
  773. if(typeName != "")
  774. setType(VLC->creh->getCreature("core", typeName));
  775. }
  776. }
  777. void CSimpleArmy::clear()
  778. {
  779. army.clear();
  780. }
  781. CSimpleArmy::operator bool() const
  782. {
  783. return army.size();
  784. }
  785. bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
  786. {
  787. assert(!vstd::contains(army, slot));
  788. army[slot] = std::make_pair(cre, count);
  789. return true;
  790. }