CQuest.cpp 21 KB

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