CQuest.cpp 24 KB

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