CQuest.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 "../CSoundBase.h"
  14. #include "../texts/CGeneralTextHandler.h"
  15. #include "CGCreature.h"
  16. #include "../IGameCallback.h"
  17. #include "../entities/artifact/CArtifact.h"
  18. #include "../entities/hero/CHeroHandler.h"
  19. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. #include "../GameConstants.h"
  22. #include "../constants/StringConstants.h"
  23. #include "../CPlayerState.h"
  24. #include "../CSkillHandler.h"
  25. #include "../mapping/CMap.h"
  26. #include "../mapObjects/CGHeroInstance.h"
  27. #include "../modding/ModScope.h"
  28. #include "../modding/ModUtility.h"
  29. #include "../networkPacks/PacksForClient.h"
  30. #include "../spells/CSpellHandler.h"
  31. #include <vstd/RNG.h>
  32. VCMI_LIB_NAMESPACE_BEGIN
  33. //TODO: Remove constructor
  34. CQuest::CQuest():
  35. qid(-1),
  36. isCompleted(false),
  37. lastDay(-1),
  38. killTarget(ObjectInstanceID::NONE),
  39. textOption(0),
  40. completedOption(0),
  41. stackDirection(0),
  42. isCustomFirst(false),
  43. isCustomNext(false),
  44. isCustomComplete(false),
  45. repeatedQuest(false),
  46. questName(CQuest::missionName(EQuestMission::NONE))
  47. {
  48. }
  49. static std::string visitedTxt(const bool visited)
  50. {
  51. int id = visited ? 352 : 353;
  52. return LIBRARY->generaltexth->allTexts[id];
  53. }
  54. const std::string & CQuest::missionName(EQuestMission mission)
  55. {
  56. static const std::array<std::string, 14> names = {
  57. "empty",
  58. "heroLevel",
  59. "primarySkill",
  60. "killHero",
  61. "killCreature",
  62. "bringArt",
  63. "bringCreature",
  64. "bringResources",
  65. "bringHero",
  66. "bringPlayer",
  67. "hotaINVALID", // only used for h3m parsing
  68. "keymaster",
  69. "heroClass",
  70. "reachDate"
  71. };
  72. if(static_cast<size_t>(mission) < names.size())
  73. return names[static_cast<size_t>(mission)];
  74. return names[0];
  75. }
  76. const std::string & CQuest::missionState(int state)
  77. {
  78. static const std::array<std::string, 5> states = {
  79. "receive",
  80. "visit",
  81. "complete",
  82. "hover",
  83. "description",
  84. };
  85. if(state < states.size())
  86. return states[state];
  87. return states[0];
  88. }
  89. bool CQuest::checkMissionArmy(const CQuest * q, const CCreatureSet * army)
  90. {
  91. return army->hasUnits(q->mission.creatures, true);
  92. }
  93. bool CQuest::checkQuest(const CGHeroInstance * h) const
  94. {
  95. if(!mission.heroAllowed(h))
  96. return false;
  97. if(killTarget.hasValue())
  98. {
  99. PlayerColor owner = h->getOwner();
  100. if (!h->cb->getPlayerState(owner)->destroyedObjects.count(killTarget))
  101. return false;
  102. }
  103. return true;
  104. }
  105. void CQuest::completeQuest(IGameCallback * cb, const CGHeroInstance *h) const
  106. {
  107. // FIXME: this should be part of 'reward', and not hacking into limiter state that should only limit access to such reward
  108. for(auto & elem : mission.artifacts)
  109. {
  110. if(h->hasArt(elem))
  111. {
  112. cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(elem, false)));
  113. continue;
  114. }
  115. // perhaps artifact is part of a combined artifact?
  116. const auto * assembly = h->getCombinedArtWithPart(elem);
  117. if (assembly)
  118. {
  119. auto parts = assembly->getPartsInfo();
  120. // Remove the assembly
  121. cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(assembly)));
  122. // Disassemble this backpack artifact
  123. for(const auto & ci : parts)
  124. {
  125. if(ci.getArtifact()->getTypeId() != elem)
  126. cb->giveHeroNewArtifact(h, ci.getArtifact()->getTypeId(), ArtifactPosition::BACKPACK_START);
  127. }
  128. continue;
  129. }
  130. logGlobal->error("Failed to find artifact %s in inventory of hero %s", elem.toEntity(LIBRARY)->getJsonKey(), h->getHeroTypeID());
  131. }
  132. cb->takeCreatures(h->id, mission.creatures);
  133. cb->giveResources(h->getOwner(), -mission.resources);
  134. }
  135. void CQuest::addTextReplacements(const CGameInfoCallback * cb, MetaString & text, std::vector<Component> & components) const
  136. {
  137. if(mission.heroLevel > 0)
  138. text.replaceNumber(mission.heroLevel);
  139. if(mission.heroExperience > 0)
  140. text.replaceNumber(mission.heroExperience);
  141. { //primary skills
  142. MetaString loot;
  143. for(int i = 0; i < 4; ++i)
  144. {
  145. if(mission.primary[i])
  146. {
  147. loot.appendRawString("%d %s");
  148. loot.replaceNumber(mission.primary[i]);
  149. loot.replaceRawString(LIBRARY->generaltexth->primarySkillNames[i]);
  150. }
  151. }
  152. for(auto & skill : mission.secondary)
  153. {
  154. loot.appendTextID(LIBRARY->skillh->getById(skill.first)->getNameTextID());
  155. }
  156. for(auto & spell : mission.spells)
  157. {
  158. loot.appendTextID(LIBRARY->spellh->getById(spell)->getNameTextID());
  159. }
  160. if(!loot.empty())
  161. text.replaceRawString(loot.buildList());
  162. }
  163. if(killTarget != ObjectInstanceID::NONE && !heroName.empty())
  164. {
  165. components.emplace_back(ComponentType::HERO_PORTRAIT, heroPortrait);
  166. addKillTargetReplacements(text);
  167. }
  168. if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE)
  169. {
  170. components.emplace_back(ComponentType::CREATURE, stackToKill);
  171. addKillTargetReplacements(text);
  172. }
  173. if(!mission.heroes.empty())
  174. text.replaceRawString(LIBRARY->heroh->getById(mission.heroes.front())->getNameTranslated());
  175. if(!mission.artifacts.empty())
  176. {
  177. MetaString loot;
  178. for(const auto & elem : mission.artifacts)
  179. {
  180. loot.appendRawString("%s");
  181. loot.replaceName(elem);
  182. }
  183. text.replaceRawString(loot.buildList());
  184. }
  185. if(!mission.creatures.empty())
  186. {
  187. MetaString loot;
  188. for(const auto & elem : mission.creatures)
  189. {
  190. loot.appendRawString("%s");
  191. loot.replaceName(elem);
  192. }
  193. text.replaceRawString(loot.buildList());
  194. }
  195. if(mission.resources.nonZero())
  196. {
  197. MetaString loot;
  198. for(auto i : GameResID::ALL_RESOURCES())
  199. {
  200. if(mission.resources[i])
  201. {
  202. loot.appendRawString("%d %s");
  203. loot.replaceNumber(mission.resources[i]);
  204. loot.replaceName(i);
  205. }
  206. }
  207. text.replaceRawString(loot.buildList());
  208. }
  209. if(!mission.players.empty())
  210. {
  211. MetaString loot;
  212. for(auto & p : mission.players)
  213. loot.appendName(p);
  214. text.replaceRawString(loot.buildList());
  215. }
  216. if(lastDay >= 0)
  217. text.replaceNumber(lastDay - cb->getDate(Date::DAY));
  218. }
  219. void CQuest::getVisitText(const CGameInfoCallback * cb, MetaString &iwText, std::vector<Component> &components, bool firstVisit, const CGHeroInstance * h) const
  220. {
  221. bool failRequirements = (h ? !checkQuest(h) : true);
  222. mission.loadComponents(components, h);
  223. if(firstVisit)
  224. iwText.appendRawString(firstVisitText.toString());
  225. else if(failRequirements)
  226. iwText.appendRawString(nextVisitText.toString());
  227. if(lastDay >= 0)
  228. iwText.appendTextID(TextIdentifier("core", "seerhut", "time", textOption).get());
  229. addTextReplacements(cb, iwText, components);
  230. }
  231. void CQuest::getRolloverText(const CGameInfoCallback * cb, MetaString &ms, bool onHover) const
  232. {
  233. if(onHover)
  234. ms.appendRawString("\n\n");
  235. std::string questState = missionState(onHover ? 3 : 4);
  236. ms.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, questState, textOption).get());
  237. std::vector<Component> components;
  238. addTextReplacements(cb, ms, components);
  239. }
  240. void CQuest::getCompletionText(const CGameInfoCallback * cb, MetaString &iwText) const
  241. {
  242. iwText.appendRawString(completedText.toString());
  243. std::vector<Component> components;
  244. addTextReplacements(cb, iwText, components);
  245. }
  246. void CQuest::defineQuestName()
  247. {
  248. //standard quests
  249. questName = CQuest::missionName(EQuestMission::NONE);
  250. if(mission.heroLevel > 0) questName = CQuest::missionName(EQuestMission::LEVEL);
  251. for(auto & s : mission.primary) if(s) questName = CQuest::missionName(EQuestMission::PRIMARY_SKILL);
  252. if(killTarget != ObjectInstanceID::NONE && !heroName.empty()) questName = CQuest::missionName(EQuestMission::KILL_HERO);
  253. if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE) questName = CQuest::missionName(EQuestMission::KILL_CREATURE);
  254. if(!mission.artifacts.empty()) questName = CQuest::missionName(EQuestMission::ARTIFACT);
  255. if(!mission.creatures.empty()) questName = CQuest::missionName(EQuestMission::ARMY);
  256. if(mission.resources.nonZero()) questName = CQuest::missionName(EQuestMission::RESOURCES);
  257. if(!mission.heroes.empty()) questName = CQuest::missionName(EQuestMission::HERO);
  258. if(!mission.players.empty()) questName = CQuest::missionName(EQuestMission::PLAYER);
  259. if(mission.daysPassed > 0) questName = CQuest::missionName(EQuestMission::HOTA_REACH_DATE);
  260. if(!mission.heroClasses.empty()) questName = CQuest::missionName(EQuestMission::HOTA_HERO_CLASS);
  261. }
  262. void CQuest::addKillTargetReplacements(MetaString &out) const
  263. {
  264. if(!heroName.empty())
  265. out.replaceRawString(heroName);
  266. if(stackToKill != CreatureID::NONE)
  267. {
  268. out.replaceNamePlural(stackToKill);
  269. out.replaceRawString(LIBRARY->generaltexth->arraytxt[147+stackDirection]);
  270. }
  271. }
  272. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  273. {
  274. auto q = handler.enterStruct(fieldName);
  275. handler.serializeStruct("firstVisitText", firstVisitText);
  276. handler.serializeStruct("nextVisitText", nextVisitText);
  277. handler.serializeStruct("completedText", completedText);
  278. handler.serializeBool("repeatedQuest", repeatedQuest, false);
  279. if(!handler.saving)
  280. {
  281. isCustomFirst = !firstVisitText.empty();
  282. isCustomNext = !nextVisitText.empty();
  283. isCustomComplete = !completedText.empty();
  284. }
  285. handler.serializeInt("timeLimit", lastDay, -1);
  286. handler.serializeStruct("limiter", mission);
  287. handler.serializeInstance("killTarget", killTarget, ObjectInstanceID::NONE);
  288. if(!handler.saving) //compatibility with legacy vmaps
  289. {
  290. std::string missionType = "None";
  291. handler.serializeString("missionType", missionType);
  292. if(missionType == "None")
  293. return;
  294. if(missionType == "Level")
  295. handler.serializeInt("heroLevel", mission.heroLevel);
  296. if(missionType == "PrimaryStat")
  297. {
  298. auto primarySkills = handler.enterStruct("primarySkills");
  299. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  300. handler.serializeInt(NPrimarySkill::names[i], mission.primary[i], 0);
  301. }
  302. if(missionType == "Artifact")
  303. handler.serializeIdArray<ArtifactID>("artifacts", mission.artifacts);
  304. if(missionType == "Army")
  305. {
  306. auto a = handler.enterArray("creatures");
  307. a.serializeStruct(mission.creatures);
  308. }
  309. if(missionType == "Resources")
  310. {
  311. auto r = handler.enterStruct("resources");
  312. for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
  313. {
  314. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mission.resources[idx], 0);
  315. }
  316. }
  317. if(missionType == "Hero")
  318. {
  319. HeroTypeID temp;
  320. handler.serializeId("hero", temp, HeroTypeID::NONE);
  321. mission.heroes.emplace_back(temp);
  322. }
  323. if(missionType == "Player")
  324. {
  325. PlayerColor temp;
  326. handler.serializeId("player", temp, PlayerColor::NEUTRAL);
  327. mission.players.emplace_back(temp);
  328. }
  329. }
  330. }
  331. IQuestObject::IQuestObject()
  332. :quest(std::make_shared<CQuest>())
  333. {}
  334. IQuestObject::~IQuestObject() = default;
  335. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  336. {
  337. return getQuest().checkQuest(h);
  338. }
  339. void CGSeerHut::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  340. {
  341. getQuest().getVisitText(cb, text, components, FirstVisit, h);
  342. }
  343. void CGSeerHut::setObjToKill()
  344. {
  345. if(getQuest().killTarget == ObjectInstanceID::NONE)
  346. return;
  347. if(getCreatureToKill(true))
  348. {
  349. getQuest().stackToKill = getCreatureToKill(false)->getCreatureID();
  350. assert(getQuest().stackToKill != CreatureID::NONE);
  351. getQuest().stackDirection = checkDirection();
  352. }
  353. else if(getHeroToKill(true))
  354. {
  355. getQuest().heroName = getHeroToKill(false)->getNameTranslated();
  356. getQuest().heroPortrait = getHeroToKill(false)->getPortraitSource();
  357. }
  358. }
  359. void CGSeerHut::init(vstd::RNG & rand)
  360. {
  361. auto names = LIBRARY->generaltexth->findStringsWithPrefix("core.seerhut.names");
  362. auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
  363. seerName = LIBRARY->generaltexth->translate(seerNameID);
  364. getQuest().textOption = rand.nextInt(2);
  365. getQuest().completedOption = rand.nextInt(1, 3);
  366. configuration.canRefuse = true;
  367. configuration.visitMode = Rewardable::EVisitMode::VISIT_ONCE;
  368. configuration.selectMode = Rewardable::ESelectMode::SELECT_PLAYER;
  369. }
  370. void CGSeerHut::initObj(vstd::RNG & rand)
  371. {
  372. init(rand);
  373. CRewardableObject::initObj(rand);
  374. setObjToKill();
  375. getQuest().defineQuestName();
  376. if(getQuest().mission == Rewardable::Limiter{} && getQuest().killTarget == ObjectInstanceID::NONE)
  377. getQuest().isCompleted = true;
  378. if(getQuest().questName == getQuest().missionName(EQuestMission::NONE))
  379. {
  380. getQuest().firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", getQuest().completedOption).get());
  381. }
  382. else
  383. {
  384. if(!getQuest().isCustomFirst)
  385. getQuest().firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", getQuest().questName, getQuest().missionState(0), getQuest().textOption).get());
  386. if(!getQuest().isCustomNext)
  387. getQuest().nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", getQuest().questName, getQuest().missionState(1), getQuest().textOption).get());
  388. if(!getQuest().isCustomComplete)
  389. getQuest().completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", getQuest(). questName, getQuest().missionState(2), getQuest().textOption).get());
  390. }
  391. getQuest().getCompletionText(cb, configuration.onSelect);
  392. for(auto & i : configuration.info)
  393. getQuest().getCompletionText(cb, i.message);
  394. }
  395. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  396. {
  397. getQuest().getRolloverText(cb, text, onHover);
  398. if(!onHover)
  399. text.replaceRawString(seerName);
  400. }
  401. std::string CGSeerHut::getHoverText(PlayerColor player) const
  402. {
  403. std::string hoverName = getObjectName();
  404. if(ID == Obj::SEER_HUT && getQuest().activeForPlayers.count(player))
  405. {
  406. hoverName = LIBRARY->generaltexth->allTexts[347];
  407. boost::algorithm::replace_first(hoverName, "%s", seerName);
  408. }
  409. if(getQuest().activeForPlayers.count(player)
  410. && (getQuest().mission != Rewardable::Limiter{}
  411. || getQuest().killTarget != ObjectInstanceID::NONE)) //rollover when the quest is active
  412. {
  413. MetaString ms;
  414. getRolloverText (ms, true);
  415. hoverName += ms.toString();
  416. }
  417. return hoverName;
  418. }
  419. std::string CGSeerHut::getHoverText(const CGHeroInstance * hero) const
  420. {
  421. return getHoverText(hero->getOwner());
  422. }
  423. std::string CGSeerHut::getPopupText(PlayerColor player) const
  424. {
  425. return getHoverText(player);
  426. }
  427. std::string CGSeerHut::getPopupText(const CGHeroInstance * hero) const
  428. {
  429. return getHoverText(hero->getOwner());
  430. }
  431. std::vector<Component> CGSeerHut::getPopupComponents(PlayerColor player) const
  432. {
  433. std::vector<Component> result;
  434. if (getQuest().activeForPlayers.count(player))
  435. getQuest().mission.loadComponents(result, nullptr);
  436. return result;
  437. }
  438. std::vector<Component> CGSeerHut::getPopupComponents(const CGHeroInstance * hero) const
  439. {
  440. std::vector<Component> result;
  441. if (getQuest().activeForPlayers.count(hero->getOwner()))
  442. getQuest().mission.loadComponents(result, hero);
  443. return result;
  444. }
  445. void CGSeerHut::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  446. {
  447. switch(what)
  448. {
  449. case ObjProperty::SEERHUT_VISITED:
  450. {
  451. getQuest().activeForPlayers.emplace(identifier.as<PlayerColor>());
  452. break;
  453. }
  454. case ObjProperty::SEERHUT_COMPLETE:
  455. {
  456. getQuest().isCompleted = identifier.getNum();
  457. getQuest().activeForPlayers.clear();
  458. break;
  459. }
  460. }
  461. }
  462. void CGSeerHut::newTurn(vstd::RNG & rand) const
  463. {
  464. CRewardableObject::newTurn(rand);
  465. if(getQuest().lastDay >= 0 && getQuest().lastDay <= cb->getDate() - 1) //time is up
  466. {
  467. cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, true);
  468. }
  469. }
  470. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  471. {
  472. InfoWindow iw;
  473. iw.player = h->getOwner();
  474. if(!getQuest().isCompleted)
  475. {
  476. bool firstVisit = !getQuest().activeForPlayers.count(h->getOwner());
  477. bool failRequirements = !checkQuest(h);
  478. if(firstVisit)
  479. {
  480. cb->setObjPropertyID(id, ObjProperty::SEERHUT_VISITED, h->getOwner());
  481. AddQuest aq;
  482. aq.quest = QuestInfo(id);
  483. aq.player = h->tempOwner;
  484. cb->sendAndApply(aq); //TODO: merge with setObjProperty?
  485. }
  486. if(firstVisit || failRequirements)
  487. {
  488. getVisitText (iw.text, iw.components, firstVisit, h);
  489. cb->showInfoDialog(&iw);
  490. }
  491. if(!failRequirements) // propose completion, also on first visit
  492. {
  493. CRewardableObject::onHeroVisit(h);
  494. return;
  495. }
  496. }
  497. else
  498. {
  499. iw.text.appendRawString(LIBRARY->generaltexth->seerEmpty[getQuest().completedOption]);
  500. if (ID == Obj::SEER_HUT)
  501. iw.text.replaceRawString(seerName);
  502. cb->showInfoDialog(&iw);
  503. }
  504. }
  505. int CGSeerHut::checkDirection() const
  506. {
  507. int3 cord = getCreatureToKill(false)->visitablePos();
  508. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  509. {
  510. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  511. return 8;
  512. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  513. return 1;
  514. else //northeast
  515. return 2;
  516. }
  517. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  518. {
  519. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  520. return 7;
  521. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  522. return 9;
  523. else //east
  524. return 3;
  525. }
  526. else //south
  527. {
  528. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  529. return 6;
  530. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  531. return 5;
  532. else //southeast
  533. return 4;
  534. }
  535. }
  536. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  537. {
  538. const CGObjectInstance *o = cb->getObj(getQuest().killTarget);
  539. if(allowNull && !o)
  540. return nullptr;
  541. return dynamic_cast<const CGHeroInstance *>(o);
  542. }
  543. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  544. {
  545. const CGObjectInstance *o = cb->getObj(getQuest().killTarget);
  546. if(allowNull && !o)
  547. return nullptr;
  548. return dynamic_cast<const CGCreature *>(o);
  549. }
  550. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
  551. {
  552. CRewardableObject::blockingDialogAnswered(hero, answer);
  553. if(answer)
  554. {
  555. getQuest().completeQuest(cb, hero);
  556. cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, !getQuest().repeatedQuest); //mission complete
  557. }
  558. }
  559. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  560. {
  561. //quest and reward
  562. CRewardableObject::serializeJsonOptions(handler);
  563. getQuest().serializeJson(handler, "quest");
  564. if(!handler.saving)
  565. {
  566. //backward compatibility for VCMI maps that use old SeerHut format
  567. auto s = handler.enterStruct("reward");
  568. const JsonNode & rewardsJson = handler.getCurrent();
  569. if (rewardsJson.Struct().empty())
  570. return;
  571. std::string fullIdentifier;
  572. std::string metaTypeName;
  573. std::string scope;
  574. std::string identifier;
  575. auto iter = rewardsJson.Struct().begin();
  576. fullIdentifier = iter->first;
  577. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  578. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  579. return;
  580. int val = 0;
  581. handler.serializeInt(fullIdentifier, val);
  582. Rewardable::VisitInfo vinfo;
  583. auto & reward = vinfo.reward;
  584. if(metaTypeName == "experience")
  585. reward.heroExperience = val;
  586. if(metaTypeName == "mana")
  587. reward.manaDiff = val;
  588. if(metaTypeName == "morale")
  589. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  590. if(metaTypeName == "luck")
  591. reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  592. if(metaTypeName == "resource")
  593. {
  594. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  595. reward.resources[rawId] = val;
  596. }
  597. if(metaTypeName == "primarySkill")
  598. {
  599. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  600. reward.primary.at(rawId) = val;
  601. }
  602. if(metaTypeName == "secondarySkill")
  603. {
  604. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  605. reward.secondary[rawId] = val;
  606. }
  607. if(metaTypeName == "artifact")
  608. {
  609. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  610. reward.grantedArtifacts.push_back(rawId);
  611. }
  612. if(metaTypeName == "spell")
  613. {
  614. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  615. reward.spells.push_back(rawId);
  616. }
  617. if(metaTypeName == "creature")
  618. {
  619. auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  620. reward.creatures.emplace_back(rawId, val);
  621. }
  622. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  623. configuration.info.push_back(vinfo);
  624. }
  625. }
  626. void CGQuestGuard::init(vstd::RNG & rand)
  627. {
  628. blockVisit = true;
  629. getQuest().textOption = rand.nextInt(3, 5);
  630. getQuest().completedOption = rand.nextInt(4, 5);
  631. configuration.info.push_back({});
  632. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  633. configuration.info.back().reward.removeObject = subID.getNum() == 0 ? true : false;
  634. configuration.canRefuse = true;
  635. }
  636. void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
  637. {
  638. if(!getQuest().isCompleted)
  639. CGSeerHut::onHeroVisit(h);
  640. else
  641. cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, false);
  642. }
  643. bool CGQuestGuard::passableFor(PlayerColor color) const
  644. {
  645. return getQuest().isCompleted;
  646. }
  647. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  648. {
  649. //quest only, do not call base class
  650. getQuest().serializeJson(handler, "quest");
  651. }
  652. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  653. {
  654. return cb->getPlayerState(player)->visitedObjectsGlobal.count({Obj::KEYMASTER, subID}) != 0;
  655. }
  656. std::string CGKeys::getHoverText(PlayerColor player) const
  657. {
  658. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  659. }
  660. std::string CGKeys::getObjectName() const
  661. {
  662. return LIBRARY->generaltexth->tentColors[subID.getNum()] + " " + CGObjectInstance::getObjectName();
  663. }
  664. std::string CGKeys::getObjectDescription(PlayerColor player) const
  665. {
  666. return visitedTxt(wasMyColorVisited(player));
  667. }
  668. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  669. {
  670. return wasMyColorVisited (player);
  671. }
  672. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  673. {
  674. int txt_id;
  675. if (!wasMyColorVisited (h->getOwner()) )
  676. {
  677. ChangeObjectVisitors cow;
  678. cow.mode = ChangeObjectVisitors::VISITOR_ADD_PLAYER;
  679. cow.hero = h->id;
  680. cow.object = id;
  681. cb->sendAndApply(cow);
  682. txt_id=19;
  683. }
  684. else
  685. txt_id=20;
  686. h->showInfoDialog(txt_id);
  687. }
  688. void CGBorderGuard::initObj(vstd::RNG & rand)
  689. {
  690. blockVisit = true;
  691. }
  692. void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  693. {
  694. text.appendLocalString(EMetaText::ADVOB_TXT, 18);
  695. }
  696. void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
  697. {
  698. if (!onHover)
  699. {
  700. text.appendRawString(LIBRARY->generaltexth->tentColors[subID.getNum()]);
  701. text.appendRawString(" ");
  702. text.appendRawString(LIBRARY->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  703. }
  704. }
  705. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  706. {
  707. return wasMyColorVisited (h->tempOwner);
  708. }
  709. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  710. {
  711. if (wasMyColorVisited (h->getOwner()) )
  712. {
  713. BlockingDialog bd (true, false);
  714. bd.player = h->getOwner();
  715. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  716. cb->showBlockingDialog (this, &bd);
  717. }
  718. else
  719. {
  720. h->showInfoDialog(18);
  721. AddQuest aq;
  722. aq.quest = QuestInfo(id);
  723. aq.player = h->tempOwner;
  724. cb->sendAndApply(aq);
  725. //TODO: add this quest only once OR check for multiple instances later
  726. }
  727. }
  728. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
  729. {
  730. if (answer)
  731. cb->removeObject(this, hero->getOwner());
  732. }
  733. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  734. {
  735. if (!wasMyColorVisited (h->getOwner()) )
  736. {
  737. h->showInfoDialog(18);
  738. AddQuest aq;
  739. aq.quest = QuestInfo(id);
  740. aq.player = h->tempOwner;
  741. cb->sendAndApply(aq);
  742. }
  743. }
  744. bool CGBorderGate::passableFor(PlayerColor color) const
  745. {
  746. return wasMyColorVisited(color);
  747. }
  748. VCMI_LIB_NAMESPACE_END