CQuest.cpp 29 KB

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