CQuest.cpp 26 KB

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