CGCreature.cpp 18 KB

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