CQuest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * CQuest.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 "CQuest.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include "../ArtifactUtils.h"
  14. #include "../NetPacks.h"
  15. #include "../CSoundBase.h"
  16. #include "../CGeneralTextHandler.h"
  17. #include "../CHeroHandler.h"
  18. #include "CGCreature.h"
  19. #include "../IGameCallback.h"
  20. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  21. #include "../serializer/JsonSerializeFormat.h"
  22. #include "../GameConstants.h"
  23. #include "../constants/StringConstants.h"
  24. #include "../CSkillHandler.h"
  25. #include "../mapping/CMap.h"
  26. #include "../modding/ModScope.h"
  27. #include "../modding/ModUtility.h"
  28. #include "../spells/CSpellHandler.h"
  29. VCMI_LIB_NAMESPACE_BEGIN
  30. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  31. //TODO: Remove constructor
  32. CQuest::CQuest():
  33. qid(-1),
  34. isCompleted(false),
  35. lastDay(-1),
  36. killTarget(ObjectInstanceID::NONE),
  37. textOption(0),
  38. completedOption(0),
  39. stackDirection(0),
  40. isCustomFirst(false),
  41. isCustomNext(false),
  42. isCustomComplete(false),
  43. repeatedQuest(false),
  44. questName(CQuest::missionName(0))
  45. {
  46. }
  47. static std::string visitedTxt(const bool visited)
  48. {
  49. int id = visited ? 352 : 353;
  50. return VLC->generaltexth->allTexts[id];
  51. }
  52. const std::string & CQuest::missionName(int mission)
  53. {
  54. static const std::array<std::string, 11> names = {
  55. "empty",
  56. "heroLevel",
  57. "primarySkill",
  58. "killHero",
  59. "killCreature",
  60. "bringArt",
  61. "bringCreature",
  62. "bringResources",
  63. "bringHero",
  64. "bringPlayer",
  65. "keymaster"
  66. };
  67. if(static_cast<size_t>(mission) < names.size())
  68. return names[static_cast<size_t>(mission)];
  69. return names[0];
  70. }
  71. const std::string & CQuest::missionState(int state)
  72. {
  73. static const std::array<std::string, 5> states = {
  74. "receive",
  75. "visit",
  76. "complete",
  77. "hover",
  78. "description",
  79. };
  80. if(state < states.size())
  81. return states[state];
  82. return states[0];
  83. }
  84. bool CQuest::checkMissionArmy(const CQuest * q, const CCreatureSet * army)
  85. {
  86. std::vector<CStackBasicDescriptor>::const_iterator cre;
  87. TSlots::const_iterator it;
  88. ui32 count = 0;
  89. ui32 slotsCount = 0;
  90. bool hasExtraCreatures = false;
  91. for(cre = q->mission.creatures.begin(); cre != q->mission.creatures.end(); ++cre)
  92. {
  93. for(count = 0, it = army->Slots().begin(); it != army->Slots().end(); ++it)
  94. {
  95. if(it->second->type == cre->type)
  96. {
  97. count += it->second->count;
  98. slotsCount++;
  99. }
  100. }
  101. if(static_cast<TQuantity>(count) < cre->count) //not enough creatures of this kind
  102. return false;
  103. hasExtraCreatures |= static_cast<TQuantity>(count) > cre->count;
  104. }
  105. return hasExtraCreatures || slotsCount < army->Slots().size();
  106. }
  107. bool CQuest::checkQuest(const CGHeroInstance * h) const
  108. {
  109. if(!mission.heroAllowed(h))
  110. return false;
  111. if(killTarget != ObjectInstanceID::NONE)
  112. {
  113. if(CGHeroInstance::cb->getObjByQuestIdentifier(killTarget))
  114. return false;
  115. }
  116. return true;
  117. }
  118. void CQuest::completeQuest(IGameCallback * cb, const CGHeroInstance *h) const
  119. {
  120. for(auto & elem : mission.artifacts)
  121. {
  122. if(h->hasArt(elem))
  123. {
  124. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  125. }
  126. else
  127. {
  128. const auto * assembly = h->getAssemblyByConstituent(elem);
  129. assert(assembly);
  130. auto parts = assembly->getPartsInfo();
  131. // Remove the assembly
  132. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  133. // Disassemble this backpack artifact
  134. for(const auto & ci : parts)
  135. {
  136. if(ci.art->getTypeId() != elem)
  137. cb->giveHeroNewArtifact(h, ci.art->artType, ArtifactPosition::BACKPACK_START);
  138. }
  139. }
  140. }
  141. cb->takeCreatures(h->id, mission.creatures);
  142. cb->giveResources(h->getOwner(), mission.resources);
  143. }
  144. void CQuest::addTextReplacements(MetaString & text, std::vector<Component> & components) const
  145. {
  146. if(mission.heroLevel > 0)
  147. text.replaceNumber(mission.heroLevel);
  148. if(mission.heroExperience > 0)
  149. text.replaceNumber(mission.heroExperience);
  150. { //primary skills
  151. MetaString loot;
  152. for(int i = 0; i < 4; ++i)
  153. {
  154. if(mission.primary[i])
  155. {
  156. loot.appendRawString("%d %s");
  157. loot.replaceNumber(mission.primary[i]);
  158. loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
  159. }
  160. }
  161. for(auto & skill : mission.secondary)
  162. {
  163. loot.appendTextID(VLC->skillh->getById(skill.first)->getNameTextID());
  164. }
  165. for(auto & spell : mission.spells)
  166. {
  167. loot.appendTextID(VLC->spellh->getById(spell)->getNameTextID());
  168. }
  169. if(!loot.empty())
  170. text.replaceRawString(loot.buildList());
  171. }
  172. if(killTarget != ObjectInstanceID::NONE && !heroName.empty())
  173. {
  174. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, heroPortrait, 0, 0);
  175. addKillTargetReplacements(text);
  176. }
  177. if(killTarget != ObjectInstanceID::NONE && stackToKill.type)
  178. {
  179. components.emplace_back(stackToKill);
  180. addKillTargetReplacements(text);
  181. }
  182. if(!mission.heroes.empty())
  183. text.replaceRawString(VLC->heroh->getById(mission.heroes.front())->getNameTranslated());
  184. if(!mission.artifacts.empty())
  185. {
  186. MetaString loot;
  187. for(const auto & elem : mission.artifacts)
  188. {
  189. loot.appendRawString("%s");
  190. loot.replaceLocalString(EMetaText::ART_NAMES, elem);
  191. }
  192. text.replaceRawString(loot.buildList());
  193. }
  194. if(!mission.creatures.empty())
  195. {
  196. MetaString loot;
  197. for(const auto & elem : mission.creatures)
  198. {
  199. loot.appendRawString("%s");
  200. loot.replaceCreatureName(elem);
  201. }
  202. text.replaceRawString(loot.buildList());
  203. }
  204. if(mission.resources.nonZero())
  205. {
  206. MetaString loot;
  207. for(int i = 0; i < 7; ++i)
  208. {
  209. if(mission.resources[i])
  210. {
  211. loot.appendRawString("%d %s");
  212. loot.replaceNumber(mission.resources[i]);
  213. loot.replaceLocalString(EMetaText::RES_NAMES, i);
  214. }
  215. }
  216. text.replaceRawString(loot.buildList());
  217. }
  218. if(!mission.players.empty())
  219. {
  220. MetaString loot;
  221. for(auto & p : mission.players)
  222. loot.appendLocalString(EMetaText::COLOR, p);
  223. text.replaceRawString(loot.buildList());
  224. }
  225. }
  226. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool firstVisit, const CGHeroInstance * h) const
  227. {
  228. bool failRequirements = (h ? !checkQuest(h) : true);
  229. mission.loadComponents(components, h);
  230. if(firstVisit)
  231. iwText.appendRawString(firstVisitText.toString());
  232. else if(failRequirements)
  233. iwText.appendRawString(nextVisitText.toString());
  234. addTextReplacements(iwText, components);
  235. }
  236. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  237. {
  238. if(onHover)
  239. ms.appendRawString("\n\n");
  240. std::string questState = missionState(onHover ? 3 : 4);
  241. ms.appendRawString(VLC->generaltexth->translate("core.seerhut.quest", questName, questState, textOption));
  242. std::vector<Component> components;
  243. addTextReplacements(ms, components);
  244. }
  245. void CQuest::getCompletionText(MetaString &iwText) const
  246. {
  247. iwText.appendRawString(completedText.toString());
  248. std::vector<Component> components;
  249. addTextReplacements(iwText, components);
  250. }
  251. void CQuest::defineQuestName()
  252. {
  253. //standard quests
  254. questName = CQuest::missionName(0);
  255. if(mission.heroLevel > 0) questName = CQuest::missionName(1);
  256. for(auto & s : mission.primary) if(s) questName = CQuest::missionName(2);
  257. if(!mission.spells.empty()) questName = CQuest::missionName(2);
  258. if(!mission.secondary.empty()) questName = CQuest::missionName(2);
  259. if(killTarget != ObjectInstanceID::NONE && !heroName.empty()) questName = CQuest::missionName(3);
  260. if(killTarget != ObjectInstanceID::NONE && stackToKill.getType()) questName = CQuest::missionName(4);
  261. if(!mission.artifacts.empty()) questName = CQuest::missionName(5);
  262. if(!mission.creatures.empty()) questName = CQuest::missionName(6);
  263. if(mission.resources.nonZero()) questName = CQuest::missionName(7);
  264. if(!mission.heroes.empty()) questName = CQuest::missionName(8);
  265. if(!mission.players.empty()) questName = CQuest::missionName(9);
  266. }
  267. void CQuest::addKillTargetReplacements(MetaString &out) const
  268. {
  269. if(!heroName.empty())
  270. out.replaceTextID(heroName);
  271. if(stackToKill.type)
  272. {
  273. out.replaceCreatureName(stackToKill);
  274. out.replaceRawString(VLC->generaltexth->arraytxt[147+stackDirection]);
  275. }
  276. }
  277. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  278. {
  279. auto q = handler.enterStruct(fieldName);
  280. handler.serializeStruct("firstVisitText", firstVisitText);
  281. handler.serializeStruct("nextVisitText", nextVisitText);
  282. handler.serializeStruct("completedText", completedText);
  283. handler.serializeBool("repeatedQuest", repeatedQuest, false);
  284. if(!handler.saving)
  285. {
  286. isCustomFirst = !firstVisitText.empty();
  287. isCustomNext = !nextVisitText.empty();
  288. isCustomComplete = !completedText.empty();
  289. }
  290. handler.serializeInt("timeLimit", lastDay, -1);
  291. handler.serializeStruct("limiter", mission);
  292. handler.serializeInstance("killTarget", killTarget, ObjectInstanceID::NONE);
  293. if(!handler.saving) //compatibility with legacy vmaps
  294. {
  295. std::string missionType = "None";
  296. handler.serializeString("missionType", missionType);
  297. if(missionType == "None")
  298. return;
  299. if(missionType == "Level")
  300. handler.serializeInt("heroLevel", mission.heroLevel);
  301. if(missionType == "PrimaryStat")
  302. {
  303. auto primarySkills = handler.enterStruct("primarySkills");
  304. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  305. handler.serializeInt(NPrimarySkill::names[i], mission.primary[i], 0);
  306. }
  307. if(missionType == "Artifact")
  308. handler.serializeIdArray<ArtifactID>("artifacts", mission.artifacts);
  309. if(missionType == "Army")
  310. {
  311. auto a = handler.enterArray("creatures");
  312. a.serializeStruct(mission.creatures);
  313. }
  314. if(missionType == "Resources")
  315. {
  316. auto r = handler.enterStruct("resources");
  317. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  318. {
  319. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mission.resources[idx], 0);
  320. }
  321. }
  322. if(missionType == "Hero")
  323. {
  324. ui32 temp;
  325. handler.serializeId<ui32, ui32, HeroTypeID>("hero", temp, 0);
  326. mission.heroes.emplace_back(temp);
  327. }
  328. if(missionType == "Player")
  329. {
  330. ui32 temp;
  331. handler.serializeId<ui32, ui32, PlayerColor>("player", temp, PlayerColor::NEUTRAL);
  332. mission.players.emplace_back(temp);
  333. }
  334. }
  335. }
  336. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  337. {
  338. return quest->checkQuest(h);
  339. }
  340. void IQuestObject::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  341. {
  342. quest->getVisitText(text, components, FirstVisit, h);
  343. }
  344. void IQuestObject::afterAddToMapCommon(CMap * map) const
  345. {
  346. map->addNewQuestInstance(quest);
  347. }
  348. void CGSeerHut::setObjToKill()
  349. {
  350. if(getCreatureToKill(true))
  351. {
  352. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  353. assert(quest->stackToKill.type);
  354. quest->stackToKill.count = 0; //no count in info window
  355. quest->stackDirection = checkDirection();
  356. }
  357. else if(getHeroToKill(true))
  358. {
  359. quest->heroName = getHeroToKill(false)->getNameTranslated();
  360. quest->heroPortrait = getHeroToKill(false)->getPortraitSource();
  361. }
  362. quest->getCompletionText(configuration.onSelect);
  363. for(auto & i : configuration.info)
  364. quest->getCompletionText(i.message);
  365. }
  366. void CGSeerHut::init(CRandomGenerator & rand)
  367. {
  368. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  369. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  370. seerName = VLC->generaltexth->translate(seerNameID);
  371. quest->textOption = rand.nextInt(2);
  372. quest->completedOption = rand.nextInt(1, 3);
  373. configuration.canRefuse = true;
  374. configuration.visitMode = Rewardable::EVisitMode::VISIT_ONCE;
  375. configuration.selectMode = Rewardable::ESelectMode::SELECT_PLAYER;
  376. }
  377. void CGSeerHut::initObj(CRandomGenerator & rand)
  378. {
  379. init(rand);
  380. CRewardableObject::initObj(rand);
  381. setObjToKill();
  382. quest->defineQuestName();
  383. if(quest->mission == Rewardable::Limiter{} && quest->killTarget == ObjectInstanceID::NONE)
  384. quest->isCompleted = true;
  385. if(quest->questName == quest->missionName(0))
  386. {
  387. quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
  388. }
  389. else
  390. {
  391. if(!quest->isCustomFirst)
  392. quest->firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(0), quest->textOption).get());
  393. if(!quest->isCustomNext)
  394. quest->nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(1), quest->textOption).get());
  395. if(!quest->isCustomComplete)
  396. quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest-> questName, quest->missionState(2), quest->textOption).get());
  397. }
  398. }
  399. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  400. {
  401. quest->getRolloverText(text, onHover);//TODO: simplify?
  402. if(!onHover)
  403. text.replaceRawString(seerName);
  404. }
  405. std::string CGSeerHut::getHoverText(PlayerColor player) const
  406. {
  407. std::string hoverName = getObjectName();
  408. if(ID == Obj::SEER_HUT && quest->activeForPlayers.count(player))
  409. {
  410. hoverName = VLC->generaltexth->allTexts[347];
  411. boost::algorithm::replace_first(hoverName, "%s", seerName);
  412. }
  413. if(quest->activeForPlayers.count(player)
  414. && (quest->mission != Rewardable::Limiter{}
  415. || quest->killTarget != ObjectInstanceID::NONE)) //rollover when the quest is active
  416. {
  417. MetaString ms;
  418. getRolloverText (ms, true);
  419. hoverName += ms.toString();
  420. }
  421. return hoverName;
  422. }
  423. void CGSeerHut::setPropertyDer(ui8 what, ui32 val)
  424. {
  425. switch(what)
  426. {
  427. case CGSeerHut::SEERHUT_VISITED:
  428. {
  429. quest->activeForPlayers.emplace(val);
  430. break;
  431. }
  432. case CGSeerHut::SEERHUT_COMPLETE:
  433. {
  434. quest->isCompleted = val;
  435. quest->activeForPlayers.clear();
  436. break;
  437. }
  438. }
  439. }
  440. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  441. {
  442. CRewardableObject::newTurn(rand);
  443. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  444. {
  445. cb->setObjProperty (id, CGSeerHut::SEERHUT_COMPLETE, true);
  446. }
  447. }
  448. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  449. {
  450. InfoWindow iw;
  451. iw.player = h->getOwner();
  452. if(!quest->isCompleted)
  453. {
  454. bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
  455. bool failRequirements = !checkQuest(h);
  456. if(firstVisit)
  457. {
  458. cb->setObjProperty(id, CGSeerHut::SEERHUT_VISITED, h->getOwner());
  459. AddQuest aq;
  460. aq.quest = QuestInfo (quest, this, visitablePos());
  461. aq.player = h->tempOwner;
  462. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  463. }
  464. if(firstVisit || failRequirements)
  465. {
  466. getVisitText (iw.text, iw.components, firstVisit, h);
  467. cb->showInfoDialog(&iw);
  468. }
  469. if(!failRequirements) // propose completion, also on first visit
  470. {
  471. CRewardableObject::onHeroVisit(h);
  472. return;
  473. }
  474. }
  475. else
  476. {
  477. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  478. if (ID == Obj::SEER_HUT)
  479. iw.text.replaceRawString(seerName);
  480. cb->showInfoDialog(&iw);
  481. }
  482. }
  483. int CGSeerHut::checkDirection() const
  484. {
  485. int3 cord = getCreatureToKill()->pos;
  486. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  487. {
  488. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  489. return 8;
  490. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  491. return 1;
  492. else //northeast
  493. return 2;
  494. }
  495. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  496. {
  497. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  498. return 7;
  499. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  500. return 9;
  501. else //east
  502. return 3;
  503. }
  504. else //south
  505. {
  506. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  507. return 6;
  508. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  509. return 5;
  510. else //southeast
  511. return 4;
  512. }
  513. }
  514. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  515. {
  516. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  517. if(allowNull && !o)
  518. return nullptr;
  519. return dynamic_cast<const CGHeroInstance *>(o);
  520. }
  521. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  522. {
  523. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  524. if(allowNull && !o)
  525. return nullptr;
  526. return dynamic_cast<const CGCreature *>(o);
  527. }
  528. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  529. {
  530. CRewardableObject::blockingDialogAnswered(hero, answer);
  531. if(answer)
  532. {
  533. quest->completeQuest(cb, hero);
  534. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, !quest->repeatedQuest); //mission complete
  535. }
  536. }
  537. void CGSeerHut::afterAddToMap(CMap* map)
  538. {
  539. IQuestObject::afterAddToMapCommon(map);
  540. }
  541. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  542. {
  543. //quest and reward
  544. CRewardableObject::serializeJsonOptions(handler);
  545. quest->serializeJson(handler, "quest");
  546. if(!handler.saving)
  547. {
  548. //backward compatibility for VCMI maps that use old SeerHut format
  549. auto s = handler.enterStruct("reward");
  550. const JsonNode & rewardsJson = handler.getCurrent();
  551. std::string fullIdentifier;
  552. std::string metaTypeName;
  553. std::string scope;
  554. std::string identifier;
  555. auto iter = rewardsJson.Struct().begin();
  556. fullIdentifier = iter->first;
  557. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  558. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  559. return;
  560. int val = 0;
  561. handler.serializeInt(fullIdentifier, val);
  562. Rewardable::VisitInfo vinfo;
  563. auto & reward = vinfo.reward;
  564. if(metaTypeName == "experience")
  565. reward.heroExperience = val;
  566. if(metaTypeName == "mana")
  567. reward.manaDiff = val;
  568. if(metaTypeName == "morale")
  569. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
  570. if(metaTypeName == "luck")
  571. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
  572. if(metaTypeName == "resource")
  573. {
  574. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  575. reward.resources[rawId] = val;
  576. }
  577. if(metaTypeName == "primarySkill")
  578. {
  579. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  580. reward.primary.at(rawId) = val;
  581. }
  582. if(metaTypeName == "secondarySkill")
  583. {
  584. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  585. reward.secondary[rawId] = val;
  586. }
  587. if(metaTypeName == "artifact")
  588. {
  589. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  590. reward.artifacts.push_back(rawId);
  591. }
  592. if(metaTypeName == "spell")
  593. {
  594. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  595. reward.spells.push_back(rawId);
  596. }
  597. if(metaTypeName == "creature")
  598. {
  599. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  600. reward.creatures.emplace_back(rawId, val);
  601. }
  602. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  603. configuration.info.push_back(vinfo);
  604. }
  605. }
  606. void CGQuestGuard::init(CRandomGenerator & rand)
  607. {
  608. blockVisit = true;
  609. quest->textOption = rand.nextInt(3, 5);
  610. quest->completedOption = rand.nextInt(4, 5);
  611. configuration.info.push_back({});
  612. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  613. configuration.info.back().reward.removeObject = subID == 0 ? true : false;
  614. configuration.canRefuse = true;
  615. }
  616. void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
  617. {
  618. if(!quest->isCompleted)
  619. CGSeerHut::onHeroVisit(h);
  620. else
  621. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, false);
  622. }
  623. bool CGQuestGuard::passableFor(PlayerColor color) const
  624. {
  625. return quest->isCompleted;
  626. }
  627. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  628. {
  629. //quest only, do not call base class
  630. quest->serializeJson(handler, "quest");
  631. }
  632. void CGKeys::reset()
  633. {
  634. playerKeyMap.clear();
  635. }
  636. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  637. {
  638. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  639. {
  640. PlayerColor player(what-101);
  641. playerKeyMap[player].insert(static_cast<ui8>(val));
  642. }
  643. else
  644. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  645. }
  646. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  647. {
  648. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  649. }
  650. std::string CGKeys::getHoverText(PlayerColor player) const
  651. {
  652. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  653. }
  654. std::string CGKeys::getObjectName() const
  655. {
  656. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  657. }
  658. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  659. {
  660. return wasMyColorVisited (player);
  661. }
  662. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  663. {
  664. int txt_id;
  665. if (!wasMyColorVisited (h->getOwner()) )
  666. {
  667. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  668. txt_id=19;
  669. }
  670. else
  671. txt_id=20;
  672. h->showInfoDialog(txt_id);
  673. }
  674. void CGBorderGuard::initObj(CRandomGenerator & rand)
  675. {
  676. blockVisit = true;
  677. }
  678. void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  679. {
  680. text.appendLocalString(EMetaText::ADVOB_TXT, 18);
  681. }
  682. void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
  683. {
  684. if (!onHover)
  685. {
  686. text.appendRawString(VLC->generaltexth->tentColors[subID]);
  687. text.appendRawString(" ");
  688. text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  689. }
  690. }
  691. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  692. {
  693. return wasMyColorVisited (h->tempOwner);
  694. }
  695. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  696. {
  697. if (wasMyColorVisited (h->getOwner()) )
  698. {
  699. BlockingDialog bd (true, false);
  700. bd.player = h->getOwner();
  701. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  702. cb->showBlockingDialog (&bd);
  703. }
  704. else
  705. {
  706. h->showInfoDialog(18);
  707. AddQuest aq;
  708. aq.quest = QuestInfo (quest, this, visitablePos());
  709. aq.player = h->tempOwner;
  710. cb->sendAndApply (&aq);
  711. //TODO: add this quest only once OR check for multiple instances later
  712. }
  713. }
  714. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  715. {
  716. if (answer)
  717. cb->removeObject(this, hero->getOwner());
  718. }
  719. void CGBorderGuard::afterAddToMap(CMap * map)
  720. {
  721. IQuestObject::afterAddToMapCommon(map);
  722. }
  723. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  724. {
  725. if (!wasMyColorVisited (h->getOwner()) )
  726. {
  727. h->showInfoDialog(18);
  728. AddQuest aq;
  729. aq.quest = QuestInfo (quest, this, visitablePos());
  730. aq.player = h->tempOwner;
  731. cb->sendAndApply (&aq);
  732. }
  733. }
  734. bool CGBorderGate::passableFor(PlayerColor color) const
  735. {
  736. return wasMyColorVisited(color);
  737. }
  738. VCMI_LIB_NAMESPACE_END