CQuest.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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(Res::ERes i = Res::WOOD; i <= Res::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(static_cast<Emission>(missionType - 1));
  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. if(!handler.saving)
  490. m7resources.resize(GameConstants::RESOURCE_QUANTITY-1);
  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.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  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)->portrait;
  521. }
  522. }
  523. void CGSeerHut::init(CRandomGenerator & rand)
  524. {
  525. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  526. seerName = *RandomGeneratorUtil::nextItem(names, rand);
  527. quest->textOption = rand.nextInt(2);
  528. quest->completedOption = rand.nextInt(1, 3);
  529. }
  530. void CGSeerHut::initObj(CRandomGenerator & rand)
  531. {
  532. init(rand);
  533. quest->progress = CQuest::NOT_ACTIVE;
  534. if(quest->missionType)
  535. {
  536. std::string questName = quest->missionName(quest->missionType);
  537. if(!quest->isCustomFirst)
  538. quest->firstVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(0), quest->textOption);
  539. if(!quest->isCustomNext)
  540. quest->nextVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(1), quest->textOption);
  541. if(!quest->isCustomComplete)
  542. quest->completedText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(2), quest->textOption);
  543. }
  544. else
  545. {
  546. quest->progress = CQuest::COMPLETE;
  547. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  548. }
  549. }
  550. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  551. {
  552. quest->getRolloverText (text, onHover);//TODO: simplify?
  553. if(!onHover)
  554. text.addReplacement(seerName);
  555. }
  556. std::string CGSeerHut::getHoverText(PlayerColor player) const
  557. {
  558. std::string hoverName = getObjectName();
  559. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  560. {
  561. hoverName = VLC->generaltexth->allTexts[347];
  562. boost::algorithm::replace_first(hoverName, "%s", seerName);
  563. }
  564. if(quest->progress & quest->missionType) //rollover when the quest is active
  565. {
  566. MetaString ms;
  567. getRolloverText (ms, true);
  568. hoverName += ms.toString();
  569. }
  570. return hoverName;
  571. }
  572. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  573. {
  574. switch(missionType)
  575. {
  576. case MISSION_KILL_CREATURE:
  577. out.addReplacement(stackToKill);
  578. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  579. {
  580. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  581. }
  582. break;
  583. case MISSION_KILL_HERO:
  584. out.addReplacement(heroName);
  585. break;
  586. }
  587. }
  588. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  589. {
  590. return quest->checkQuest(h);
  591. }
  592. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  593. {
  594. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  595. }
  596. void IQuestObject::afterAddToMapCommon(CMap * map) const
  597. {
  598. map->addNewQuestInstance(quest);
  599. }
  600. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  601. {
  602. quest->getCompletionText (text, components, isCustom, h);
  603. switch(rewardType)
  604. {
  605. case EXPERIENCE:
  606. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(rVal)), 0);
  607. break;
  608. case MANA_POINTS:
  609. components.emplace_back(Component::EComponentType::PRIM_SKILL, 5, rVal, 0);
  610. break;
  611. case MORALE_BONUS:
  612. components.emplace_back(Component::EComponentType::MORALE, 0, rVal, 0);
  613. break;
  614. case LUCK_BONUS:
  615. components.emplace_back(Component::EComponentType::LUCK, 0, rVal, 0);
  616. break;
  617. case RESOURCES:
  618. components.emplace_back(Component::EComponentType::RESOURCE, rID, rVal, 0);
  619. break;
  620. case PRIMARY_SKILL:
  621. components.emplace_back(Component::EComponentType::PRIM_SKILL, rID, rVal, 0);
  622. break;
  623. case SECONDARY_SKILL:
  624. components.emplace_back(Component::EComponentType::SEC_SKILL, rID, rVal, 0);
  625. break;
  626. case ARTIFACT:
  627. components.emplace_back(Component::EComponentType::ARTIFACT, rID, 0, 0);
  628. break;
  629. case SPELL:
  630. components.emplace_back(Component::EComponentType::SPELL, rID, 0, 0);
  631. break;
  632. case CREATURE:
  633. components.emplace_back(Component::EComponentType::CREATURE, rID, rVal, 0);
  634. break;
  635. }
  636. }
  637. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  638. {
  639. switch(what)
  640. {
  641. case 10:
  642. quest->progress = static_cast<CQuest::Eprogress>(val);
  643. break;
  644. }
  645. }
  646. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  647. {
  648. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  649. {
  650. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  651. }
  652. }
  653. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  654. {
  655. InfoWindow iw;
  656. iw.player = h->getOwner();
  657. if(quest->progress < CQuest::COMPLETE)
  658. {
  659. bool firstVisit = !quest->progress;
  660. bool failRequirements = !checkQuest(h);
  661. bool isCustom = false;
  662. if(firstVisit)
  663. {
  664. isCustom = quest->isCustomFirst;
  665. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  666. AddQuest aq;
  667. aq.quest = QuestInfo (quest, this, visitablePos());
  668. aq.player = h->tempOwner;
  669. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  670. }
  671. else if(failRequirements)
  672. {
  673. isCustom = quest->isCustomNext;
  674. }
  675. if(firstVisit || failRequirements)
  676. {
  677. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  678. cb->showInfoDialog(&iw);
  679. }
  680. if(!failRequirements) // propose completion, also on first visit
  681. {
  682. BlockingDialog bd (true, false);
  683. bd.player = h->getOwner();
  684. getCompletionText (bd.text, bd.components, isCustom, h);
  685. cb->showBlockingDialog (&bd);
  686. return;
  687. }
  688. }
  689. else
  690. {
  691. iw.text << VLC->generaltexth->seerEmpty[quest->completedOption];
  692. if (ID == Obj::SEER_HUT)
  693. iw.text.addReplacement(seerName);
  694. cb->showInfoDialog(&iw);
  695. }
  696. }
  697. int CGSeerHut::checkDirection() const
  698. {
  699. int3 cord = getCreatureToKill()->pos;
  700. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  701. {
  702. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  703. return 8;
  704. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  705. return 1;
  706. else //northeast
  707. return 2;
  708. }
  709. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  710. {
  711. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  712. return 7;
  713. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  714. return 9;
  715. else //east
  716. return 3;
  717. }
  718. else //south
  719. {
  720. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  721. return 6;
  722. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  723. return 5;
  724. else //southeast
  725. return 4;
  726. }
  727. }
  728. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  729. {
  730. if (accept)
  731. {
  732. switch (quest->missionType)
  733. {
  734. case CQuest::MISSION_ART:
  735. for(auto & elem : quest->m5arts)
  736. {
  737. if(h->hasArt(elem))
  738. {
  739. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  740. }
  741. else
  742. {
  743. const auto * assembly = h->getAssemblyByConstituent(elem);
  744. assert(assembly);
  745. auto parts = assembly->constituentsInfo;
  746. // Remove the assembly
  747. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  748. // Disassemble this backpack artifact
  749. for(const auto & ci : parts)
  750. {
  751. if(ci.art->artType->getId() != elem)
  752. cb->giveHeroNewArtifact(h, ci.art->artType, GameConstants::BACKPACK_START);
  753. }
  754. }
  755. }
  756. break;
  757. case CQuest::MISSION_ARMY:
  758. cb->takeCreatures(h->id, quest->m6creatures);
  759. break;
  760. case CQuest::MISSION_RESOURCES:
  761. for (int i = 0; i < 7; ++i)
  762. {
  763. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(i), -static_cast<int>(quest->m7resources[i]));
  764. }
  765. break;
  766. default:
  767. break;
  768. }
  769. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  770. completeQuest(h); //make sure to remove QuestGuard at the very end
  771. }
  772. }
  773. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  774. {
  775. switch (rewardType)
  776. {
  777. case EXPERIENCE:
  778. {
  779. TExpType expVal = h->calculateXp(rVal);
  780. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  781. break;
  782. }
  783. case MANA_POINTS:
  784. {
  785. cb->setManaPoints(h->id, h->mana+rVal);
  786. break;
  787. }
  788. case MORALE_BONUS: case LUCK_BONUS:
  789. {
  790. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  791. Bonus::OBJECT, rVal, h->id.getNum(), "", -1);
  792. GiveBonus gb;
  793. gb.id = h->id.getNum();
  794. gb.bonus = hb;
  795. cb->giveHeroBonus(&gb);
  796. }
  797. break;
  798. case RESOURCES:
  799. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(rID), rVal);
  800. break;
  801. case PRIMARY_SKILL:
  802. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  803. break;
  804. case SECONDARY_SKILL:
  805. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  806. break;
  807. case ARTIFACT:
  808. if(ArtifactUtils::isPossibleToGetArt(h, VLC->arth->objects[rID]->getId()))
  809. cb->giveHeroNewArtifact(h, VLC->arth->objects[rID],ArtifactPosition::FIRST_AVAILABLE);
  810. break;
  811. case SPELL:
  812. {
  813. std::set<SpellID> spell;
  814. spell.insert (SpellID(rID));
  815. cb->changeSpells(h, true, spell);
  816. }
  817. break;
  818. case CREATURE:
  819. {
  820. CCreatureSet creatures;
  821. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  822. cb->giveCreatures(this, h, creatures, false);
  823. }
  824. break;
  825. default:
  826. break;
  827. }
  828. }
  829. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  830. {
  831. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  832. if(allowNull && !o)
  833. return nullptr;
  834. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  835. return dynamic_cast<const CGHeroInstance *>(o);
  836. }
  837. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  838. {
  839. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  840. if(allowNull && !o)
  841. return nullptr;
  842. assert(o && o->ID == Obj::MONSTER);
  843. return dynamic_cast<const CGCreature *>(o);
  844. }
  845. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  846. {
  847. finishQuest(hero, answer);
  848. }
  849. void CGSeerHut::afterAddToMap(CMap* map)
  850. {
  851. IQuestObject::afterAddToMapCommon(map);
  852. }
  853. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  854. {
  855. static const std::map<ERewardType, std::string> REWARD_MAP =
  856. {
  857. {NOTHING, ""},
  858. {EXPERIENCE, "experience"},
  859. {MANA_POINTS, "mana"},
  860. {MORALE_BONUS, "morale"},
  861. {LUCK_BONUS, "luck"},
  862. {RESOURCES, "resource"},
  863. {PRIMARY_SKILL, "primarySkill"},
  864. {SECONDARY_SKILL,"secondarySkill"},
  865. {ARTIFACT, "artifact"},
  866. {SPELL, "spell"},
  867. {CREATURE, "creature"}
  868. };
  869. static const std::map<std::string, ERewardType> REWARD_RMAP =
  870. {
  871. {"experience", EXPERIENCE},
  872. {"mana", MANA_POINTS},
  873. {"morale", MORALE_BONUS},
  874. {"luck", LUCK_BONUS},
  875. {"resource", RESOURCES},
  876. {"primarySkill", PRIMARY_SKILL},
  877. {"secondarySkill",SECONDARY_SKILL},
  878. {"artifact", ARTIFACT},
  879. {"spell", SPELL},
  880. {"creature", CREATURE}
  881. };
  882. //quest and reward
  883. quest->serializeJson(handler, "quest");
  884. //only one reward is supported
  885. //todo: full reward format support after CRewardInfo integration
  886. auto s = handler.enterStruct("reward");
  887. std::string fullIdentifier;
  888. std::string metaTypeName;
  889. std::string scope;
  890. std::string identifier;
  891. if(handler.saving)
  892. {
  893. si32 amount = rVal;
  894. metaTypeName = REWARD_MAP.at(rewardType);
  895. switch (rewardType)
  896. {
  897. case NOTHING:
  898. break;
  899. case EXPERIENCE:
  900. case MANA_POINTS:
  901. case MORALE_BONUS:
  902. case LUCK_BONUS:
  903. identifier.clear();
  904. break;
  905. case RESOURCES:
  906. identifier = GameConstants::RESOURCE_NAMES[rID];
  907. break;
  908. case PRIMARY_SKILL:
  909. identifier = PrimarySkill::names[rID];
  910. break;
  911. case SECONDARY_SKILL:
  912. identifier = CSkillHandler::encodeSkill(rID);
  913. break;
  914. case ARTIFACT:
  915. identifier = ArtifactID(rID).toArtifact(VLC->artifacts())->getJsonKey();
  916. amount = 1;
  917. break;
  918. case SPELL:
  919. identifier = SpellID(rID).toSpell(VLC->spells())->getJsonKey();
  920. amount = 1;
  921. break;
  922. case CREATURE:
  923. identifier = CreatureID(rID).toCreature(VLC->creatures())->getJsonKey();
  924. break;
  925. default:
  926. assert(false);
  927. break;
  928. }
  929. if(rewardType != NOTHING)
  930. {
  931. fullIdentifier = CModHandler::makeFullIdentifier(scope, metaTypeName, identifier);
  932. handler.serializeInt(fullIdentifier, amount);
  933. }
  934. }
  935. else
  936. {
  937. rewardType = NOTHING;
  938. const JsonNode & rewardsJson = handler.getCurrent();
  939. fullIdentifier.clear();
  940. if(rewardsJson.Struct().empty())
  941. return;
  942. else
  943. {
  944. auto iter = rewardsJson.Struct().begin();
  945. fullIdentifier = iter->first;
  946. }
  947. CModHandler::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  948. auto it = REWARD_RMAP.find(metaTypeName);
  949. if(it == REWARD_RMAP.end())
  950. {
  951. logGlobal->error("%s: invalid metatype in reward item %s", instanceName, fullIdentifier);
  952. return;
  953. }
  954. else
  955. {
  956. rewardType = it->second;
  957. }
  958. bool doRequest = false;
  959. switch (rewardType)
  960. {
  961. case NOTHING:
  962. return;
  963. case EXPERIENCE:
  964. case MANA_POINTS:
  965. case MORALE_BONUS:
  966. case LUCK_BONUS:
  967. break;
  968. case PRIMARY_SKILL:
  969. doRequest = true;
  970. break;
  971. case RESOURCES:
  972. case SECONDARY_SKILL:
  973. case ARTIFACT:
  974. case SPELL:
  975. case CREATURE:
  976. doRequest = true;
  977. break;
  978. default:
  979. assert(false);
  980. break;
  981. }
  982. if(doRequest)
  983. {
  984. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), fullIdentifier, false);
  985. if(rawId)
  986. {
  987. rID = rawId.get();
  988. }
  989. else
  990. {
  991. rewardType = NOTHING;//fallback in case of error
  992. return;
  993. }
  994. }
  995. handler.serializeInt(fullIdentifier, rVal);
  996. }
  997. }
  998. void CGQuestGuard::init(CRandomGenerator & rand)
  999. {
  1000. blockVisit = true;
  1001. quest->textOption = rand.nextInt(3, 5);
  1002. quest->completedOption = rand.nextInt(4, 5);
  1003. }
  1004. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  1005. {
  1006. cb->removeObject(this);
  1007. }
  1008. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  1009. {
  1010. //quest only, do not call base class
  1011. quest->serializeJson(handler, "quest");
  1012. }
  1013. void CGKeys::reset()
  1014. {
  1015. playerKeyMap.clear();
  1016. }
  1017. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  1018. {
  1019. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  1020. {
  1021. PlayerColor player(what-101);
  1022. playerKeyMap[player].insert(static_cast<ui8>(val));
  1023. }
  1024. else
  1025. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  1026. }
  1027. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  1028. {
  1029. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  1030. }
  1031. std::string CGKeys::getHoverText(PlayerColor player) const
  1032. {
  1033. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  1034. }
  1035. std::string CGKeys::getObjectName() const
  1036. {
  1037. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  1038. }
  1039. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  1040. {
  1041. return wasMyColorVisited (player);
  1042. }
  1043. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  1044. {
  1045. int txt_id;
  1046. if (!wasMyColorVisited (h->getOwner()) )
  1047. {
  1048. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  1049. txt_id=19;
  1050. }
  1051. else
  1052. txt_id=20;
  1053. h->showInfoDialog(txt_id);
  1054. }
  1055. void CGBorderGuard::initObj(CRandomGenerator & rand)
  1056. {
  1057. //ui32 m13489val = subID; //store color as quest info
  1058. blockVisit = true;
  1059. }
  1060. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  1061. {
  1062. text << std::pair<ui8,ui32>(11,18);
  1063. }
  1064. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  1065. {
  1066. if (!onHover)
  1067. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID);
  1068. }
  1069. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  1070. {
  1071. return wasMyColorVisited (h->tempOwner);
  1072. }
  1073. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  1074. {
  1075. if (wasMyColorVisited (h->getOwner()) )
  1076. {
  1077. BlockingDialog bd (true, false);
  1078. bd.player = h->getOwner();
  1079. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  1080. cb->showBlockingDialog (&bd);
  1081. }
  1082. else
  1083. {
  1084. h->showInfoDialog(18);
  1085. AddQuest aq;
  1086. aq.quest = QuestInfo (quest, this, visitablePos());
  1087. aq.player = h->tempOwner;
  1088. cb->sendAndApply (&aq);
  1089. //TODO: add this quest only once OR check for multiple instances later
  1090. }
  1091. }
  1092. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1093. {
  1094. if (answer)
  1095. cb->removeObject(this);
  1096. }
  1097. void CGBorderGuard::afterAddToMap(CMap * map)
  1098. {
  1099. IQuestObject::afterAddToMapCommon(map);
  1100. }
  1101. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1102. {
  1103. if (!wasMyColorVisited (h->getOwner()) )
  1104. {
  1105. h->showInfoDialog(18);
  1106. AddQuest aq;
  1107. aq.quest = QuestInfo (quest, this, visitablePos());
  1108. aq.player = h->tempOwner;
  1109. cb->sendAndApply (&aq);
  1110. }
  1111. }
  1112. bool CGBorderGate::passableFor(PlayerColor color) const
  1113. {
  1114. return wasMyColorVisited(color);
  1115. }
  1116. VCMI_LIB_NAMESPACE_END