CQuest.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. if (onHover)
  217. ms << "\n\n";
  218. ms << VLC->generaltexth->quests[missionType-1][onHover ? 3 : 4][textOption];
  219. switch (missionType)
  220. {
  221. case MISSION_LEVEL:
  222. ms.addReplacement(m13489val);
  223. break;
  224. case MISSION_PRIMARY_STAT:
  225. {
  226. MetaString loot;
  227. for (int i = 0; i < 4; ++i)
  228. {
  229. if (m2stats[i])
  230. {
  231. loot << "%d %s";
  232. loot.addReplacement(m2stats[i]);
  233. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  234. }
  235. }
  236. ms.addReplacement(loot.buildList());
  237. }
  238. break;
  239. case MISSION_KILL_HERO:
  240. ms.addReplacement(heroName);
  241. break;
  242. case MISSION_KILL_CREATURE:
  243. ms.addReplacement(stackToKill);
  244. break;
  245. case MISSION_ART:
  246. {
  247. MetaString loot;
  248. for (auto & elem : m5arts)
  249. {
  250. loot << "%s";
  251. loot.addReplacement(MetaString::ART_NAMES, elem);
  252. }
  253. ms.addReplacement(loot.buildList());
  254. }
  255. break;
  256. case MISSION_ARMY:
  257. {
  258. MetaString loot;
  259. for (auto & elem : m6creatures)
  260. {
  261. loot << "%s";
  262. loot.addReplacement(elem);
  263. }
  264. ms.addReplacement(loot.buildList());
  265. }
  266. break;
  267. case MISSION_RESOURCES:
  268. {
  269. MetaString loot;
  270. for (int i = 0; i < 7; ++i)
  271. {
  272. if (m7resources[i])
  273. {
  274. loot << "%d %s";
  275. loot.addReplacement(m7resources[i]);
  276. loot.addReplacement(MetaString::RES_NAMES, i);
  277. }
  278. }
  279. ms.addReplacement(loot.buildList());
  280. }
  281. break;
  282. case MISSION_HERO:
  283. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  284. break;
  285. case MISSION_PLAYER:
  286. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  287. break;
  288. default:
  289. break;
  290. }
  291. }
  292. void CQuest::getCompletionText (MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  293. {
  294. iwText << completedText;
  295. switch (missionType)
  296. {
  297. case CQuest::MISSION_LEVEL:
  298. if (!isCustomComplete)
  299. iwText.addReplacement(m13489val);
  300. break;
  301. case CQuest::MISSION_PRIMARY_STAT:
  302. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  303. {
  304. MetaString loot;
  305. for (int i = 0; i < 4; ++i)
  306. {
  307. if (m2stats[i])
  308. {
  309. loot << "%d %s";
  310. loot.addReplacement(m2stats[i]);
  311. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  312. }
  313. }
  314. if (!isCustomComplete)
  315. iwText.addReplacement(loot.buildList());
  316. }
  317. break;
  318. case CQuest::MISSION_ART:
  319. {
  320. MetaString loot;
  321. for (auto & elem : m5arts)
  322. {
  323. loot << "%s";
  324. loot.addReplacement(MetaString::ART_NAMES, elem);
  325. }
  326. if (!isCustomComplete)
  327. iwText.addReplacement(loot.buildList());
  328. }
  329. break;
  330. case CQuest::MISSION_ARMY:
  331. {
  332. MetaString loot;
  333. for (auto & elem : m6creatures)
  334. {
  335. loot << "%s";
  336. loot.addReplacement(elem);
  337. }
  338. if (!isCustomComplete)
  339. iwText.addReplacement(loot.buildList());
  340. }
  341. break;
  342. case CQuest::MISSION_RESOURCES:
  343. {
  344. MetaString loot;
  345. for (int i = 0; i < 7; ++i)
  346. {
  347. if (m7resources[i])
  348. {
  349. loot << "%d %s";
  350. loot.addReplacement(m7resources[i]);
  351. loot.addReplacement(MetaString::RES_NAMES, i);
  352. }
  353. }
  354. if (!isCustomComplete)
  355. iwText.addReplacement(loot.buildList());
  356. }
  357. break;
  358. case MISSION_KILL_HERO:
  359. case MISSION_KILL_CREATURE:
  360. if (!isCustomComplete)
  361. addReplacements(iwText, completedText);
  362. break;
  363. case MISSION_HERO:
  364. if (!isCustomComplete)
  365. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  366. break;
  367. case MISSION_PLAYER:
  368. if (!isCustomComplete)
  369. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  370. break;
  371. }
  372. }
  373. void CGSeerHut::setObjToKill()
  374. {
  375. if (quest->missionType == CQuest::MISSION_KILL_CREATURE)
  376. {
  377. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  378. assert(quest->stackToKill.type);
  379. quest->stackToKill.count = 0; //no count in info window
  380. quest->stackDirection = checkDirection();
  381. }
  382. else if (quest->missionType == CQuest::MISSION_KILL_HERO)
  383. {
  384. quest->heroName = getHeroToKill(false)->name;
  385. quest->heroPortrait = getHeroToKill(false)->portrait;
  386. }
  387. }
  388. void CGSeerHut::init()
  389. {
  390. seerName = *RandomGeneratorUtil::nextItem(VLC->generaltexth->seerNames, cb->gameState()->getRandomGenerator());
  391. quest->textOption = cb->gameState()->getRandomGenerator().nextInt(2);
  392. }
  393. void CGSeerHut::initObj()
  394. {
  395. init();
  396. quest->progress = CQuest::NOT_ACTIVE;
  397. if (quest->missionType)
  398. {
  399. if (!quest->isCustomFirst)
  400. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  401. if (!quest->isCustomNext)
  402. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  403. if (!quest->isCustomComplete)
  404. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  405. }
  406. else
  407. {
  408. quest->progress = CQuest::COMPLETE;
  409. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->textOption];
  410. }
  411. }
  412. void CGSeerHut::getRolloverText (MetaString &text, bool onHover) const
  413. {
  414. quest->getRolloverText (text, onHover);//TODO: simplify?
  415. if (!onHover)
  416. text.addReplacement(seerName);
  417. }
  418. std::string CGSeerHut::getHoverText(PlayerColor player) const
  419. {
  420. std::string hoverName = getObjectName();
  421. if (ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  422. {
  423. hoverName = VLC->generaltexth->allTexts[347];
  424. boost::algorithm::replace_first(hoverName,"%s", seerName);
  425. }
  426. if (quest->progress & quest->missionType) //rollover when the quest is active
  427. {
  428. MetaString ms;
  429. getRolloverText (ms, true);
  430. hoverName += ms.toString();
  431. }
  432. return hoverName;
  433. }
  434. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  435. {
  436. switch(missionType)
  437. {
  438. case MISSION_KILL_CREATURE:
  439. out.addReplacement(stackToKill);
  440. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  441. {
  442. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  443. }
  444. break;
  445. case MISSION_KILL_HERO:
  446. out.addReplacement(heroName);
  447. break;
  448. }
  449. }
  450. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  451. {
  452. return quest->checkQuest(h);
  453. }
  454. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  455. {
  456. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  457. }
  458. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  459. {
  460. quest->getCompletionText (text, components, isCustom, h);
  461. switch (rewardType)
  462. {
  463. case EXPERIENCE: components.push_back(Component (Component::EXPERIENCE, 0, h->calculateXp(rVal), 0));
  464. break;
  465. case MANA_POINTS: components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  466. break;
  467. case MORALE_BONUS: components.push_back(Component (Component::MORALE, 0, rVal, 0));
  468. break;
  469. case LUCK_BONUS: components.push_back(Component (Component::LUCK, 0, rVal, 0));
  470. break;
  471. case RESOURCES: components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  472. break;
  473. case PRIMARY_SKILL: components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  474. break;
  475. case SECONDARY_SKILL: components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  476. break;
  477. case ARTIFACT: components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  478. break;
  479. case SPELL: components.push_back(Component (Component::SPELL, rID, 0, 0));
  480. break;
  481. case CREATURE: components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  482. break;
  483. }
  484. }
  485. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  486. {
  487. switch (what)
  488. {
  489. case 10:
  490. quest->progress = static_cast<CQuest::Eprogress>(val);
  491. break;
  492. }
  493. }
  494. void CGSeerHut::newTurn() const
  495. {
  496. if (quest->lastDay >= 0 && quest->lastDay < cb->getDate()-1) //time is up
  497. {
  498. cb->setObjProperty (id, 10, CQuest::COMPLETE);
  499. }
  500. }
  501. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  502. {
  503. InfoWindow iw;
  504. iw.player = h->getOwner();
  505. if (quest->progress < CQuest::COMPLETE)
  506. {
  507. bool firstVisit = !quest->progress;
  508. bool failRequirements = !checkQuest(h);
  509. bool isCustom=false;
  510. if (firstVisit)
  511. {
  512. isCustom = quest->isCustomFirst;
  513. cb->setObjProperty (id, 10, CQuest::IN_PROGRESS);
  514. AddQuest aq;
  515. aq.quest = QuestInfo (quest, this, visitablePos());
  516. aq.player = h->tempOwner;
  517. cb->sendAndApply (&aq); //TODO: merge with setObjProperty?
  518. }
  519. else if (failRequirements)
  520. {
  521. isCustom = quest->isCustomNext;
  522. }
  523. if (firstVisit || failRequirements)
  524. {
  525. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  526. cb->showInfoDialog(&iw);
  527. }
  528. if (!failRequirements) // propose completion, also on first visit
  529. {
  530. BlockingDialog bd (true, false);
  531. bd.player = h->getOwner();
  532. bd.soundID = soundBase::QUEST;
  533. getCompletionText (bd.text, bd.components, isCustom, h);
  534. cb->showBlockingDialog (&bd);
  535. return;
  536. }
  537. }
  538. else
  539. {
  540. iw.text << VLC->generaltexth->seerEmpty[quest->textOption];
  541. if (ID == Obj::SEER_HUT)
  542. iw.text.addReplacement(seerName);
  543. cb->showInfoDialog(&iw);
  544. }
  545. }
  546. int CGSeerHut::checkDirection() const
  547. {
  548. int3 cord = getCreatureToKill()->pos;
  549. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  550. {
  551. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  552. return 8;
  553. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  554. return 1;
  555. else //northeast
  556. return 2;
  557. }
  558. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  559. {
  560. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  561. return 7;
  562. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  563. return 9;
  564. else //east
  565. return 3;
  566. }
  567. else //south
  568. {
  569. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  570. return 6;
  571. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  572. return 5;
  573. else //southeast
  574. return 4;
  575. }
  576. }
  577. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  578. {
  579. if (accept)
  580. {
  581. switch (quest->missionType)
  582. {
  583. case CQuest::MISSION_ART:
  584. for (auto & elem : quest->m5arts)
  585. {
  586. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  587. }
  588. break;
  589. case CQuest::MISSION_ARMY:
  590. cb->takeCreatures(h->id, quest->m6creatures);
  591. break;
  592. case CQuest::MISSION_RESOURCES:
  593. for (int i = 0; i < 7; ++i)
  594. {
  595. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(i), -quest->m7resources[i]);
  596. }
  597. break;
  598. default:
  599. break;
  600. }
  601. cb->setObjProperty (id, 10, CQuest::COMPLETE); //mission complete
  602. completeQuest(h); //make sure to remove QuestGuard at the very end
  603. }
  604. }
  605. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  606. {
  607. switch (rewardType)
  608. {
  609. case EXPERIENCE:
  610. {
  611. TExpType expVal = h->calculateXp(rVal);
  612. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  613. break;
  614. }
  615. case MANA_POINTS:
  616. {
  617. cb->setManaPoints(h->id, h->mana+rVal);
  618. break;
  619. }
  620. case MORALE_BONUS: case LUCK_BONUS:
  621. {
  622. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  623. Bonus::OBJECT, rVal, h->id.getNum(), "", -1);
  624. GiveBonus gb;
  625. gb.id = h->id.getNum();
  626. gb.bonus = hb;
  627. cb->giveHeroBonus(&gb);
  628. }
  629. break;
  630. case RESOURCES:
  631. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(rID), rVal);
  632. break;
  633. case PRIMARY_SKILL:
  634. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  635. break;
  636. case SECONDARY_SKILL:
  637. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  638. break;
  639. case ARTIFACT:
  640. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],ArtifactPosition::FIRST_AVAILABLE);
  641. break;
  642. case SPELL:
  643. {
  644. std::set<SpellID> spell;
  645. spell.insert (SpellID(rID));
  646. cb->changeSpells(h, true, spell);
  647. }
  648. break;
  649. case CREATURE:
  650. {
  651. CCreatureSet creatures;
  652. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  653. cb->giveCreatures(this, h, creatures, false);
  654. }
  655. break;
  656. default:
  657. break;
  658. }
  659. }
  660. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  661. {
  662. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  663. if(allowNull && !o)
  664. return nullptr;
  665. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  666. return static_cast<const CGHeroInstance*>(o);
  667. }
  668. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  669. {
  670. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  671. if(allowNull && !o)
  672. return nullptr;
  673. assert(o && o->ID == Obj::MONSTER);
  674. return static_cast<const CGCreature*>(o);
  675. }
  676. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  677. {
  678. finishQuest(hero, answer);
  679. }
  680. void CGQuestGuard::init()
  681. {
  682. blockVisit = true;
  683. quest->textOption = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  684. }
  685. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  686. {
  687. cb->removeObject(this);
  688. }
  689. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  690. {
  691. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  692. {
  693. PlayerColor player(what-101);
  694. playerKeyMap[player].insert((ui8)val);
  695. }
  696. else
  697. logGlobal->errorStream() << boost::format("Unexpected properties requested to set: what=%d, val=%d") % (int)what % val;
  698. }
  699. bool CGKeys::wasMyColorVisited (PlayerColor player) const
  700. {
  701. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  702. return true;
  703. else
  704. return false;
  705. }
  706. std::string CGKeys::getHoverText(PlayerColor player) const
  707. {
  708. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  709. }
  710. std::string CGKeys::getObjectName() const
  711. {
  712. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  713. }
  714. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  715. {
  716. return wasMyColorVisited (player);
  717. }
  718. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  719. {
  720. int txt_id;
  721. if (!wasMyColorVisited (h->getOwner()) )
  722. {
  723. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  724. txt_id=19;
  725. }
  726. else
  727. txt_id=20;
  728. showInfoDialog(h,txt_id,soundBase::CAVEHEAD);
  729. }
  730. void CGBorderGuard::initObj()
  731. {
  732. //ui32 m13489val = subID; //store color as quest info
  733. blockVisit = true;
  734. }
  735. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  736. {
  737. text << std::pair<ui8,ui32>(11,18);
  738. }
  739. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  740. {
  741. if (!onHover)
  742. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER);
  743. }
  744. bool CGBorderGuard::checkQuest (const CGHeroInstance * h) const
  745. {
  746. return wasMyColorVisited (h->tempOwner);
  747. }
  748. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  749. {
  750. if (wasMyColorVisited (h->getOwner()) )
  751. {
  752. BlockingDialog bd (true, false);
  753. bd.player = h->getOwner();
  754. bd.soundID = soundBase::QUEST;
  755. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  756. cb->showBlockingDialog (&bd);
  757. }
  758. else
  759. {
  760. showInfoDialog(h,18,soundBase::CAVEHEAD);
  761. AddQuest aq;
  762. aq.quest = QuestInfo (quest, this, visitablePos());
  763. aq.player = h->tempOwner;
  764. cb->sendAndApply (&aq);
  765. //TODO: add this quest only once OR check for multiple instances later
  766. }
  767. }
  768. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  769. {
  770. if (answer)
  771. cb->removeObject(this);
  772. }
  773. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  774. {
  775. if (!wasMyColorVisited (h->getOwner()) )
  776. {
  777. showInfoDialog(h,18,0);
  778. AddQuest aq;
  779. aq.quest = QuestInfo (quest, this, visitablePos());
  780. aq.player = h->tempOwner;
  781. cb->sendAndApply (&aq);
  782. }
  783. }
  784. bool CGBorderGate::passableFor(PlayerColor color) const
  785. {
  786. return wasMyColorVisited(color);
  787. }