CGCreature.cpp 18 KB

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