CQuest.cpp 24 KB

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