CQuest.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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.type)
  181. {
  182. components.emplace_back(ComponentType::CREATURE, stackToKill.getId(), stackToKill.getCount());
  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(int i = 0; i < 7; ++i)
  211. {
  212. if(mission.resources[i])
  213. {
  214. loot.appendRawString("%d %s");
  215. loot.replaceNumber(mission.resources[i]);
  216. loot.replaceLocalString(EMetaText::RES_NAMES, 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.getType()) 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.type)
  281. {
  282. out.replaceName(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)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  364. assert(quest->stackToKill.type);
  365. quest->stackToKill.count = 0; //no count in info window
  366. quest->stackDirection = checkDirection();
  367. }
  368. else if(getHeroToKill(true))
  369. {
  370. quest->heroName = getHeroToKill(false)->getNameTranslated();
  371. quest->heroPortrait = getHeroToKill(false)->getPortraitSource();
  372. }
  373. }
  374. void CGSeerHut::init(CRandomGenerator & rand)
  375. {
  376. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  377. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  378. seerName = VLC->generaltexth->translate(seerNameID);
  379. quest->textOption = rand.nextInt(2);
  380. quest->completedOption = rand.nextInt(1, 3);
  381. configuration.canRefuse = true;
  382. configuration.visitMode = Rewardable::EVisitMode::VISIT_ONCE;
  383. configuration.selectMode = Rewardable::ESelectMode::SELECT_PLAYER;
  384. }
  385. void CGSeerHut::initObj(CRandomGenerator & rand)
  386. {
  387. init(rand);
  388. CRewardableObject::initObj(rand);
  389. setObjToKill();
  390. quest->defineQuestName();
  391. if(quest->mission == Rewardable::Limiter{} && quest->killTarget == ObjectInstanceID::NONE)
  392. quest->isCompleted = true;
  393. if(quest->questName == quest->missionName(0))
  394. {
  395. quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
  396. }
  397. else
  398. {
  399. if(!quest->isCustomFirst)
  400. quest->firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(0), quest->textOption).get());
  401. if(!quest->isCustomNext)
  402. quest->nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(1), quest->textOption).get());
  403. if(!quest->isCustomComplete)
  404. quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest-> questName, quest->missionState(2), quest->textOption).get());
  405. }
  406. quest->getCompletionText(configuration.onSelect);
  407. for(auto & i : configuration.info)
  408. quest->getCompletionText(i.message);
  409. }
  410. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  411. {
  412. quest->getRolloverText(text, onHover);//TODO: simplify?
  413. if(!onHover)
  414. text.replaceRawString(seerName);
  415. }
  416. std::string CGSeerHut::getHoverText(PlayerColor player) const
  417. {
  418. std::string hoverName = getObjectName();
  419. if(ID == Obj::SEER_HUT && quest->activeForPlayers.count(player))
  420. {
  421. hoverName = VLC->generaltexth->allTexts[347];
  422. boost::algorithm::replace_first(hoverName, "%s", seerName);
  423. }
  424. if(quest->activeForPlayers.count(player)
  425. && (quest->mission != Rewardable::Limiter{}
  426. || quest->killTarget != ObjectInstanceID::NONE)) //rollover when the quest is active
  427. {
  428. MetaString ms;
  429. getRolloverText (ms, true);
  430. hoverName += ms.toString();
  431. }
  432. return hoverName;
  433. }
  434. std::string CGSeerHut::getHoverText(const CGHeroInstance * hero) const
  435. {
  436. return getHoverText(hero->getOwner());
  437. }
  438. std::string CGSeerHut::getPopupText(PlayerColor player) const
  439. {
  440. return getHoverText(player);
  441. }
  442. std::string CGSeerHut::getPopupText(const CGHeroInstance * hero) const
  443. {
  444. return getHoverText(hero->getOwner());
  445. }
  446. std::vector<Component> CGSeerHut::getPopupComponents(PlayerColor player) const
  447. {
  448. std::vector<Component> result;
  449. if (quest->activeForPlayers.count(player))
  450. quest->mission.loadComponents(result, nullptr);
  451. return result;
  452. }
  453. std::vector<Component> CGSeerHut::getPopupComponents(const CGHeroInstance * hero) const
  454. {
  455. std::vector<Component> result;
  456. if (quest->activeForPlayers.count(hero->getOwner()))
  457. quest->mission.loadComponents(result, hero);
  458. return result;
  459. }
  460. void CGSeerHut::setPropertyDer(ui8 what, ui32 val)
  461. {
  462. switch(what)
  463. {
  464. case CGSeerHut::SEERHUT_VISITED:
  465. {
  466. quest->activeForPlayers.emplace(val);
  467. break;
  468. }
  469. case CGSeerHut::SEERHUT_COMPLETE:
  470. {
  471. quest->isCompleted = val;
  472. quest->activeForPlayers.clear();
  473. break;
  474. }
  475. }
  476. }
  477. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  478. {
  479. CRewardableObject::newTurn(rand);
  480. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  481. {
  482. cb->setObjProperty (id, CGSeerHut::SEERHUT_COMPLETE, true);
  483. }
  484. }
  485. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  486. {
  487. InfoWindow iw;
  488. iw.player = h->getOwner();
  489. if(!quest->isCompleted)
  490. {
  491. bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
  492. bool failRequirements = !checkQuest(h);
  493. if(firstVisit)
  494. {
  495. cb->setObjProperty(id, CGSeerHut::SEERHUT_VISITED, h->getOwner());
  496. AddQuest aq;
  497. aq.quest = QuestInfo (quest, this, visitablePos());
  498. aq.player = h->tempOwner;
  499. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  500. }
  501. if(firstVisit || failRequirements)
  502. {
  503. getVisitText (iw.text, iw.components, firstVisit, h);
  504. cb->showInfoDialog(&iw);
  505. }
  506. if(!failRequirements) // propose completion, also on first visit
  507. {
  508. CRewardableObject::onHeroVisit(h);
  509. return;
  510. }
  511. }
  512. else
  513. {
  514. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  515. if (ID == Obj::SEER_HUT)
  516. iw.text.replaceRawString(seerName);
  517. cb->showInfoDialog(&iw);
  518. }
  519. }
  520. int CGSeerHut::checkDirection() const
  521. {
  522. int3 cord = getCreatureToKill()->pos;
  523. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  524. {
  525. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  526. return 8;
  527. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  528. return 1;
  529. else //northeast
  530. return 2;
  531. }
  532. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  533. {
  534. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  535. return 7;
  536. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  537. return 9;
  538. else //east
  539. return 3;
  540. }
  541. else //south
  542. {
  543. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  544. return 6;
  545. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  546. return 5;
  547. else //southeast
  548. return 4;
  549. }
  550. }
  551. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  552. {
  553. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  554. if(allowNull && !o)
  555. return nullptr;
  556. return dynamic_cast<const CGHeroInstance *>(o);
  557. }
  558. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  559. {
  560. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  561. if(allowNull && !o)
  562. return nullptr;
  563. return dynamic_cast<const CGCreature *>(o);
  564. }
  565. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  566. {
  567. CRewardableObject::blockingDialogAnswered(hero, answer);
  568. if(answer)
  569. {
  570. quest->completeQuest(cb, hero);
  571. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, !quest->repeatedQuest); //mission complete
  572. }
  573. }
  574. void CGSeerHut::afterAddToMap(CMap* map)
  575. {
  576. IQuestObject::afterAddToMapCommon(map);
  577. }
  578. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  579. {
  580. //quest and reward
  581. CRewardableObject::serializeJsonOptions(handler);
  582. quest->serializeJson(handler, "quest");
  583. if(!handler.saving)
  584. {
  585. //backward compatibility for VCMI maps that use old SeerHut format
  586. auto s = handler.enterStruct("reward");
  587. const JsonNode & rewardsJson = handler.getCurrent();
  588. std::string fullIdentifier;
  589. std::string metaTypeName;
  590. std::string scope;
  591. std::string identifier;
  592. auto iter = rewardsJson.Struct().begin();
  593. fullIdentifier = iter->first;
  594. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  595. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  596. return;
  597. int val = 0;
  598. handler.serializeInt(fullIdentifier, val);
  599. Rewardable::VisitInfo vinfo;
  600. auto & reward = vinfo.reward;
  601. if(metaTypeName == "experience")
  602. reward.heroExperience = val;
  603. if(metaTypeName == "mana")
  604. reward.manaDiff = val;
  605. if(metaTypeName == "morale")
  606. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  607. if(metaTypeName == "luck")
  608. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  609. if(metaTypeName == "resource")
  610. {
  611. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  612. reward.resources[rawId] = val;
  613. }
  614. if(metaTypeName == "primarySkill")
  615. {
  616. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  617. reward.primary.at(rawId) = val;
  618. }
  619. if(metaTypeName == "secondarySkill")
  620. {
  621. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  622. reward.secondary[rawId] = val;
  623. }
  624. if(metaTypeName == "artifact")
  625. {
  626. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  627. reward.artifacts.push_back(rawId);
  628. }
  629. if(metaTypeName == "spell")
  630. {
  631. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  632. reward.spells.push_back(rawId);
  633. }
  634. if(metaTypeName == "creature")
  635. {
  636. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  637. reward.creatures.emplace_back(rawId, val);
  638. }
  639. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  640. configuration.info.push_back(vinfo);
  641. }
  642. }
  643. void CGQuestGuard::init(CRandomGenerator & rand)
  644. {
  645. blockVisit = true;
  646. quest->textOption = rand.nextInt(3, 5);
  647. quest->completedOption = rand.nextInt(4, 5);
  648. configuration.info.push_back({});
  649. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  650. configuration.info.back().reward.removeObject = subID.getNum() == 0 ? true : false;
  651. configuration.canRefuse = true;
  652. }
  653. void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
  654. {
  655. if(!quest->isCompleted)
  656. CGSeerHut::onHeroVisit(h);
  657. else
  658. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, false);
  659. }
  660. bool CGQuestGuard::passableFor(PlayerColor color) const
  661. {
  662. return quest->isCompleted;
  663. }
  664. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  665. {
  666. //quest only, do not call base class
  667. quest->serializeJson(handler, "quest");
  668. }
  669. void CGKeys::reset()
  670. {
  671. playerKeyMap.clear();
  672. }
  673. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  674. {
  675. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  676. {
  677. PlayerColor player(what-101);
  678. playerKeyMap[player].insert(static_cast<ui8>(val));
  679. }
  680. else
  681. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  682. }
  683. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  684. {
  685. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  686. }
  687. std::string CGKeys::getHoverText(PlayerColor player) const
  688. {
  689. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  690. }
  691. std::string CGKeys::getObjectName() const
  692. {
  693. return VLC->generaltexth->tentColors[subID.getNum()] + " " + CGObjectInstance::getObjectName();
  694. }
  695. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  696. {
  697. return wasMyColorVisited (player);
  698. }
  699. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  700. {
  701. int txt_id;
  702. if (!wasMyColorVisited (h->getOwner()) )
  703. {
  704. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  705. txt_id=19;
  706. }
  707. else
  708. txt_id=20;
  709. h->showInfoDialog(txt_id);
  710. }
  711. void CGBorderGuard::initObj(CRandomGenerator & rand)
  712. {
  713. blockVisit = true;
  714. }
  715. void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  716. {
  717. text.appendLocalString(EMetaText::ADVOB_TXT, 18);
  718. }
  719. void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
  720. {
  721. if (!onHover)
  722. {
  723. text.appendRawString(VLC->generaltexth->tentColors[subID.getNum()]);
  724. text.appendRawString(" ");
  725. text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  726. }
  727. }
  728. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  729. {
  730. return wasMyColorVisited (h->tempOwner);
  731. }
  732. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  733. {
  734. if (wasMyColorVisited (h->getOwner()) )
  735. {
  736. BlockingDialog bd (true, false);
  737. bd.player = h->getOwner();
  738. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  739. cb->showBlockingDialog (&bd);
  740. }
  741. else
  742. {
  743. h->showInfoDialog(18);
  744. AddQuest aq;
  745. aq.quest = QuestInfo (quest, this, visitablePos());
  746. aq.player = h->tempOwner;
  747. cb->sendAndApply (&aq);
  748. //TODO: add this quest only once OR check for multiple instances later
  749. }
  750. }
  751. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  752. {
  753. if (answer)
  754. cb->removeObject(this, hero->getOwner());
  755. }
  756. void CGBorderGuard::afterAddToMap(CMap * map)
  757. {
  758. IQuestObject::afterAddToMapCommon(map);
  759. }
  760. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  761. {
  762. if (!wasMyColorVisited (h->getOwner()) )
  763. {
  764. h->showInfoDialog(18);
  765. AddQuest aq;
  766. aq.quest = QuestInfo (quest, this, visitablePos());
  767. aq.player = h->tempOwner;
  768. cb->sendAndApply (&aq);
  769. }
  770. }
  771. bool CGBorderGate::passableFor(PlayerColor color) const
  772. {
  773. return wasMyColorVisited(color);
  774. }
  775. VCMI_LIB_NAMESPACE_END