CQuest.cpp 25 KB

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