CQuest.cpp 23 KB

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