CQuest.cpp 28 KB

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