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