CQuest.cpp 23 KB

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