CGCreature.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * CGCreature.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 "CGCreature.h"
  12. #include "CGHeroInstance.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CConfigHandler.h"
  15. #include "../GameSettings.h"
  16. #include "../IGameCallback.h"
  17. #include "../networkPacks/PacksForClient.h"
  18. #include "../networkPacks/PacksForClientBattle.h"
  19. #include "../networkPacks/StackLocation.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. std::string CGCreature::getHoverText(PlayerColor player) const
  23. {
  24. if(stacks.empty())
  25. {
  26. //should not happen...
  27. logGlobal->error("Invalid stack at tile %s: subID=%d; id=%d", pos.toString(), getCreature(), id.getNum());
  28. return "INVALID_STACK";
  29. }
  30. MetaString ms;
  31. CCreature::CreatureQuantityId monsterQuantityId = stacks.begin()->second->getQuantityID();
  32. int quantityTextIndex = 172 + 3 * (int)monsterQuantityId;
  33. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  34. ms.appendRawString(CCreature::getQuantityRangeStringForId(monsterQuantityId));
  35. else
  36. ms.appendLocalString(EMetaText::ARRAY_TXT, quantityTextIndex);
  37. ms.appendRawString(" ");
  38. ms.appendNamePlural(getCreature());
  39. return ms.toString();
  40. }
  41. std::string CGCreature::getHoverText(const CGHeroInstance * hero) const
  42. {
  43. if(hero->hasVisions(this, BonusCustomSubtype::visionsMonsters))
  44. {
  45. MetaString ms;
  46. ms.appendNumber(stacks.begin()->second->count);
  47. ms.appendRawString(" ");
  48. ms.appendName(getCreature(), stacks.begin()->second->count);
  49. return ms.toString();
  50. }
  51. else
  52. {
  53. return getHoverText(hero->tempOwner);
  54. }
  55. }
  56. std::string CGCreature::getPopupText(const CGHeroInstance * hero) const
  57. {
  58. std::string hoverName;
  59. if(hero->hasVisions(this, BonusCustomSubtype::visionsMonsters))
  60. {
  61. MetaString ms;
  62. ms.appendRawString(getHoverText(hero));
  63. ms.appendRawString("\n\n");
  64. int decision = takenAction(hero, true);
  65. switch (decision)
  66. {
  67. case FIGHT:
  68. ms.appendLocalString(EMetaText::GENERAL_TXT,246);
  69. break;
  70. case FLEE:
  71. ms.appendLocalString(EMetaText::GENERAL_TXT,245);
  72. break;
  73. case JOIN_FOR_FREE:
  74. ms.appendLocalString(EMetaText::GENERAL_TXT,243);
  75. break;
  76. default: //decision = cost in gold
  77. ms.appendLocalString(EMetaText::GENERAL_TXT,244);
  78. ms.replaceNumber(decision);
  79. break;
  80. }
  81. hoverName = ms.toString();
  82. }
  83. else
  84. {
  85. hoverName = getHoverText(hero->tempOwner);
  86. }
  87. if (settings["general"]["enableUiEnhancements"].Bool())
  88. {
  89. hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.title");
  90. int choice;
  91. double ratio = (static_cast<double>(getArmyStrength()) / hero->getTotalStrength());
  92. if (ratio < 0.1) choice = 0;
  93. else if (ratio < 0.25) choice = 1;
  94. else if (ratio < 0.6) choice = 2;
  95. else if (ratio < 0.9) choice = 3;
  96. else if (ratio < 1.1) choice = 4;
  97. else if (ratio < 1.3) choice = 5;
  98. else if (ratio < 1.8) choice = 6;
  99. else if (ratio < 2.5) choice = 7;
  100. else if (ratio < 4) choice = 8;
  101. else if (ratio < 8) choice = 9;
  102. else if (ratio < 20) choice = 10;
  103. else choice = 11;
  104. hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.levels." + std::to_string(choice));
  105. }
  106. return hoverName;
  107. }
  108. std::string CGCreature::getPopupText(PlayerColor player) const
  109. {
  110. return getHoverText(player);
  111. }
  112. std::vector<Component> CGCreature::getPopupComponents(PlayerColor player) const
  113. {
  114. return {
  115. Component(ComponentType::CREATURE, getCreature())
  116. };
  117. }
  118. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  119. {
  120. //show message
  121. if(!message.empty())
  122. {
  123. InfoWindow iw;
  124. iw.player = h->tempOwner;
  125. iw.text = message;
  126. iw.type = EInfoWindowMode::MODAL;
  127. cb->showInfoDialog(&iw);
  128. }
  129. int action = takenAction(h);
  130. switch( action ) //decide what we do...
  131. {
  132. case FIGHT:
  133. fight(h);
  134. break;
  135. case FLEE:
  136. {
  137. flee(h);
  138. break;
  139. }
  140. case JOIN_FOR_FREE: //join for free
  141. {
  142. BlockingDialog ynd(true,false);
  143. ynd.player = h->tempOwner;
  144. ynd.text.appendLocalString(EMetaText::ADVOB_TXT, 86);
  145. ynd.text.replaceName(getCreature(), getStackCount(SlotID(0)));
  146. cb->showBlockingDialog(&ynd);
  147. break;
  148. }
  149. default: //join for gold
  150. {
  151. assert(action > 0);
  152. //ask if player agrees to pay gold
  153. BlockingDialog ynd(true,false);
  154. ynd.player = h->tempOwner;
  155. std::string tmp = VLC->generaltexth->advobtxt[90];
  156. boost::algorithm::replace_first(tmp, "%d", std::to_string(getStackCount(SlotID(0))));
  157. boost::algorithm::replace_first(tmp, "%d", std::to_string(action));
  158. boost::algorithm::replace_first(tmp,"%s",VLC->creatures()->getById(getCreature())->getNamePluralTranslated());
  159. ynd.text.appendRawString(tmp);
  160. cb->showBlockingDialog(&ynd);
  161. break;
  162. }
  163. }
  164. }
  165. CreatureID CGCreature::getCreature() const
  166. {
  167. return CreatureID(getObjTypeIndex().getNum());
  168. }
  169. void CGCreature::pickRandomObject(CRandomGenerator & rand)
  170. {
  171. switch(ID.toEnum())
  172. {
  173. case MapObjectID::RANDOM_MONSTER:
  174. subID = VLC->creh->pickRandomMonster(rand);
  175. break;
  176. case MapObjectID::RANDOM_MONSTER_L1:
  177. subID = VLC->creh->pickRandomMonster(rand, 1);
  178. break;
  179. case MapObjectID::RANDOM_MONSTER_L2:
  180. subID = VLC->creh->pickRandomMonster(rand, 2);
  181. break;
  182. case MapObjectID::RANDOM_MONSTER_L3:
  183. subID = VLC->creh->pickRandomMonster(rand, 3);
  184. break;
  185. case MapObjectID::RANDOM_MONSTER_L4:
  186. subID = VLC->creh->pickRandomMonster(rand, 4);
  187. break;
  188. case MapObjectID::RANDOM_MONSTER_L5:
  189. subID = VLC->creh->pickRandomMonster(rand, 5);
  190. break;
  191. case MapObjectID::RANDOM_MONSTER_L6:
  192. subID = VLC->creh->pickRandomMonster(rand, 6);
  193. break;
  194. case MapObjectID::RANDOM_MONSTER_L7:
  195. subID = VLC->creh->pickRandomMonster(rand, 7);
  196. break;
  197. }
  198. ID = MapObjectID::MONSTER;
  199. setType(ID, subID);
  200. }
  201. void CGCreature::initObj(CRandomGenerator & rand)
  202. {
  203. blockVisit = true;
  204. switch(character)
  205. {
  206. case 0:
  207. character = -4;
  208. break;
  209. case 1:
  210. character = rand.nextInt(1, 7);
  211. break;
  212. case 2:
  213. character = rand.nextInt(1, 10);
  214. break;
  215. case 3:
  216. character = rand.nextInt(4, 10);
  217. break;
  218. case 4:
  219. character = 10;
  220. break;
  221. }
  222. stacks[SlotID(0)]->setType(getCreature());
  223. TQuantity &amount = stacks[SlotID(0)]->count;
  224. const Creature * c = VLC->creatures()->getById(getCreature());
  225. if(amount == 0)
  226. {
  227. amount = rand.nextInt(c->getAdvMapAmountMin(), c->getAdvMapAmountMax());
  228. if(amount == 0) //armies with 0 creatures are illegal
  229. {
  230. logGlobal->warn("Stack cannot have 0 creatures. Check properties of %s", c->getJsonKey());
  231. amount = 1;
  232. }
  233. }
  234. temppower = stacks[SlotID(0)]->count * static_cast<ui64>(1000);
  235. refusedJoining = false;
  236. }
  237. void CGCreature::newTurn(CRandomGenerator & rand) const
  238. {//Works only for stacks of single type of size up to 2 millions
  239. if (!notGrowingTeam)
  240. {
  241. if (stacks.begin()->second->count < VLC->settings()->getInteger(EGameSettings::CREATURES_WEEKLY_GROWTH_CAP) && cb->getDate(Date::DAY_OF_WEEK) == 1 && cb->getDate(Date::DAY) > 1)
  242. {
  243. ui32 power = static_cast<ui32>(temppower * (100 + VLC->settings()->getInteger(EGameSettings::CREATURES_WEEKLY_GROWTH_PERCENT)) / 100);
  244. cb->setObjPropertyValue(id, ObjProperty::MONSTER_COUNT, std::min<uint32_t>(power / 1000, VLC->settings()->getInteger(EGameSettings::CREATURES_WEEKLY_GROWTH_CAP))); //set new amount
  245. cb->setObjPropertyValue(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  246. }
  247. }
  248. if (VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
  249. cb->setObjPropertyValue(id, ObjProperty::MONSTER_EXP, VLC->settings()->getInteger(EGameSettings::CREATURES_DAILY_STACK_EXPERIENCE)); //for testing purpose
  250. }
  251. void CGCreature::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  252. {
  253. switch (what)
  254. {
  255. case ObjProperty::MONSTER_COUNT:
  256. stacks[SlotID(0)]->count = identifier.getNum();
  257. break;
  258. case ObjProperty::MONSTER_POWER:
  259. temppower = identifier.getNum();
  260. break;
  261. case ObjProperty::MONSTER_EXP:
  262. giveStackExp(identifier.getNum());
  263. break;
  264. case ObjProperty::MONSTER_REFUSED_JOIN:
  265. refusedJoining = identifier.getNum();
  266. break;
  267. }
  268. }
  269. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  270. {
  271. //calculate relative strength of hero and creatures armies
  272. double relStrength = static_cast<double>(h->getTotalStrength()) / getArmyStrength();
  273. int powerFactor;
  274. if(relStrength >= 7)
  275. powerFactor = 11;
  276. else if(relStrength >= 1)
  277. powerFactor = static_cast<int>(2 * (relStrength - 1));
  278. else if(relStrength >= 0.5)
  279. powerFactor = -1;
  280. else if(relStrength >= 0.333)
  281. powerFactor = -2;
  282. else
  283. powerFactor = -3;
  284. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  285. const CCreature * myCreature = getCreature().toCreature();
  286. myKindCres.insert(myCreature->getId()); //we
  287. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  288. for(auto const & crea : VLC->creh->objects)
  289. {
  290. if(vstd::contains(crea->upgrades, myCreature->getId())) //it's our base creatures
  291. myKindCres.insert(crea->getId());
  292. }
  293. int count = 0; //how many creatures of similar kind has hero
  294. int totalCount = 0;
  295. for(const auto & elem : h->Slots())
  296. {
  297. if(vstd::contains(myKindCres,elem.second->type->getId()))
  298. count += elem.second->count;
  299. totalCount += elem.second->count;
  300. }
  301. int sympathy = 0; // 0 if hero have no similar creatures
  302. if(count)
  303. sympathy++; // 1 - if hero have at least 1 similar creature
  304. if(count*2 > totalCount)
  305. sympathy++; // 2 - hero have similar creatures more that 50%
  306. int diplomacy = h->valOfBonuses(BonusType::WANDERING_CREATURES_JOIN_BONUS);
  307. int charisma = powerFactor + diplomacy + sympathy;
  308. if(charisma < character)
  309. return FIGHT;
  310. if (allowJoin)
  311. {
  312. if(diplomacy + sympathy + 1 >= character)
  313. return JOIN_FOR_FREE;
  314. else if(diplomacy * 2 + sympathy + 1 >= character)
  315. return VLC->creatures()->getById(getCreature())->getRecruitCost(EGameResID::GOLD) * getStackCount(SlotID(0)); //join for gold
  316. }
  317. //we are still here - creatures have not joined hero, flee or fight
  318. if (charisma > character && !neverFlees)
  319. return FLEE;
  320. else
  321. return FIGHT;
  322. }
  323. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  324. {
  325. if(refusedJoining)
  326. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  327. if(pursue)
  328. {
  329. fight(h);
  330. }
  331. else
  332. {
  333. cb->removeObject(this, h->getOwner());
  334. }
  335. }
  336. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  337. {
  338. if(!accept)
  339. {
  340. if(takenAction(h,false) == FLEE)
  341. {
  342. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  343. flee(h);
  344. }
  345. else //they fight
  346. {
  347. h->showInfoDialog(87, 0, EInfoWindowMode::MODAL);//Insulted by your refusal of their offer, the monsters attack!
  348. fight(h);
  349. }
  350. }
  351. else //accepted
  352. {
  353. if (cb->getResource(h->tempOwner, EGameResID::GOLD) < cost) //player don't have enough gold!
  354. {
  355. InfoWindow iw;
  356. iw.player = h->tempOwner;
  357. iw.text.appendLocalString(EMetaText::GENERAL_TXT,29); //You don't have enough gold
  358. cb->showInfoDialog(&iw);
  359. //act as if player refused
  360. joinDecision(h,cost,false);
  361. return;
  362. }
  363. //take gold
  364. if(cost)
  365. cb->giveResource(h->tempOwner,EGameResID::GOLD,-cost);
  366. giveReward(h);
  367. cb->tryJoiningArmy(this, h, true, true);
  368. }
  369. }
  370. void CGCreature::fight( const CGHeroInstance *h ) const
  371. {
  372. //split stacks
  373. int stacksCount = getNumberOfStacks(h);
  374. //source: http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266335#focus
  375. int amount = getStackCount(SlotID(0));
  376. int m = amount / stacksCount;
  377. int b = stacksCount * (m + 1) - amount;
  378. int a = stacksCount - b;
  379. SlotID sourceSlot = stacks.begin()->first;
  380. for (int slotID = 1; slotID < a; ++slotID)
  381. {
  382. int stackSize = m + 1;
  383. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  384. }
  385. for (int slotID = a; slotID < stacksCount; ++slotID)
  386. {
  387. int stackSize = m;
  388. if (slotID) //don't do this when a = 0 -> stack is single
  389. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  390. }
  391. if (stacksCount > 1)
  392. {
  393. if (containsUpgradedStack()) //upgrade
  394. {
  395. SlotID slotID = SlotID(static_cast<si32>(std::floor(static_cast<float>(stacks.size()) / 2.0f)));
  396. const auto & upgrades = getStack(slotID).type->upgrades;
  397. if(!upgrades.empty())
  398. {
  399. auto it = RandomGeneratorUtil::nextItem(upgrades, CRandomGenerator::getDefault());
  400. cb->changeStackType(StackLocation(this, slotID), it->toCreature());
  401. }
  402. }
  403. }
  404. cb->startBattleI(h, this);
  405. }
  406. void CGCreature::flee( const CGHeroInstance * h ) const
  407. {
  408. BlockingDialog ynd(true,false);
  409. ynd.player = h->tempOwner;
  410. ynd.text.appendLocalString(EMetaText::ADVOB_TXT,91);
  411. ynd.text.replaceName(getCreature(), getStackCount(SlotID(0)));
  412. cb->showBlockingDialog(&ynd);
  413. }
  414. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  415. {
  416. if(result.winner == 0)
  417. {
  418. giveReward(hero);
  419. cb->removeObject(this, hero->getOwner());
  420. }
  421. else if(result.winner > 1) // draw
  422. {
  423. // guarded reward is lost forever on draw
  424. cb->removeObject(this, hero->getOwner());
  425. }
  426. else
  427. {
  428. //merge stacks into one
  429. TSlots::const_iterator i;
  430. const CCreature * cre = getCreature().toCreature();
  431. for(i = stacks.begin(); i != stacks.end(); i++)
  432. {
  433. if(cre->isMyUpgrade(i->second->type))
  434. {
  435. cb->changeStackType(StackLocation(this, i->first), cre); //un-upgrade creatures
  436. }
  437. }
  438. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  439. if(!hasStackAtSlot(SlotID(0)))
  440. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  441. while(stacks.size() > 1) //hopefully that's enough
  442. {
  443. // TODO it's either overcomplicated (if we assume there'll be only one stack) or buggy (if we allow multiple stacks... but that'll also cause troubles elsewhere)
  444. i = stacks.end();
  445. i--;
  446. SlotID slot = getSlotFor(i->second->type);
  447. if(slot == i->first) //no reason to move stack to its own slot
  448. break;
  449. else
  450. cb->moveStack(StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  451. }
  452. cb->setObjPropertyValue(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  453. }
  454. }
  455. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  456. {
  457. auto action = takenAction(hero);
  458. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  459. joinDecision(hero, action, answer);
  460. else if(action != FIGHT)
  461. fleeDecision(hero, answer);
  462. else
  463. assert(0);
  464. }
  465. bool CGCreature::containsUpgradedStack() const
  466. {
  467. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=830557#focus
  468. float a = 2992.911117f;
  469. float b = 14174.264968f;
  470. float c = 5325.181015f;
  471. float d = 32788.727920f;
  472. int val = static_cast<int>(std::floor(a * pos.x + b * pos.y + c * pos.z + d));
  473. return ((val % 32768) % 100) < 50;
  474. }
  475. int CGCreature::getNumberOfStacks(const CGHeroInstance *hero) const
  476. {
  477. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266094#focus
  478. double strengthRatio = static_cast<double>(hero->getArmyStrength()) / getArmyStrength();
  479. int split = 1;
  480. if (strengthRatio < 0.5f)
  481. split = 7;
  482. else if (strengthRatio < 0.67f)
  483. split = 6;
  484. else if (strengthRatio < 1)
  485. split = 5;
  486. else if (strengthRatio < 1.5f)
  487. split = 4;
  488. else if (strengthRatio < 2)
  489. split = 3;
  490. else
  491. split = 2;
  492. ui32 a = 1550811371u;
  493. ui32 b = 3359066809u;
  494. ui32 c = 1943276003u;
  495. ui32 d = 3174620878u;
  496. ui32 R1 = a * static_cast<ui32>(pos.x) + b * static_cast<ui32>(pos.y) + c * static_cast<ui32>(pos.z) + d;
  497. ui32 R2 = (R1 >> 16) & 0x7fff;
  498. int R4 = R2 % 100 + 1;
  499. if (R4 <= 20)
  500. split -= 1;
  501. else if (R4 >= 80)
  502. split += 1;
  503. vstd::amin(split, getStack(SlotID(0)).count); //can't divide into more stacks than creatures total
  504. vstd::amin(split, 7); //can't have more than 7 stacks
  505. return split;
  506. }
  507. void CGCreature::giveReward(const CGHeroInstance * h) const
  508. {
  509. InfoWindow iw;
  510. iw.player = h->tempOwner;
  511. if(!resources.empty())
  512. {
  513. cb->giveResources(h->tempOwner, resources);
  514. for(auto const & res : GameResID::ALL_RESOURCES())
  515. {
  516. if(resources[res] > 0)
  517. iw.components.emplace_back(ComponentType::RESOURCE, res, resources[res]);
  518. }
  519. }
  520. if(gainedArtifact != ArtifactID::NONE)
  521. {
  522. cb->giveHeroNewArtifact(h, gainedArtifact.toArtifact(), ArtifactPosition::FIRST_AVAILABLE);
  523. iw.components.emplace_back(ComponentType::ARTIFACT, gainedArtifact);
  524. }
  525. if(!iw.components.empty())
  526. {
  527. iw.type = EInfoWindowMode::AUTO;
  528. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 183); // % has found treasure
  529. iw.text.replaceRawString(h->getNameTranslated());
  530. cb->showInfoDialog(&iw);
  531. }
  532. }
  533. static const std::vector<std::string> CHARACTER_JSON =
  534. {
  535. "compliant", "friendly", "aggressive", "hostile", "savage"
  536. };
  537. void CGCreature::serializeJsonOptions(JsonSerializeFormat & handler)
  538. {
  539. handler.serializeEnum("character", character, CHARACTER_JSON);
  540. if(handler.saving)
  541. {
  542. if(hasStackAtSlot(SlotID(0)))
  543. {
  544. si32 amount = getStack(SlotID(0)).count;
  545. handler.serializeInt("amount", amount, 0);
  546. }
  547. }
  548. else
  549. {
  550. si32 amount = 0;
  551. handler.serializeInt("amount", amount);
  552. auto * hlp = new CStackInstance();
  553. hlp->count = amount;
  554. //type will be set during initialization
  555. putStack(SlotID(0), hlp);
  556. }
  557. resources.serializeJson(handler, "rewardResources");
  558. handler.serializeId("rewardArtifact", gainedArtifact, ArtifactID(ArtifactID::NONE));
  559. handler.serializeBool("noGrowing", notGrowingTeam);
  560. handler.serializeBool("neverFlees", neverFlees);
  561. handler.serializeStruct("rewardMessage", message);
  562. }
  563. VCMI_LIB_NAMESPACE_END