CQuest.cpp 30 KB

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