CQuest.cpp 29 KB

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