CQuest.cpp 30 KB

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