CQuest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. *
  3. * CQuest.cpp, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. #include "StdInc.h"
  12. #include "CQuest.h"
  13. #include "../NetPacks.h"
  14. #include "../CSoundBase.h"
  15. #include "../CGeneralTextHandler.h"
  16. #include "../CHeroHandler.h"
  17. #include "CObjectClassesHandler.h"
  18. #include "MiscObjects.h"
  19. #include "../IGameCallback.h"
  20. #include "../CGameState.h"
  21. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  22. CQuest::CQuest()
  23. : qid(-1), missionType(MISSION_NONE), progress(NOT_ACTIVE), lastDay(-1), m13489val(0),
  24. textOption(0), completedOption(0), stackDirection(0), heroPortrait(-1),
  25. isCustomFirst(false), isCustomNext(false), isCustomComplete(false)
  26. {
  27. }
  28. ///helpers
  29. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  30. {
  31. InfoWindow iw;
  32. iw.soundID = soundID;
  33. iw.player = playerID;
  34. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  35. IObjectInterface::cb->sendAndApply(&iw);
  36. }
  37. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  38. {
  39. const PlayerColor playerID = h->getOwner();
  40. showInfoDialog(playerID,txtID,soundID);
  41. }
  42. static std::string & visitedTxt(const bool visited)
  43. {
  44. int id = visited ? 352 : 353;
  45. return VLC->generaltexth->allTexts[id];
  46. }
  47. bool CQuest::checkQuest(const CGHeroInstance * h) const
  48. {
  49. switch (missionType)
  50. {
  51. case MISSION_NONE:
  52. return true;
  53. case MISSION_LEVEL:
  54. if(m13489val <= h->level)
  55. return true;
  56. return false;
  57. case MISSION_PRIMARY_STAT:
  58. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  59. {
  60. if(h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)) < m2stats[i])
  61. return false;
  62. }
  63. return true;
  64. case MISSION_KILL_HERO:
  65. case MISSION_KILL_CREATURE:
  66. if (!h->cb->getObjByQuestIdentifier(m13489val))
  67. return true;
  68. return false;
  69. case MISSION_ART:
  70. for(auto & elem : m5arts)
  71. {
  72. if(h->hasArt(elem, false, true))
  73. continue;
  74. return false; //if the artifact was not found
  75. }
  76. return true;
  77. case MISSION_ARMY:
  78. {
  79. std::vector<CStackBasicDescriptor>::const_iterator cre;
  80. TSlots::const_iterator it;
  81. ui32 count;
  82. for(cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  83. {
  84. for(count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  85. {
  86. if(it->second->type == cre->type)
  87. count += it->second->count;
  88. }
  89. if(count < cre->count) //not enough creatures of this kind
  90. return false;
  91. }
  92. }
  93. return true;
  94. case MISSION_RESOURCES:
  95. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, +1)) //including Mithril ?
  96. { //Quest has no direct access to callback
  97. if(h->cb->getResource (h->tempOwner, i) < m7resources[i])
  98. return false;
  99. }
  100. return true;
  101. case MISSION_HERO:
  102. if(m13489val == h->type->ID.getNum())
  103. return true;
  104. return false;
  105. case MISSION_PLAYER:
  106. if(m13489val == h->getOwner().getNum())
  107. return true;
  108. return false;
  109. default:
  110. return false;
  111. }
  112. }
  113. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  114. {
  115. std::string text;
  116. bool failRequirements = (h ? !checkQuest(h) : true);
  117. if(firstVisit)
  118. {
  119. isCustom = isCustomFirst;
  120. iwText << (text = firstVisitText);
  121. }
  122. else if(failRequirements)
  123. {
  124. isCustom = isCustomNext;
  125. iwText << (text = nextVisitText);
  126. }
  127. switch (missionType)
  128. {
  129. case MISSION_LEVEL:
  130. components.push_back(Component (Component::EXPERIENCE, 0, m13489val, 0));
  131. if(!isCustom)
  132. iwText.addReplacement(m13489val);
  133. break;
  134. case MISSION_PRIMARY_STAT:
  135. {
  136. MetaString loot;
  137. for(int i = 0; i < 4; ++i)
  138. {
  139. if(m2stats[i])
  140. {
  141. components.push_back(Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  142. loot << "%d %s";
  143. loot.addReplacement(m2stats[i]);
  144. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  145. }
  146. }
  147. if (!isCustom)
  148. iwText.addReplacement(loot.buildList());
  149. }
  150. break;
  151. case MISSION_KILL_HERO:
  152. components.push_back(Component(Component::HERO_PORTRAIT, heroPortrait, 0, 0));
  153. if(!isCustom)
  154. addReplacements(iwText, text);
  155. break;
  156. case MISSION_HERO:
  157. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  158. components.push_back(Component (Component::HERO_PORTRAIT, VLC->heroh->heroes[m13489val]->imageIndex, 0, 0));
  159. if(!isCustom)
  160. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  161. break;
  162. case MISSION_KILL_CREATURE:
  163. {
  164. components.push_back(Component(stackToKill));
  165. if(!isCustom)
  166. {
  167. addReplacements(iwText, text);
  168. }
  169. }
  170. break;
  171. case MISSION_ART:
  172. {
  173. MetaString loot;
  174. for(auto & elem : m5arts)
  175. {
  176. components.push_back(Component (Component::ARTIFACT, elem, 0, 0));
  177. loot << "%s";
  178. loot.addReplacement(MetaString::ART_NAMES, elem);
  179. }
  180. if(!isCustom)
  181. iwText.addReplacement(loot.buildList());
  182. }
  183. break;
  184. case MISSION_ARMY:
  185. {
  186. MetaString loot;
  187. for(auto & elem : m6creatures)
  188. {
  189. components.push_back(Component(elem));
  190. loot << "%s";
  191. loot.addReplacement(elem);
  192. }
  193. if(!isCustom)
  194. iwText.addReplacement(loot.buildList());
  195. }
  196. break;
  197. case MISSION_RESOURCES:
  198. {
  199. MetaString loot;
  200. for(int i = 0; i < 7; ++i)
  201. {
  202. if(m7resources[i])
  203. {
  204. components.push_back(Component (Component::RESOURCE, i, m7resources[i], 0));
  205. loot << "%d %s";
  206. loot.addReplacement(m7resources[i]);
  207. loot.addReplacement(MetaString::RES_NAMES, i);
  208. }
  209. }
  210. if(!isCustom)
  211. iwText.addReplacement(loot.buildList());
  212. }
  213. break;
  214. case MISSION_PLAYER:
  215. components.push_back(Component (Component::FLAG, m13489val, 0, 0));
  216. if(!isCustom)
  217. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  218. break;
  219. }
  220. }
  221. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  222. {
  223. // Quests with MISSION_NONE type don't have a text for them
  224. assert(missionType != MISSION_NONE);
  225. if(onHover)
  226. ms << "\n\n";
  227. ms << VLC->generaltexth->quests[missionType-1][onHover ? 3 : 4][textOption];
  228. switch(missionType)
  229. {
  230. case MISSION_LEVEL:
  231. ms.addReplacement(boost::lexical_cast<std::string>(m13489val));
  232. break;
  233. case MISSION_PRIMARY_STAT:
  234. {
  235. MetaString loot;
  236. for (int i = 0; i < 4; ++i)
  237. {
  238. if (m2stats[i])
  239. {
  240. loot << "%d %s";
  241. loot.addReplacement(m2stats[i]);
  242. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  243. }
  244. }
  245. ms.addReplacement(loot.buildList());
  246. }
  247. break;
  248. case MISSION_KILL_HERO:
  249. ms.addReplacement(heroName);
  250. break;
  251. case MISSION_KILL_CREATURE:
  252. ms.addReplacement(stackToKill);
  253. break;
  254. case MISSION_ART:
  255. {
  256. MetaString loot;
  257. for (auto & elem : m5arts)
  258. {
  259. loot << "%s";
  260. loot.addReplacement(MetaString::ART_NAMES, elem);
  261. }
  262. ms.addReplacement(loot.buildList());
  263. }
  264. break;
  265. case MISSION_ARMY:
  266. {
  267. MetaString loot;
  268. for (auto & elem : m6creatures)
  269. {
  270. loot << "%s";
  271. loot.addReplacement(elem);
  272. }
  273. ms.addReplacement(loot.buildList());
  274. }
  275. break;
  276. case MISSION_RESOURCES:
  277. {
  278. MetaString loot;
  279. for (int i = 0; i < 7; ++i)
  280. {
  281. if (m7resources[i])
  282. {
  283. loot << "%d %s";
  284. loot.addReplacement(m7resources[i]);
  285. loot.addReplacement(MetaString::RES_NAMES, i);
  286. }
  287. }
  288. ms.addReplacement(loot.buildList());
  289. }
  290. break;
  291. case MISSION_HERO:
  292. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  293. break;
  294. case MISSION_PLAYER:
  295. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  302. {
  303. iwText << completedText;
  304. switch(missionType)
  305. {
  306. case CQuest::MISSION_LEVEL:
  307. if (!isCustomComplete)
  308. iwText.addReplacement(m13489val);
  309. break;
  310. case CQuest::MISSION_PRIMARY_STAT:
  311. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  312. {
  313. MetaString loot;
  314. for (int i = 0; i < 4; ++i)
  315. {
  316. if (m2stats[i])
  317. {
  318. loot << "%d %s";
  319. loot.addReplacement(m2stats[i]);
  320. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  321. }
  322. }
  323. if (!isCustomComplete)
  324. iwText.addReplacement(loot.buildList());
  325. }
  326. break;
  327. case CQuest::MISSION_ART:
  328. {
  329. MetaString loot;
  330. for (auto & elem : m5arts)
  331. {
  332. loot << "%s";
  333. loot.addReplacement(MetaString::ART_NAMES, elem);
  334. }
  335. if (!isCustomComplete)
  336. iwText.addReplacement(loot.buildList());
  337. }
  338. break;
  339. case CQuest::MISSION_ARMY:
  340. {
  341. MetaString loot;
  342. for (auto & elem : m6creatures)
  343. {
  344. loot << "%s";
  345. loot.addReplacement(elem);
  346. }
  347. if (!isCustomComplete)
  348. iwText.addReplacement(loot.buildList());
  349. }
  350. break;
  351. case CQuest::MISSION_RESOURCES:
  352. {
  353. MetaString loot;
  354. for (int i = 0; i < 7; ++i)
  355. {
  356. if (m7resources[i])
  357. {
  358. loot << "%d %s";
  359. loot.addReplacement(m7resources[i]);
  360. loot.addReplacement(MetaString::RES_NAMES, i);
  361. }
  362. }
  363. if (!isCustomComplete)
  364. iwText.addReplacement(loot.buildList());
  365. }
  366. break;
  367. case MISSION_KILL_HERO:
  368. case MISSION_KILL_CREATURE:
  369. if (!isCustomComplete)
  370. addReplacements(iwText, completedText);
  371. break;
  372. case MISSION_HERO:
  373. if (!isCustomComplete)
  374. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  375. break;
  376. case MISSION_PLAYER:
  377. if (!isCustomComplete)
  378. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  379. break;
  380. }
  381. }
  382. CGSeerHut::CGSeerHut() : IQuestObject(),
  383. rewardType(NOTHING), rID(-1), rVal(-1)
  384. {
  385. quest->lastDay = -1;
  386. quest->isCustomFirst = false;
  387. quest->isCustomNext = false;
  388. quest->isCustomComplete = false;
  389. }
  390. void CGSeerHut::setObjToKill()
  391. {
  392. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  393. {
  394. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  395. assert(quest->stackToKill.type);
  396. quest->stackToKill.count = 0; //no count in info window
  397. quest->stackDirection = checkDirection();
  398. }
  399. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  400. {
  401. quest->heroName = getHeroToKill(false)->name;
  402. quest->heroPortrait = getHeroToKill(false)->portrait;
  403. }
  404. }
  405. void CGSeerHut::init()
  406. {
  407. seerName = *RandomGeneratorUtil::nextItem(VLC->generaltexth->seerNames, cb->gameState()->getRandomGenerator());
  408. quest->textOption = cb->gameState()->getRandomGenerator().nextInt(2);
  409. quest->completedOption = cb->gameState()->getRandomGenerator().nextInt(1, 3);
  410. }
  411. void CGSeerHut::initObj()
  412. {
  413. init();
  414. quest->progress = CQuest::NOT_ACTIVE;
  415. if(quest->missionType)
  416. {
  417. if(!quest->isCustomFirst)
  418. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  419. if(!quest->isCustomNext)
  420. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  421. if(!quest->isCustomComplete)
  422. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  423. }
  424. else
  425. {
  426. quest->progress = CQuest::COMPLETE;
  427. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  428. }
  429. }
  430. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  431. {
  432. quest->getRolloverText (text, onHover);//TODO: simplify?
  433. if(!onHover)
  434. text.addReplacement(seerName);
  435. }
  436. std::string CGSeerHut::getHoverText(PlayerColor player) const
  437. {
  438. std::string hoverName = getObjectName();
  439. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  440. {
  441. hoverName = VLC->generaltexth->allTexts[347];
  442. boost::algorithm::replace_first(hoverName, "%s", seerName);
  443. }
  444. if(quest->progress & quest->missionType) //rollover when the quest is active
  445. {
  446. MetaString ms;
  447. getRolloverText (ms, true);
  448. hoverName += ms.toString();
  449. }
  450. return hoverName;
  451. }
  452. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  453. {
  454. switch(missionType)
  455. {
  456. case MISSION_KILL_CREATURE:
  457. out.addReplacement(stackToKill);
  458. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  459. {
  460. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  461. }
  462. break;
  463. case MISSION_KILL_HERO:
  464. out.addReplacement(heroName);
  465. break;
  466. }
  467. }
  468. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  469. {
  470. return quest->checkQuest(h);
  471. }
  472. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  473. {
  474. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  475. }
  476. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  477. {
  478. quest->getCompletionText (text, components, isCustom, h);
  479. switch(rewardType)
  480. {
  481. case EXPERIENCE: components.push_back(Component (Component::EXPERIENCE, 0, h->calculateXp(rVal), 0));
  482. break;
  483. case MANA_POINTS: components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  484. break;
  485. case MORALE_BONUS: components.push_back(Component (Component::MORALE, 0, rVal, 0));
  486. break;
  487. case LUCK_BONUS: components.push_back(Component (Component::LUCK, 0, rVal, 0));
  488. break;
  489. case RESOURCES: components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  490. break;
  491. case PRIMARY_SKILL: components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  492. break;
  493. case SECONDARY_SKILL: components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  494. break;
  495. case ARTIFACT: components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  496. break;
  497. case SPELL: components.push_back(Component (Component::SPELL, rID, 0, 0));
  498. break;
  499. case CREATURE: components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  500. break;
  501. }
  502. }
  503. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  504. {
  505. switch(what)
  506. {
  507. case 10:
  508. quest->progress = static_cast<CQuest::Eprogress>(val);
  509. break;
  510. }
  511. }
  512. void CGSeerHut::newTurn() const
  513. {
  514. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  515. {
  516. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  517. }
  518. }
  519. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  520. {
  521. InfoWindow iw;
  522. iw.player = h->getOwner();
  523. if(quest->progress < CQuest::COMPLETE)
  524. {
  525. bool firstVisit = !quest->progress;
  526. bool failRequirements = !checkQuest(h);
  527. bool isCustom = false;
  528. if(firstVisit)
  529. {
  530. isCustom = quest->isCustomFirst;
  531. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  532. AddQuest aq;
  533. aq.quest = QuestInfo (quest, this, visitablePos());
  534. aq.player = h->tempOwner;
  535. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  536. }
  537. else if(failRequirements)
  538. {
  539. isCustom = quest->isCustomNext;
  540. }
  541. if(firstVisit || failRequirements)
  542. {
  543. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  544. cb->showInfoDialog(&iw);
  545. }
  546. if(!failRequirements) // propose completion, also on first visit
  547. {
  548. BlockingDialog bd (true, false);
  549. bd.player = h->getOwner();
  550. bd.soundID = soundBase::QUEST;
  551. getCompletionText (bd.text, bd.components, isCustom, h);
  552. cb->showBlockingDialog (&bd);
  553. return;
  554. }
  555. }
  556. else
  557. {
  558. iw.text << VLC->generaltexth->seerEmpty[quest->completedOption];
  559. if (ID == Obj::SEER_HUT)
  560. iw.text.addReplacement(seerName);
  561. cb->showInfoDialog(&iw);
  562. }
  563. }
  564. int CGSeerHut::checkDirection() const
  565. {
  566. int3 cord = getCreatureToKill()->pos;
  567. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  568. {
  569. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  570. return 8;
  571. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  572. return 1;
  573. else //northeast
  574. return 2;
  575. }
  576. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  577. {
  578. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  579. return 7;
  580. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  581. return 9;
  582. else //east
  583. return 3;
  584. }
  585. else //south
  586. {
  587. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  588. return 6;
  589. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  590. return 5;
  591. else //southeast
  592. return 4;
  593. }
  594. }
  595. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  596. {
  597. if (accept)
  598. {
  599. switch (quest->missionType)
  600. {
  601. case CQuest::MISSION_ART:
  602. for (auto & elem : quest->m5arts)
  603. {
  604. if(!h->hasArt(elem))
  605. {
  606. // first we need to disassemble this backpack artifact
  607. auto assembly = h->getAssemblyByConstituent(elem);
  608. assert(assembly);
  609. for(auto & ci : assembly->constituentsInfo)
  610. {
  611. cb->giveHeroNewArtifact(h, ci.art->artType, ArtifactPosition::PRE_FIRST);
  612. }
  613. // remove the assembly
  614. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  615. }
  616. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  617. }
  618. break;
  619. case CQuest::MISSION_ARMY:
  620. cb->takeCreatures(h->id, quest->m6creatures);
  621. break;
  622. case CQuest::MISSION_RESOURCES:
  623. for (int i = 0; i < 7; ++i)
  624. {
  625. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(i), -quest->m7resources[i]);
  626. }
  627. break;
  628. default:
  629. break;
  630. }
  631. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  632. completeQuest(h); //make sure to remove QuestGuard at the very end
  633. }
  634. }
  635. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  636. {
  637. switch (rewardType)
  638. {
  639. case EXPERIENCE:
  640. {
  641. TExpType expVal = h->calculateXp(rVal);
  642. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  643. break;
  644. }
  645. case MANA_POINTS:
  646. {
  647. cb->setManaPoints(h->id, h->mana+rVal);
  648. break;
  649. }
  650. case MORALE_BONUS: case LUCK_BONUS:
  651. {
  652. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  653. Bonus::OBJECT, rVal, h->id.getNum(), "", -1);
  654. GiveBonus gb;
  655. gb.id = h->id.getNum();
  656. gb.bonus = hb;
  657. cb->giveHeroBonus(&gb);
  658. }
  659. break;
  660. case RESOURCES:
  661. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(rID), rVal);
  662. break;
  663. case PRIMARY_SKILL:
  664. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  665. break;
  666. case SECONDARY_SKILL:
  667. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  668. break;
  669. case ARTIFACT:
  670. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],ArtifactPosition::FIRST_AVAILABLE);
  671. break;
  672. case SPELL:
  673. {
  674. std::set<SpellID> spell;
  675. spell.insert (SpellID(rID));
  676. cb->changeSpells(h, true, spell);
  677. }
  678. break;
  679. case CREATURE:
  680. {
  681. CCreatureSet creatures;
  682. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  683. cb->giveCreatures(this, h, creatures, false);
  684. }
  685. break;
  686. default:
  687. break;
  688. }
  689. }
  690. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  691. {
  692. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  693. if(allowNull && !o)
  694. return nullptr;
  695. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  696. return static_cast<const CGHeroInstance*>(o);
  697. }
  698. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  699. {
  700. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  701. if(allowNull && !o)
  702. return nullptr;
  703. assert(o && o->ID == Obj::MONSTER);
  704. return static_cast<const CGCreature*>(o);
  705. }
  706. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  707. {
  708. finishQuest(hero, answer);
  709. }
  710. void CGQuestGuard::init()
  711. {
  712. blockVisit = true;
  713. quest->textOption = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  714. quest->completedOption = cb->gameState()->getRandomGenerator().nextInt(4, 5);
  715. }
  716. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  717. {
  718. cb->removeObject(this);
  719. }
  720. void CGKeys::reset()
  721. {
  722. playerKeyMap.clear();
  723. }
  724. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  725. {
  726. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  727. {
  728. PlayerColor player(what-101);
  729. playerKeyMap[player].insert((ui8)val);
  730. }
  731. else
  732. logGlobal->errorStream() << boost::format("Unexpected properties requested to set: what=%d, val=%d") % (int)what % val;
  733. }
  734. bool CGKeys::wasMyColorVisited (PlayerColor player) const
  735. {
  736. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  737. return true;
  738. else
  739. return false;
  740. }
  741. std::string CGKeys::getHoverText(PlayerColor player) const
  742. {
  743. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  744. }
  745. std::string CGKeys::getObjectName() const
  746. {
  747. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  748. }
  749. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  750. {
  751. return wasMyColorVisited (player);
  752. }
  753. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  754. {
  755. int txt_id;
  756. if (!wasMyColorVisited (h->getOwner()) )
  757. {
  758. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  759. txt_id=19;
  760. }
  761. else
  762. txt_id=20;
  763. showInfoDialog(h,txt_id,soundBase::CAVEHEAD);
  764. }
  765. void CGBorderGuard::initObj()
  766. {
  767. //ui32 m13489val = subID; //store color as quest info
  768. blockVisit = true;
  769. }
  770. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  771. {
  772. text << std::pair<ui8,ui32>(11,18);
  773. }
  774. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  775. {
  776. if (!onHover)
  777. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER);
  778. }
  779. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  780. {
  781. return wasMyColorVisited (h->tempOwner);
  782. }
  783. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  784. {
  785. if (wasMyColorVisited (h->getOwner()) )
  786. {
  787. BlockingDialog bd (true, false);
  788. bd.player = h->getOwner();
  789. bd.soundID = soundBase::QUEST;
  790. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  791. cb->showBlockingDialog (&bd);
  792. }
  793. else
  794. {
  795. showInfoDialog(h,18,soundBase::CAVEHEAD);
  796. AddQuest aq;
  797. aq.quest = QuestInfo (quest, this, visitablePos());
  798. aq.player = h->tempOwner;
  799. cb->sendAndApply (&aq);
  800. //TODO: add this quest only once OR check for multiple instances later
  801. }
  802. }
  803. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  804. {
  805. if (answer)
  806. cb->removeObject(this);
  807. }
  808. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  809. {
  810. if (!wasMyColorVisited (h->getOwner()) )
  811. {
  812. showInfoDialog(h,18,0);
  813. AddQuest aq;
  814. aq.quest = QuestInfo (quest, this, visitablePos());
  815. aq.player = h->tempOwner;
  816. cb->sendAndApply (&aq);
  817. }
  818. }
  819. bool CGBorderGate::passableFor(PlayerColor color) const
  820. {
  821. return wasMyColorVisited(color);
  822. }