CGCreature.cpp 17 KB

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