2
0

CQuest.cpp 23 KB

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