CQuest.cpp 27 KB

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