CQuest.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "../CSkillHandler.h"
  26. #include "../mapping/CMap.h"
  27. std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
  28. CQuest::CQuest()
  29. : qid(-1), missionType(MISSION_NONE), progress(NOT_ACTIVE), lastDay(-1), m13489val(0),
  30. textOption(0), completedOption(0), stackDirection(0), heroPortrait(-1),
  31. isCustomFirst(false), isCustomNext(false), isCustomComplete(false)
  32. {
  33. }
  34. ///helpers
  35. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID = 0)
  36. {
  37. InfoWindow iw;
  38. iw.soundID = soundID;
  39. iw.player = playerID;
  40. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  41. IObjectInterface::cb->sendAndApply(&iw);
  42. }
  43. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0)
  44. {
  45. const PlayerColor playerID = h->getOwner();
  46. showInfoDialog(playerID,txtID,soundID);
  47. }
  48. static std::string & visitedTxt(const bool visited)
  49. {
  50. int id = visited ? 352 : 353;
  51. return VLC->generaltexth->allTexts[id];
  52. }
  53. bool CQuest::checkQuest(const CGHeroInstance * h) const
  54. {
  55. switch (missionType)
  56. {
  57. case MISSION_NONE:
  58. return true;
  59. case MISSION_LEVEL:
  60. if(m13489val <= h->level)
  61. return true;
  62. return false;
  63. case MISSION_PRIMARY_STAT:
  64. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  65. {
  66. if(h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)) < m2stats[i])
  67. return false;
  68. }
  69. return true;
  70. case MISSION_KILL_HERO:
  71. case MISSION_KILL_CREATURE:
  72. if (!h->cb->getObjByQuestIdentifier(m13489val))
  73. return true;
  74. return false;
  75. case MISSION_ART:
  76. for(auto & elem : m5arts)
  77. {
  78. if(h->hasArt(elem, false, true))
  79. continue;
  80. return false; //if the artifact was not found
  81. }
  82. return true;
  83. case MISSION_ARMY:
  84. {
  85. std::vector<CStackBasicDescriptor>::const_iterator cre;
  86. TSlots::const_iterator it;
  87. ui32 count;
  88. for(cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  89. {
  90. for(count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  91. {
  92. if(it->second->type == cre->type)
  93. count += it->second->count;
  94. }
  95. if(count < cre->count) //not enough creatures of this kind
  96. return false;
  97. }
  98. }
  99. return true;
  100. case MISSION_RESOURCES:
  101. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, +1)) //including Mithril ?
  102. { //Quest has no direct access to callback
  103. if(h->cb->getResource (h->tempOwner, i) < m7resources[i])
  104. return false;
  105. }
  106. return true;
  107. case MISSION_HERO:
  108. if(m13489val == h->type->ID.getNum())
  109. return true;
  110. return false;
  111. case MISSION_PLAYER:
  112. if(m13489val == h->getOwner().getNum())
  113. return true;
  114. return false;
  115. default:
  116. return false;
  117. }
  118. }
  119. void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  120. {
  121. std::string text;
  122. bool failRequirements = (h ? !checkQuest(h) : true);
  123. if(firstVisit)
  124. {
  125. isCustom = isCustomFirst;
  126. iwText << (text = firstVisitText);
  127. }
  128. else if(failRequirements)
  129. {
  130. isCustom = isCustomNext;
  131. iwText << (text = nextVisitText);
  132. }
  133. switch (missionType)
  134. {
  135. case MISSION_LEVEL:
  136. components.push_back(Component(Component::EXPERIENCE, 0, m13489val, 0));
  137. if(!isCustom)
  138. iwText.addReplacement(m13489val);
  139. break;
  140. case MISSION_PRIMARY_STAT:
  141. {
  142. MetaString loot;
  143. for(int i = 0; i < 4; ++i)
  144. {
  145. if(m2stats[i])
  146. {
  147. components.push_back(Component(Component::PRIM_SKILL, i, m2stats[i], 0));
  148. loot << "%d %s";
  149. loot.addReplacement(m2stats[i]);
  150. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  151. }
  152. }
  153. if (!isCustom)
  154. iwText.addReplacement(loot.buildList());
  155. }
  156. break;
  157. case MISSION_KILL_HERO:
  158. components.push_back(Component(Component::HERO_PORTRAIT, heroPortrait, 0, 0));
  159. if(!isCustom)
  160. addReplacements(iwText, text);
  161. break;
  162. case MISSION_HERO:
  163. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  164. components.push_back(Component(Component::HERO_PORTRAIT, VLC->heroh->heroes[m13489val]->imageIndex, 0, 0));
  165. if(!isCustom)
  166. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  167. break;
  168. case MISSION_KILL_CREATURE:
  169. {
  170. components.push_back(Component(stackToKill));
  171. if(!isCustom)
  172. {
  173. addReplacements(iwText, text);
  174. }
  175. }
  176. break;
  177. case MISSION_ART:
  178. {
  179. MetaString loot;
  180. for(auto & elem : m5arts)
  181. {
  182. components.push_back(Component(Component::ARTIFACT, elem, 0, 0));
  183. loot << "%s";
  184. loot.addReplacement(MetaString::ART_NAMES, elem);
  185. }
  186. if(!isCustom)
  187. iwText.addReplacement(loot.buildList());
  188. }
  189. break;
  190. case MISSION_ARMY:
  191. {
  192. MetaString loot;
  193. for(auto & elem : m6creatures)
  194. {
  195. components.push_back(Component(elem));
  196. loot << "%s";
  197. loot.addReplacement(elem);
  198. }
  199. if(!isCustom)
  200. iwText.addReplacement(loot.buildList());
  201. }
  202. break;
  203. case MISSION_RESOURCES:
  204. {
  205. MetaString loot;
  206. for(int i = 0; i < 7; ++i)
  207. {
  208. if(m7resources[i])
  209. {
  210. components.push_back(Component (Component::RESOURCE, i, m7resources[i], 0));
  211. loot << "%d %s";
  212. loot.addReplacement(m7resources[i]);
  213. loot.addReplacement(MetaString::RES_NAMES, i);
  214. }
  215. }
  216. if(!isCustom)
  217. iwText.addReplacement(loot.buildList());
  218. }
  219. break;
  220. case MISSION_PLAYER:
  221. components.push_back(Component (Component::FLAG, m13489val, 0, 0));
  222. if(!isCustom)
  223. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  224. break;
  225. }
  226. }
  227. void CQuest::getRolloverText(MetaString &ms, bool onHover) const
  228. {
  229. // Quests with MISSION_NONE type don't have a text for them
  230. assert(missionType != MISSION_NONE);
  231. if(onHover)
  232. ms << "\n\n";
  233. ms << VLC->generaltexth->quests[missionType-1][onHover ? 3 : 4][textOption];
  234. switch(missionType)
  235. {
  236. case MISSION_LEVEL:
  237. ms.addReplacement(m13489val);
  238. break;
  239. case MISSION_PRIMARY_STAT:
  240. {
  241. MetaString loot;
  242. for (int i = 0; i < 4; ++i)
  243. {
  244. if (m2stats[i])
  245. {
  246. loot << "%d %s";
  247. loot.addReplacement(m2stats[i]);
  248. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  249. }
  250. }
  251. ms.addReplacement(loot.buildList());
  252. }
  253. break;
  254. case MISSION_KILL_HERO:
  255. ms.addReplacement(heroName);
  256. break;
  257. case MISSION_KILL_CREATURE:
  258. ms.addReplacement(stackToKill);
  259. break;
  260. case MISSION_ART:
  261. {
  262. MetaString loot;
  263. for (auto & elem : m5arts)
  264. {
  265. loot << "%s";
  266. loot.addReplacement(MetaString::ART_NAMES, elem);
  267. }
  268. ms.addReplacement(loot.buildList());
  269. }
  270. break;
  271. case MISSION_ARMY:
  272. {
  273. MetaString loot;
  274. for (auto & elem : m6creatures)
  275. {
  276. loot << "%s";
  277. loot.addReplacement(elem);
  278. }
  279. ms.addReplacement(loot.buildList());
  280. }
  281. break;
  282. case MISSION_RESOURCES:
  283. {
  284. MetaString loot;
  285. for (int i = 0; i < 7; ++i)
  286. {
  287. if (m7resources[i])
  288. {
  289. loot << "%d %s";
  290. loot.addReplacement(m7resources[i]);
  291. loot.addReplacement(MetaString::RES_NAMES, i);
  292. }
  293. }
  294. ms.addReplacement(loot.buildList());
  295. }
  296. break;
  297. case MISSION_HERO:
  298. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  299. break;
  300. case MISSION_PLAYER:
  301. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  308. {
  309. iwText << completedText;
  310. switch(missionType)
  311. {
  312. case CQuest::MISSION_LEVEL:
  313. if (!isCustomComplete)
  314. iwText.addReplacement(m13489val);
  315. break;
  316. case CQuest::MISSION_PRIMARY_STAT:
  317. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  318. {
  319. MetaString loot;
  320. for (int i = 0; i < 4; ++i)
  321. {
  322. if (m2stats[i])
  323. {
  324. loot << "%d %s";
  325. loot.addReplacement(m2stats[i]);
  326. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  327. }
  328. }
  329. if (!isCustomComplete)
  330. iwText.addReplacement(loot.buildList());
  331. }
  332. break;
  333. case CQuest::MISSION_ART:
  334. {
  335. MetaString loot;
  336. for (auto & elem : m5arts)
  337. {
  338. loot << "%s";
  339. loot.addReplacement(MetaString::ART_NAMES, elem);
  340. }
  341. if (!isCustomComplete)
  342. iwText.addReplacement(loot.buildList());
  343. }
  344. break;
  345. case CQuest::MISSION_ARMY:
  346. {
  347. MetaString loot;
  348. for (auto & elem : m6creatures)
  349. {
  350. loot << "%s";
  351. loot.addReplacement(elem);
  352. }
  353. if (!isCustomComplete)
  354. iwText.addReplacement(loot.buildList());
  355. }
  356. break;
  357. case CQuest::MISSION_RESOURCES:
  358. {
  359. MetaString loot;
  360. for (int i = 0; i < 7; ++i)
  361. {
  362. if (m7resources[i])
  363. {
  364. loot << "%d %s";
  365. loot.addReplacement(m7resources[i]);
  366. loot.addReplacement(MetaString::RES_NAMES, i);
  367. }
  368. }
  369. if (!isCustomComplete)
  370. iwText.addReplacement(loot.buildList());
  371. }
  372. break;
  373. case MISSION_KILL_HERO:
  374. case MISSION_KILL_CREATURE:
  375. if (!isCustomComplete)
  376. addReplacements(iwText, completedText);
  377. break;
  378. case MISSION_HERO:
  379. if (!isCustomComplete)
  380. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  381. break;
  382. case MISSION_PLAYER:
  383. if (!isCustomComplete)
  384. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  385. break;
  386. }
  387. }
  388. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  389. {
  390. auto q = handler.enterStruct(fieldName);
  391. handler.serializeString("firstVisitText", firstVisitText);
  392. handler.serializeString("nextVisitText", nextVisitText);
  393. handler.serializeString("completedText", completedText);
  394. if(!handler.saving)
  395. {
  396. isCustomFirst = firstVisitText.size() > 0;
  397. isCustomNext = nextVisitText.size() > 0;
  398. isCustomComplete = completedText.size() > 0;
  399. }
  400. static const std::vector<std::string> MISSION_TYPE_JSON =
  401. {
  402. "None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
  403. };
  404. handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
  405. handler.serializeInt("timeLimit", lastDay, -1);
  406. switch (missionType)
  407. {
  408. case MISSION_NONE:
  409. break;
  410. case MISSION_LEVEL:
  411. handler.serializeInt("heroLevel", m13489val, -1);
  412. break;
  413. case MISSION_PRIMARY_STAT:
  414. {
  415. auto primarySkills = handler.enterStruct("primarySkills");
  416. if(!handler.saving)
  417. m2stats.resize(GameConstants::PRIMARY_SKILLS);
  418. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  419. handler.serializeInt(PrimarySkill::names[i], m2stats[i], 0);
  420. }
  421. break;
  422. case MISSION_KILL_HERO:
  423. case MISSION_KILL_CREATURE:
  424. handler.serializeInstance<ui32>("killTarget", m13489val, ui32(-1));
  425. break;
  426. case MISSION_ART:
  427. //todo: ban artifacts
  428. handler.serializeIdArray<ui16, ArtifactID>("artifacts", m5arts);
  429. break;
  430. case MISSION_ARMY:
  431. {
  432. auto a = handler.enterArray("creatures");
  433. a.serializeStruct(m6creatures);
  434. }
  435. break;
  436. case MISSION_RESOURCES:
  437. {
  438. auto r = handler.enterStruct("resources");
  439. if(!handler.saving)
  440. m7resources.resize(GameConstants::RESOURCE_QUANTITY-1);
  441. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  442. {
  443. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);
  444. }
  445. }
  446. break;
  447. case MISSION_HERO:
  448. handler.serializeId<ui32>("hero", m13489val, 0, &CHeroHandler::decodeHero, &CHeroHandler::encodeHero);
  449. break;
  450. case MISSION_PLAYER:
  451. handler.serializeEnum("player", m13489val, PlayerColor::CANNOT_DETERMINE.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  452. break;
  453. default:
  454. logGlobal->error("Invalid quest mission type");
  455. break;
  456. }
  457. }
  458. CGSeerHut::CGSeerHut() : IQuestObject(),
  459. rewardType(NOTHING), rID(-1), rVal(-1)
  460. {
  461. quest->lastDay = -1;
  462. quest->isCustomFirst = false;
  463. quest->isCustomNext = false;
  464. quest->isCustomComplete = false;
  465. }
  466. void CGSeerHut::setObjToKill()
  467. {
  468. if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
  469. {
  470. quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
  471. assert(quest->stackToKill.type);
  472. quest->stackToKill.count = 0; //no count in info window
  473. quest->stackDirection = checkDirection();
  474. }
  475. else if(quest->missionType == CQuest::MISSION_KILL_HERO)
  476. {
  477. quest->heroName = getHeroToKill(false)->name;
  478. quest->heroPortrait = getHeroToKill(false)->portrait;
  479. }
  480. }
  481. void CGSeerHut::init(CRandomGenerator & rand)
  482. {
  483. seerName = *RandomGeneratorUtil::nextItem(VLC->generaltexth->seerNames, rand);
  484. quest->textOption = rand.nextInt(2);
  485. quest->completedOption = rand.nextInt(1, 3);
  486. }
  487. void CGSeerHut::initObj(CRandomGenerator & rand)
  488. {
  489. init(rand);
  490. quest->progress = CQuest::NOT_ACTIVE;
  491. if(quest->missionType)
  492. {
  493. if(!quest->isCustomFirst)
  494. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  495. if(!quest->isCustomNext)
  496. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  497. if(!quest->isCustomComplete)
  498. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  499. }
  500. else
  501. {
  502. quest->progress = CQuest::COMPLETE;
  503. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->completedOption];
  504. }
  505. }
  506. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  507. {
  508. quest->getRolloverText (text, onHover);//TODO: simplify?
  509. if(!onHover)
  510. text.addReplacement(seerName);
  511. }
  512. std::string CGSeerHut::getHoverText(PlayerColor player) const
  513. {
  514. std::string hoverName = getObjectName();
  515. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  516. {
  517. hoverName = VLC->generaltexth->allTexts[347];
  518. boost::algorithm::replace_first(hoverName, "%s", seerName);
  519. }
  520. if(quest->progress & quest->missionType) //rollover when the quest is active
  521. {
  522. MetaString ms;
  523. getRolloverText (ms, true);
  524. hoverName += ms.toString();
  525. }
  526. return hoverName;
  527. }
  528. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  529. {
  530. switch(missionType)
  531. {
  532. case MISSION_KILL_CREATURE:
  533. out.addReplacement(stackToKill);
  534. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  535. {
  536. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  537. }
  538. break;
  539. case MISSION_KILL_HERO:
  540. out.addReplacement(heroName);
  541. break;
  542. }
  543. }
  544. IQuestObject::IQuestObject():
  545. quest(new CQuest())
  546. {
  547. }
  548. IQuestObject::~IQuestObject()
  549. {
  550. ///Information about quest should remain accessible even if IQuestObject removed from map
  551. ///All CQuest objects are freed in CMap destructor
  552. //delete quest;
  553. }
  554. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  555. {
  556. return quest->checkQuest(h);
  557. }
  558. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  559. {
  560. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  561. }
  562. void IQuestObject::afterAddToMapCommon(CMap * map)
  563. {
  564. map->addNewQuestInstance(quest);
  565. }
  566. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  567. {
  568. quest->getCompletionText (text, components, isCustom, h);
  569. switch(rewardType)
  570. {
  571. case EXPERIENCE: components.push_back(Component (Component::EXPERIENCE, 0, h->calculateXp(rVal), 0));
  572. break;
  573. case MANA_POINTS: components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  574. break;
  575. case MORALE_BONUS: components.push_back(Component (Component::MORALE, 0, rVal, 0));
  576. break;
  577. case LUCK_BONUS: components.push_back(Component (Component::LUCK, 0, rVal, 0));
  578. break;
  579. case RESOURCES: components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  580. break;
  581. case PRIMARY_SKILL: components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  582. break;
  583. case SECONDARY_SKILL: components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  584. break;
  585. case ARTIFACT: components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  586. break;
  587. case SPELL: components.push_back(Component (Component::SPELL, rID, 0, 0));
  588. break;
  589. case CREATURE: components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  590. break;
  591. }
  592. }
  593. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  594. {
  595. switch(what)
  596. {
  597. case 10:
  598. quest->progress = static_cast<CQuest::Eprogress>(val);
  599. break;
  600. }
  601. }
  602. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  603. {
  604. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  605. {
  606. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  607. }
  608. }
  609. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  610. {
  611. InfoWindow iw;
  612. iw.player = h->getOwner();
  613. if(quest->progress < CQuest::COMPLETE)
  614. {
  615. bool firstVisit = !quest->progress;
  616. bool failRequirements = !checkQuest(h);
  617. bool isCustom = false;
  618. if(firstVisit)
  619. {
  620. isCustom = quest->isCustomFirst;
  621. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  622. AddQuest aq;
  623. aq.quest = QuestInfo (quest, this, visitablePos());
  624. aq.player = h->tempOwner;
  625. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  626. }
  627. else if(failRequirements)
  628. {
  629. isCustom = quest->isCustomNext;
  630. }
  631. if(firstVisit || failRequirements)
  632. {
  633. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  634. cb->showInfoDialog(&iw);
  635. }
  636. if(!failRequirements) // propose completion, also on first visit
  637. {
  638. BlockingDialog bd (true, false);
  639. bd.player = h->getOwner();
  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 = CSkillHandler::encodeSkill(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->error("%s: invalid metatype in reward item %s", instanceName, 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);
  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.text.addTxt (MetaString::ADVOB_TXT, 17);
  1030. cb->showBlockingDialog (&bd);
  1031. }
  1032. else
  1033. {
  1034. showInfoDialog(h, 18);
  1035. AddQuest aq;
  1036. aq.quest = QuestInfo (quest, this, visitablePos());
  1037. aq.player = h->tempOwner;
  1038. cb->sendAndApply (&aq);
  1039. //TODO: add this quest only once OR check for multiple instances later
  1040. }
  1041. }
  1042. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1043. {
  1044. if (answer)
  1045. cb->removeObject(this);
  1046. }
  1047. void CGBorderGuard::afterAddToMap(CMap * map)
  1048. {
  1049. IQuestObject::afterAddToMapCommon(map);
  1050. }
  1051. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  1052. {
  1053. if (!wasMyColorVisited (h->getOwner()) )
  1054. {
  1055. showInfoDialog(h,18,0);
  1056. AddQuest aq;
  1057. aq.quest = QuestInfo (quest, this, visitablePos());
  1058. aq.player = h->tempOwner;
  1059. cb->sendAndApply (&aq);
  1060. }
  1061. }
  1062. bool CGBorderGate::passableFor(PlayerColor color) const
  1063. {
  1064. return wasMyColorVisited(color);
  1065. }