CQuest.cpp 21 KB

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