CCreatureSet.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. auto armyObj = castToArmyObj();
  56. bool isHypotheticArmy = armyObj ? armyObj->isHypothetic() : false;
  57. putStack(slot, new CStackInstance(type, quantity, isHypotheticArmy));
  58. return true;
  59. }
  60. SlotID CCreatureSet::getSlotFor(CreatureID creature, ui32 slotsAmount) const /*returns -1 if no slot available */
  61. {
  62. return getSlotFor(VLC->creh->objects[creature], slotsAmount);
  63. }
  64. SlotID CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount) const
  65. {
  66. assert(c->valid());
  67. for(auto & elem : stacks)
  68. {
  69. assert(elem.second->type->valid());
  70. if(elem.second->type == c)
  71. {
  72. return elem.first; //if there is already such creature we return its slot id
  73. }
  74. }
  75. return getFreeSlot(slotsAmount);
  76. }
  77. SlotID CCreatureSet::getFreeSlot(ui32 slotsAmount) const
  78. {
  79. for(ui32 i=0; i<slotsAmount; i++)
  80. {
  81. if(!vstd::contains(stacks, SlotID(i)))
  82. {
  83. return SlotID(i); //return first free slot
  84. }
  85. }
  86. return SlotID(); //no slot available
  87. }
  88. TQuantity CCreatureSet::getStackCount(SlotID slot) const
  89. {
  90. auto i = stacks.find(slot);
  91. if (i != stacks.end())
  92. return i->second->count;
  93. else
  94. return 0; //TODO? consider issuing a warning
  95. }
  96. TExpType CCreatureSet::getStackExperience(SlotID slot) const
  97. {
  98. auto i = stacks.find(slot);
  99. if (i != stacks.end())
  100. return i->second->experience;
  101. else
  102. return 0; //TODO? consider issuing a warning
  103. }
  104. bool CCreatureSet::mergableStacks(std::pair<SlotID, SlotID> &out, SlotID preferable) const /*looks for two same stacks, returns slot positions */
  105. {
  106. //try to match creature to our preferred stack
  107. if(preferable.validSlot() && vstd::contains(stacks, preferable))
  108. {
  109. const CCreature *cr = stacks.find(preferable)->second->type;
  110. for(auto & elem : stacks)
  111. {
  112. if(cr == elem.second->type && elem.first != preferable)
  113. {
  114. out.first = preferable;
  115. out.second = elem.first;
  116. return true;
  117. }
  118. }
  119. }
  120. for(auto i=stacks.begin(); i!=stacks.end(); ++i)
  121. {
  122. for(auto & elem : stacks)
  123. {
  124. if(i->second->type == elem.second->type && i->first != elem.first)
  125. {
  126. out.first = i->first;
  127. out.second = elem.first;
  128. return true;
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. void CCreatureSet::sweep()
  135. {
  136. for(auto i=stacks.begin(); i!=stacks.end(); ++i)
  137. {
  138. if(!i->second->count)
  139. {
  140. stacks.erase(i);
  141. sweep();
  142. break;
  143. }
  144. }
  145. }
  146. void CCreatureSet::addToSlot(SlotID slot, CreatureID cre, TQuantity count, bool allowMerging)
  147. {
  148. const CCreature *c = VLC->creh->objects[cre];
  149. if(!hasStackAtSlot(slot))
  150. {
  151. setCreature(slot, cre, count);
  152. }
  153. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  154. {
  155. setStackCount(slot, getStackCount(slot) + count);
  156. }
  157. else
  158. {
  159. logGlobal->error("Failed adding to slot!");
  160. }
  161. }
  162. void CCreatureSet::addToSlot(SlotID slot, CStackInstance *stack, bool allowMerging)
  163. {
  164. assert(stack->valid(true));
  165. if(!hasStackAtSlot(slot))
  166. {
  167. putStack(slot, stack);
  168. }
  169. else if(allowMerging && stack->type == getCreature(slot))
  170. {
  171. joinStack(slot, stack);
  172. }
  173. else
  174. {
  175. logGlobal->error("Cannot add to slot %d stack %s", slot.getNum(), stack->nodeName());
  176. }
  177. }
  178. bool CCreatureSet::validTypes(bool allowUnrandomized) const
  179. {
  180. for(auto & elem : stacks)
  181. {
  182. if(!elem.second->valid(allowUnrandomized))
  183. return false;
  184. }
  185. return true;
  186. }
  187. bool CCreatureSet::slotEmpty(SlotID slot) const
  188. {
  189. return !hasStackAtSlot(slot);
  190. }
  191. bool CCreatureSet::needsLastStack() const
  192. {
  193. return false;
  194. }
  195. ui64 CCreatureSet::getArmyStrength() const
  196. {
  197. ui64 ret = 0;
  198. for(auto & elem : stacks)
  199. ret += elem.second->getPower();
  200. return ret;
  201. }
  202. ui64 CCreatureSet::getPower (SlotID slot) const
  203. {
  204. return getStack(slot).getPower();
  205. }
  206. std::string CCreatureSet::getRoughAmount(SlotID slot, int mode) const
  207. {
  208. /// Mode represent return string format
  209. /// "Pack" - 0, "A pack of" - 1, "a pack of" - 2
  210. int quantity = CCreature::getQuantityID(getStackCount(slot));
  211. if(quantity)
  212. return VLC->generaltexth->arraytxt[(174 + mode) + 3*CCreature::getQuantityID(getStackCount(slot))];
  213. return "";
  214. }
  215. std::string CCreatureSet::getArmyDescription() const
  216. {
  217. std::string text;
  218. std::vector<std::string> guards;
  219. for(auto & elem : stacks)
  220. {
  221. auto str = boost::str(boost::format("%s %s") % getRoughAmount(elem.first, 2) % getCreature(elem.first)->namePl);
  222. guards.push_back(str);
  223. }
  224. if(guards.size())
  225. {
  226. for(int i = 0; i < guards.size(); i++)
  227. {
  228. text += guards[i];
  229. if(i + 2 < guards.size())
  230. text += ", ";
  231. else if(i + 2 == guards.size())
  232. text += VLC->generaltexth->allTexts[237];
  233. }
  234. }
  235. return text;
  236. }
  237. int CCreatureSet::stacksCount() const
  238. {
  239. return static_cast<int>(stacks.size());
  240. }
  241. void CCreatureSet::setFormation(bool tight)
  242. {
  243. formation = tight;
  244. }
  245. void CCreatureSet::setStackCount(SlotID slot, TQuantity count)
  246. {
  247. assert(hasStackAtSlot(slot));
  248. assert(stacks[slot]->count + count > 0);
  249. if (VLC->modh->modules.STACK_EXP && count > stacks[slot]->count)
  250. stacks[slot]->experience = static_cast<TExpType>(stacks[slot]->experience * (count / static_cast<double>(stacks[slot]->count)));
  251. stacks[slot]->count = count;
  252. armyChanged();
  253. }
  254. void CCreatureSet::giveStackExp(TExpType exp)
  255. {
  256. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  257. i->second->giveStackExp(exp);
  258. }
  259. void CCreatureSet::setStackExp(SlotID slot, TExpType exp)
  260. {
  261. assert(hasStackAtSlot(slot));
  262. stacks[slot]->experience = exp;
  263. }
  264. void CCreatureSet::clear()
  265. {
  266. while(!stacks.empty())
  267. {
  268. eraseStack(stacks.begin()->first);
  269. }
  270. }
  271. const CStackInstance& CCreatureSet::getStack(SlotID slot) const
  272. {
  273. assert(hasStackAtSlot(slot));
  274. return *getStackPtr(slot);
  275. }
  276. const CStackInstance* CCreatureSet::getStackPtr(SlotID slot) const
  277. {
  278. if(hasStackAtSlot(slot))
  279. return stacks.find(slot)->second;
  280. else return nullptr;
  281. }
  282. void CCreatureSet::eraseStack(SlotID slot)
  283. {
  284. assert(hasStackAtSlot(slot));
  285. CStackInstance *toErase = detachStack(slot);
  286. vstd::clear_pointer(toErase);
  287. }
  288. bool CCreatureSet::contains(const CStackInstance *stack) const
  289. {
  290. if(!stack)
  291. return false;
  292. for(auto & elem : stacks)
  293. if(elem.second == stack)
  294. return true;
  295. return false;
  296. }
  297. SlotID CCreatureSet::findStack(const CStackInstance *stack) const
  298. {
  299. auto h = dynamic_cast<const CGHeroInstance *>(this);
  300. if (h && h->commander == stack)
  301. return SlotID::COMMANDER_SLOT_PLACEHOLDER;
  302. if(!stack)
  303. return SlotID();
  304. for(auto & elem : stacks)
  305. if(elem.second == stack)
  306. return elem.first;
  307. return SlotID();
  308. }
  309. CArmedInstance * CCreatureSet::castToArmyObj()
  310. {
  311. return dynamic_cast<CArmedInstance *>(this);
  312. }
  313. void CCreatureSet::putStack(SlotID slot, CStackInstance *stack)
  314. {
  315. assert(slot.getNum() < GameConstants::ARMY_SIZE);
  316. assert(!hasStackAtSlot(slot));
  317. stacks[slot] = stack;
  318. stack->setArmyObj(castToArmyObj());
  319. armyChanged();
  320. }
  321. void CCreatureSet::joinStack(SlotID slot, CStackInstance * stack)
  322. {
  323. const CCreature *c = getCreature(slot);
  324. assert(c == stack->type);
  325. assert(c);
  326. UNUSED(c);
  327. //TODO move stuff
  328. changeStackCount(slot, stack->count);
  329. vstd::clear_pointer(stack);
  330. }
  331. void CCreatureSet::changeStackCount(SlotID slot, TQuantity toAdd)
  332. {
  333. setStackCount(slot, getStackCount(slot) + toAdd);
  334. }
  335. CCreatureSet::CCreatureSet()
  336. {
  337. formation = false;
  338. }
  339. CCreatureSet::CCreatureSet(const CCreatureSet&)
  340. {
  341. assert(0);
  342. }
  343. CCreatureSet::~CCreatureSet()
  344. {
  345. clear();
  346. }
  347. void CCreatureSet::setToArmy(CSimpleArmy &src)
  348. {
  349. clear();
  350. while(src)
  351. {
  352. auto i = src.army.begin();
  353. putStack(i->first, new CStackInstance(i->second.first, i->second.second));
  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((si32)idx), new_stack);
  454. }
  455. }
  456. }
  457. }
  458. CStackInstance::CStackInstance()
  459. : armyObj(_armyObj)
  460. {
  461. init();
  462. }
  463. CStackInstance::CStackInstance(CreatureID id, TQuantity Count, bool isHypothetic)
  464. : CBonusSystemNode(isHypothetic), armyObj(_armyObj)
  465. {
  466. init();
  467. setType(id);
  468. count = Count;
  469. }
  470. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count, bool isHypothetic)
  471. : CBonusSystemNode(isHypothetic), 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 = (int)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 = (int)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->objects.size())
  543. setType(VLC->creh->objects[creID]);
  544. else
  545. setType((const CCreature*)nullptr);
  546. }
  547. void CStackInstance::copyOppositeBonusesFromCreature(const CCreature * creature)
  548. {
  549. auto owner = _armyObj ? _armyObj->getOwner() : PlayerColor::NEUTRAL;
  550. if(owner == PlayerColor::UNFLAGGABLE)
  551. owner = PlayerColor::NEUTRAL;
  552. auto & creatureBonuses = const_cast<CCreature *>(type)->getExportedBonusList();
  553. for(auto & creatureBonus : creatureBonuses)
  554. {
  555. if(creatureBonus->effectRange == Bonus::ONLY_ENEMY_ARMY) //it has better prformance than dynamic_cast
  556. {
  557. auto bCopy = std::make_shared<Bonus>(*creatureBonus);
  558. bCopy->propagator.reset(new CPropagatorNodeType(CBonusSystemNode::BATTLE));
  559. bCopy->limiter.reset(new OppositeSideLimiter(owner));
  560. this->addNewBonus(bCopy);
  561. }
  562. }
  563. }
  564. void CStackInstance::removeOppositeBonuses()
  565. {
  566. removeBonuses(Selector::effectRange()(Bonus::ONLY_ENEMY_ARMY));
  567. }
  568. void CStackInstance::setType(const CCreature *c)
  569. {
  570. if(type)
  571. {
  572. removeOppositeBonuses();
  573. detachFrom(const_cast<CCreature*>(type));
  574. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  575. experience = static_cast<TExpType>(experience * VLC->creh->expAfterUpgrade / 100.0);
  576. }
  577. CStackBasicDescriptor::setType(c);
  578. if(type)
  579. {
  580. auto creature = const_cast<CCreature*>(type);
  581. copyOppositeBonusesFromCreature(creature);
  582. attachTo(creature);
  583. }
  584. }
  585. std::string CStackInstance::bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const
  586. {
  587. if(Bonus::MAGIC_RESISTANCE == bonus->type)
  588. {
  589. return "";
  590. }
  591. else
  592. {
  593. return VLC->getBth()->bonusToString(bonus, this, description);
  594. }
  595. }
  596. std::string CStackInstance::bonusToGraphics(const std::shared_ptr<Bonus>& bonus) const
  597. {
  598. return VLC->getBth()->bonusToGraphics(bonus);
  599. }
  600. void CStackInstance::setArmyObj(const CArmedInstance * ArmyObj)
  601. {
  602. if(_armyObj)
  603. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  604. _armyObj = ArmyObj;
  605. if(ArmyObj)
  606. {
  607. auto owner = _armyObj->getOwner();
  608. if(owner == PlayerColor::UNFLAGGABLE)
  609. owner = PlayerColor::NEUTRAL;
  610. auto & bonusList = getExportedBonusList();
  611. for(auto & bonus : bonusList)
  612. {
  613. if(bonus->effectRange != Bonus::ONLY_ENEMY_ARMY)
  614. continue;
  615. auto limPtr = bonus->limiter.get();
  616. if(limPtr)
  617. static_cast<OppositeSideLimiter *>(limPtr)->owner = owner;
  618. else
  619. logGlobal->error("setArmyObj. Limiter has been lost. Creature is %s", type ? type->nodeShortInfo() : std::string("No creature"));
  620. }
  621. attachTo(const_cast<CArmedInstance*>(_armyObj));
  622. }
  623. }
  624. std::string CStackInstance::getQuantityTXT(bool capitalized) const
  625. {
  626. int quantity = getQuantityID();
  627. if (quantity)
  628. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  629. else
  630. return "";
  631. }
  632. bool CStackInstance::valid(bool allowUnrandomized) const
  633. {
  634. bool isRand = (idRand != -1);
  635. if(!isRand)
  636. {
  637. return (type && type == VLC->creh->objects[type->idNumber]);
  638. }
  639. else
  640. return allowUnrandomized;
  641. }
  642. CStackInstance::~CStackInstance()
  643. {
  644. }
  645. std::string CStackInstance::nodeName() const
  646. {
  647. std::ostringstream oss;
  648. oss << "Stack of " << count << " of ";
  649. if(type)
  650. oss << type->namePl;
  651. else if(idRand >= 0)
  652. oss << "[no type, idRand=" << idRand << "]";
  653. else
  654. oss << "[UNDEFINED TYPE]";
  655. return oss.str();
  656. }
  657. void CStackInstance::deserializationFix()
  658. {
  659. const CCreature *backup = type;
  660. type = nullptr;
  661. setType(backup);
  662. const CArmedInstance *armyBackup = _armyObj;
  663. _armyObj = nullptr;
  664. setArmyObj(armyBackup);
  665. artDeserializationFix(this);
  666. }
  667. CreatureID CStackInstance::getCreatureID() const
  668. {
  669. if(type)
  670. return type->idNumber;
  671. else
  672. return CreatureID::NONE;
  673. }
  674. std::string CStackInstance::getName() const
  675. {
  676. return (count > 1) ? type->namePl : type->nameSing;
  677. }
  678. ui64 CStackInstance::getPower() const
  679. {
  680. assert(type);
  681. return type->AIValue * count;
  682. }
  683. ArtBearer::ArtBearer CStackInstance::bearerType() const
  684. {
  685. return ArtBearer::CREATURE;
  686. }
  687. void CStackInstance::putArtifact(ArtifactPosition pos, CArtifactInstance * art)
  688. {
  689. assert(!getArt(pos));
  690. art->putAt(ArtifactLocation(this, pos));
  691. }
  692. void CStackInstance::serializeJson(JsonSerializeFormat & handler)
  693. {
  694. //todo: artifacts
  695. CStackBasicDescriptor::serializeJson(handler);//must be first
  696. if(handler.saving)
  697. {
  698. if(idRand > -1)
  699. {
  700. int level = (int)idRand / 2;
  701. boost::logic::tribool upgraded = (idRand % 2) > 0;
  702. handler.serializeInt("level", level, 0);
  703. handler.serializeBool("upgraded", upgraded);
  704. }
  705. }
  706. else
  707. {
  708. //type set by CStackBasicDescriptor::serializeJson
  709. if(type == nullptr)
  710. {
  711. int level = 0;
  712. bool upgraded = false;
  713. handler.serializeInt("level", level, 0);
  714. handler.serializeBool("upgraded", upgraded);
  715. idRand = level * 2 + (int)(bool)upgraded;
  716. }
  717. }
  718. }
  719. CCommanderInstance::CCommanderInstance()
  720. {
  721. init();
  722. name = "Unnamed";
  723. }
  724. CCommanderInstance::CCommanderInstance (CreatureID id)
  725. {
  726. init();
  727. setType(id);
  728. name = "Commando"; //TODO - parse them
  729. }
  730. void CCommanderInstance::init()
  731. {
  732. alive = true;
  733. experience = 0;
  734. level = 1;
  735. count = 1;
  736. type = nullptr;
  737. idRand = -1;
  738. _armyObj = nullptr;
  739. setNodeType (CBonusSystemNode::COMMANDER);
  740. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  741. }
  742. CCommanderInstance::~CCommanderInstance()
  743. {
  744. }
  745. void CCommanderInstance::setAlive (bool Alive)
  746. {
  747. //TODO: helm of immortality
  748. alive = Alive;
  749. if (!alive)
  750. {
  751. removeBonusesRecursive(Bonus::UntilCommanderKilled);
  752. }
  753. }
  754. void CCommanderInstance::giveStackExp (TExpType exp)
  755. {
  756. if (alive)
  757. experience += exp;
  758. }
  759. int CCommanderInstance::getExpRank() const
  760. {
  761. return VLC->heroh->level (experience);
  762. }
  763. int CCommanderInstance::getLevel() const
  764. {
  765. return std::max (1, getExpRank());
  766. }
  767. void CCommanderInstance::levelUp ()
  768. {
  769. level++;
  770. for (auto bonus : VLC->creh->commanderLevelPremy)
  771. { //grant all regular level-up bonuses
  772. accumulateBonus(bonus);
  773. }
  774. }
  775. ArtBearer::ArtBearer CCommanderInstance::bearerType() const
  776. {
  777. return ArtBearer::COMMANDER;
  778. }
  779. bool CCommanderInstance::gainsLevel() const
  780. {
  781. return experience >= (TExpType)VLC->heroh->reqExp(level+1);
  782. }
  783. CStackBasicDescriptor::CStackBasicDescriptor()
  784. {
  785. type = nullptr;
  786. count = -1;
  787. }
  788. CStackBasicDescriptor::CStackBasicDescriptor(CreatureID id, TQuantity Count)
  789. : type (VLC->creh->objects[id]), count(Count)
  790. {
  791. }
  792. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  793. : type(c), count(Count)
  794. {
  795. }
  796. const Creature * CStackBasicDescriptor::getType() const
  797. {
  798. return type;
  799. }
  800. TQuantity CStackBasicDescriptor::getCount() const
  801. {
  802. return count;
  803. }
  804. void CStackBasicDescriptor::setType(const CCreature * c)
  805. {
  806. type = c;
  807. }
  808. void CStackBasicDescriptor::serializeJson(JsonSerializeFormat & handler)
  809. {
  810. handler.serializeInt("amount", count);
  811. if(handler.saving)
  812. {
  813. if(type)
  814. {
  815. std::string typeName = type->identifier;
  816. handler.serializeString("type", typeName);
  817. }
  818. }
  819. else
  820. {
  821. std::string typeName("");
  822. handler.serializeString("type", typeName);
  823. if(typeName != "")
  824. setType(VLC->creh->getCreature("core", typeName));
  825. }
  826. }
  827. void CSimpleArmy::clear()
  828. {
  829. army.clear();
  830. }
  831. CSimpleArmy::operator bool() const
  832. {
  833. return army.size();
  834. }
  835. bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
  836. {
  837. assert(!vstd::contains(army, slot));
  838. army[slot] = std::make_pair(cre, count);
  839. return true;
  840. }