CGCreature.cpp 17 KB

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