2
0

CQuest.cpp 30 KB

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