CQuest.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 "../NetPacks.h"
  14. #include "../CSoundBase.h"
  15. #include "../CGeneralTextHandler.h"
  16. #include "../CHeroHandler.h"
  17. #include "CObjectClassesHandler.h"
  18. #include "MiscObjects.h"
  19. #include "../IGameCallback.h"
  20. #include "../CGameState.h"
  21. #include "../serializer/JsonSerializeFormat.h"
  22. #include "../CModHandler.h"
  23. #include "../GameConstants.h"
  24. #include "../StringConstants.h"
  25. #include "../CSkillHandler.h"
  26. #include "../mapping/CMap.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  29. //TODO: Remove constructor
  30. CQuest::CQuest():
  31. qid(-1),
  32. missionType(MISSION_NONE),
  33. progress(NOT_ACTIVE),
  34. lastDay(-1),
  35. m13489val(0),
  36. textOption(0),
  37. completedOption(0),
  38. stackDirection(0),
  39. heroPortrait(-1),
  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::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)->constituentsInfo.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(auto i = EGameResID::WOOD; i <= EGameResID::GOLD; vstd::advance(i, +1)) //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. std::string text;
  165. bool failRequirements = (h ? !checkQuest(h) : true);
  166. if(firstVisit)
  167. {
  168. isCustom = isCustomFirst;
  169. iwText << (text = firstVisitText);
  170. }
  171. else if(failRequirements)
  172. {
  173. isCustom = isCustomNext;
  174. iwText << (text = nextVisitText);
  175. }
  176. switch (missionType)
  177. {
  178. case MISSION_LEVEL:
  179. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, m13489val, 0);
  180. if(!isCustom)
  181. iwText.addReplacement(m13489val);
  182. break;
  183. case MISSION_PRIMARY_STAT:
  184. {
  185. MetaString loot;
  186. for(int i = 0; i < 4; ++i)
  187. {
  188. if(m2stats[i])
  189. {
  190. components.emplace_back(Component::EComponentType::PRIM_SKILL, i, m2stats[i], 0);
  191. loot << "%d %s";
  192. loot.addReplacement(m2stats[i]);
  193. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  194. }
  195. }
  196. if (!isCustom)
  197. iwText.addReplacement(loot.buildList());
  198. }
  199. break;
  200. case MISSION_KILL_HERO:
  201. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, heroPortrait, 0, 0);
  202. if(!isCustom)
  203. addReplacements(iwText, text);
  204. break;
  205. case MISSION_HERO:
  206. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  207. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroh->objects[m13489val]->imageIndex, 0, 0);
  208. if(!isCustom)
  209. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  210. break;
  211. case MISSION_KILL_CREATURE:
  212. {
  213. components.emplace_back(stackToKill);
  214. if(!isCustom)
  215. {
  216. addReplacements(iwText, text);
  217. }
  218. }
  219. break;
  220. case MISSION_ART:
  221. {
  222. MetaString loot;
  223. for(const auto & elem : m5arts)
  224. {
  225. components.emplace_back(Component::EComponentType::ARTIFACT, elem, 0, 0);
  226. loot << "%s";
  227. loot.addReplacement(MetaString::ART_NAMES, elem);
  228. }
  229. if(!isCustom)
  230. iwText.addReplacement(loot.buildList());
  231. }
  232. break;
  233. case MISSION_ARMY:
  234. {
  235. MetaString loot;
  236. for(const auto & elem : m6creatures)
  237. {
  238. components.emplace_back(elem);
  239. loot << "%s";
  240. loot.addReplacement(elem);
  241. }
  242. if(!isCustom)
  243. iwText.addReplacement(loot.buildList());
  244. }
  245. break;
  246. case MISSION_RESOURCES:
  247. {
  248. MetaString loot;
  249. for(int i = 0; i < 7; ++i)
  250. {
  251. if(m7resources[i])
  252. {
  253. components.emplace_back(Component::EComponentType::RESOURCE, i, m7resources[i], 0);
  254. loot << "%d %s";
  255. loot.addReplacement(m7resources[i]);
  256. loot.addReplacement(MetaString::RES_NAMES, i);
  257. }
  258. }
  259. if(!isCustom)
  260. iwText.addReplacement(loot.buildList());
  261. }
  262. break;
  263. case MISSION_PLAYER:
  264. components.emplace_back(Component::EComponentType::FLAG, m13489val, 0, 0);
  265. if(!isCustom)
  266. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  267. break;
  268. }
  269. }
  270. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  271. {
  272. // Quests with MISSION_NONE type don't have a text for them
  273. assert(missionType != MISSION_NONE);
  274. if(onHover)
  275. ms << "\n\n";
  276. std::string questName = missionName(missionType);
  277. std::string questState = missionState(onHover ? 3 : 4);
  278. ms << VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption);
  279. switch(missionType)
  280. {
  281. case MISSION_LEVEL:
  282. ms.addReplacement(m13489val);
  283. break;
  284. case MISSION_PRIMARY_STAT:
  285. {
  286. MetaString loot;
  287. for (int i = 0; i < 4; ++i)
  288. {
  289. if (m2stats[i])
  290. {
  291. loot << "%d %s";
  292. loot.addReplacement(m2stats[i]);
  293. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  294. }
  295. }
  296. ms.addReplacement(loot.buildList());
  297. }
  298. break;
  299. case MISSION_KILL_HERO:
  300. ms.addReplacement(heroName);
  301. break;
  302. case MISSION_KILL_CREATURE:
  303. ms.addReplacement(stackToKill);
  304. break;
  305. case MISSION_ART:
  306. {
  307. MetaString loot;
  308. for(const auto & elem : m5arts)
  309. {
  310. loot << "%s";
  311. loot.addReplacement(MetaString::ART_NAMES, elem);
  312. }
  313. ms.addReplacement(loot.buildList());
  314. }
  315. break;
  316. case MISSION_ARMY:
  317. {
  318. MetaString loot;
  319. for(const auto & elem : m6creatures)
  320. {
  321. loot << "%s";
  322. loot.addReplacement(elem);
  323. }
  324. ms.addReplacement(loot.buildList());
  325. }
  326. break;
  327. case MISSION_RESOURCES:
  328. {
  329. MetaString loot;
  330. for (int i = 0; i < 7; ++i)
  331. {
  332. if (m7resources[i])
  333. {
  334. loot << "%d %s";
  335. loot.addReplacement(m7resources[i]);
  336. loot.addReplacement(MetaString::RES_NAMES, i);
  337. }
  338. }
  339. ms.addReplacement(loot.buildList());
  340. }
  341. break;
  342. case MISSION_HERO:
  343. ms.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  344. break;
  345. case MISSION_PLAYER:
  346. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  353. {
  354. iwText << completedText;
  355. switch(missionType)
  356. {
  357. case CQuest::MISSION_LEVEL:
  358. if (!isCustomComplete)
  359. iwText.addReplacement(m13489val);
  360. break;
  361. case CQuest::MISSION_PRIMARY_STAT:
  362. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  363. {
  364. MetaString loot;
  365. for (int i = 0; i < 4; ++i)
  366. {
  367. if (m2stats[i])
  368. {
  369. loot << "%d %s";
  370. loot.addReplacement(m2stats[i]);
  371. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  372. }
  373. }
  374. if (!isCustomComplete)
  375. iwText.addReplacement(loot.buildList());
  376. }
  377. break;
  378. case CQuest::MISSION_ART:
  379. {
  380. MetaString loot;
  381. for(const auto & elem : m5arts)
  382. {
  383. loot << "%s";
  384. loot.addReplacement(MetaString::ART_NAMES, elem);
  385. }
  386. if (!isCustomComplete)
  387. iwText.addReplacement(loot.buildList());
  388. }
  389. break;
  390. case CQuest::MISSION_ARMY:
  391. {
  392. MetaString loot;
  393. for(const auto & elem : m6creatures)
  394. {
  395. loot << "%s";
  396. loot.addReplacement(elem);
  397. }
  398. if (!isCustomComplete)
  399. iwText.addReplacement(loot.buildList());
  400. }
  401. break;
  402. case CQuest::MISSION_RESOURCES:
  403. {
  404. MetaString loot;
  405. for (int i = 0; i < 7; ++i)
  406. {
  407. if (m7resources[i])
  408. {
  409. loot << "%d %s";
  410. loot.addReplacement(m7resources[i]);
  411. loot.addReplacement(MetaString::RES_NAMES, i);
  412. }
  413. }
  414. if (!isCustomComplete)
  415. iwText.addReplacement(loot.buildList());
  416. }
  417. break;
  418. case MISSION_KILL_HERO:
  419. case MISSION_KILL_CREATURE:
  420. if (!isCustomComplete)
  421. addReplacements(iwText, completedText);
  422. break;
  423. case MISSION_HERO:
  424. if (!isCustomComplete)
  425. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  426. break;
  427. case MISSION_PLAYER:
  428. if (!isCustomComplete)
  429. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  430. break;
  431. }
  432. }
  433. void CQuest::addArtifactID(const ArtifactID & id)
  434. {
  435. m5arts.push_back(id);
  436. ++artifactsRequirements[id];
  437. }
  438. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  439. {
  440. auto q = handler.enterStruct(fieldName);
  441. handler.serializeString("firstVisitText", firstVisitText);
  442. handler.serializeString("nextVisitText", nextVisitText);
  443. handler.serializeString("completedText", completedText);
  444. if(!handler.saving)
  445. {
  446. isCustomFirst = !firstVisitText.empty();
  447. isCustomNext = !nextVisitText.empty();
  448. isCustomComplete = !completedText.empty();
  449. }
  450. static const std::vector<std::string> MISSION_TYPE_JSON =
  451. {
  452. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  453. };
  454. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  455. handler.serializeInt("timeLimit", lastDay, -1);
  456. switch (missionType)
  457. {
  458. case MISSION_NONE:
  459. break;
  460. case MISSION_LEVEL:
  461. handler.serializeInt("heroLevel", m13489val, -1);
  462. break;
  463. case MISSION_PRIMARY_STAT:
  464. {
  465. auto primarySkills = handler.enterStruct("primarySkills");
  466. if(!handler.saving)
  467. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  468. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  469. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  470. }
  471. break;
  472. case MISSION_KILL_HERO:
  473. case MISSION_KILL_CREATURE:
  474. handler.serializeInstance<ui32>("killTarget", m13489val, static_cast<ui32>(-1));
  475. break;
  476. case MISSION_ART:
  477. //todo: ban artifacts
  478. handler.serializeIdArray<ArtifactID>("artifacts", m5arts);
  479. break;
  480. case MISSION_ARMY:
  481. {
  482. auto a = handler.enterArray("creatures");
  483. a.serializeStruct(m6creatures);
  484. }
  485. break;
  486. case MISSION_RESOURCES:
  487. {
  488. auto r = handler.enterStruct("resources");
  489. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  490. {
  491. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  492. }
  493. }
  494. break;
  495. case MISSION_HERO:
  496. handler.serializeId<ui32, ui32, HeroTypeID>("hero", m13489val, 0);
  497. break;
  498. case MISSION_PLAYER:
  499. handler.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  500. break;
  501. default:
  502. logGlobal->error("Invalid quest mission type");
  503. break;
  504. }
  505. }
  506. void CGSeerHut::setObjToKill()
  507. {
  508. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  509. {
  510. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  511. assert(quest->stackToKill.type);
  512. quest->stackToKill.count = 0; //no count in info window
  513. quest->stackDirection = checkDirection();
  514. }
  515. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  516. {
  517. quest->heroName = getHeroToKill(false)->getNameTranslated();
  518. quest->heroPortrait = getHeroToKill(false)->portrait;
  519. }
  520. }
  521. void CGSeerHut::init(CRandomGenerator & rand)
  522. {
  523. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  524. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  525. seerName = VLC->generaltexth->translate(seerNameID);
  526. quest->textOption = rand.nextInt(2);
  527. quest->completedOption = rand.nextInt(1, 3);
  528. }
  529. void CGSeerHut::initObj(CRandomGenerator & rand)
  530. {
  531. init(rand);
  532. quest->progress = CQuest::NOT_ACTIVE;
  533. if(quest->missionType)
  534. {
  535. std::string questName = quest->missionName(quest->missionType);
  536. if(!quest->isCustomFirst)
  537. quest->firstVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(0), quest->textOption);
  538. if(!quest->isCustomNext)
  539. quest->nextVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(1), quest->textOption);
  540. if(!quest->isCustomComplete)
  541. quest->completedText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(2), quest->textOption);
  542. }
  543. else
  544. {
  545. quest->progress = CQuest::COMPLETE;
  546. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  547. }
  548. }
  549. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  550. {
  551. quest->getRolloverText (text, onHover);//TODO: simplify?
  552. if(!onHover)
  553. text.addReplacement(seerName);
  554. }
  555. std::string CGSeerHut::getHoverText(PlayerColor player) const
  556. {
  557. std::string hoverName = getObjectName();
  558. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  559. {
  560. hoverName = VLC->generaltexth->allTexts[347];
  561. boost::algorithm::replace_first(hoverName, "%s", seerName);
  562. }
  563. if(quest->progress & quest->missionType) //rollover when the quest is active
  564. {
  565. MetaString ms;
  566. getRolloverText (ms, true);
  567. hoverName += ms.toString();
  568. }
  569. return hoverName;
  570. }
  571. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  572. {
  573. switch(missionType)
  574. {
  575. case MISSION_KILL_CREATURE:
  576. out.addReplacement(stackToKill);
  577. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  578. {
  579. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  580. }
  581. break;
  582. case MISSION_KILL_HERO:
  583. out.addReplacement(heroName);
  584. break;
  585. }
  586. }
  587. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  588. {
  589. return quest->checkQuest(h);
  590. }
  591. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  592. {
  593. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  594. }
  595. void IQuestObject::afterAddToMapCommon(CMap * map) const
  596. {
  597. map->addNewQuestInstance(quest);
  598. }
  599. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  600. {
  601. quest->getCompletionText (text, components, isCustom, h);
  602. switch(rewardType)
  603. {
  604. case EXPERIENCE:
  605. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(rVal)), 0);
  606. break;
  607. case MANA_POINTS:
  608. components.emplace_back(Component::EComponentType::PRIM_SKILL, 5, rVal, 0);
  609. break;
  610. case MORALE_BONUS:
  611. components.emplace_back(Component::EComponentType::MORALE, 0, rVal, 0);
  612. break;
  613. case LUCK_BONUS:
  614. components.emplace_back(Component::EComponentType::LUCK, 0, rVal, 0);
  615. break;
  616. case RESOURCES:
  617. components.emplace_back(Component::EComponentType::RESOURCE, rID, rVal, 0);
  618. break;
  619. case PRIMARY_SKILL:
  620. components.emplace_back(Component::EComponentType::PRIM_SKILL, rID, rVal, 0);
  621. break;
  622. case SECONDARY_SKILL:
  623. components.emplace_back(Component::EComponentType::SEC_SKILL, rID, rVal, 0);
  624. break;
  625. case ARTIFACT:
  626. components.emplace_back(Component::EComponentType::ARTIFACT, rID, 0, 0);
  627. break;
  628. case SPELL:
  629. components.emplace_back(Component::EComponentType::SPELL, rID, 0, 0);
  630. break;
  631. case CREATURE:
  632. components.emplace_back(Component::EComponentType::CREATURE, rID, rVal, 0);
  633. break;
  634. }
  635. }
  636. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  637. {
  638. switch(what)
  639. {
  640. case 10:
  641. quest->progress = static_cast<CQuest::Eprogress>(val);
  642. break;
  643. }
  644. }
  645. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  646. {
  647. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  648. {
  649. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  650. }
  651. }
  652. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  653. {
  654. InfoWindow iw;
  655. iw.player = h->getOwner();
  656. if(quest->progress < CQuest::COMPLETE)
  657. {
  658. bool firstVisit = !quest->progress;
  659. bool failRequirements = !checkQuest(h);
  660. bool isCustom = false;
  661. if(firstVisit)
  662. {
  663. isCustom = quest->isCustomFirst;
  664. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  665. AddQuest aq;
  666. aq.quest = QuestInfo (quest, this, visitablePos());
  667. aq.player = h->tempOwner;
  668. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  669. }
  670. else if(failRequirements)
  671. {
  672. isCustom = quest->isCustomNext;
  673. }
  674. if(firstVisit || failRequirements)
  675. {
  676. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  677. cb->showInfoDialog(&iw);
  678. }
  679. if(!failRequirements) // propose completion, also on first visit
  680. {
  681. BlockingDialog bd (true, false);
  682. bd.player = h->getOwner();
  683. getCompletionText (bd.text, bd.components, isCustom, h);
  684. cb->showBlockingDialog (&bd);
  685. return;
  686. }
  687. }
  688. else
  689. {
  690. iw.text << VLC->generaltexth->seerEmpty[quest->completedOption];
  691. if (ID == Obj::SEER_HUT)
  692. iw.text.addReplacement(seerName);
  693. cb->showInfoDialog(&iw);
  694. }
  695. }
  696. int CGSeerHut::checkDirection() const
  697. {
  698. int3 cord = getCreatureToKill()->pos;
  699. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  700. {
  701. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  702. return 8;
  703. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  704. return 1;
  705. else //northeast
  706. return 2;
  707. }
  708. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  709. {
  710. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  711. return 7;
  712. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  713. return 9;
  714. else //east
  715. return 3;
  716. }
  717. else //south
  718. {
  719. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  720. return 6;
  721. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  722. return 5;
  723. else //southeast
  724. return 4;
  725. }
  726. }
  727. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  728. {
  729. if (accept)
  730. {
  731. switch (quest->missionType)
  732. {
  733. case CQuest::MISSION_ART:
  734. for(auto & elem : quest->m5arts)
  735. {
  736. if(h->hasArt(elem))
  737. {
  738. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  739. }
  740. else
  741. {
  742. const auto * assembly = h->getAssemblyByConstituent(elem);
  743. assert(assembly);
  744. auto parts = assembly->constituentsInfo;
  745. // Remove the assembly
  746. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  747. // Disassemble this backpack artifact
  748. for(const auto & ci : parts)
  749. {
  750. if(ci.art->getTypeId() != elem)
  751. cb->giveHeroNewArtifact(h, ci.art->artType, GameConstants::BACKPACK_START);
  752. }
  753. }
  754. }
  755. break;
  756. case CQuest::MISSION_ARMY:
  757. cb->takeCreatures(h->id, quest->m6creatures);
  758. break;
  759. case CQuest::MISSION_RESOURCES:
  760. for (int i = 0; i < 7; ++i)
  761. {
  762. cb->giveResource(h->getOwner(), static_cast<EGameResID>(i), -static_cast<int>(quest->m7resources[i]));
  763. }
  764. break;
  765. default:
  766. break;
  767. }
  768. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  769. completeQuest(h); //make sure to remove QuestGuard at the very end
  770. }
  771. }
  772. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  773. {
  774. switch (rewardType)
  775. {
  776. case EXPERIENCE:
  777. {
  778. TExpType expVal = h->calculateXp(rVal);
  779. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  780. break;
  781. }
  782. case MANA_POINTS:
  783. {
  784. cb->setManaPoints(h->id, h->mana+rVal);
  785. break;
  786. }
  787. case MORALE_BONUS: case LUCK_BONUS:
  788. {
  789. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  790. Bonus::OBJECT, rVal, h->id.getNum(), "", -1);
  791. GiveBonus gb;
  792. gb.id = h->id.getNum();
  793. gb.bonus = hb;
  794. cb->giveHeroBonus(&gb);
  795. }
  796. break;
  797. case RESOURCES:
  798. cb->giveResource(h->getOwner(), static_cast<EGameResID>(rID), rVal);
  799. break;
  800. case PRIMARY_SKILL:
  801. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  802. break;
  803. case SECONDARY_SKILL:
  804. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  805. break;
  806. case ARTIFACT:
  807. cb->giveHeroNewArtifact(h, VLC->arth->objects[rID],ArtifactPosition::FIRST_AVAILABLE);
  808. break;
  809. case SPELL:
  810. {
  811. std::set<SpellID> spell;
  812. spell.insert (SpellID(rID));
  813. cb->changeSpells(h, true, spell);
  814. }
  815. break;
  816. case CREATURE:
  817. {
  818. CCreatureSet creatures;
  819. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  820. cb->giveCreatures(this, h, creatures, false);
  821. }
  822. break;
  823. default:
  824. break;
  825. }
  826. }
  827. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  828. {
  829. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  830. if(allowNull && !o)
  831. return nullptr;
  832. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  833. return dynamic_cast<const CGHeroInstance *>(o);
  834. }
  835. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  836. {
  837. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  838. if(allowNull && !o)
  839. return nullptr;
  840. assert(o && o->ID == Obj::MONSTER);
  841. return dynamic_cast<const CGCreature *>(o);
  842. }
  843. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  844. {
  845. finishQuest(hero, answer);
  846. }
  847. void CGSeerHut::afterAddToMap(CMap* map)
  848. {
  849. IQuestObject::afterAddToMapCommon(map);
  850. }
  851. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  852. {
  853. static const std::map<ERewardType, std::string> REWARD_MAP =
  854. {
  855. {NOTHING, ""},
  856. {EXPERIENCE, "experience"},
  857. {MANA_POINTS, "mana"},
  858. {MORALE_BONUS, "morale"},
  859. {LUCK_BONUS, "luck"},
  860. {RESOURCES, "resource"},
  861. {PRIMARY_SKILL, "primarySkill"},
  862. {SECONDARY_SKILL,"secondarySkill"},
  863. {ARTIFACT, "artifact"},
  864. {SPELL, "spell"},
  865. {CREATURE, "creature"}
  866. };
  867. static const std::map<std::string, ERewardType> REWARD_RMAP =
  868. {
  869. {"experience", EXPERIENCE},
  870. {"mana", MANA_POINTS},
  871. {"morale", MORALE_BONUS},
  872. {"luck", LUCK_BONUS},
  873. {"resource", RESOURCES},
  874. {"primarySkill", PRIMARY_SKILL},
  875. {"secondarySkill",SECONDARY_SKILL},
  876. {"artifact", ARTIFACT},
  877. {"spell", SPELL},
  878. {"creature", CREATURE}
  879. };
  880. //quest and reward
  881. quest->serializeJson(handler, "quest");
  882. //only one reward is supported
  883. //todo: full reward format support after CRewardInfo integration
  884. auto s = handler.enterStruct("reward");
  885. std::string fullIdentifier;
  886. std::string metaTypeName;
  887. std::string scope;
  888. std::string identifier;
  889. if(handler.saving)
  890. {
  891. si32 amount = rVal;
  892. metaTypeName = REWARD_MAP.at(rewardType);
  893. switch (rewardType)
  894. {
  895. case NOTHING:
  896. break;
  897. case EXPERIENCE:
  898. case MANA_POINTS:
  899. case MORALE_BONUS:
  900. case LUCK_BONUS:
  901. identifier.clear();
  902. break;
  903. case RESOURCES:
  904. identifier = GameConstants::RESOURCE_NAMES[rID];
  905. break;
  906. case PRIMARY_SKILL:
  907. identifier = PrimarySkill::names[rID];
  908. break;
  909. case SECONDARY_SKILL:
  910. identifier = CSkillHandler::encodeSkill(rID);
  911. break;
  912. case ARTIFACT:
  913. identifier = ArtifactID(rID).toArtifact(VLC->artifacts())->getJsonKey();
  914. amount = 1;
  915. break;
  916. case SPELL:
  917. identifier = SpellID(rID).toSpell(VLC->spells())->getJsonKey();
  918. amount = 1;
  919. break;
  920. case CREATURE:
  921. identifier = CreatureID(rID).toCreature(VLC->creatures())->getJsonKey();
  922. break;
  923. default:
  924. assert(false);
  925. break;
  926. }
  927. if(rewardType != NOTHING)
  928. {
  929. fullIdentifier = CModHandler::makeFullIdentifier(scope, metaTypeName, identifier);
  930. handler.serializeInt(fullIdentifier, amount);
  931. }
  932. }
  933. else
  934. {
  935. rewardType = NOTHING;
  936. const JsonNode & rewardsJson = handler.getCurrent();
  937. fullIdentifier.clear();
  938. if(rewardsJson.Struct().empty())
  939. return;
  940. else
  941. {
  942. auto iter = rewardsJson.Struct().begin();
  943. fullIdentifier = iter->first;
  944. }
  945. CModHandler::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  946. auto it = REWARD_RMAP.find(metaTypeName);
  947. if(it == REWARD_RMAP.end())
  948. {
  949. logGlobal->error("%s: invalid metatype in reward item %s", instanceName, fullIdentifier);
  950. return;
  951. }
  952. else
  953. {
  954. rewardType = it->second;
  955. }
  956. bool doRequest = false;
  957. switch (rewardType)
  958. {
  959. case NOTHING:
  960. return;
  961. case EXPERIENCE:
  962. case MANA_POINTS:
  963. case MORALE_BONUS:
  964. case LUCK_BONUS:
  965. break;
  966. case PRIMARY_SKILL:
  967. doRequest = true;
  968. break;
  969. case RESOURCES:
  970. case SECONDARY_SKILL:
  971. case ARTIFACT:
  972. case SPELL:
  973. case CREATURE:
  974. doRequest = true;
  975. break;
  976. default:
  977. assert(false);
  978. break;
  979. }
  980. if(doRequest)
  981. {
  982. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), fullIdentifier, false);
  983. if(rawId)
  984. {
  985. rID = rawId.value();
  986. }
  987. else
  988. {
  989. rewardType = NOTHING;//fallback in case of error
  990. return;
  991. }
  992. }
  993. handler.serializeInt(fullIdentifier, rVal);
  994. }
  995. }
  996. void CGQuestGuard::init(CRandomGenerator & rand)
  997. {
  998. blockVisit = true;
  999. quest->textOption = rand.nextInt(3, 5);
  1000. quest->completedOption = rand.nextInt(4, 5);
  1001. }
  1002. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  1003. {
  1004. cb->removeObject(this);
  1005. }
  1006. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  1007. {
  1008. //quest only, do not call base class
  1009. quest->serializeJson(handler, "quest");
  1010. }
  1011. void CGKeys::reset()
  1012. {
  1013. playerKeyMap.clear();
  1014. }
  1015. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  1016. {
  1017. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  1018. {
  1019. PlayerColor player(what-101);
  1020. playerKeyMap[player].insert(static_cast<ui8>(val));
  1021. }
  1022. else
  1023. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  1024. }
  1025. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  1026. {
  1027. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  1028. }
  1029. std::string CGKeys::getHoverText(PlayerColor player) const
  1030. {
  1031. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  1032. }
  1033. std::string CGKeys::getObjectName() const
  1034. {
  1035. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  1036. }
  1037. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  1038. {
  1039. return wasMyColorVisited (player);
  1040. }
  1041. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  1042. {
  1043. int txt_id;
  1044. if (!wasMyColorVisited (h->getOwner()) )
  1045. {
  1046. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  1047. txt_id=19;
  1048. }
  1049. else
  1050. txt_id=20;
  1051. h->showInfoDialog(txt_id);
  1052. }
  1053. void CGBorderGuard::initObj(CRandomGenerator & rand)
  1054. {
  1055. //ui32 m13489val = subID; //store color as quest info
  1056. blockVisit = true;
  1057. }
  1058. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  1059. {
  1060. text << std::pair<ui8,ui32>(11,18);
  1061. }
  1062. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  1063. {
  1064. if (!onHover)
  1065. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID);
  1066. }
  1067. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  1068. {
  1069. return wasMyColorVisited (h->tempOwner);
  1070. }
  1071. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  1072. {
  1073. if (wasMyColorVisited (h->getOwner()) )
  1074. {
  1075. BlockingDialog bd (true, false);
  1076. bd.player = h->getOwner();
  1077. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  1078. cb->showBlockingDialog (&bd);
  1079. }
  1080. else
  1081. {
  1082. h->showInfoDialog(18);
  1083. AddQuest aq;
  1084. aq.quest = QuestInfo (quest, this, visitablePos());
  1085. aq.player = h->tempOwner;
  1086. cb->sendAndApply (&aq);
  1087. //TODO: add this quest only once OR check for multiple instances later
  1088. }
  1089. }
  1090. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1091. {
  1092. if (answer)
  1093. cb->removeObject(this);
  1094. }
  1095. void CGBorderGuard::afterAddToMap(CMap * map)
  1096. {
  1097. IQuestObject::afterAddToMapCommon(map);
  1098. }
  1099. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1100. {
  1101. if (!wasMyColorVisited (h->getOwner()) )
  1102. {
  1103. h->showInfoDialog(18);
  1104. AddQuest aq;
  1105. aq.quest = QuestInfo (quest, this, visitablePos());
  1106. aq.player = h->tempOwner;
  1107. cb->sendAndApply (&aq);
  1108. }
  1109. }
  1110. bool CGBorderGate::passableFor(PlayerColor color) const
  1111. {
  1112. return wasMyColorVisited(color);
  1113. }
  1114. VCMI_LIB_NAMESPACE_END