CQuest.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 "MiscObjects.h"
  19. #include "../IGameCallback.h"
  20. #include "../CGameState.h"
  21. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  22. #include "../serializer/JsonSerializeFormat.h"
  23. #include "../CModHandler.h"
  24. #include "../GameConstants.h"
  25. #include "../StringConstants.h"
  26. #include "../CSkillHandler.h"
  27. #include "../mapping/CMap.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  30. //TODO: Remove constructor
  31. CQuest::CQuest():
  32. qid(-1),
  33. missionType(MISSION_NONE),
  34. progress(NOT_ACTIVE),
  35. lastDay(-1),
  36. m13489val(0),
  37. textOption(0),
  38. completedOption(0),
  39. stackDirection(0),
  40. heroPortrait(-1),
  41. isCustomFirst(false),
  42. isCustomNext(false),
  43. isCustomComplete(false)
  44. {
  45. }
  46. static std::string visitedTxt(const bool visited)
  47. {
  48. int id = visited ? 352 : 353;
  49. return VLC->generaltexth->allTexts[id];
  50. }
  51. const std::string & CQuest::missionName(CQuest::Emission mission)
  52. {
  53. static const std::array<std::string, 11> names = {
  54. "empty",
  55. "heroLevel",
  56. "primarySkill",
  57. "killHero",
  58. "killCreature",
  59. "bringArt",
  60. "bringCreature",
  61. "bringResources",
  62. "bringHero",
  63. "bringPlayer",
  64. "keymaster"
  65. };
  66. if(static_cast<size_t>(mission) < names.size())
  67. return names[static_cast<size_t>(mission)];
  68. return names[0];
  69. }
  70. const std::string & CQuest::missionState(int state)
  71. {
  72. static const std::array<std::string, 5> states = {
  73. "receive",
  74. "visit",
  75. "complete",
  76. "hover",
  77. "description",
  78. };
  79. if(state < states.size())
  80. return states[state];
  81. return states[0];
  82. }
  83. bool CQuest::checkMissionArmy(const CQuest * q, const CCreatureSet * army)
  84. {
  85. std::vector<CStackBasicDescriptor>::const_iterator cre;
  86. TSlots::const_iterator it;
  87. ui32 count = 0;
  88. ui32 slotsCount = 0;
  89. bool hasExtraCreatures = false;
  90. for(cre = q->m6creatures.begin(); cre != q->m6creatures.end(); ++cre)
  91. {
  92. for(count = 0, it = army->Slots().begin(); it != army->Slots().end(); ++it)
  93. {
  94. if(it->second->type == cre->type)
  95. {
  96. count += it->second->count;
  97. slotsCount++;
  98. }
  99. }
  100. if(static_cast<TQuantity>(count) < cre->count) //not enough creatures of this kind
  101. return false;
  102. hasExtraCreatures |= static_cast<TQuantity>(count) > cre->count;
  103. }
  104. return hasExtraCreatures || slotsCount < army->Slots().size();
  105. }
  106. bool CQuest::checkQuest(const CGHeroInstance * h) const
  107. {
  108. switch (missionType)
  109. {
  110. case MISSION_NONE:
  111. return true;
  112. case MISSION_LEVEL:
  113. return m13489val <= h->level;
  114. case MISSION_PRIMARY_STAT:
  115. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  116. {
  117. if(h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)) < static_cast<int>(m2stats[i]))
  118. return false;
  119. }
  120. return true;
  121. case MISSION_KILL_HERO:
  122. case MISSION_KILL_CREATURE:
  123. if(!CGHeroInstance::cb->getObjByQuestIdentifier(m13489val))
  124. return true;
  125. return false;
  126. case MISSION_ART:
  127. {
  128. // if the object was deserialized
  129. if(artifactsRequirements.empty())
  130. for(const auto & id : m5arts)
  131. ++artifactsRequirements[id];
  132. size_t reqSlots = 0;
  133. for(const auto & elem : artifactsRequirements)
  134. {
  135. // check required amount of artifacts
  136. if(h->getArtPosCount(elem.first, false, true, true) < elem.second)
  137. return false;
  138. if(!h->hasArt(elem.first))
  139. reqSlots += h->getAssemblyByConstituent(elem.first)->constituentsInfo.size() - 2;
  140. }
  141. if(ArtifactUtils::isBackpackFreeSlots(h, reqSlots))
  142. return true;
  143. else
  144. return false;
  145. }
  146. case MISSION_ARMY:
  147. return checkMissionArmy(this, h);
  148. case MISSION_RESOURCES:
  149. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i) //including Mithril ?
  150. { //Quest has no direct access to callback
  151. if(CGHeroInstance::cb->getResource(h->tempOwner, i) < static_cast<int>(m7resources[i]))
  152. return false;
  153. }
  154. return true;
  155. case MISSION_HERO:
  156. return m13489val == h->type->getIndex();
  157. case MISSION_PLAYER:
  158. return m13489val == h->getOwner().getNum();
  159. default:
  160. return false;
  161. }
  162. }
  163. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  164. {
  165. std::string text;
  166. bool failRequirements = (h ? !checkQuest(h) : true);
  167. if(firstVisit)
  168. {
  169. isCustom = isCustomFirst;
  170. iwText << (text = firstVisitText);
  171. }
  172. else if(failRequirements)
  173. {
  174. isCustom = isCustomNext;
  175. iwText << (text = nextVisitText);
  176. }
  177. switch (missionType)
  178. {
  179. case MISSION_LEVEL:
  180. components.emplace_back(Component::EComponentType::EXPERIENCE, 0, m13489val, 0);
  181. if(!isCustom)
  182. iwText.addReplacement(m13489val);
  183. break;
  184. case MISSION_PRIMARY_STAT:
  185. {
  186. MetaString loot;
  187. for(int i = 0; i < 4; ++i)
  188. {
  189. if(m2stats[i])
  190. {
  191. components.emplace_back(Component::EComponentType::PRIM_SKILL, i, m2stats[i], 0);
  192. loot << "%d %s";
  193. loot.addReplacement(m2stats[i]);
  194. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  195. }
  196. }
  197. if (!isCustom)
  198. iwText.addReplacement(loot.buildList());
  199. }
  200. break;
  201. case MISSION_KILL_HERO:
  202. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, heroPortrait, 0, 0);
  203. if(!isCustom)
  204. addReplacements(iwText, text);
  205. break;
  206. case MISSION_HERO:
  207. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  208. components.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroh->objects[m13489val]->imageIndex, 0, 0);
  209. if(!isCustom)
  210. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  211. break;
  212. case MISSION_KILL_CREATURE:
  213. {
  214. components.emplace_back(stackToKill);
  215. if(!isCustom)
  216. {
  217. addReplacements(iwText, text);
  218. }
  219. }
  220. break;
  221. case MISSION_ART:
  222. {
  223. MetaString loot;
  224. for(const auto & elem : m5arts)
  225. {
  226. components.emplace_back(Component::EComponentType::ARTIFACT, elem, 0, 0);
  227. loot << "%s";
  228. loot.addReplacement(MetaString::ART_NAMES, elem);
  229. }
  230. if(!isCustom)
  231. iwText.addReplacement(loot.buildList());
  232. }
  233. break;
  234. case MISSION_ARMY:
  235. {
  236. MetaString loot;
  237. for(const auto & elem : m6creatures)
  238. {
  239. components.emplace_back(elem);
  240. loot << "%s";
  241. loot.addReplacement(elem);
  242. }
  243. if(!isCustom)
  244. iwText.addReplacement(loot.buildList());
  245. }
  246. break;
  247. case MISSION_RESOURCES:
  248. {
  249. MetaString loot;
  250. for(int i = 0; i < 7; ++i)
  251. {
  252. if(m7resources[i])
  253. {
  254. components.emplace_back(Component::EComponentType::RESOURCE, i, m7resources[i], 0);
  255. loot << "%d %s";
  256. loot.addReplacement(m7resources[i]);
  257. loot.addReplacement(MetaString::RES_NAMES, i);
  258. }
  259. }
  260. if(!isCustom)
  261. iwText.addReplacement(loot.buildList());
  262. }
  263. break;
  264. case MISSION_PLAYER:
  265. components.emplace_back(Component::EComponentType::FLAG, m13489val, 0, 0);
  266. if(!isCustom)
  267. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  268. break;
  269. }
  270. }
  271. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  272. {
  273. // Quests with MISSION_NONE type don't have a text for them
  274. assert(missionType != MISSION_NONE);
  275. if(onHover)
  276. ms << "\n\n";
  277. std::string questName = missionName(missionType);
  278. std::string questState = missionState(onHover ? 3 : 4);
  279. ms << VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption);
  280. switch(missionType)
  281. {
  282. case MISSION_LEVEL:
  283. ms.addReplacement(m13489val);
  284. break;
  285. case MISSION_PRIMARY_STAT:
  286. {
  287. MetaString loot;
  288. for (int i = 0; i < 4; ++i)
  289. {
  290. if (m2stats[i])
  291. {
  292. loot << "%d %s";
  293. loot.addReplacement(m2stats[i]);
  294. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  295. }
  296. }
  297. ms.addReplacement(loot.buildList());
  298. }
  299. break;
  300. case MISSION_KILL_HERO:
  301. ms.addReplacement(heroName);
  302. break;
  303. case MISSION_KILL_CREATURE:
  304. ms.addReplacement(stackToKill);
  305. break;
  306. case MISSION_ART:
  307. {
  308. MetaString loot;
  309. for(const auto & elem : m5arts)
  310. {
  311. loot << "%s";
  312. loot.addReplacement(MetaString::ART_NAMES, elem);
  313. }
  314. ms.addReplacement(loot.buildList());
  315. }
  316. break;
  317. case MISSION_ARMY:
  318. {
  319. MetaString loot;
  320. for(const auto & elem : m6creatures)
  321. {
  322. loot << "%s";
  323. loot.addReplacement(elem);
  324. }
  325. ms.addReplacement(loot.buildList());
  326. }
  327. break;
  328. case MISSION_RESOURCES:
  329. {
  330. MetaString loot;
  331. for (int i = 0; i < 7; ++i)
  332. {
  333. if (m7resources[i])
  334. {
  335. loot << "%d %s";
  336. loot.addReplacement(m7resources[i]);
  337. loot.addReplacement(MetaString::RES_NAMES, i);
  338. }
  339. }
  340. ms.addReplacement(loot.buildList());
  341. }
  342. break;
  343. case MISSION_HERO:
  344. ms.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  345. break;
  346. case MISSION_PLAYER:
  347. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  354. {
  355. iwText << completedText;
  356. switch(missionType)
  357. {
  358. case CQuest::MISSION_LEVEL:
  359. if (!isCustomComplete)
  360. iwText.addReplacement(m13489val);
  361. break;
  362. case CQuest::MISSION_PRIMARY_STAT:
  363. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  364. {
  365. MetaString loot;
  366. for (int i = 0; i < 4; ++i)
  367. {
  368. if (m2stats[i])
  369. {
  370. loot << "%d %s";
  371. loot.addReplacement(m2stats[i]);
  372. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  373. }
  374. }
  375. if (!isCustomComplete)
  376. iwText.addReplacement(loot.buildList());
  377. }
  378. break;
  379. case CQuest::MISSION_ART:
  380. {
  381. MetaString loot;
  382. for(const auto & elem : m5arts)
  383. {
  384. loot << "%s";
  385. loot.addReplacement(MetaString::ART_NAMES, elem);
  386. }
  387. if (!isCustomComplete)
  388. iwText.addReplacement(loot.buildList());
  389. }
  390. break;
  391. case CQuest::MISSION_ARMY:
  392. {
  393. MetaString loot;
  394. for(const auto & elem : m6creatures)
  395. {
  396. loot << "%s";
  397. loot.addReplacement(elem);
  398. }
  399. if (!isCustomComplete)
  400. iwText.addReplacement(loot.buildList());
  401. }
  402. break;
  403. case CQuest::MISSION_RESOURCES:
  404. {
  405. MetaString loot;
  406. for (int i = 0; i < 7; ++i)
  407. {
  408. if (m7resources[i])
  409. {
  410. loot << "%d %s";
  411. loot.addReplacement(m7resources[i]);
  412. loot.addReplacement(MetaString::RES_NAMES, i);
  413. }
  414. }
  415. if (!isCustomComplete)
  416. iwText.addReplacement(loot.buildList());
  417. }
  418. break;
  419. case MISSION_KILL_HERO:
  420. case MISSION_KILL_CREATURE:
  421. if (!isCustomComplete)
  422. addReplacements(iwText, completedText);
  423. break;
  424. case MISSION_HERO:
  425. if (!isCustomComplete)
  426. iwText.addReplacement(VLC->heroh->objects[m13489val]->getNameTranslated());
  427. break;
  428. case MISSION_PLAYER:
  429. if (!isCustomComplete)
  430. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  431. break;
  432. }
  433. }
  434. void CQuest::addArtifactID(const ArtifactID & id)
  435. {
  436. m5arts.push_back(id);
  437. ++artifactsRequirements[id];
  438. }
  439. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  440. {
  441. auto q = handler.enterStruct(fieldName);
  442. handler.serializeString("firstVisitText", firstVisitText);
  443. handler.serializeString("nextVisitText", nextVisitText);
  444. handler.serializeString("completedText", completedText);
  445. if(!handler.saving)
  446. {
  447. isCustomFirst = !firstVisitText.empty();
  448. isCustomNext = !nextVisitText.empty();
  449. isCustomComplete = !completedText.empty();
  450. }
  451. static const std::vector<std::string> MISSION_TYPE_JSON =
  452. {
  453. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  454. };
  455. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  456. handler.serializeInt("timeLimit", lastDay, -1);
  457. switch (missionType)
  458. {
  459. case MISSION_NONE:
  460. break;
  461. case MISSION_LEVEL:
  462. handler.serializeInt("heroLevel", m13489val, -1);
  463. break;
  464. case MISSION_PRIMARY_STAT:
  465. {
  466. auto primarySkills = handler.enterStruct("primarySkills");
  467. if(!handler.saving)
  468. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  469. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  470. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  471. }
  472. break;
  473. case MISSION_KILL_HERO:
  474. case MISSION_KILL_CREATURE:
  475. handler.serializeInstance<ui32>("killTarget", m13489val, static_cast<ui32>(-1));
  476. break;
  477. case MISSION_ART:
  478. //todo: ban artifacts
  479. handler.serializeIdArray<ArtifactID>("artifacts", m5arts);
  480. break;
  481. case MISSION_ARMY:
  482. {
  483. auto a = handler.enterArray("creatures");
  484. a.serializeStruct(m6creatures);
  485. }
  486. break;
  487. case MISSION_RESOURCES:
  488. {
  489. auto r = handler.enterStruct("resources");
  490. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  491. {
  492. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  493. }
  494. }
  495. break;
  496. case MISSION_HERO:
  497. handler.serializeId<ui32, ui32, HeroTypeID>("hero", m13489val, 0);
  498. break;
  499. case MISSION_PLAYER:
  500. handler.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  501. break;
  502. default:
  503. logGlobal->error("Invalid quest mission type");
  504. break;
  505. }
  506. }
  507. void CGSeerHut::setObjToKill()
  508. {
  509. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  510. {
  511. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  512. assert(quest->stackToKill.type);
  513. quest->stackToKill.count = 0; //no count in info window
  514. quest->stackDirection = checkDirection();
  515. }
  516. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  517. {
  518. quest->heroName = getHeroToKill(false)->getNameTranslated();
  519. quest->heroPortrait = getHeroToKill(false)->portrait;
  520. }
  521. }
  522. void CGSeerHut::init(CRandomGenerator & rand)
  523. {
  524. auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
  525. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  526. seerName = VLC->generaltexth->translate(seerNameID);
  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->getTypeId() != 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<EGameResID>(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(BonusDuration::ONE_WEEK, (rewardType == 3 ? BonusType::MORALE : BonusType::LUCK),
  791. BonusSource::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<EGameResID>(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. cb->giveHeroNewArtifact(h, VLC->arth->objects[rID],ArtifactPosition::FIRST_AVAILABLE);
  809. break;
  810. case SPELL:
  811. {
  812. std::set<SpellID> spell;
  813. spell.insert (SpellID(rID));
  814. cb->changeSpells(h, true, spell);
  815. }
  816. break;
  817. case CREATURE:
  818. {
  819. CCreatureSet creatures;
  820. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  821. cb->giveCreatures(this, h, creatures, false);
  822. }
  823. break;
  824. default:
  825. break;
  826. }
  827. }
  828. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  829. {
  830. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  831. if(allowNull && !o)
  832. return nullptr;
  833. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  834. return dynamic_cast<const CGHeroInstance *>(o);
  835. }
  836. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  837. {
  838. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  839. if(allowNull && !o)
  840. return nullptr;
  841. assert(o && o->ID == Obj::MONSTER);
  842. return dynamic_cast<const CGCreature *>(o);
  843. }
  844. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  845. {
  846. finishQuest(hero, answer);
  847. }
  848. void CGSeerHut::afterAddToMap(CMap* map)
  849. {
  850. IQuestObject::afterAddToMapCommon(map);
  851. }
  852. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  853. {
  854. static const std::map<ERewardType, std::string> REWARD_MAP =
  855. {
  856. {NOTHING, ""},
  857. {EXPERIENCE, "experience"},
  858. {MANA_POINTS, "mana"},
  859. {MORALE_BONUS, "morale"},
  860. {LUCK_BONUS, "luck"},
  861. {RESOURCES, "resource"},
  862. {PRIMARY_SKILL, "primarySkill"},
  863. {SECONDARY_SKILL,"secondarySkill"},
  864. {ARTIFACT, "artifact"},
  865. {SPELL, "spell"},
  866. {CREATURE, "creature"}
  867. };
  868. static const std::map<std::string, ERewardType> REWARD_RMAP =
  869. {
  870. {"experience", EXPERIENCE},
  871. {"mana", MANA_POINTS},
  872. {"morale", MORALE_BONUS},
  873. {"luck", LUCK_BONUS},
  874. {"resource", RESOURCES},
  875. {"primarySkill", PRIMARY_SKILL},
  876. {"secondarySkill",SECONDARY_SKILL},
  877. {"artifact", ARTIFACT},
  878. {"spell", SPELL},
  879. {"creature", CREATURE}
  880. };
  881. //quest and reward
  882. quest->serializeJson(handler, "quest");
  883. //only one reward is supported
  884. //todo: full reward format support after CRewardInfo integration
  885. auto s = handler.enterStruct("reward");
  886. std::string fullIdentifier;
  887. std::string metaTypeName;
  888. std::string scope;
  889. std::string identifier;
  890. if(handler.saving)
  891. {
  892. si32 amount = rVal;
  893. metaTypeName = REWARD_MAP.at(rewardType);
  894. switch (rewardType)
  895. {
  896. case NOTHING:
  897. break;
  898. case EXPERIENCE:
  899. case MANA_POINTS:
  900. case MORALE_BONUS:
  901. case LUCK_BONUS:
  902. identifier.clear();
  903. break;
  904. case RESOURCES:
  905. identifier = GameConstants::RESOURCE_NAMES[rID];
  906. break;
  907. case PRIMARY_SKILL:
  908. identifier = PrimarySkill::names[rID];
  909. break;
  910. case SECONDARY_SKILL:
  911. identifier = CSkillHandler::encodeSkill(rID);
  912. break;
  913. case ARTIFACT:
  914. identifier = ArtifactID(rID).toArtifact(VLC->artifacts())->getJsonKey();
  915. amount = 1;
  916. break;
  917. case SPELL:
  918. identifier = SpellID(rID).toSpell(VLC->spells())->getJsonKey();
  919. amount = 1;
  920. break;
  921. case CREATURE:
  922. identifier = CreatureID(rID).toCreature(VLC->creatures())->getJsonKey();
  923. break;
  924. default:
  925. assert(false);
  926. break;
  927. }
  928. if(rewardType != NOTHING)
  929. {
  930. fullIdentifier = CModHandler::makeFullIdentifier(scope, metaTypeName, identifier);
  931. handler.serializeInt(fullIdentifier, amount);
  932. }
  933. }
  934. else
  935. {
  936. rewardType = NOTHING;
  937. const JsonNode & rewardsJson = handler.getCurrent();
  938. fullIdentifier.clear();
  939. if(rewardsJson.Struct().empty())
  940. return;
  941. else
  942. {
  943. auto iter = rewardsJson.Struct().begin();
  944. fullIdentifier = iter->first;
  945. }
  946. CModHandler::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  947. auto it = REWARD_RMAP.find(metaTypeName);
  948. if(it == REWARD_RMAP.end())
  949. {
  950. logGlobal->error("%s: invalid metatype in reward item %s", instanceName, fullIdentifier);
  951. return;
  952. }
  953. else
  954. {
  955. rewardType = it->second;
  956. }
  957. bool doRequest = false;
  958. switch (rewardType)
  959. {
  960. case NOTHING:
  961. return;
  962. case EXPERIENCE:
  963. case MANA_POINTS:
  964. case MORALE_BONUS:
  965. case LUCK_BONUS:
  966. break;
  967. case PRIMARY_SKILL:
  968. doRequest = true;
  969. break;
  970. case RESOURCES:
  971. case SECONDARY_SKILL:
  972. case ARTIFACT:
  973. case SPELL:
  974. case CREATURE:
  975. doRequest = true;
  976. break;
  977. default:
  978. assert(false);
  979. break;
  980. }
  981. if(doRequest)
  982. {
  983. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), fullIdentifier, false);
  984. if(rawId)
  985. {
  986. rID = rawId.value();
  987. }
  988. else
  989. {
  990. rewardType = NOTHING;//fallback in case of error
  991. return;
  992. }
  993. }
  994. handler.serializeInt(fullIdentifier, rVal);
  995. }
  996. }
  997. void CGQuestGuard::init(CRandomGenerator & rand)
  998. {
  999. blockVisit = true;
  1000. quest->textOption = rand.nextInt(3, 5);
  1001. quest->completedOption = rand.nextInt(4, 5);
  1002. }
  1003. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  1004. {
  1005. cb->removeObject(this);
  1006. }
  1007. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  1008. {
  1009. //quest only, do not call base class
  1010. quest->serializeJson(handler, "quest");
  1011. }
  1012. void CGKeys::reset()
  1013. {
  1014. playerKeyMap.clear();
  1015. }
  1016. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  1017. {
  1018. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  1019. {
  1020. PlayerColor player(what-101);
  1021. playerKeyMap[player].insert(static_cast<ui8>(val));
  1022. }
  1023. else
  1024. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  1025. }
  1026. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  1027. {
  1028. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  1029. }
  1030. std::string CGKeys::getHoverText(PlayerColor player) const
  1031. {
  1032. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  1033. }
  1034. std::string CGKeys::getObjectName() const
  1035. {
  1036. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  1037. }
  1038. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  1039. {
  1040. return wasMyColorVisited (player);
  1041. }
  1042. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  1043. {
  1044. int txt_id;
  1045. if (!wasMyColorVisited (h->getOwner()) )
  1046. {
  1047. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  1048. txt_id=19;
  1049. }
  1050. else
  1051. txt_id=20;
  1052. h->showInfoDialog(txt_id);
  1053. }
  1054. void CGBorderGuard::initObj(CRandomGenerator & rand)
  1055. {
  1056. //ui32 m13489val = subID; //store color as quest info
  1057. blockVisit = true;
  1058. }
  1059. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  1060. {
  1061. text << std::pair<ui8,ui32>(11,18);
  1062. }
  1063. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  1064. {
  1065. if (!onHover)
  1066. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID);
  1067. }
  1068. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  1069. {
  1070. return wasMyColorVisited (h->tempOwner);
  1071. }
  1072. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  1073. {
  1074. if (wasMyColorVisited (h->getOwner()) )
  1075. {
  1076. BlockingDialog bd (true, false);
  1077. bd.player = h->getOwner();
  1078. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  1079. cb->showBlockingDialog (&bd);
  1080. }
  1081. else
  1082. {
  1083. h->showInfoDialog(18);
  1084. AddQuest aq;
  1085. aq.quest = QuestInfo (quest, this, visitablePos());
  1086. aq.player = h->tempOwner;
  1087. cb->sendAndApply (&aq);
  1088. //TODO: add this quest only once OR check for multiple instances later
  1089. }
  1090. }
  1091. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1092. {
  1093. if (answer)
  1094. cb->removeObject(this);
  1095. }
  1096. void CGBorderGuard::afterAddToMap(CMap * map)
  1097. {
  1098. IQuestObject::afterAddToMapCommon(map);
  1099. }
  1100. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1101. {
  1102. if (!wasMyColorVisited (h->getOwner()) )
  1103. {
  1104. h->showInfoDialog(18);
  1105. AddQuest aq;
  1106. aq.quest = QuestInfo (quest, this, visitablePos());
  1107. aq.player = h->tempOwner;
  1108. cb->sendAndApply (&aq);
  1109. }
  1110. }
  1111. bool CGBorderGate::passableFor(PlayerColor color) const
  1112. {
  1113. return wasMyColorVisited(color);
  1114. }
  1115. VCMI_LIB_NAMESPACE_END