CQuest.cpp 25 KB

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