CGCreature.cpp 18 KB

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