CQuest.cpp 29 KB

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