CQuest.cpp 21 KB

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