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. ///helpers
  46. static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID = 0)
  47. {
  48. InfoWindow iw;
  49. iw.soundID = soundID;
  50. iw.player = playerID;
  51. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  52. IObjectInterface::cb->sendAndApply(&iw);
  53. }
  54. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0)
  55. {
  56. const PlayerColor playerID = h->getOwner();
  57. showInfoDialog(playerID,txtID,soundID);
  58. }
  59. static std::string visitedTxt(const bool visited)
  60. {
  61. int id = visited ? 352 : 353;
  62. return VLC->generaltexth->allTexts[id];
  63. }
  64. const std::string & CQuest::missionName(CQuest::Emission mission)
  65. {
  66. static const std::array<std::string, 11> names = {
  67. "empty",
  68. "heroLevel",
  69. "primarySkill",
  70. "killHero",
  71. "killCreature",
  72. "bringArt",
  73. "bringCreature",
  74. "bringResources",
  75. "bringHero",
  76. "bringPlayer",
  77. "keymaster"
  78. };
  79. if(static_cast<size_t>(mission) < names.size())
  80. return names[static_cast<size_t>(mission)];
  81. return names[0];
  82. }
  83. const std::string & CQuest::missionState(int state)
  84. {
  85. static const std::array<std::string, 5> states = {
  86. "receive",
  87. "visit",
  88. "complete",
  89. "hover",
  90. "description",
  91. };
  92. if(state < states.size())
  93. return states[state];
  94. return states[0];
  95. }
  96. bool CQuest::checkMissionArmy(const CQuest * q, const CCreatureSet * army)
  97. {
  98. std::vector<CStackBasicDescriptor>::const_iterator cre;
  99. TSlots::const_iterator it;
  100. ui32 count = 0;
  101. ui32 slotsCount = 0;
  102. bool hasExtraCreatures = false;
  103. for(cre = q->m6creatures.begin(); cre != q->m6creatures.end(); ++cre)
  104. {
  105. for(count = 0, it = army->Slots().begin(); it != army->Slots().end(); ++it)
  106. {
  107. if(it->second->type == cre->type)
  108. {
  109. count += it->second->count;
  110. slotsCount++;
  111. }
  112. }
  113. if(static_cast<TQuantity>(count) < cre->count) //not enough creatures of this kind
  114. return false;
  115. hasExtraCreatures |= static_cast<TQuantity>(count) > cre->count;
  116. }
  117. return hasExtraCreatures || slotsCount < army->Slots().size();
  118. }
  119. bool CQuest::checkQuest(const CGHeroInstance * h) const
  120. {
  121. switch (missionType)
  122. {
  123. case MISSION_NONE:
  124. return true;
  125. case MISSION_LEVEL:
  126. return m13489val <= h->level;
  127. case MISSION_PRIMARY_STAT:
  128. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  129. {
  130. if(h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)) < static_cast<int>(m2stats[i]))
  131. return false;
  132. }
  133. return true;
  134. case MISSION_KILL_HERO:
  135. case MISSION_KILL_CREATURE:
  136. if(!CGHeroInstance::cb->getObjByQuestIdentifier(m13489val))
  137. return true;
  138. return false;
  139. case MISSION_ART:
  140. // if the object was deserialized
  141. if(artifactsRequirements.empty())
  142. for(const auto & id : m5arts)
  143. ++artifactsRequirements[id];
  144. for(const auto & elem : artifactsRequirements)
  145. {
  146. // check required amount of artifacts
  147. if(h->getArtPosCount(elem.first, false, true, true) < elem.second)
  148. return false;
  149. }
  150. return true;
  151. case MISSION_ARMY:
  152. return checkMissionArmy(this, h);
  153. case MISSION_RESOURCES:
  154. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, +1)) //including Mithril ?
  155. { //Quest has no direct access to callback
  156. if(CGHeroInstance::cb->getResource(h->tempOwner, i) < static_cast<int>(m7resources[i]))
  157. return false;
  158. }
  159. return true;
  160. case MISSION_HERO:
  161. return m13489val == h->type->getIndex();
  162. case MISSION_PLAYER:
  163. return m13489val == h->getOwner().getNum();
  164. default:
  165. return false;
  166. }
  167. }
  168. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  169. {
  170. std::string text;
  171. bool failRequirements = (h ? !checkQuest(h) : true);
  172. if(firstVisit)
  173. {
  174. isCustom = isCustomFirst;
  175. iwText << (text = firstVisitText);
  176. }
  177. else if(failRequirements)
  178. {
  179. isCustom = isCustomNext;
  180. iwText << (text = nextVisitText);
  181. }
  182. switch (missionType)
  183. {
  184. case MISSION_LEVEL:
  185. components.emplace_back(Component::EXPERIENCE, 0, m13489val, 0);
  186. if(!isCustom)
  187. iwText.addReplacement(m13489val);
  188. break;
  189. case MISSION_PRIMARY_STAT:
  190. {
  191. MetaString loot;
  192. for(int i = 0; i < 4; ++i)
  193. {
  194. if(m2stats[i])
  195. {
  196. components.emplace_back(Component::PRIM_SKILL, i, m2stats[i], 0);
  197. loot << "%d %s";
  198. loot.addReplacement(m2stats[i]);
  199. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  200. }
  201. }
  202. if (!isCustom)
  203. iwText.addReplacement(loot.buildList());
  204. }
  205. break;
  206. case MISSION_KILL_HERO:
  207. components.emplace_back(Component::HERO_PORTRAIT, heroPortrait, 0, 0);
  208. if(!isCustom)
  209. addReplacements(iwText, text);
  210. break;
  211. case MISSION_HERO:
  212. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  213. components.emplace_back(Component::HERO_PORTRAIT, VLC->heroh->objects[m13489val]->imageIndex, 0, 0);
  214. if(!isCustom)
  215. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  216. break;
  217. case MISSION_KILL_CREATURE:
  218. {
  219. components.emplace_back(stackToKill);
  220. if(!isCustom)
  221. {
  222. addReplacements(iwText, text);
  223. }
  224. }
  225. break;
  226. case MISSION_ART:
  227. {
  228. MetaString loot;
  229. for(const auto & elem : m5arts)
  230. {
  231. components.emplace_back(Component::ARTIFACT, elem, 0, 0);
  232. loot << "%s";
  233. loot.addReplacement(MetaString::ART_NAMES, elem);
  234. }
  235. if(!isCustom)
  236. iwText.addReplacement(loot.buildList());
  237. }
  238. break;
  239. case MISSION_ARMY:
  240. {
  241. MetaString loot;
  242. for(const auto & elem : m6creatures)
  243. {
  244. components.emplace_back(elem);
  245. loot << "%s";
  246. loot.addReplacement(elem);
  247. }
  248. if(!isCustom)
  249. iwText.addReplacement(loot.buildList());
  250. }
  251. break;
  252. case MISSION_RESOURCES:
  253. {
  254. MetaString loot;
  255. for(int i = 0; i < 7; ++i)
  256. {
  257. if(m7resources[i])
  258. {
  259. components.emplace_back(Component::RESOURCE, i, m7resources[i], 0);
  260. loot << "%d %s";
  261. loot.addReplacement(m7resources[i]);
  262. loot.addReplacement(MetaString::RES_NAMES, i);
  263. }
  264. }
  265. if(!isCustom)
  266. iwText.addReplacement(loot.buildList());
  267. }
  268. break;
  269. case MISSION_PLAYER:
  270. components.emplace_back(Component::FLAG, m13489val, 0, 0);
  271. if(!isCustom)
  272. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  273. break;
  274. }
  275. }
  276. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  277. {
  278. // Quests with MISSION_NONE type don't have a text for them
  279. assert(missionType != MISSION_NONE);
  280. if(onHover)
  281. ms << "\n\n";
  282. std::string questName = missionName(static_cast<Emission>(missionType - 1));
  283. std::string questState = missionState(onHover ? 3 : 4);
  284. ms << VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption);
  285. switch(missionType)
  286. {
  287. case MISSION_LEVEL:
  288. ms.addReplacement(m13489val);
  289. break;
  290. case MISSION_PRIMARY_STAT:
  291. {
  292. MetaString loot;
  293. for (int i = 0; i < 4; ++i)
  294. {
  295. if (m2stats[i])
  296. {
  297. loot << "%d %s";
  298. loot.addReplacement(m2stats[i]);
  299. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  300. }
  301. }
  302. ms.addReplacement(loot.buildList());
  303. }
  304. break;
  305. case MISSION_KILL_HERO:
  306. ms.addReplacement(heroName);
  307. break;
  308. case MISSION_KILL_CREATURE:
  309. ms.addReplacement(stackToKill);
  310. break;
  311. case MISSION_ART:
  312. {
  313. MetaString loot;
  314. for(const auto & elem : m5arts)
  315. {
  316. loot << "%s";
  317. loot.addReplacement(MetaString::ART_NAMES, elem);
  318. }
  319. ms.addReplacement(loot.buildList());
  320. }
  321. break;
  322. case MISSION_ARMY:
  323. {
  324. MetaString loot;
  325. for(const auto & elem : m6creatures)
  326. {
  327. loot << "%s";
  328. loot.addReplacement(elem);
  329. }
  330. ms.addReplacement(loot.buildList());
  331. }
  332. break;
  333. case MISSION_RESOURCES:
  334. {
  335. MetaString loot;
  336. for (int i = 0; i < 7; ++i)
  337. {
  338. if (m7resources[i])
  339. {
  340. loot << "%d %s";
  341. loot.addReplacement(m7resources[i]);
  342. loot.addReplacement(MetaString::RES_NAMES, i);
  343. }
  344. }
  345. ms.addReplacement(loot.buildList());
  346. }
  347. break;
  348. case MISSION_HERO:
  349. ms.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  350. break;
  351. case MISSION_PLAYER:
  352. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  359. {
  360. iwText << completedText;
  361. switch(missionType)
  362. {
  363. case CQuest::MISSION_LEVEL:
  364. if (!isCustomComplete)
  365. iwText.addReplacement(m13489val);
  366. break;
  367. case CQuest::MISSION_PRIMARY_STAT:
  368. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  369. {
  370. MetaString loot;
  371. for (int i = 0; i < 4; ++i)
  372. {
  373. if (m2stats[i])
  374. {
  375. loot << "%d %s";
  376. loot.addReplacement(m2stats[i]);
  377. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  378. }
  379. }
  380. if (!isCustomComplete)
  381. iwText.addReplacement(loot.buildList());
  382. }
  383. break;
  384. case CQuest::MISSION_ART:
  385. {
  386. MetaString loot;
  387. for(const auto & elem : m5arts)
  388. {
  389. loot << "%s";
  390. loot.addReplacement(MetaString::ART_NAMES, elem);
  391. }
  392. if (!isCustomComplete)
  393. iwText.addReplacement(loot.buildList());
  394. }
  395. break;
  396. case CQuest::MISSION_ARMY:
  397. {
  398. MetaString loot;
  399. for(const auto & elem : m6creatures)
  400. {
  401. loot << "%s";
  402. loot.addReplacement(elem);
  403. }
  404. if (!isCustomComplete)
  405. iwText.addReplacement(loot.buildList());
  406. }
  407. break;
  408. case CQuest::MISSION_RESOURCES:
  409. {
  410. MetaString loot;
  411. for (int i = 0; i < 7; ++i)
  412. {
  413. if (m7resources[i])
  414. {
  415. loot << "%d %s";
  416. loot.addReplacement(m7resources[i]);
  417. loot.addReplacement(MetaString::RES_NAMES, i);
  418. }
  419. }
  420. if (!isCustomComplete)
  421. iwText.addReplacement(loot.buildList());
  422. }
  423. break;
  424. case MISSION_KILL_HERO:
  425. case MISSION_KILL_CREATURE:
  426. if (!isCustomComplete)
  427. addReplacements(iwText, completedText);
  428. break;
  429. case MISSION_HERO:
  430. if (!isCustomComplete)
  431. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  432. break;
  433. case MISSION_PLAYER:
  434. if (!isCustomComplete)
  435. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  436. break;
  437. }
  438. }
  439. void CQuest::addArtifactID(const ArtifactID & id)
  440. {
  441. m5arts.push_back(id);
  442. ++artifactsRequirements[id];
  443. }
  444. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  445. {
  446. auto q = handler.enterStruct(fieldName);
  447. handler.serializeString("firstVisitText", firstVisitText);
  448. handler.serializeString("nextVisitText", nextVisitText);
  449. handler.serializeString("completedText", completedText);
  450. if(!handler.saving)
  451. {
  452. isCustomFirst = !firstVisitText.empty();
  453. isCustomNext = !nextVisitText.empty();
  454. isCustomComplete = !completedText.empty();
  455. }
  456. static const std::vector<std::string> MISSION_TYPE_JSON =
  457. {
  458. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  459. };
  460. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  461. handler.serializeInt("timeLimit", lastDay, -1);
  462. switch (missionType)
  463. {
  464. case MISSION_NONE:
  465. break;
  466. case MISSION_LEVEL:
  467. handler.serializeInt("heroLevel", m13489val, -1);
  468. break;
  469. case MISSION_PRIMARY_STAT:
  470. {
  471. auto primarySkills = handler.enterStruct("primarySkills");
  472. if(!handler.saving)
  473. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  474. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  475. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  476. }
  477. break;
  478. case MISSION_KILL_HERO:
  479. case MISSION_KILL_CREATURE:
  480. handler.serializeInstance<ui32>("killTarget", m13489val, static_cast<ui32>(-1));
  481. break;
  482. case MISSION_ART:
  483. //todo: ban artifacts
  484. handler.serializeIdArray<ArtifactID>("artifacts", m5arts);
  485. break;
  486. case MISSION_ARMY:
  487. {
  488. auto a = handler.enterArray("creatures");
  489. a.serializeStruct(m6creatures);
  490. }
  491. break;
  492. case MISSION_RESOURCES:
  493. {
  494. auto r = handler.enterStruct("resources");
  495. if(!handler.saving)
  496. m7resources.resize(GameConstants::RESOURCE_QUANTITY-1);
  497. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  498. {
  499. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  500. }
  501. }
  502. break;
  503. case MISSION_HERO:
  504. handler.serializeId<ui32, ui32, HeroTypeID>("hero", m13489val, 0);
  505. break;
  506. case MISSION_PLAYER:
  507. handler.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  508. break;
  509. default:
  510. logGlobal->error("Invalid quest mission type");
  511. break;
  512. }
  513. }
  514. void CGSeerHut::setObjToKill()
  515. {
  516. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  517. {
  518. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  519. assert(quest->stackToKill.type);
  520. quest->stackToKill.count = 0; //no count in info window
  521. quest->stackDirection = checkDirection();
  522. }
  523. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  524. {
  525. quest->heroName = getHeroToKill(false)->getNameTranslated();
  526. quest->heroPortrait = getHeroToKill(false)->portrait;
  527. }
  528. }
  529. void CGSeerHut::init(CRandomGenerator & rand)
  530. {
  531. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  532. seerName = *RandomGeneratorUtil::nextItem(names, rand);
  533. quest->textOption = rand.nextInt(2);
  534. quest->completedOption = rand.nextInt(1, 3);
  535. }
  536. void CGSeerHut::initObj(CRandomGenerator & rand)
  537. {
  538. init(rand);
  539. quest->progress = CQuest::NOT_ACTIVE;
  540. if(quest->missionType)
  541. {
  542. std::string questName = quest->missionName(quest->missionType);
  543. if(!quest->isCustomFirst)
  544. quest->firstVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(0), quest->textOption);
  545. if(!quest->isCustomNext)
  546. quest->nextVisitText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(1), quest->textOption);
  547. if(!quest->isCustomComplete)
  548. quest->completedText = VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + quest->missionState(2), quest->textOption);
  549. }
  550. else
  551. {
  552. quest->progress = CQuest::COMPLETE;
  553. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  554. }
  555. }
  556. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  557. {
  558. quest->getRolloverText (text, onHover);//TODO: simplify?
  559. if(!onHover)
  560. text.addReplacement(seerName);
  561. }
  562. std::string CGSeerHut::getHoverText(PlayerColor player) const
  563. {
  564. std::string hoverName = getObjectName();
  565. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  566. {
  567. hoverName = VLC->generaltexth->allTexts[347];
  568. boost::algorithm::replace_first(hoverName, "%s", seerName);
  569. }
  570. if(quest->progress & quest->missionType) //rollover when the quest is active
  571. {
  572. MetaString ms;
  573. getRolloverText (ms, true);
  574. hoverName += ms.toString();
  575. }
  576. return hoverName;
  577. }
  578. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  579. {
  580. switch(missionType)
  581. {
  582. case MISSION_KILL_CREATURE:
  583. out.addReplacement(stackToKill);
  584. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  585. {
  586. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  587. }
  588. break;
  589. case MISSION_KILL_HERO:
  590. out.addReplacement(heroName);
  591. break;
  592. }
  593. }
  594. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  595. {
  596. return quest->checkQuest(h);
  597. }
  598. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  599. {
  600. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  601. }
  602. void IQuestObject::afterAddToMapCommon(CMap * map) const
  603. {
  604. map->addNewQuestInstance(quest);
  605. }
  606. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  607. {
  608. quest->getCompletionText (text, components, isCustom, h);
  609. switch(rewardType)
  610. {
  611. case EXPERIENCE:
  612. components.emplace_back(Component::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(rVal)), 0);
  613. break;
  614. case MANA_POINTS:
  615. components.emplace_back(Component::PRIM_SKILL, 5, rVal, 0);
  616. break;
  617. case MORALE_BONUS:
  618. components.emplace_back(Component::MORALE, 0, rVal, 0);
  619. break;
  620. case LUCK_BONUS:
  621. components.emplace_back(Component::LUCK, 0, rVal, 0);
  622. break;
  623. case RESOURCES:
  624. components.emplace_back(Component::RESOURCE, rID, rVal, 0);
  625. break;
  626. case PRIMARY_SKILL:
  627. components.emplace_back(Component::PRIM_SKILL, rID, rVal, 0);
  628. break;
  629. case SECONDARY_SKILL:
  630. components.emplace_back(Component::SEC_SKILL, rID, rVal, 0);
  631. break;
  632. case ARTIFACT:
  633. components.emplace_back(Component::ARTIFACT, rID, 0, 0);
  634. break;
  635. case SPELL:
  636. components.emplace_back(Component::SPELL, rID, 0, 0);
  637. break;
  638. case CREATURE:
  639. components.emplace_back(Component::CREATURE, rID, rVal, 0);
  640. break;
  641. }
  642. }
  643. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  644. {
  645. switch(what)
  646. {
  647. case 10:
  648. quest->progress = static_cast<CQuest::Eprogress>(val);
  649. break;
  650. }
  651. }
  652. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  653. {
  654. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  655. {
  656. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  657. }
  658. }
  659. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  660. {
  661. InfoWindow iw;
  662. iw.player = h->getOwner();
  663. if(quest->progress < CQuest::COMPLETE)
  664. {
  665. bool firstVisit = !quest->progress;
  666. bool failRequirements = !checkQuest(h);
  667. bool isCustom = false;
  668. if(firstVisit)
  669. {
  670. isCustom = quest->isCustomFirst;
  671. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  672. AddQuest aq;
  673. aq.quest = QuestInfo (quest, this, visitablePos());
  674. aq.player = h->tempOwner;
  675. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  676. }
  677. else if(failRequirements)
  678. {
  679. isCustom = quest->isCustomNext;
  680. }
  681. if(firstVisit || failRequirements)
  682. {
  683. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  684. cb->showInfoDialog(&iw);
  685. }
  686. if(!failRequirements) // propose completion, also on first visit
  687. {
  688. BlockingDialog bd (true, false);
  689. bd.player = h->getOwner();
  690. getCompletionText (bd.text, bd.components, isCustom, h);
  691. cb->showBlockingDialog (&bd);
  692. return;
  693. }
  694. }
  695. else
  696. {
  697. iw.text << VLC->generaltexth->seerEmpty[quest->completedOption];
  698. if (ID == Obj::SEER_HUT)
  699. iw.text.addReplacement(seerName);
  700. cb->showInfoDialog(&iw);
  701. }
  702. }
  703. int CGSeerHut::checkDirection() const
  704. {
  705. int3 cord = getCreatureToKill()->pos;
  706. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  707. {
  708. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  709. return 8;
  710. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  711. return 1;
  712. else //northeast
  713. return 2;
  714. }
  715. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  716. {
  717. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  718. return 7;
  719. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  720. return 9;
  721. else //east
  722. return 3;
  723. }
  724. else //south
  725. {
  726. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  727. return 6;
  728. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  729. return 5;
  730. else //southeast
  731. return 4;
  732. }
  733. }
  734. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  735. {
  736. if (accept)
  737. {
  738. switch (quest->missionType)
  739. {
  740. case CQuest::MISSION_ART:
  741. for (auto & elem : quest->m5arts)
  742. {
  743. if(!h->hasArt(elem))
  744. {
  745. // first we need to disassemble this backpack artifact
  746. const auto * assembly = h->getAssemblyByConstituent(elem);
  747. assert(assembly);
  748. for(const auto & ci : assembly->constituentsInfo)
  749. {
  750. cb->giveHeroNewArtifact(h, ci.art->artType, ArtifactPosition::PRE_FIRST);
  751. }
  752. // remove the assembly
  753. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  754. }
  755. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  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<Res::ERes>(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(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  792. Bonus::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<Res::ERes>(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.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. showInfoDialog(h, 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. showInfoDialog(h, 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. showInfoDialog(h,18,0);
  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