CQuest.cpp 30 KB

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