CQuest.cpp 30 KB

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