CQuest.cpp 25 KB

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