CGCreature.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  298. const CCreature * myCreature = getCreature().toCreature();
  299. myKindCres.insert(myCreature->getId()); //we
  300. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  301. for(auto const & crea : VLC->creh->objects)
  302. {
  303. if(vstd::contains(crea->upgrades, myCreature->getId())) //it's our base creatures
  304. myKindCres.insert(crea->getId());
  305. }
  306. int count = 0; //how many creatures of similar kind has hero
  307. int totalCount = 0;
  308. for(const auto & elem : h->Slots())
  309. {
  310. if(vstd::contains(myKindCres,elem.second->type->getId()))
  311. count += elem.second->count;
  312. totalCount += elem.second->count;
  313. }
  314. int sympathy = 0; // 0 if hero have no similar creatures
  315. if(count)
  316. sympathy++; // 1 - if hero have at least 1 similar creature
  317. if(count*2 > totalCount)
  318. sympathy++; // 2 - hero have similar creatures more that 50%
  319. int diplomacy = h->valOfBonuses(BonusType::WANDERING_CREATURES_JOIN_BONUS);
  320. int charisma = powerFactor + diplomacy + sympathy;
  321. if(charisma < character)
  322. return FIGHT;
  323. if (allowJoin)
  324. {
  325. if(diplomacy + sympathy + 1 >= character)
  326. return JOIN_FOR_FREE;
  327. else if(diplomacy * 2 + sympathy + 1 >= character)
  328. return VLC->creatures()->getById(getCreature())->getRecruitCost(EGameResID::GOLD) * getStackCount(SlotID(0)); //join for gold
  329. }
  330. //we are still here - creatures have not joined hero, flee or fight
  331. if (charisma > character && !neverFlees)
  332. return FLEE;
  333. else
  334. return FIGHT;
  335. }
  336. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  337. {
  338. if(refusedJoining)
  339. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  340. if(pursue)
  341. {
  342. fight(h);
  343. }
  344. else
  345. {
  346. cb->removeObject(this, h->getOwner());
  347. }
  348. }
  349. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  350. {
  351. if(!accept)
  352. {
  353. if(takenAction(h,false) == FLEE)
  354. {
  355. cb->setObjPropertyValue(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  356. flee(h);
  357. }
  358. else //they fight
  359. {
  360. h->showInfoDialog(87, 0, EInfoWindowMode::MODAL);//Insulted by your refusal of their offer, the monsters attack!
  361. fight(h);
  362. }
  363. }
  364. else //accepted
  365. {
  366. if (cb->getResource(h->tempOwner, EGameResID::GOLD) < cost) //player don't have enough gold!
  367. {
  368. InfoWindow iw;
  369. iw.player = h->tempOwner;
  370. iw.text.appendLocalString(EMetaText::GENERAL_TXT,29); //You don't have enough gold
  371. cb->showInfoDialog(&iw);
  372. //act as if player refused
  373. joinDecision(h,cost,false);
  374. return;
  375. }
  376. //take gold
  377. if(cost)
  378. cb->giveResource(h->tempOwner,EGameResID::GOLD,-cost);
  379. giveReward(h);
  380. cb->tryJoiningArmy(this, h, true, true);
  381. }
  382. }
  383. void CGCreature::fight( const CGHeroInstance *h ) const
  384. {
  385. //split stacks
  386. int stacksCount = getNumberOfStacks(h);
  387. //source: http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266335#focus
  388. int amount = getStackCount(SlotID(0));
  389. int m = amount / stacksCount;
  390. int b = stacksCount * (m + 1) - amount;
  391. int a = stacksCount - b;
  392. SlotID sourceSlot = stacks.begin()->first;
  393. for (int slotID = 1; slotID < a; ++slotID)
  394. {
  395. int stackSize = m + 1;
  396. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  397. }
  398. for (int slotID = a; slotID < stacksCount; ++slotID)
  399. {
  400. int stackSize = m;
  401. if (slotID) //don't do this when a = 0 -> stack is single
  402. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  403. }
  404. if (stacksCount > 1)
  405. {
  406. if (containsUpgradedStack()) //upgrade
  407. {
  408. SlotID slotID = SlotID(static_cast<si32>(std::floor(static_cast<float>(stacks.size()) / 2.0f)));
  409. const auto & upgrades = getStack(slotID).type->upgrades;
  410. if(!upgrades.empty())
  411. {
  412. auto it = RandomGeneratorUtil::nextItem(upgrades, CRandomGenerator::getDefault());
  413. cb->changeStackType(StackLocation(this, slotID), it->toCreature());
  414. }
  415. }
  416. }
  417. cb->startBattleI(h, this);
  418. }
  419. void CGCreature::flee( const CGHeroInstance * h ) const
  420. {
  421. BlockingDialog ynd(true,false);
  422. ynd.player = h->tempOwner;
  423. ynd.text.appendLocalString(EMetaText::ADVOB_TXT,91);
  424. ynd.text.replaceName(getCreature(), getStackCount(SlotID(0)));
  425. cb->showBlockingDialog(&ynd);
  426. }
  427. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  428. {
  429. if(result.winner == 0)
  430. {
  431. giveReward(hero);
  432. cb->removeObject(this, hero->getOwner());
  433. }
  434. else if(result.winner > 1) // draw
  435. {
  436. // guarded reward is lost forever on draw
  437. cb->removeObject(this, hero->getOwner());
  438. }
  439. else
  440. {
  441. //merge stacks into one
  442. TSlots::const_iterator i;
  443. const CCreature * cre = getCreature().toCreature();
  444. for(i = stacks.begin(); i != stacks.end(); i++)
  445. {
  446. if(cre->isMyUpgrade(i->second->type))
  447. {
  448. cb->changeStackType(StackLocation(this, i->first), cre); //un-upgrade creatures
  449. }
  450. }
  451. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  452. if(!hasStackAtSlot(SlotID(0)))
  453. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  454. while(stacks.size() > 1) //hopefully that's enough
  455. {
  456. // 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)
  457. i = stacks.end();
  458. i--;
  459. SlotID slot = getSlotFor(i->second->type);
  460. if(slot == i->first) //no reason to move stack to its own slot
  461. break;
  462. else
  463. cb->moveStack(StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  464. }
  465. cb->setObjPropertyValue(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  466. }
  467. }
  468. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  469. {
  470. auto action = takenAction(hero);
  471. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  472. joinDecision(hero, action, answer);
  473. else if(action != FIGHT)
  474. fleeDecision(hero, answer);
  475. else
  476. assert(0);
  477. }
  478. bool CGCreature::containsUpgradedStack() const
  479. {
  480. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=830557#focus
  481. float a = 2992.911117f;
  482. float b = 14174.264968f;
  483. float c = 5325.181015f;
  484. float d = 32788.727920f;
  485. int val = static_cast<int>(std::floor(a * pos.x + b * pos.y + c * pos.z + d));
  486. return ((val % 32768) % 100) < 50;
  487. }
  488. int CGCreature::getNumberOfStacks(const CGHeroInstance *hero) const
  489. {
  490. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266094#focus
  491. double strengthRatio = static_cast<double>(hero->getArmyStrength()) / getArmyStrength();
  492. int split = 1;
  493. if (strengthRatio < 0.5f)
  494. split = 7;
  495. else if (strengthRatio < 0.67f)
  496. split = 6;
  497. else if (strengthRatio < 1)
  498. split = 5;
  499. else if (strengthRatio < 1.5f)
  500. split = 4;
  501. else if (strengthRatio < 2)
  502. split = 3;
  503. else
  504. split = 2;
  505. ui32 a = 1550811371u;
  506. ui32 b = 3359066809u;
  507. ui32 c = 1943276003u;
  508. ui32 d = 3174620878u;
  509. ui32 R1 = a * static_cast<ui32>(pos.x) + b * static_cast<ui32>(pos.y) + c * static_cast<ui32>(pos.z) + d;
  510. ui32 R2 = (R1 >> 16) & 0x7fff;
  511. int R4 = R2 % 100 + 1;
  512. if (R4 <= 20)
  513. split -= 1;
  514. else if (R4 >= 80)
  515. split += 1;
  516. vstd::amin(split, getStack(SlotID(0)).count); //can't divide into more stacks than creatures total
  517. vstd::amin(split, 7); //can't have more than 7 stacks
  518. return split;
  519. }
  520. void CGCreature::giveReward(const CGHeroInstance * h) const
  521. {
  522. InfoWindow iw;
  523. iw.player = h->tempOwner;
  524. if(!resources.empty())
  525. {
  526. cb->giveResources(h->tempOwner, resources);
  527. for(auto const & res : GameResID::ALL_RESOURCES())
  528. {
  529. if(resources[res] > 0)
  530. iw.components.emplace_back(ComponentType::RESOURCE, res, resources[res]);
  531. }
  532. }
  533. if(gainedArtifact != ArtifactID::NONE)
  534. {
  535. cb->giveHeroNewArtifact(h, gainedArtifact.toArtifact(), ArtifactPosition::FIRST_AVAILABLE);
  536. iw.components.emplace_back(ComponentType::ARTIFACT, gainedArtifact);
  537. }
  538. if(!iw.components.empty())
  539. {
  540. iw.type = EInfoWindowMode::AUTO;
  541. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 183); // % has found treasure
  542. iw.text.replaceRawString(h->getNameTranslated());
  543. cb->showInfoDialog(&iw);
  544. }
  545. }
  546. static const std::vector<std::string> CHARACTER_JSON =
  547. {
  548. "compliant", "friendly", "aggressive", "hostile", "savage"
  549. };
  550. void CGCreature::serializeJsonOptions(JsonSerializeFormat & handler)
  551. {
  552. handler.serializeEnum("character", character, CHARACTER_JSON);
  553. if(handler.saving)
  554. {
  555. if(hasStackAtSlot(SlotID(0)))
  556. {
  557. si32 amount = getStack(SlotID(0)).count;
  558. handler.serializeInt("amount", amount, 0);
  559. }
  560. }
  561. else
  562. {
  563. si32 amount = 0;
  564. handler.serializeInt("amount", amount);
  565. auto * hlp = new CStackInstance();
  566. hlp->count = amount;
  567. //type will be set during initialization
  568. putStack(SlotID(0), hlp);
  569. }
  570. resources.serializeJson(handler, "rewardResources");
  571. handler.serializeId("rewardArtifact", gainedArtifact, ArtifactID(ArtifactID::NONE));
  572. handler.serializeBool("noGrowing", notGrowingTeam);
  573. handler.serializeBool("neverFlees", neverFlees);
  574. handler.serializeStruct("rewardMessage", message);
  575. }
  576. VCMI_LIB_NAMESPACE_END