CQuest.cpp 25 KB

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