CQuest.cpp 29 KB

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