CGCreature.cpp 18 KB

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