CQuest.cpp 21 KB

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