CQuest.cpp 21 KB

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