CGCreature.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. if (ID != MapObjectID::MONSTER)
  199. {
  200. ID = MapObjectID::MONSTER;
  201. setType(ID, subID);
  202. }
  203. }
  204. void CGCreature::initObj(CRandomGenerator & rand)
  205. {
  206. blockVisit = true;
  207. switch(character)
  208. {
  209. case 0:
  210. character = -4;
  211. break;
  212. case 1:
  213. character = rand.nextInt(1, 7);
  214. break;
  215. case 2:
  216. character = rand.nextInt(1, 10);
  217. break;
  218. case 3:
  219. character = rand.nextInt(4, 10);
  220. break;
  221. case 4:
  222. character = 10;
  223. break;
  224. }
  225. stacks[SlotID(0)]->setType(getCreature());
  226. TQuantity &amount = stacks[SlotID(0)]->count;
  227. const Creature * c = VLC->creatures()->getById(getCreature());
  228. if(amount == 0)
  229. {
  230. amount = rand.nextInt(c->getAdvMapAmountMin(), c->getAdvMapAmountMax());
  231. if(amount == 0) //armies with 0 creatures are illegal
  232. {
  233. logGlobal->warn("Stack cannot have 0 creatures. Check properties of %s", c->getJsonKey());
  234. amount = 1;
  235. }
  236. }
  237. temppower = stacks[SlotID(0)]->count * static_cast<ui64>(1000);
  238. refusedJoining = false;
  239. }
  240. void CGCreature::newTurn(CRandomGenerator & rand) const
  241. {//Works only for stacks of single type of size up to 2 millions
  242. if (!notGrowingTeam)
  243. {
  244. 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)
  245. {
  246. ui32 power = static_cast<ui32>(temppower * (100 + VLC->settings()->getInteger(EGameSettings::CREATURES_WEEKLY_GROWTH_PERCENT)) / 100);
  247. cb->setObjPropertyValue(id, ObjProperty::MONSTER_COUNT, std::min<uint32_t>(power / 1000, VLC->settings()->getInteger(EGameSettings::CREATURES_WEEKLY_GROWTH_CAP))); //set new amount
  248. cb->setObjPropertyValue(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  249. }
  250. }
  251. if (VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
  252. cb->setObjPropertyValue(id, ObjProperty::MONSTER_EXP, VLC->settings()->getInteger(EGameSettings::CREATURES_DAILY_STACK_EXPERIENCE)); //for testing purpose
  253. }
  254. void CGCreature::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  255. {
  256. switch (what)
  257. {
  258. case ObjProperty::MONSTER_COUNT:
  259. stacks[SlotID(0)]->count = identifier.getNum();
  260. break;
  261. case ObjProperty::MONSTER_POWER:
  262. temppower = identifier.getNum();
  263. break;
  264. case ObjProperty::MONSTER_EXP:
  265. giveStackExp(identifier.getNum());
  266. break;
  267. case ObjProperty::MONSTER_REFUSED_JOIN:
  268. refusedJoining = identifier.getNum();
  269. break;
  270. }
  271. }
  272. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  273. {
  274. //calculate relative strength of hero and creatures armies
  275. double relStrength = static_cast<double>(h->getTotalStrength()) / getArmyStrength();
  276. int powerFactor;
  277. if(relStrength >= 7)
  278. powerFactor = 11;
  279. else if(relStrength >= 1)
  280. powerFactor = static_cast<int>(2 * (relStrength - 1));
  281. else if(relStrength >= 0.5)
  282. powerFactor = -1;
  283. else if(relStrength >= 0.333)
  284. powerFactor = -2;
  285. else
  286. powerFactor = -3;
  287. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  288. const CCreature * myCreature = VLC->creh->objects[getCreature().getNum()];
  289. myKindCres.insert(myCreature->getId()); //we
  290. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  291. for(ConstTransitivePtr<CCreature> &crea : VLC->creh->objects)
  292. {
  293. if(vstd::contains(crea->upgrades, myCreature->getId())) //it's our base creatures
  294. myKindCres.insert(crea->getId());
  295. }
  296. int count = 0; //how many creatures of similar kind has hero
  297. int totalCount = 0;
  298. for(const auto & elem : h->Slots())
  299. {
  300. if(vstd::contains(myKindCres,elem.second->type->getId()))
  301. count += elem.second->count;
  302. totalCount += elem.second->count;
  303. }
  304. int sympathy = 0; // 0 if hero have no similar creatures
  305. if(count)
  306. sympathy++; // 1 - if hero have at least 1 similar creature
  307. if(count*2 > totalCount)
  308. sympathy++; // 2 - hero have similar creatures more that 50%
  309. int diplomacy = h->valOfBonuses(BonusType::WANDERING_CREATURES_JOIN_BONUS);
  310. int charisma = powerFactor + diplomacy + sympathy;
  311. if(charisma < character)
  312. return FIGHT;
  313. if (allowJoin)
  314. {
  315. if(diplomacy + sympathy + 1 >= character)
  316. return JOIN_FOR_FREE;
  317. else if(diplomacy * 2 + sympathy + 1 >= character)
  318. return VLC->creatures()->getById(getCreature())->getRecruitCost(EGameResID::GOLD) * getStackCount(SlotID(0)); //join for gold
  319. }
  320. //we are still here - creatures have not joined hero, flee or fight
  321. if (charisma > character && !neverFlees)
  322. return FLEE;
  323. else
  324. return FIGHT;
  325. }
  326. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  327. {
  328. if(refusedJoining)
  329. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  330. if(pursue)
  331. {
  332. fight(h);
  333. }
  334. else
  335. {
  336. cb->removeObject(this, h->getOwner());
  337. }
  338. }
  339. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  340. {
  341. if(!accept)
  342. {
  343. if(takenAction(h,false) == FLEE)
  344. {
  345. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  346. flee(h);
  347. }
  348. else //they fight
  349. {
  350. h->showInfoDialog(87, 0, EInfoWindowMode::MODAL);//Insulted by your refusal of their offer, the monsters attack!
  351. fight(h);
  352. }
  353. }
  354. else //accepted
  355. {
  356. if (cb->getResource(h->tempOwner, EGameResID::GOLD) < cost) //player don't have enough gold!
  357. {
  358. InfoWindow iw;
  359. iw.player = h->tempOwner;
  360. iw.text.appendLocalString(EMetaText::GENERAL_TXT,29); //You don't have enough gold
  361. cb->showInfoDialog(&iw);
  362. //act as if player refused
  363. joinDecision(h,cost,false);
  364. return;
  365. }
  366. //take gold
  367. if(cost)
  368. cb->giveResource(h->tempOwner,EGameResID::GOLD,-cost);
  369. giveReward(h);
  370. cb->tryJoiningArmy(this, h, true, true);
  371. }
  372. }
  373. void CGCreature::fight( const CGHeroInstance *h ) const
  374. {
  375. //split stacks
  376. int stacksCount = getNumberOfStacks(h);
  377. //source: http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266335#focus
  378. int amount = getStackCount(SlotID(0));
  379. int m = amount / stacksCount;
  380. int b = stacksCount * (m + 1) - amount;
  381. int a = stacksCount - b;
  382. SlotID sourceSlot = stacks.begin()->first;
  383. for (int slotID = 1; slotID < a; ++slotID)
  384. {
  385. int stackSize = m + 1;
  386. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  387. }
  388. for (int slotID = a; slotID < stacksCount; ++slotID)
  389. {
  390. int stackSize = m;
  391. if (slotID) //don't do this when a = 0 -> stack is single
  392. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  393. }
  394. if (stacksCount > 1)
  395. {
  396. if (containsUpgradedStack()) //upgrade
  397. {
  398. SlotID slotID = SlotID(static_cast<si32>(std::floor(static_cast<float>(stacks.size()) / 2.0f)));
  399. const auto & upgrades = getStack(slotID).type->upgrades;
  400. if(!upgrades.empty())
  401. {
  402. auto it = RandomGeneratorUtil::nextItem(upgrades, CRandomGenerator::getDefault());
  403. cb->changeStackType(StackLocation(this, slotID), it->toCreature());
  404. }
  405. }
  406. }
  407. cb->startBattleI(h, this);
  408. }
  409. void CGCreature::flee( const CGHeroInstance * h ) const
  410. {
  411. BlockingDialog ynd(true,false);
  412. ynd.player = h->tempOwner;
  413. ynd.text.appendLocalString(EMetaText::ADVOB_TXT,91);
  414. ynd.text.replaceName(getCreature(), getStackCount(SlotID(0)));
  415. cb->showBlockingDialog(&ynd);
  416. }
  417. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  418. {
  419. if(result.winner == 0)
  420. {
  421. giveReward(hero);
  422. cb->removeObject(this, hero->getOwner());
  423. }
  424. else if(result.winner > 1) // draw
  425. {
  426. // guarded reward is lost forever on draw
  427. cb->removeObject(this, hero->getOwner());
  428. }
  429. else
  430. {
  431. //merge stacks into one
  432. TSlots::const_iterator i;
  433. const CCreature * cre = getCreature().toCreature();
  434. for(i = stacks.begin(); i != stacks.end(); i++)
  435. {
  436. if(cre->isMyUpgrade(i->second->type))
  437. {
  438. cb->changeStackType(StackLocation(this, i->first), cre); //un-upgrade creatures
  439. }
  440. }
  441. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  442. if(!hasStackAtSlot(SlotID(0)))
  443. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  444. while(stacks.size() > 1) //hopefully that's enough
  445. {
  446. // 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)
  447. i = stacks.end();
  448. i--;
  449. SlotID slot = getSlotFor(i->second->type);
  450. if(slot == i->first) //no reason to move stack to its own slot
  451. break;
  452. else
  453. cb->moveStack(StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  454. }
  455. cb->setObjPropertyValue(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  456. }
  457. }
  458. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  459. {
  460. auto action = takenAction(hero);
  461. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  462. joinDecision(hero, action, answer);
  463. else if(action != FIGHT)
  464. fleeDecision(hero, answer);
  465. else
  466. assert(0);
  467. }
  468. bool CGCreature::containsUpgradedStack() const
  469. {
  470. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=830557#focus
  471. float a = 2992.911117f;
  472. float b = 14174.264968f;
  473. float c = 5325.181015f;
  474. float d = 32788.727920f;
  475. int val = static_cast<int>(std::floor(a * pos.x + b * pos.y + c * pos.z + d));
  476. return ((val % 32768) % 100) < 50;
  477. }
  478. int CGCreature::getNumberOfStacks(const CGHeroInstance *hero) const
  479. {
  480. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266094#focus
  481. double strengthRatio = static_cast<double>(hero->getArmyStrength()) / getArmyStrength();
  482. int split = 1;
  483. if (strengthRatio < 0.5f)
  484. split = 7;
  485. else if (strengthRatio < 0.67f)
  486. split = 6;
  487. else if (strengthRatio < 1)
  488. split = 5;
  489. else if (strengthRatio < 1.5f)
  490. split = 4;
  491. else if (strengthRatio < 2)
  492. split = 3;
  493. else
  494. split = 2;
  495. ui32 a = 1550811371u;
  496. ui32 b = 3359066809u;
  497. ui32 c = 1943276003u;
  498. ui32 d = 3174620878u;
  499. ui32 R1 = a * static_cast<ui32>(pos.x) + b * static_cast<ui32>(pos.y) + c * static_cast<ui32>(pos.z) + d;
  500. ui32 R2 = (R1 >> 16) & 0x7fff;
  501. int R4 = R2 % 100 + 1;
  502. if (R4 <= 20)
  503. split -= 1;
  504. else if (R4 >= 80)
  505. split += 1;
  506. vstd::amin(split, getStack(SlotID(0)).count); //can't divide into more stacks than creatures total
  507. vstd::amin(split, 7); //can't have more than 7 stacks
  508. return split;
  509. }
  510. void CGCreature::giveReward(const CGHeroInstance * h) const
  511. {
  512. InfoWindow iw;
  513. iw.player = h->tempOwner;
  514. if(!resources.empty())
  515. {
  516. cb->giveResources(h->tempOwner, resources);
  517. for(auto const & res : GameResID::ALL_RESOURCES())
  518. {
  519. if(resources[res] > 0)
  520. iw.components.emplace_back(ComponentType::RESOURCE, res, resources[res]);
  521. }
  522. }
  523. if(gainedArtifact != ArtifactID::NONE)
  524. {
  525. cb->giveHeroNewArtifact(h, gainedArtifact.toArtifact(), ArtifactPosition::FIRST_AVAILABLE);
  526. iw.components.emplace_back(ComponentType::ARTIFACT, gainedArtifact);
  527. }
  528. if(!iw.components.empty())
  529. {
  530. iw.type = EInfoWindowMode::AUTO;
  531. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 183); // % has found treasure
  532. iw.text.replaceRawString(h->getNameTranslated());
  533. cb->showInfoDialog(&iw);
  534. }
  535. }
  536. static const std::vector<std::string> CHARACTER_JSON =
  537. {
  538. "compliant", "friendly", "aggressive", "hostile", "savage"
  539. };
  540. void CGCreature::serializeJsonOptions(JsonSerializeFormat & handler)
  541. {
  542. handler.serializeEnum("character", character, CHARACTER_JSON);
  543. if(handler.saving)
  544. {
  545. if(hasStackAtSlot(SlotID(0)))
  546. {
  547. si32 amount = getStack(SlotID(0)).count;
  548. handler.serializeInt("amount", amount, 0);
  549. }
  550. }
  551. else
  552. {
  553. si32 amount = 0;
  554. handler.serializeInt("amount", amount);
  555. auto * hlp = new CStackInstance();
  556. hlp->count = amount;
  557. //type will be set during initialization
  558. putStack(SlotID(0), hlp);
  559. }
  560. resources.serializeJson(handler, "rewardResources");
  561. handler.serializeId("rewardArtifact", gainedArtifact, ArtifactID(ArtifactID::NONE));
  562. handler.serializeBool("noGrowing", notGrowingTeam);
  563. handler.serializeBool("neverFlees", neverFlees);
  564. handler.serializeStruct("rewardMessage", message);
  565. }
  566. VCMI_LIB_NAMESPACE_END