2
0

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.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.replaceLocalString(EMetaText::ART_NAMES, 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.replaceCreatureName(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.appendLocalString(EMetaText::COLOR, 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.replaceCreatureName(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. 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. quest->mission.loadComponents(result, hero);
  456. return result;
  457. }
  458. void CGSeerHut::setPropertyDer(ui8 what, ui32 val)
  459. {
  460. switch(what)
  461. {
  462. case CGSeerHut::SEERHUT_VISITED:
  463. {
  464. quest->activeForPlayers.emplace(val);
  465. break;
  466. }
  467. case CGSeerHut::SEERHUT_COMPLETE:
  468. {
  469. quest->isCompleted = val;
  470. quest->activeForPlayers.clear();
  471. break;
  472. }
  473. }
  474. }
  475. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  476. {
  477. CRewardableObject::newTurn(rand);
  478. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  479. {
  480. cb->setObjProperty (id, CGSeerHut::SEERHUT_COMPLETE, true);
  481. }
  482. }
  483. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  484. {
  485. InfoWindow iw;
  486. iw.player = h->getOwner();
  487. if(!quest->isCompleted)
  488. {
  489. bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
  490. bool failRequirements = !checkQuest(h);
  491. if(firstVisit)
  492. {
  493. cb->setObjProperty(id, CGSeerHut::SEERHUT_VISITED, h->getOwner());
  494. AddQuest aq;
  495. aq.quest = QuestInfo (quest, this, visitablePos());
  496. aq.player = h->tempOwner;
  497. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  498. }
  499. if(firstVisit || failRequirements)
  500. {
  501. getVisitText (iw.text, iw.components, firstVisit, h);
  502. cb->showInfoDialog(&iw);
  503. }
  504. if(!failRequirements) // propose completion, also on first visit
  505. {
  506. CRewardableObject::onHeroVisit(h);
  507. return;
  508. }
  509. }
  510. else
  511. {
  512. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  513. if (ID == Obj::SEER_HUT)
  514. iw.text.replaceRawString(seerName);
  515. cb->showInfoDialog(&iw);
  516. }
  517. }
  518. int CGSeerHut::checkDirection() const
  519. {
  520. int3 cord = getCreatureToKill()->pos;
  521. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  522. {
  523. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  524. return 8;
  525. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  526. return 1;
  527. else //northeast
  528. return 2;
  529. }
  530. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  531. {
  532. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  533. return 7;
  534. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  535. return 9;
  536. else //east
  537. return 3;
  538. }
  539. else //south
  540. {
  541. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  542. return 6;
  543. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  544. return 5;
  545. else //southeast
  546. return 4;
  547. }
  548. }
  549. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  550. {
  551. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  552. if(allowNull && !o)
  553. return nullptr;
  554. return dynamic_cast<const CGHeroInstance *>(o);
  555. }
  556. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  557. {
  558. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  559. if(allowNull && !o)
  560. return nullptr;
  561. return dynamic_cast<const CGCreature *>(o);
  562. }
  563. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  564. {
  565. CRewardableObject::blockingDialogAnswered(hero, answer);
  566. if(answer)
  567. {
  568. quest->completeQuest(cb, hero);
  569. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, !quest->repeatedQuest); //mission complete
  570. }
  571. }
  572. void CGSeerHut::afterAddToMap(CMap* map)
  573. {
  574. IQuestObject::afterAddToMapCommon(map);
  575. }
  576. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  577. {
  578. //quest and reward
  579. CRewardableObject::serializeJsonOptions(handler);
  580. quest->serializeJson(handler, "quest");
  581. if(!handler.saving)
  582. {
  583. //backward compatibility for VCMI maps that use old SeerHut format
  584. auto s = handler.enterStruct("reward");
  585. const JsonNode & rewardsJson = handler.getCurrent();
  586. std::string fullIdentifier;
  587. std::string metaTypeName;
  588. std::string scope;
  589. std::string identifier;
  590. auto iter = rewardsJson.Struct().begin();
  591. fullIdentifier = iter->first;
  592. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  593. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  594. return;
  595. int val = 0;
  596. handler.serializeInt(fullIdentifier, val);
  597. Rewardable::VisitInfo vinfo;
  598. auto & reward = vinfo.reward;
  599. if(metaTypeName == "experience")
  600. reward.heroExperience = val;
  601. if(metaTypeName == "mana")
  602. reward.manaDiff = val;
  603. if(metaTypeName == "morale")
  604. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  605. if(metaTypeName == "luck")
  606. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  607. if(metaTypeName == "resource")
  608. {
  609. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  610. reward.resources[rawId] = val;
  611. }
  612. if(metaTypeName == "primarySkill")
  613. {
  614. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  615. reward.primary.at(rawId) = val;
  616. }
  617. if(metaTypeName == "secondarySkill")
  618. {
  619. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  620. reward.secondary[rawId] = val;
  621. }
  622. if(metaTypeName == "artifact")
  623. {
  624. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  625. reward.artifacts.push_back(rawId);
  626. }
  627. if(metaTypeName == "spell")
  628. {
  629. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  630. reward.spells.push_back(rawId);
  631. }
  632. if(metaTypeName == "creature")
  633. {
  634. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  635. reward.creatures.emplace_back(rawId, val);
  636. }
  637. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  638. configuration.info.push_back(vinfo);
  639. }
  640. }
  641. void CGQuestGuard::init(CRandomGenerator & rand)
  642. {
  643. blockVisit = true;
  644. quest->textOption = rand.nextInt(3, 5);
  645. quest->completedOption = rand.nextInt(4, 5);
  646. configuration.info.push_back({});
  647. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  648. configuration.info.back().reward.removeObject = subID.getNum() == 0 ? true : false;
  649. configuration.canRefuse = true;
  650. }
  651. void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
  652. {
  653. if(!quest->isCompleted)
  654. CGSeerHut::onHeroVisit(h);
  655. else
  656. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, false);
  657. }
  658. bool CGQuestGuard::passableFor(PlayerColor color) const
  659. {
  660. return quest->isCompleted;
  661. }
  662. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  663. {
  664. //quest only, do not call base class
  665. quest->serializeJson(handler, "quest");
  666. }
  667. void CGKeys::reset()
  668. {
  669. playerKeyMap.clear();
  670. }
  671. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  672. {
  673. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  674. {
  675. PlayerColor player(what-101);
  676. playerKeyMap[player].insert(static_cast<ui8>(val));
  677. }
  678. else
  679. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  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->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  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