CQuest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 <ui8> > 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. ui32 temp;
  334. handler.serializeId<ui32, ui32, HeroTypeID>("hero", temp, 0);
  335. mission.heroes.emplace_back(temp);
  336. }
  337. if(missionType == "Player")
  338. {
  339. ui32 temp;
  340. handler.serializeId<ui32, ui32, PlayerColor>("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. void CGSeerHut::setPropertyDer(ui8 what, ui32 val)
  435. {
  436. switch(what)
  437. {
  438. case CGSeerHut::SEERHUT_VISITED:
  439. {
  440. quest->activeForPlayers.emplace(val);
  441. break;
  442. }
  443. case CGSeerHut::SEERHUT_COMPLETE:
  444. {
  445. quest->isCompleted = val;
  446. quest->activeForPlayers.clear();
  447. break;
  448. }
  449. }
  450. }
  451. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  452. {
  453. CRewardableObject::newTurn(rand);
  454. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  455. {
  456. cb->setObjProperty (id, CGSeerHut::SEERHUT_COMPLETE, true);
  457. }
  458. }
  459. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  460. {
  461. InfoWindow iw;
  462. iw.player = h->getOwner();
  463. if(!quest->isCompleted)
  464. {
  465. bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
  466. bool failRequirements = !checkQuest(h);
  467. if(firstVisit)
  468. {
  469. cb->setObjProperty(id, CGSeerHut::SEERHUT_VISITED, h->getOwner());
  470. AddQuest aq;
  471. aq.quest = QuestInfo (quest, this, visitablePos());
  472. aq.player = h->tempOwner;
  473. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  474. }
  475. if(firstVisit || failRequirements)
  476. {
  477. getVisitText (iw.text, iw.components, firstVisit, h);
  478. cb->showInfoDialog(&iw);
  479. }
  480. if(!failRequirements) // propose completion, also on first visit
  481. {
  482. CRewardableObject::onHeroVisit(h);
  483. return;
  484. }
  485. }
  486. else
  487. {
  488. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  489. if (ID == Obj::SEER_HUT)
  490. iw.text.replaceRawString(seerName);
  491. cb->showInfoDialog(&iw);
  492. }
  493. }
  494. int CGSeerHut::checkDirection() const
  495. {
  496. int3 cord = getCreatureToKill()->pos;
  497. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  498. {
  499. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  500. return 8;
  501. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  502. return 1;
  503. else //northeast
  504. return 2;
  505. }
  506. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  507. {
  508. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  509. return 7;
  510. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  511. return 9;
  512. else //east
  513. return 3;
  514. }
  515. else //south
  516. {
  517. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  518. return 6;
  519. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  520. return 5;
  521. else //southeast
  522. return 4;
  523. }
  524. }
  525. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  526. {
  527. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  528. if(allowNull && !o)
  529. return nullptr;
  530. return dynamic_cast<const CGHeroInstance *>(o);
  531. }
  532. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  533. {
  534. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  535. if(allowNull && !o)
  536. return nullptr;
  537. return dynamic_cast<const CGCreature *>(o);
  538. }
  539. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  540. {
  541. CRewardableObject::blockingDialogAnswered(hero, answer);
  542. if(answer)
  543. {
  544. quest->completeQuest(cb, hero);
  545. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, !quest->repeatedQuest); //mission complete
  546. }
  547. }
  548. void CGSeerHut::afterAddToMap(CMap* map)
  549. {
  550. IQuestObject::afterAddToMapCommon(map);
  551. }
  552. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  553. {
  554. //quest and reward
  555. CRewardableObject::serializeJsonOptions(handler);
  556. quest->serializeJson(handler, "quest");
  557. if(!handler.saving)
  558. {
  559. //backward compatibility for VCMI maps that use old SeerHut format
  560. auto s = handler.enterStruct("reward");
  561. const JsonNode & rewardsJson = handler.getCurrent();
  562. std::string fullIdentifier;
  563. std::string metaTypeName;
  564. std::string scope;
  565. std::string identifier;
  566. auto iter = rewardsJson.Struct().begin();
  567. fullIdentifier = iter->first;
  568. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  569. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  570. return;
  571. int val = 0;
  572. handler.serializeInt(fullIdentifier, val);
  573. Rewardable::VisitInfo vinfo;
  574. auto & reward = vinfo.reward;
  575. if(metaTypeName == "experience")
  576. reward.heroExperience = val;
  577. if(metaTypeName == "mana")
  578. reward.manaDiff = val;
  579. if(metaTypeName == "morale")
  580. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  581. if(metaTypeName == "luck")
  582. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  583. if(metaTypeName == "resource")
  584. {
  585. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  586. reward.resources[rawId] = val;
  587. }
  588. if(metaTypeName == "primarySkill")
  589. {
  590. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  591. reward.primary.at(rawId) = val;
  592. }
  593. if(metaTypeName == "secondarySkill")
  594. {
  595. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  596. reward.secondary[rawId] = val;
  597. }
  598. if(metaTypeName == "artifact")
  599. {
  600. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  601. reward.artifacts.push_back(rawId);
  602. }
  603. if(metaTypeName == "spell")
  604. {
  605. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  606. reward.spells.push_back(rawId);
  607. }
  608. if(metaTypeName == "creature")
  609. {
  610. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  611. reward.creatures.emplace_back(rawId, val);
  612. }
  613. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  614. configuration.info.push_back(vinfo);
  615. }
  616. }
  617. void CGQuestGuard::init(CRandomGenerator & rand)
  618. {
  619. blockVisit = true;
  620. quest->textOption = rand.nextInt(3, 5);
  621. quest->completedOption = rand.nextInt(4, 5);
  622. configuration.info.push_back({});
  623. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  624. configuration.info.back().reward.removeObject = subID == 0 ? true : false;
  625. configuration.canRefuse = true;
  626. }
  627. void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
  628. {
  629. if(!quest->isCompleted)
  630. CGSeerHut::onHeroVisit(h);
  631. else
  632. cb->setObjProperty(id, CGSeerHut::SEERHUT_COMPLETE, false);
  633. }
  634. bool CGQuestGuard::passableFor(PlayerColor color) const
  635. {
  636. return quest->isCompleted;
  637. }
  638. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  639. {
  640. //quest only, do not call base class
  641. quest->serializeJson(handler, "quest");
  642. }
  643. void CGKeys::reset()
  644. {
  645. playerKeyMap.clear();
  646. }
  647. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  648. {
  649. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  650. {
  651. PlayerColor player(what-101);
  652. playerKeyMap[player].insert(static_cast<ui8>(val));
  653. }
  654. else
  655. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  656. }
  657. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  658. {
  659. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  660. }
  661. std::string CGKeys::getHoverText(PlayerColor player) const
  662. {
  663. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  664. }
  665. std::string CGKeys::getObjectName() const
  666. {
  667. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  668. }
  669. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  670. {
  671. return wasMyColorVisited (player);
  672. }
  673. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  674. {
  675. int txt_id;
  676. if (!wasMyColorVisited (h->getOwner()) )
  677. {
  678. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  679. txt_id=19;
  680. }
  681. else
  682. txt_id=20;
  683. h->showInfoDialog(txt_id);
  684. }
  685. void CGBorderGuard::initObj(CRandomGenerator & rand)
  686. {
  687. blockVisit = true;
  688. }
  689. void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  690. {
  691. text.appendLocalString(EMetaText::ADVOB_TXT, 18);
  692. }
  693. void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
  694. {
  695. if (!onHover)
  696. {
  697. text.appendRawString(VLC->generaltexth->tentColors[subID]);
  698. text.appendRawString(" ");
  699. text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  700. }
  701. }
  702. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  703. {
  704. return wasMyColorVisited (h->tempOwner);
  705. }
  706. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  707. {
  708. if (wasMyColorVisited (h->getOwner()) )
  709. {
  710. BlockingDialog bd (true, false);
  711. bd.player = h->getOwner();
  712. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  713. cb->showBlockingDialog (&bd);
  714. }
  715. else
  716. {
  717. h->showInfoDialog(18);
  718. AddQuest aq;
  719. aq.quest = QuestInfo (quest, this, visitablePos());
  720. aq.player = h->tempOwner;
  721. cb->sendAndApply (&aq);
  722. //TODO: add this quest only once OR check for multiple instances later
  723. }
  724. }
  725. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  726. {
  727. if (answer)
  728. cb->removeObject(this, hero->getOwner());
  729. }
  730. void CGBorderGuard::afterAddToMap(CMap * map)
  731. {
  732. IQuestObject::afterAddToMapCommon(map);
  733. }
  734. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  735. {
  736. if (!wasMyColorVisited (h->getOwner()) )
  737. {
  738. h->showInfoDialog(18);
  739. AddQuest aq;
  740. aq.quest = QuestInfo (quest, this, visitablePos());
  741. aq.player = h->tempOwner;
  742. cb->sendAndApply (&aq);
  743. }
  744. }
  745. bool CGBorderGate::passableFor(PlayerColor color) const
  746. {
  747. return wasMyColorVisited(color);
  748. }
  749. VCMI_LIB_NAMESPACE_END