CQuest.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /*
  2. * CQuest.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CQuest.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include "../ArtifactUtils.h"
  14. #include "../NetPacks.h"
  15. #include "../CSoundBase.h"
  16. #include "../CGeneralTextHandler.h"
  17. #include "../CHeroHandler.h"
  18. #include "CGCreature.h"
  19. #include "../IGameCallback.h"
  20. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  21. #include "../serializer/JsonSerializeFormat.h"
  22. #include "../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)->partsInfo.size() - 2;
  139. }
  140. if(ArtifactUtils::isBackpackFreeSlots(h, reqSlots))
  141. return true;
  142. else
  143. return false;
  144. }
  145. case MISSION_ARMY:
  146. return checkMissionArmy(this, h);
  147. case MISSION_RESOURCES:
  148. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i) //including Mithril ?
  149. { //Quest has no direct access to callback
  150. if(CGHeroInstance::cb->getResource(h->tempOwner, i) < static_cast<int>(m7resources[i]))
  151. return false;
  152. }
  153. return true;
  154. case MISSION_HERO:
  155. return m13489val == h->type->getIndex();
  156. case MISSION_PLAYER:
  157. return m13489val == h->getOwner().getNum();
  158. default:
  159. return false;
  160. }
  161. }
  162. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  163. {
  164. std::string text;
  165. bool failRequirements = (h ? !checkQuest(h) : true);
  166. if(firstVisit)
  167. {
  168. isCustom = isCustomFirst;
  169. text = firstVisitText;
  170. iwText.appendRawString(text);
  171. }
  172. else if(failRequirements)
  173. {
  174. isCustom = isCustomNext;
  175. text = nextVisitText;
  176. iwText.appendRawString(text);
  177. }
  178. switch (missionType)
  179. {
  180. case MISSION_LEVEL:
  181. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, m13489val, 0);
  182. if(!isCustom)
  183. iwText.replaceNumber(m13489val);
  184. break;
  185. case MISSION_PRIMARY_STAT:
  186. {
  187. MetaString loot;
  188. for(int i = 0; i < 4; ++i)
  189. {
  190. if(m2stats[i])
  191. {
  192. components.emplace_back(Component::EComponentType::PRIM_SKILL, i, m2stats[i], 0);
  193. loot.appendRawString("%d %s");
  194. loot.replaceNumber(m2stats[i]);
  195. loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
  196. }
  197. }
  198. if (!isCustom)
  199. iwText.replaceRawString(loot.buildList());
  200. }
  201. break;
  202. case MISSION_KILL_HERO:
  203. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, heroPortrait, 0, 0);
  204. if(!isCustom)
  205. addReplacements(iwText, text);
  206. break;
  207. case MISSION_HERO:
  208. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  209. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroh->objects[m13489val]->imageIndex, 0, 0);
  210. if(!isCustom)
  211. iwText.replaceRawString(VLC->heroh->objects[m13489val]->getNameTranslated());
  212. break;
  213. case MISSION_KILL_CREATURE:
  214. {
  215. components.emplace_back(stackToKill);
  216. if(!isCustom)
  217. {
  218. addReplacements(iwText, text);
  219. }
  220. }
  221. break;
  222. case MISSION_ART:
  223. {
  224. MetaString loot;
  225. for(const auto & elem : m5arts)
  226. {
  227. components.emplace_back(Component::EComponentType::ARTIFACT, elem, 0, 0);
  228. loot.appendRawString("%s");
  229. loot.replaceLocalString(EMetaText::ART_NAMES, elem);
  230. }
  231. if(!isCustom)
  232. iwText.replaceRawString(loot.buildList());
  233. }
  234. break;
  235. case MISSION_ARMY:
  236. {
  237. MetaString loot;
  238. for(const auto & elem : m6creatures)
  239. {
  240. components.emplace_back(elem);
  241. loot.appendRawString("%s");
  242. loot.replaceCreatureName(elem);
  243. }
  244. if(!isCustom)
  245. iwText.replaceRawString(loot.buildList());
  246. }
  247. break;
  248. case MISSION_RESOURCES:
  249. {
  250. MetaString loot;
  251. for(int i = 0; i < 7; ++i)
  252. {
  253. if(m7resources[i])
  254. {
  255. components.emplace_back(Component::EComponentType::RESOURCE, i, m7resources[i], 0);
  256. loot.appendRawString("%d %s");
  257. loot.replaceNumber(m7resources[i]);
  258. loot.replaceLocalString(EMetaText::RES_NAMES, i);
  259. }
  260. }
  261. if(!isCustom)
  262. iwText.replaceRawString(loot.buildList());
  263. }
  264. break;
  265. case MISSION_PLAYER:
  266. components.emplace_back(Component::EComponentType::FLAG, m13489val, 0, 0);
  267. if(!isCustom)
  268. iwText.replaceRawString(VLC->generaltexth->colors[m13489val]);
  269. break;
  270. }
  271. }
  272. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  273. {
  274. // Quests with MISSION_NONE type don't have a text for them
  275. assert(missionType != MISSION_NONE);
  276. if(onHover)
  277. ms.appendRawString("\n\n");
  278. std::string questName = missionName(missionType);
  279. std::string questState = missionState(onHover ? 3 : 4);
  280. ms.appendRawString(VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption));
  281. switch(missionType)
  282. {
  283. case MISSION_LEVEL:
  284. ms.replaceNumber(m13489val);
  285. break;
  286. case MISSION_PRIMARY_STAT:
  287. {
  288. MetaString loot;
  289. for (int i = 0; i < 4; ++i)
  290. {
  291. if (m2stats[i])
  292. {
  293. loot.appendRawString("%d %s");
  294. loot.replaceNumber(m2stats[i]);
  295. loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
  296. }
  297. }
  298. ms.replaceRawString(loot.buildList());
  299. }
  300. break;
  301. case MISSION_KILL_HERO:
  302. ms.replaceRawString(heroName);
  303. break;
  304. case MISSION_KILL_CREATURE:
  305. ms.replaceCreatureName(stackToKill);
  306. break;
  307. case MISSION_ART:
  308. {
  309. MetaString loot;
  310. for(const auto & elem : m5arts)
  311. {
  312. loot.appendRawString("%s");
  313. loot.replaceLocalString(EMetaText::ART_NAMES, elem);
  314. }
  315. ms.replaceRawString(loot.buildList());
  316. }
  317. break;
  318. case MISSION_ARMY:
  319. {
  320. MetaString loot;
  321. for(const auto & elem : m6creatures)
  322. {
  323. loot.appendRawString("%s");
  324. loot.replaceCreatureName(elem);
  325. }
  326. ms.replaceRawString(loot.buildList());
  327. }
  328. break;
  329. case MISSION_RESOURCES:
  330. {
  331. MetaString loot;
  332. for (int i = 0; i < 7; ++i)
  333. {
  334. if (m7resources[i])
  335. {
  336. loot.appendRawString("%d %s");
  337. loot.replaceNumber(m7resources[i]);
  338. loot.replaceLocalString(EMetaText::RES_NAMES, i);
  339. }
  340. }
  341. ms.replaceRawString(loot.buildList());
  342. }
  343. break;
  344. case MISSION_HERO:
  345. ms.replaceRawString(VLC->heroh->objects[m13489val]->getNameTranslated());
  346. break;
  347. case MISSION_PLAYER:
  348. ms.replaceRawString(VLC->generaltexth->colors[m13489val]);
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  355. {
  356. iwText.appendRawString(completedText);
  357. switch(missionType)
  358. {
  359. case CQuest::MISSION_LEVEL:
  360. if (!isCustomComplete)
  361. iwText.replaceNumber(m13489val);
  362. break;
  363. case CQuest::MISSION_PRIMARY_STAT:
  364. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  365. {
  366. MetaString loot;
  367. for (int i = 0; i < 4; ++i)
  368. {
  369. if (m2stats[i])
  370. {
  371. loot.appendRawString("%d %s");
  372. loot.replaceNumber(m2stats[i]);
  373. loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
  374. }
  375. }
  376. if (!isCustomComplete)
  377. iwText.replaceRawString(loot.buildList());
  378. }
  379. break;
  380. case CQuest::MISSION_ART:
  381. {
  382. MetaString loot;
  383. for(const auto & elem : m5arts)
  384. {
  385. loot.appendRawString("%s");
  386. loot.replaceLocalString(EMetaText::ART_NAMES, elem);
  387. }
  388. if (!isCustomComplete)
  389. iwText.replaceRawString(loot.buildList());
  390. }
  391. break;
  392. case CQuest::MISSION_ARMY:
  393. {
  394. MetaString loot;
  395. for(const auto & elem : m6creatures)
  396. {
  397. loot.appendRawString("%s");
  398. loot.replaceCreatureName(elem);
  399. }
  400. if (!isCustomComplete)
  401. iwText.replaceRawString(loot.buildList());
  402. }
  403. break;
  404. case CQuest::MISSION_RESOURCES:
  405. {
  406. MetaString loot;
  407. for (int i = 0; i < 7; ++i)
  408. {
  409. if (m7resources[i])
  410. {
  411. loot.appendRawString("%d %s");
  412. loot.replaceNumber(m7resources[i]);
  413. loot.replaceLocalString(EMetaText::RES_NAMES, i);
  414. }
  415. }
  416. if (!isCustomComplete)
  417. iwText.replaceRawString(loot.buildList());
  418. }
  419. break;
  420. case MISSION_KILL_HERO:
  421. case MISSION_KILL_CREATURE:
  422. if (!isCustomComplete)
  423. addReplacements(iwText, completedText);
  424. break;
  425. case MISSION_HERO:
  426. if (!isCustomComplete)
  427. iwText.replaceRawString(VLC->heroh->objects[m13489val]->getNameTranslated());
  428. break;
  429. case MISSION_PLAYER:
  430. if (!isCustomComplete)
  431. iwText.replaceRawString(VLC->generaltexth->colors[m13489val]);
  432. break;
  433. }
  434. }
  435. void CQuest::addArtifactID(const ArtifactID & id)
  436. {
  437. m5arts.push_back(id);
  438. ++artifactsRequirements[id];
  439. }
  440. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  441. {
  442. auto q = handler.enterStruct(fieldName);
  443. handler.serializeString("firstVisitText", firstVisitText);
  444. handler.serializeString("nextVisitText", nextVisitText);
  445. handler.serializeString("completedText", completedText);
  446. if(!handler.saving)
  447. {
  448. isCustomFirst = !firstVisitText.empty();
  449. isCustomNext = !nextVisitText.empty();
  450. isCustomComplete = !completedText.empty();
  451. }
  452. static const std::vector<std::string> MISSION_TYPE_JSON =
  453. {
  454. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  455. };
  456. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  457. handler.serializeInt("timeLimit", lastDay, -1);
  458. switch (missionType)
  459. {
  460. case MISSION_NONE:
  461. break;
  462. case MISSION_LEVEL:
  463. handler.serializeInt("heroLevel", m13489val, -1);
  464. break;
  465. case MISSION_PRIMARY_STAT:
  466. {
  467. auto primarySkills = handler.enterStruct("primarySkills");
  468. if(!handler.saving)
  469. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  470. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  471. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  472. }
  473. break;
  474. case MISSION_KILL_HERO:
  475. case MISSION_KILL_CREATURE:
  476. handler.serializeInstance<ui32>("killTarget", m13489val, static_cast<ui32>(-1));
  477. break;
  478. case MISSION_ART:
  479. //todo: ban artifacts
  480. handler.serializeIdArray<ArtifactID>("artifacts", m5arts);
  481. break;
  482. case MISSION_ARMY:
  483. {
  484. auto a = handler.enterArray("creatures");
  485. a.serializeStruct(m6creatures);
  486. }
  487. break;
  488. case MISSION_RESOURCES:
  489. {
  490. auto r = handler.enterStruct("resources");
  491. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  492. {
  493. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  494. }
  495. }
  496. break;
  497. case MISSION_HERO:
  498. handler.serializeId<ui32, ui32, HeroTypeID>("hero", m13489val, 0);
  499. break;
  500. case MISSION_PLAYER:
  501. handler.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. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  527. seerName = VLC->generaltexth->translate(seerNameID);
  528. quest->textOption = rand.nextInt(2);
  529. quest->completedOption = rand.nextInt(1, 3);
  530. }
  531. void CGSeerHut::initObj(CRandomGenerator & rand)
  532. {
  533. init(rand);
  534. quest->progress = CQuest::NOT_ACTIVE;
  535. if(quest->missionType)
  536. {
  537. std::string questName = quest->missionName(quest->missionType);
  538. if(!quest->isCustomFirst)
  539. quest->firstVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(0), quest->textOption);
  540. if(!quest->isCustomNext)
  541. quest->nextVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(1), quest->textOption);
  542. if(!quest->isCustomComplete)
  543. quest->completedText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(2), quest->textOption);
  544. }
  545. else
  546. {
  547. quest->progress = CQuest::COMPLETE;
  548. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  549. }
  550. }
  551. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  552. {
  553. quest->getRolloverText (text, onHover);//TODO: simplify?
  554. if(!onHover)
  555. text.replaceRawString(seerName);
  556. }
  557. std::string CGSeerHut::getHoverText(PlayerColor player) const
  558. {
  559. std::string hoverName = getObjectName();
  560. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  561. {
  562. hoverName = VLC->generaltexth->allTexts[347];
  563. boost::algorithm::replace_first(hoverName, "%s", seerName);
  564. }
  565. if(quest->progress & quest->missionType) //rollover when the quest is active
  566. {
  567. MetaString ms;
  568. getRolloverText (ms, true);
  569. hoverName += ms.toString();
  570. }
  571. return hoverName;
  572. }
  573. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  574. {
  575. switch(missionType)
  576. {
  577. case MISSION_KILL_CREATURE:
  578. out.replaceCreatureName(stackToKill);
  579. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  580. {
  581. out.replaceRawString(VLC->generaltexth->arraytxt[147+stackDirection]);
  582. }
  583. break;
  584. case MISSION_KILL_HERO:
  585. out.replaceRawString(heroName);
  586. break;
  587. }
  588. }
  589. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  590. {
  591. return quest->checkQuest(h);
  592. }
  593. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  594. {
  595. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  596. }
  597. void IQuestObject::afterAddToMapCommon(CMap * map) const
  598. {
  599. map->addNewQuestInstance(quest);
  600. }
  601. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  602. {
  603. quest->getCompletionText (text, components, isCustom, h);
  604. switch(rewardType)
  605. {
  606. case EXPERIENCE:
  607. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(rVal)), 0);
  608. break;
  609. case MANA_POINTS:
  610. components.emplace_back(Component::EComponentType::PRIM_SKILL, 5, rVal, 0);
  611. break;
  612. case MORALE_BONUS:
  613. components.emplace_back(Component::EComponentType::MORALE, 0, rVal, 0);
  614. break;
  615. case LUCK_BONUS:
  616. components.emplace_back(Component::EComponentType::LUCK, 0, rVal, 0);
  617. break;
  618. case RESOURCES:
  619. components.emplace_back(Component::EComponentType::RESOURCE, rID, rVal, 0);
  620. break;
  621. case PRIMARY_SKILL:
  622. components.emplace_back(Component::EComponentType::PRIM_SKILL, rID, rVal, 0);
  623. break;
  624. case SECONDARY_SKILL:
  625. components.emplace_back(Component::EComponentType::SEC_SKILL, rID, rVal, 0);
  626. break;
  627. case ARTIFACT:
  628. components.emplace_back(Component::EComponentType::ARTIFACT, rID, 0, 0);
  629. break;
  630. case SPELL:
  631. components.emplace_back(Component::EComponentType::SPELL, rID, 0, 0);
  632. break;
  633. case CREATURE:
  634. components.emplace_back(Component::EComponentType::CREATURE, rID, rVal, 0);
  635. break;
  636. }
  637. }
  638. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  639. {
  640. switch(what)
  641. {
  642. case 10:
  643. quest->progress = static_cast<CQuest::Eprogress>(val);
  644. break;
  645. }
  646. }
  647. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  648. {
  649. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  650. {
  651. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  652. }
  653. }
  654. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  655. {
  656. InfoWindow iw;
  657. iw.player = h->getOwner();
  658. if(quest->progress < CQuest::COMPLETE)
  659. {
  660. bool firstVisit = !quest->progress;
  661. bool failRequirements = !checkQuest(h);
  662. bool isCustom = false;
  663. if(firstVisit)
  664. {
  665. isCustom = quest->isCustomFirst;
  666. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  667. AddQuest aq;
  668. aq.quest = QuestInfo (quest, this, visitablePos());
  669. aq.player = h->tempOwner;
  670. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  671. }
  672. else if(failRequirements)
  673. {
  674. isCustom = quest->isCustomNext;
  675. }
  676. if(firstVisit || failRequirements)
  677. {
  678. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  679. cb->showInfoDialog(&iw);
  680. }
  681. if(!failRequirements) // propose completion, also on first visit
  682. {
  683. BlockingDialog bd (true, false);
  684. bd.player = h->getOwner();
  685. getCompletionText (bd.text, bd.components, isCustom, h);
  686. cb->showBlockingDialog (&bd);
  687. return;
  688. }
  689. }
  690. else
  691. {
  692. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  693. if (ID == Obj::SEER_HUT)
  694. iw.text.replaceRawString(seerName);
  695. cb->showInfoDialog(&iw);
  696. }
  697. }
  698. int CGSeerHut::checkDirection() const
  699. {
  700. int3 cord = getCreatureToKill()->pos;
  701. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  702. {
  703. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  704. return 8;
  705. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  706. return 1;
  707. else //northeast
  708. return 2;
  709. }
  710. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  711. {
  712. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  713. return 7;
  714. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  715. return 9;
  716. else //east
  717. return 3;
  718. }
  719. else //south
  720. {
  721. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  722. return 6;
  723. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  724. return 5;
  725. else //southeast
  726. return 4;
  727. }
  728. }
  729. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  730. {
  731. if (accept)
  732. {
  733. switch (quest->missionType)
  734. {
  735. case CQuest::MISSION_ART:
  736. for(auto & elem : quest->m5arts)
  737. {
  738. if(h->hasArt(elem))
  739. {
  740. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  741. }
  742. else
  743. {
  744. const auto * assembly = h->getAssemblyByConstituent(elem);
  745. assert(assembly);
  746. auto parts = assembly->partsInfo;
  747. // Remove the assembly
  748. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  749. // Disassemble this backpack artifact
  750. for(const auto & ci : parts)
  751. {
  752. if(ci.art->getTypeId() != elem)
  753. cb->giveHeroNewArtifact(h, ci.art->artType, GameConstants::BACKPACK_START);
  754. }
  755. }
  756. }
  757. break;
  758. case CQuest::MISSION_ARMY:
  759. cb->takeCreatures(h->id, quest->m6creatures);
  760. break;
  761. case CQuest::MISSION_RESOURCES:
  762. for (int i = 0; i < 7; ++i)
  763. {
  764. cb->giveResource(h->getOwner(), static_cast<EGameResID>(i), -static_cast<int>(quest->m7resources[i]));
  765. }
  766. break;
  767. default:
  768. break;
  769. }
  770. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  771. completeQuest(h); //make sure to remove QuestGuard at the very end
  772. }
  773. }
  774. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  775. {
  776. switch (rewardType)
  777. {
  778. case EXPERIENCE:
  779. {
  780. TExpType expVal = h->calculateXp(rVal);
  781. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  782. break;
  783. }
  784. case MANA_POINTS:
  785. {
  786. cb->setManaPoints(h->id, h->mana+rVal);
  787. break;
  788. }
  789. case MORALE_BONUS: case LUCK_BONUS:
  790. {
  791. Bonus hb(BonusDuration::ONE_WEEK, (rewardType == 3 ? BonusType::MORALE : BonusType::LUCK),
  792. BonusSource::OBJECT, rVal, h->id.getNum(), "", -1);
  793. GiveBonus gb;
  794. gb.id = h->id.getNum();
  795. gb.bonus = hb;
  796. cb->giveHeroBonus(&gb);
  797. }
  798. break;
  799. case RESOURCES:
  800. cb->giveResource(h->getOwner(), static_cast<EGameResID>(rID), rVal);
  801. break;
  802. case PRIMARY_SKILL:
  803. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  804. break;
  805. case SECONDARY_SKILL:
  806. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  807. break;
  808. case ARTIFACT:
  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.value();
  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.appendLocalString(EMetaText::ADVOB_TXT,18);
  1063. }
  1064. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  1065. {
  1066. if (!onHover)
  1067. {
  1068. text.appendRawString(VLC->generaltexth->tentColors[subID]);
  1069. text.appendRawString(" ");
  1070. text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  1071. }
  1072. }
  1073. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  1074. {
  1075. return wasMyColorVisited (h->tempOwner);
  1076. }
  1077. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  1078. {
  1079. if (wasMyColorVisited (h->getOwner()) )
  1080. {
  1081. BlockingDialog bd (true, false);
  1082. bd.player = h->getOwner();
  1083. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  1084. cb->showBlockingDialog (&bd);
  1085. }
  1086. else
  1087. {
  1088. h->showInfoDialog(18);
  1089. AddQuest aq;
  1090. aq.quest = QuestInfo (quest, this, visitablePos());
  1091. aq.player = h->tempOwner;
  1092. cb->sendAndApply (&aq);
  1093. //TODO: add this quest only once OR check for multiple instances later
  1094. }
  1095. }
  1096. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1097. {
  1098. if (answer)
  1099. cb->removeObject(this);
  1100. }
  1101. void CGBorderGuard::afterAddToMap(CMap * map)
  1102. {
  1103. IQuestObject::afterAddToMapCommon(map);
  1104. }
  1105. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1106. {
  1107. if (!wasMyColorVisited (h->getOwner()) )
  1108. {
  1109. h->showInfoDialog(18);
  1110. AddQuest aq;
  1111. aq.quest = QuestInfo (quest, this, visitablePos());
  1112. aq.player = h->tempOwner;
  1113. cb->sendAndApply (&aq);
  1114. }
  1115. }
  1116. bool CGBorderGate::passableFor(PlayerColor color) const
  1117. {
  1118. return wasMyColorVisited(color);
  1119. }
  1120. VCMI_LIB_NAMESPACE_END