CQuest.cpp 26 KB

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