CQuest.cpp 25 KB

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