CQuest.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * CQuest.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CQuest.h"
  12. #include "../NetPacks.h"
  13. #include "../CSoundBase.h"
  14. #include "../CGeneralTextHandler.h"
  15. #include "../CHeroHandler.h"
  16. #include "CObjectClassesHandler.h"
  17. #include "MiscObjects.h"
  18. #include "../IGameCallback.h"
  19. #include "../CGameState.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. #include "../CModHandler.h"
  22. #include "../GameConstants.h"
  23. #include "../StringConstants.h"
  24. #include "../spells/CSpellHandler.h"
  25. #include "../mapping/CMap.h"
  26. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  27. CQuest::CQuest()
  28. : qid(-1), missionType(MISSION_NONE), progress(NOT_ACTIVE), lastDay(-1), m13489val(0),
  29. textOption(0), completedOption(0), stackDirection(0), heroPortrait(-1),
  30. isCustomFirst(false), isCustomNext(false), isCustomComplete(false)
  31. {
  32. }
  33. ///helpers
  34. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  35. {
  36. InfoWindow iw;
  37. iw.soundID = soundID;
  38. iw.player = playerID;
  39. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  40. IObjectInterface::cb->sendAndApply(&iw);
  41. }
  42. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  43. {
  44. const PlayerColor playerID = h->getOwner();
  45. showInfoDialog(playerID,txtID,soundID);
  46. }
  47. static std::string & visitedTxt(const bool visited)
  48. {
  49. int id = visited ? 352 : 353;
  50. return VLC->generaltexth->allTexts[id];
  51. }
  52. bool CQuest::checkQuest(const CGHeroInstance * h) const
  53. {
  54. switch (missionType)
  55. {
  56. case MISSION_NONE:
  57. return true;
  58. case MISSION_LEVEL:
  59. if(m13489val <= h->level)
  60. return true;
  61. return false;
  62. case MISSION_PRIMARY_STAT:
  63. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  64. {
  65. if(h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)) < m2stats[i])
  66. return false;
  67. }
  68. return true;
  69. case MISSION_KILL_HERO:
  70. case MISSION_KILL_CREATURE:
  71. if (!h->cb->getObjByQuestIdentifier(m13489val))
  72. return true;
  73. return false;
  74. case MISSION_ART:
  75. for(auto & elem : m5arts)
  76. {
  77. if(h->hasArt(elem, false, true))
  78. continue;
  79. return false; //if the artifact was not found
  80. }
  81. return true;
  82. case MISSION_ARMY:
  83. {
  84. std::vector<CStackBasicDescriptor>::const_iterator cre;
  85. TSlots::const_iterator it;
  86. ui32 count;
  87. for(cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  88. {
  89. for(count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  90. {
  91. if(it->second->type == cre->type)
  92. count += it->second->count;
  93. }
  94. if(count < cre->count) //not enough creatures of this kind
  95. return false;
  96. }
  97. }
  98. return true;
  99. case MISSION_RESOURCES:
  100. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, +1)) //including Mithril ?
  101. { //Quest has no direct access to callback
  102. if(h->cb->getResource (h->tempOwner, i) < m7resources[i])
  103. return false;
  104. }
  105. return true;
  106. case MISSION_HERO:
  107. if(m13489val == h->type->ID.getNum())
  108. return true;
  109. return false;
  110. case MISSION_PLAYER:
  111. if(m13489val == h->getOwner().getNum())
  112. return true;
  113. return false;
  114. default:
  115. return false;
  116. }
  117. }
  118. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  119. {
  120. std::string text;
  121. bool failRequirements = (h ? !checkQuest(h) : true);
  122. if(firstVisit)
  123. {
  124. isCustom = isCustomFirst;
  125. iwText << (text = firstVisitText);
  126. }
  127. else if(failRequirements)
  128. {
  129. isCustom = isCustomNext;
  130. iwText << (text = nextVisitText);
  131. }
  132. switch (missionType)
  133. {
  134. case MISSION_LEVEL:
  135. components.push_back(Component(Component::EXPERIENCE, 0, m13489val, 0));
  136. if(!isCustom)
  137. iwText.addReplacement(m13489val);
  138. break;
  139. case MISSION_PRIMARY_STAT:
  140. {
  141. MetaString loot;
  142. for(int i = 0; i < 4; ++i)
  143. {
  144. if(m2stats[i])
  145. {
  146. components.push_back(Component(Component::PRIM_SKILL, i, m2stats[i], 0));
  147. loot << "%d %s";
  148. loot.addReplacement(m2stats[i]);
  149. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  150. }
  151. }
  152. if (!isCustom)
  153. iwText.addReplacement(loot.buildList());
  154. }
  155. break;
  156. case MISSION_KILL_HERO:
  157. components.push_back(Component(Component::HERO_PORTRAIT, heroPortrait, 0, 0));
  158. if(!isCustom)
  159. addReplacements(iwText, text);
  160. break;
  161. case MISSION_HERO:
  162. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  163. components.push_back(Component(Component::HERO_PORTRAIT, VLC->heroh->heroes[m13489val]->imageIndex, 0, 0));
  164. if(!isCustom)
  165. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  166. break;
  167. case MISSION_KILL_CREATURE:
  168. {
  169. components.push_back(Component(stackToKill));
  170. if(!isCustom)
  171. {
  172. addReplacements(iwText, text);
  173. }
  174. }
  175. break;
  176. case MISSION_ART:
  177. {
  178. MetaString loot;
  179. for(auto & elem : m5arts)
  180. {
  181. components.push_back(Component(Component::ARTIFACT, elem, 0, 0));
  182. loot << "%s";
  183. loot.addReplacement(MetaString::ART_NAMES, elem);
  184. }
  185. if(!isCustom)
  186. iwText.addReplacement(loot.buildList());
  187. }
  188. break;
  189. case MISSION_ARMY:
  190. {
  191. MetaString loot;
  192. for(auto & elem : m6creatures)
  193. {
  194. components.push_back(Component(elem));
  195. loot << "%s";
  196. loot.addReplacement(elem);
  197. }
  198. if(!isCustom)
  199. iwText.addReplacement(loot.buildList());
  200. }
  201. break;
  202. case MISSION_RESOURCES:
  203. {
  204. MetaString loot;
  205. for(int i = 0; i < 7; ++i)
  206. {
  207. if(m7resources[i])
  208. {
  209. components.push_back(Component (Component::RESOURCE, i, m7resources[i], 0));
  210. loot << "%d %s";
  211. loot.addReplacement(m7resources[i]);
  212. loot.addReplacement(MetaString::RES_NAMES, i);
  213. }
  214. }
  215. if(!isCustom)
  216. iwText.addReplacement(loot.buildList());
  217. }
  218. break;
  219. case MISSION_PLAYER:
  220. components.push_back(Component (Component::FLAG, m13489val, 0, 0));
  221. if(!isCustom)
  222. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  223. break;
  224. }
  225. }
  226. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  227. {
  228. // Quests with MISSION_NONE type don't have a text for them
  229. assert(missionType != MISSION_NONE);
  230. if(onHover)
  231. ms << "\n\n";
  232. ms << VLC->generaltexth->quests[missionType-1][onHover ? 3 : 4][textOption];
  233. switch(missionType)
  234. {
  235. case MISSION_LEVEL:
  236. ms.addReplacement(m13489val);
  237. break;
  238. case MISSION_PRIMARY_STAT:
  239. {
  240. MetaString loot;
  241. for (int i = 0; i < 4; ++i)
  242. {
  243. if (m2stats[i])
  244. {
  245. loot << "%d %s";
  246. loot.addReplacement(m2stats[i]);
  247. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  248. }
  249. }
  250. ms.addReplacement(loot.buildList());
  251. }
  252. break;
  253. case MISSION_KILL_HERO:
  254. ms.addReplacement(heroName);
  255. break;
  256. case MISSION_KILL_CREATURE:
  257. ms.addReplacement(stackToKill);
  258. break;
  259. case MISSION_ART:
  260. {
  261. MetaString loot;
  262. for (auto & elem : m5arts)
  263. {
  264. loot << "%s";
  265. loot.addReplacement(MetaString::ART_NAMES, elem);
  266. }
  267. ms.addReplacement(loot.buildList());
  268. }
  269. break;
  270. case MISSION_ARMY:
  271. {
  272. MetaString loot;
  273. for (auto & elem : m6creatures)
  274. {
  275. loot << "%s";
  276. loot.addReplacement(elem);
  277. }
  278. ms.addReplacement(loot.buildList());
  279. }
  280. break;
  281. case MISSION_RESOURCES:
  282. {
  283. MetaString loot;
  284. for (int i = 0; i < 7; ++i)
  285. {
  286. if (m7resources[i])
  287. {
  288. loot << "%d %s";
  289. loot.addReplacement(m7resources[i]);
  290. loot.addReplacement(MetaString::RES_NAMES, i);
  291. }
  292. }
  293. ms.addReplacement(loot.buildList());
  294. }
  295. break;
  296. case MISSION_HERO:
  297. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  298. break;
  299. case MISSION_PLAYER:
  300. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  307. {
  308. iwText << completedText;
  309. switch(missionType)
  310. {
  311. case CQuest::MISSION_LEVEL:
  312. if (!isCustomComplete)
  313. iwText.addReplacement(m13489val);
  314. break;
  315. case CQuest::MISSION_PRIMARY_STAT:
  316. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  317. {
  318. MetaString loot;
  319. for (int i = 0; i < 4; ++i)
  320. {
  321. if (m2stats[i])
  322. {
  323. loot << "%d %s";
  324. loot.addReplacement(m2stats[i]);
  325. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  326. }
  327. }
  328. if (!isCustomComplete)
  329. iwText.addReplacement(loot.buildList());
  330. }
  331. break;
  332. case CQuest::MISSION_ART:
  333. {
  334. MetaString loot;
  335. for (auto & elem : m5arts)
  336. {
  337. loot << "%s";
  338. loot.addReplacement(MetaString::ART_NAMES, elem);
  339. }
  340. if (!isCustomComplete)
  341. iwText.addReplacement(loot.buildList());
  342. }
  343. break;
  344. case CQuest::MISSION_ARMY:
  345. {
  346. MetaString loot;
  347. for (auto & elem : m6creatures)
  348. {
  349. loot << "%s";
  350. loot.addReplacement(elem);
  351. }
  352. if (!isCustomComplete)
  353. iwText.addReplacement(loot.buildList());
  354. }
  355. break;
  356. case CQuest::MISSION_RESOURCES:
  357. {
  358. MetaString loot;
  359. for (int i = 0; i < 7; ++i)
  360. {
  361. if (m7resources[i])
  362. {
  363. loot << "%d %s";
  364. loot.addReplacement(m7resources[i]);
  365. loot.addReplacement(MetaString::RES_NAMES, i);
  366. }
  367. }
  368. if (!isCustomComplete)
  369. iwText.addReplacement(loot.buildList());
  370. }
  371. break;
  372. case MISSION_KILL_HERO:
  373. case MISSION_KILL_CREATURE:
  374. if (!isCustomComplete)
  375. addReplacements(iwText, completedText);
  376. break;
  377. case MISSION_HERO:
  378. if (!isCustomComplete)
  379. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  380. break;
  381. case MISSION_PLAYER:
  382. if (!isCustomComplete)
  383. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  384. break;
  385. }
  386. }
  387. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  388. {
  389. auto q = handler.enterStruct(fieldName);
  390. handler.serializeString("firstVisitText", firstVisitText);
  391. handler.serializeString("nextVisitText", nextVisitText);
  392. handler.serializeString("completedText", completedText);
  393. if(!handler.saving)
  394. {
  395. isCustomFirst = firstVisitText.size() > 0;
  396. isCustomNext = nextVisitText.size() > 0;
  397. isCustomComplete = completedText.size() > 0;
  398. }
  399. static const std::vector<std::string> MISSION_TYPE_JSON =
  400. {
  401. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  402. };
  403. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  404. handler.serializeInt("timeLimit", lastDay, -1);
  405. switch (missionType)
  406. {
  407. case MISSION_NONE:
  408. break;
  409. case MISSION_LEVEL:
  410. handler.serializeInt("heroLevel", m13489val, -1);
  411. break;
  412. case MISSION_PRIMARY_STAT:
  413. {
  414. auto primarySkills = handler.enterStruct("primarySkills");
  415. if(!handler.saving)
  416. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  417. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  418. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  419. }
  420. break;
  421. case MISSION_KILL_HERO:
  422. case MISSION_KILL_CREATURE:
  423. handler.serializeInstance<ui32>("killTarget", m13489val, ui32(-1));
  424. break;
  425. case MISSION_ART:
  426. //todo: ban artifacts
  427. handler.serializeIdArray("artifacts", m5arts, &CArtHandler::decodeArfifact, &CArtHandler::encodeArtifact);
  428. break;
  429. case MISSION_ARMY:
  430. {
  431. auto a = handler.enterArray("creatures");
  432. a.serializeStruct(m6creatures);
  433. }
  434. break;
  435. case MISSION_RESOURCES:
  436. {
  437. auto r = handler.enterStruct("resources");
  438. if(!handler.saving)
  439. m7resources.resize(GameConstants::RESOURCE_QUANTITY-1);
  440. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  441. {
  442. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  443. }
  444. }
  445. break;
  446. case MISSION_HERO:
  447. handler.serializeId<ui32>("hero", m13489val, 0, &CHeroHandler::decodeHero, &CHeroHandler::encodeHero);
  448. break;
  449. case MISSION_PLAYER:
  450. handler.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  451. break;
  452. default:
  453. logGlobal->error("Invalid quest mission type");
  454. break;
  455. }
  456. }
  457. CGSeerHut::CGSeerHut() : IQuestObject(),
  458. rewardType(NOTHING), rID(-1), rVal(-1)
  459. {
  460. quest->lastDay = -1;
  461. quest->isCustomFirst = false;
  462. quest->isCustomNext = false;
  463. quest->isCustomComplete = false;
  464. }
  465. void CGSeerHut::setObjToKill()
  466. {
  467. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  468. {
  469. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  470. assert(quest->stackToKill.type);
  471. quest->stackToKill.count = 0; //no count in info window
  472. quest->stackDirection = checkDirection();
  473. }
  474. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  475. {
  476. quest->heroName = getHeroToKill(false)->name;
  477. quest->heroPortrait = getHeroToKill(false)->portrait;
  478. }
  479. }
  480. void CGSeerHut::init(CRandomGenerator & rand)
  481. {
  482. seerName = *RandomGeneratorUtil::nextItem(VLC->generaltexth->seerNames, rand);
  483. quest->textOption = rand.nextInt(2);
  484. quest->completedOption = rand.nextInt(1, 3);
  485. }
  486. void CGSeerHut::initObj(CRandomGenerator & rand)
  487. {
  488. init(rand);
  489. quest->progress = CQuest::NOT_ACTIVE;
  490. if(quest->missionType)
  491. {
  492. if(!quest->isCustomFirst)
  493. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  494. if(!quest->isCustomNext)
  495. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  496. if(!quest->isCustomComplete)
  497. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  498. }
  499. else
  500. {
  501. quest->progress = CQuest::COMPLETE;
  502. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  503. }
  504. }
  505. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  506. {
  507. quest->getRolloverText (text, onHover);//TODO: simplify?
  508. if(!onHover)
  509. text.addReplacement(seerName);
  510. }
  511. std::string CGSeerHut::getHoverText(PlayerColor player) const
  512. {
  513. std::string hoverName = getObjectName();
  514. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  515. {
  516. hoverName = VLC->generaltexth->allTexts[347];
  517. boost::algorithm::replace_first(hoverName, "%s", seerName);
  518. }
  519. if(quest->progress & quest->missionType) //rollover when the quest is active
  520. {
  521. MetaString ms;
  522. getRolloverText (ms, true);
  523. hoverName += ms.toString();
  524. }
  525. return hoverName;
  526. }
  527. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  528. {
  529. switch(missionType)
  530. {
  531. case MISSION_KILL_CREATURE:
  532. out.addReplacement(stackToKill);
  533. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  534. {
  535. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  536. }
  537. break;
  538. case MISSION_KILL_HERO:
  539. out.addReplacement(heroName);
  540. break;
  541. }
  542. }
  543. IQuestObject::IQuestObject():
  544. quest(new CQuest())
  545. {
  546. }
  547. IQuestObject::~IQuestObject()
  548. {
  549. ///Information about quest should remain accessible even if IQuestObject removed from map
  550. ///All CQuest objects are freed in CMap destructor
  551. //delete quest;
  552. }
  553. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  554. {
  555. return quest->checkQuest(h);
  556. }
  557. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  558. {
  559. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  560. }
  561. void IQuestObject::afterAddToMapCommon(CMap * map)
  562. {
  563. map->addNewQuestInstance(quest);
  564. }
  565. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  566. {
  567. quest->getCompletionText (text, components, isCustom, h);
  568. switch(rewardType)
  569. {
  570. case EXPERIENCE: components.push_back(Component (Component::EXPERIENCE, 0, h->calculateXp(rVal), 0));
  571. break;
  572. case MANA_POINTS: components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  573. break;
  574. case MORALE_BONUS: components.push_back(Component (Component::MORALE, 0, rVal, 0));
  575. break;
  576. case LUCK_BONUS: components.push_back(Component (Component::LUCK, 0, rVal, 0));
  577. break;
  578. case RESOURCES: components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  579. break;
  580. case PRIMARY_SKILL: components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  581. break;
  582. case SECONDARY_SKILL: components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  583. break;
  584. case ARTIFACT: components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  585. break;
  586. case SPELL: components.push_back(Component (Component::SPELL, rID, 0, 0));
  587. break;
  588. case CREATURE: components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  589. break;
  590. }
  591. }
  592. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  593. {
  594. switch(what)
  595. {
  596. case 10:
  597. quest->progress = static_cast<CQuest::Eprogress>(val);
  598. break;
  599. }
  600. }
  601. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  602. {
  603. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  604. {
  605. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  606. }
  607. }
  608. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  609. {
  610. InfoWindow iw;
  611. iw.player = h->getOwner();
  612. if(quest->progress < CQuest::COMPLETE)
  613. {
  614. bool firstVisit = !quest->progress;
  615. bool failRequirements = !checkQuest(h);
  616. bool isCustom = false;
  617. if(firstVisit)
  618. {
  619. isCustom = quest->isCustomFirst;
  620. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  621. AddQuest aq;
  622. aq.quest = QuestInfo (quest, this, visitablePos());
  623. aq.player = h->tempOwner;
  624. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  625. }
  626. else if(failRequirements)
  627. {
  628. isCustom = quest->isCustomNext;
  629. }
  630. if(firstVisit || failRequirements)
  631. {
  632. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  633. cb->showInfoDialog(&iw);
  634. }
  635. if(!failRequirements) // propose completion, also on first visit
  636. {
  637. BlockingDialog bd (true, false);
  638. bd.player = h->getOwner();
  639. bd.soundID = soundBase::QUEST;
  640. getCompletionText (bd.text, bd.components, isCustom, h);
  641. cb->showBlockingDialog (&bd);
  642. return;
  643. }
  644. }
  645. else
  646. {
  647. iw.text << VLC->generaltexth->seerEmpty[quest->completedOption];
  648. if (ID == Obj::SEER_HUT)
  649. iw.text.addReplacement(seerName);
  650. cb->showInfoDialog(&iw);
  651. }
  652. }
  653. int CGSeerHut::checkDirection() const
  654. {
  655. int3 cord = getCreatureToKill()->pos;
  656. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  657. {
  658. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  659. return 8;
  660. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  661. return 1;
  662. else //northeast
  663. return 2;
  664. }
  665. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  666. {
  667. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  668. return 7;
  669. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  670. return 9;
  671. else //east
  672. return 3;
  673. }
  674. else //south
  675. {
  676. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  677. return 6;
  678. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  679. return 5;
  680. else //southeast
  681. return 4;
  682. }
  683. }
  684. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  685. {
  686. if (accept)
  687. {
  688. switch (quest->missionType)
  689. {
  690. case CQuest::MISSION_ART:
  691. for (auto & elem : quest->m5arts)
  692. {
  693. if(!h->hasArt(elem))
  694. {
  695. // first we need to disassemble this backpack artifact
  696. auto assembly = h->getAssemblyByConstituent(elem);
  697. assert(assembly);
  698. for(auto & ci : assembly->constituentsInfo)
  699. {
  700. cb->giveHeroNewArtifact(h, ci.art->artType, ArtifactPosition::PRE_FIRST);
  701. }
  702. // remove the assembly
  703. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(assembly)));
  704. }
  705. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(elem, false)));
  706. }
  707. break;
  708. case CQuest::MISSION_ARMY:
  709. cb->takeCreatures(h->id, quest->m6creatures);
  710. break;
  711. case CQuest::MISSION_RESOURCES:
  712. for (int i = 0; i < 7; ++i)
  713. {
  714. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(i), -quest->m7resources[i]);
  715. }
  716. break;
  717. default:
  718. break;
  719. }
  720. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  721. completeQuest(h); //make sure to remove QuestGuard at the very end
  722. }
  723. }
  724. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  725. {
  726. switch (rewardType)
  727. {
  728. case EXPERIENCE:
  729. {
  730. TExpType expVal = h->calculateXp(rVal);
  731. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  732. break;
  733. }
  734. case MANA_POINTS:
  735. {
  736. cb->setManaPoints(h->id, h->mana+rVal);
  737. break;
  738. }
  739. case MORALE_BONUS: case LUCK_BONUS:
  740. {
  741. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  742. Bonus::OBJECT, rVal, h->id.getNum(), "", -1);
  743. GiveBonus gb;
  744. gb.id = h->id.getNum();
  745. gb.bonus = hb;
  746. cb->giveHeroBonus(&gb);
  747. }
  748. break;
  749. case RESOURCES:
  750. cb->giveResource(h->getOwner(), static_cast<Res::ERes>(rID), rVal);
  751. break;
  752. case PRIMARY_SKILL:
  753. cb->changePrimSkill(h, static_cast<PrimarySkill::PrimarySkill>(rID), rVal, false);
  754. break;
  755. case SECONDARY_SKILL:
  756. cb->changeSecSkill(h, SecondarySkill(rID), rVal, false);
  757. break;
  758. case ARTIFACT:
  759. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],ArtifactPosition::FIRST_AVAILABLE);
  760. break;
  761. case SPELL:
  762. {
  763. std::set<SpellID> spell;
  764. spell.insert (SpellID(rID));
  765. cb->changeSpells(h, true, spell);
  766. }
  767. break;
  768. case CREATURE:
  769. {
  770. CCreatureSet creatures;
  771. creatures.setCreature(SlotID(0), CreatureID(rID), rVal);
  772. cb->giveCreatures(this, h, creatures, false);
  773. }
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  780. {
  781. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  782. if(allowNull && !o)
  783. return nullptr;
  784. assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
  785. return static_cast<const CGHeroInstance*>(o);
  786. }
  787. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  788. {
  789. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  790. if(allowNull && !o)
  791. return nullptr;
  792. assert(o && o->ID == Obj::MONSTER);
  793. return static_cast<const CGCreature*>(o);
  794. }
  795. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  796. {
  797. finishQuest(hero, answer);
  798. }
  799. void CGSeerHut::afterAddToMap(CMap* map)
  800. {
  801. IQuestObject::afterAddToMapCommon(map);
  802. }
  803. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  804. {
  805. static const std::map<ERewardType, std::string> REWARD_MAP =
  806. {
  807. {NOTHING, ""},
  808. {EXPERIENCE, "experience"},
  809. {MANA_POINTS, "mana"},
  810. {MORALE_BONUS, "morale"},
  811. {LUCK_BONUS, "luck"},
  812. {RESOURCES, "resource"},
  813. {PRIMARY_SKILL, "primarySkill"},
  814. {SECONDARY_SKILL,"secondarySkill"},
  815. {ARTIFACT, "artifact"},
  816. {SPELL, "spell"},
  817. {CREATURE, "creature"}
  818. };
  819. static const std::map<std::string, ERewardType> REWARD_RMAP =
  820. {
  821. {"experience", EXPERIENCE},
  822. {"mana", MANA_POINTS},
  823. {"morale", MORALE_BONUS},
  824. {"luck", LUCK_BONUS},
  825. {"resource", RESOURCES},
  826. {"primarySkill", PRIMARY_SKILL},
  827. {"secondarySkill",SECONDARY_SKILL},
  828. {"artifact", ARTIFACT},
  829. {"spell", SPELL},
  830. {"creature", CREATURE}
  831. };
  832. //quest and reward
  833. quest->serializeJson(handler, "quest");
  834. //only one reward is supported
  835. //todo: full reward format support after CRewardInfo integration
  836. auto s = handler.enterStruct("reward");
  837. std::string fullIdentifier, metaTypeName, scope, identifier;
  838. if(handler.saving)
  839. {
  840. si32 amount = rVal;
  841. metaTypeName = REWARD_MAP.at(rewardType);
  842. switch (rewardType)
  843. {
  844. case NOTHING:
  845. break;
  846. case EXPERIENCE:
  847. case MANA_POINTS:
  848. case MORALE_BONUS:
  849. case LUCK_BONUS:
  850. identifier = "";
  851. break;
  852. case RESOURCES:
  853. identifier = GameConstants::RESOURCE_NAMES[rID];
  854. break;
  855. case PRIMARY_SKILL:
  856. identifier = PrimarySkill::names[rID];
  857. break;
  858. case SECONDARY_SKILL:
  859. identifier = NSecondarySkill::names[rID];
  860. break;
  861. case ARTIFACT:
  862. identifier = ArtifactID(rID).toArtifact()->identifier;
  863. amount = 1;
  864. break;
  865. case SPELL:
  866. identifier = SpellID(rID).toSpell()->identifier;
  867. amount = 1;
  868. break;
  869. case CREATURE:
  870. identifier = CreatureID(rID).toCreature()->identifier;
  871. break;
  872. default:
  873. assert(false);
  874. break;
  875. }
  876. if(rewardType != NOTHING)
  877. {
  878. fullIdentifier = CModHandler::makeFullIdentifier(scope, metaTypeName, identifier);
  879. handler.serializeInt(fullIdentifier, amount);
  880. }
  881. }
  882. else
  883. {
  884. rewardType = NOTHING;
  885. const JsonNode & rewardsJson = handler.getCurrent();
  886. fullIdentifier = "";
  887. if(rewardsJson.Struct().empty())
  888. return;
  889. else
  890. {
  891. auto iter = rewardsJson.Struct().begin();
  892. fullIdentifier = iter->first;
  893. }
  894. CModHandler::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  895. auto it = REWARD_RMAP.find(metaTypeName);
  896. if(it == REWARD_RMAP.end())
  897. {
  898. logGlobal->errorStream() << instanceName << ": invalid metatype in reward item " << fullIdentifier;
  899. return;
  900. }
  901. else
  902. {
  903. rewardType = it->second;
  904. }
  905. bool doRequest = false;
  906. switch (rewardType)
  907. {
  908. case NOTHING:
  909. return;
  910. case EXPERIENCE:
  911. case MANA_POINTS:
  912. case MORALE_BONUS:
  913. case LUCK_BONUS:
  914. break;
  915. case PRIMARY_SKILL:
  916. doRequest = true;
  917. break;
  918. case RESOURCES:
  919. case SECONDARY_SKILL:
  920. case ARTIFACT:
  921. case SPELL:
  922. case CREATURE:
  923. doRequest = true;
  924. break;
  925. default:
  926. assert(false);
  927. break;
  928. }
  929. if(doRequest)
  930. {
  931. auto rawId = VLC->modh->identifiers.getIdentifier("core", fullIdentifier, false);
  932. if(rawId)
  933. {
  934. rID = rawId.get();
  935. }
  936. else
  937. {
  938. rewardType = NOTHING;//fallback in case of error
  939. return;
  940. }
  941. }
  942. handler.serializeInt(fullIdentifier, rVal);
  943. }
  944. }
  945. void CGQuestGuard::init(CRandomGenerator & rand)
  946. {
  947. blockVisit = true;
  948. quest->textOption = rand.nextInt(3, 5);
  949. quest->completedOption = rand.nextInt(4, 5);
  950. }
  951. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  952. {
  953. cb->removeObject(this);
  954. }
  955. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  956. {
  957. //quest only, do not call base class
  958. quest->serializeJson(handler, "quest");
  959. }
  960. void CGKeys::reset()
  961. {
  962. playerKeyMap.clear();
  963. }
  964. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  965. {
  966. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  967. {
  968. PlayerColor player(what-101);
  969. playerKeyMap[player].insert((ui8)val);
  970. }
  971. else
  972. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", (int)what, val);
  973. }
  974. bool CGKeys::wasMyColorVisited (PlayerColor player) const
  975. {
  976. if(playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID))
  977. return true;
  978. else
  979. return false;
  980. }
  981. std::string CGKeys::getHoverText(PlayerColor player) const
  982. {
  983. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  984. }
  985. std::string CGKeys::getObjectName() const
  986. {
  987. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  988. }
  989. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  990. {
  991. return wasMyColorVisited (player);
  992. }
  993. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  994. {
  995. int txt_id;
  996. if (!wasMyColorVisited (h->getOwner()) )
  997. {
  998. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  999. txt_id=19;
  1000. }
  1001. else
  1002. txt_id=20;
  1003. showInfoDialog(h,txt_id,soundBase::CAVEHEAD);
  1004. }
  1005. void CGBorderGuard::initObj(CRandomGenerator & rand)
  1006. {
  1007. //ui32 m13489val = subID; //store color as quest info
  1008. blockVisit = true;
  1009. }
  1010. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  1011. {
  1012. text << std::pair<ui8,ui32>(11,18);
  1013. }
  1014. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  1015. {
  1016. if (!onHover)
  1017. text << VLC->generaltexth->tentColors[subID] << " " << VLC->objtypeh->getObjectName(Obj::KEYMASTER);
  1018. }
  1019. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  1020. {
  1021. return wasMyColorVisited (h->tempOwner);
  1022. }
  1023. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  1024. {
  1025. if (wasMyColorVisited (h->getOwner()) )
  1026. {
  1027. BlockingDialog bd (true, false);
  1028. bd.player = h->getOwner();
  1029. bd.soundID = soundBase::QUEST;
  1030. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  1031. cb->showBlockingDialog (&bd);
  1032. }
  1033. else
  1034. {
  1035. showInfoDialog(h,18,soundBase::CAVEHEAD);
  1036. AddQuest aq;
  1037. aq.quest = QuestInfo (quest, this, visitablePos());
  1038. aq.player = h->tempOwner;
  1039. cb->sendAndApply (&aq);
  1040. //TODO: add this quest only once OR check for multiple instances later
  1041. }
  1042. }
  1043. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1044. {
  1045. if (answer)
  1046. cb->removeObject(this);
  1047. }
  1048. void CGBorderGuard::afterAddToMap(CMap * map)
  1049. {
  1050. IQuestObject::afterAddToMapCommon(map);
  1051. }
  1052. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1053. {
  1054. if (!wasMyColorVisited (h->getOwner()) )
  1055. {
  1056. showInfoDialog(h,18,0);
  1057. AddQuest aq;
  1058. aq.quest = QuestInfo (quest, this, visitablePos());
  1059. aq.player = h->tempOwner;
  1060. cb->sendAndApply (&aq);
  1061. }
  1062. }
  1063. bool CGBorderGate::passableFor(PlayerColor color) const
  1064. {
  1065. return wasMyColorVisited(color);
  1066. }