2
0

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/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. if(h->hasArt(elem))
  115. {
  116. gameEvents.removeArtifact(ArtifactLocation(h->id, h->getArtPos(elem, false)));
  117. continue;
  118. }
  119. // perhaps artifact is part of a combined artifact?
  120. const auto * assembly = h->getCombinedArtWithPart(elem);
  121. if (assembly)
  122. {
  123. auto parts = assembly->getPartsInfo();
  124. // Remove the assembly
  125. gameEvents.removeArtifact(ArtifactLocation(h->id, h->getArtPos(assembly)));
  126. // Disassemble this backpack artifact
  127. for(const auto & ci : parts)
  128. {
  129. if(ci.getArtifact()->getTypeId() != elem)
  130. gameEvents.giveHeroNewArtifact(h, ci.getArtifact()->getTypeId(), ArtifactPosition::BACKPACK_START);
  131. }
  132. continue;
  133. }
  134. logGlobal->error("Failed to find artifact %s in inventory of hero %s", elem.toEntity(LIBRARY)->getJsonKey(), h->getHeroTypeID());
  135. }
  136. gameEvents.takeCreatures(h->id, mission.creatures, allowFullArmyRemoval);
  137. gameEvents.giveResources(h->getOwner(), -mission.resources);
  138. }
  139. void CQuest::addTextReplacements(const IGameInfoCallback * cb, MetaString & text, std::vector<Component> & components) const
  140. {
  141. if(mission.heroLevel > 0)
  142. text.replaceNumber(mission.heroLevel);
  143. if(mission.heroExperience > 0)
  144. text.replaceNumber(mission.heroExperience);
  145. { //primary skills
  146. MetaString loot;
  147. for(int i = 0; i < 4; ++i)
  148. {
  149. if(mission.primary[i])
  150. {
  151. loot.appendRawString("%d %s");
  152. loot.replaceNumber(mission.primary[i]);
  153. loot.replaceRawString(LIBRARY->generaltexth->primarySkillNames[i]);
  154. }
  155. }
  156. for(auto & skill : mission.secondary)
  157. {
  158. loot.appendTextID(LIBRARY->skillh->getById(skill.first)->getNameTextID());
  159. }
  160. for(auto & spell : mission.spells)
  161. {
  162. loot.appendTextID(LIBRARY->spellh->getById(spell)->getNameTextID());
  163. }
  164. if(!loot.empty())
  165. text.replaceRawString(loot.buildList());
  166. }
  167. if(killTarget != ObjectInstanceID::NONE && !heroName.empty())
  168. {
  169. components.emplace_back(ComponentType::HERO_PORTRAIT, heroPortrait);
  170. addKillTargetReplacements(text);
  171. }
  172. if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE)
  173. {
  174. components.emplace_back(ComponentType::CREATURE, stackToKill);
  175. addKillTargetReplacements(text);
  176. }
  177. if(!mission.heroes.empty())
  178. text.replaceRawString(LIBRARY->heroh->getById(mission.heroes.front())->getNameTranslated());
  179. if(!mission.artifacts.empty())
  180. {
  181. MetaString loot;
  182. for(const auto & elem : mission.artifacts)
  183. {
  184. loot.appendRawString("%s");
  185. loot.replaceName(elem);
  186. }
  187. text.replaceRawString(loot.buildList());
  188. }
  189. if(!mission.creatures.empty())
  190. {
  191. MetaString loot;
  192. for(const auto & elem : mission.creatures)
  193. {
  194. loot.appendRawString("%s");
  195. loot.replaceName(elem);
  196. }
  197. text.replaceRawString(loot.buildList());
  198. }
  199. if(mission.resources.nonZero())
  200. {
  201. MetaString loot;
  202. for(auto i : LIBRARY->resourceTypeHandler->getAllObjects())
  203. {
  204. if(mission.resources[i])
  205. {
  206. loot.appendRawString("%d %s");
  207. loot.replaceNumber(mission.resources[i]);
  208. loot.replaceName(i);
  209. }
  210. }
  211. text.replaceRawString(loot.buildList());
  212. }
  213. if(!mission.players.empty())
  214. {
  215. MetaString loot;
  216. for(auto & p : mission.players)
  217. loot.appendName(p);
  218. text.replaceRawString(loot.buildList());
  219. }
  220. if(lastDay >= 0)
  221. text.replaceNumber(lastDay - cb->getDate(Date::DAY));
  222. }
  223. void CQuest::getVisitText(const IGameInfoCallback * cb, MetaString &iwText, std::vector<Component> &components, bool firstVisit, const CGHeroInstance * h) const
  224. {
  225. bool failRequirements = (h ? !checkQuest(h) : true);
  226. mission.loadComponents(components, h);
  227. if(firstVisit)
  228. iwText.appendRawString(firstVisitText.toString());
  229. else if(failRequirements)
  230. iwText.appendRawString(nextVisitText.toString());
  231. if(lastDay >= 0)
  232. iwText.appendTextID(TextIdentifier("core", "seerhut", "time", textOption).get());
  233. addTextReplacements(cb, iwText, components);
  234. }
  235. void CQuest::getRolloverText(const IGameInfoCallback * cb, MetaString &ms, bool onHover) const
  236. {
  237. if(onHover)
  238. ms.appendRawString("\n\n");
  239. std::string questState = missionState(onHover ? 3 : 4);
  240. ms.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, questState, textOption).get());
  241. std::vector<Component> components;
  242. addTextReplacements(cb, ms, components);
  243. }
  244. void CQuest::getCompletionText(const IGameInfoCallback * cb, MetaString &iwText) const
  245. {
  246. iwText.appendRawString(completedText.toString());
  247. std::vector<Component> components;
  248. addTextReplacements(cb, iwText, components);
  249. }
  250. void CQuest::defineQuestName()
  251. {
  252. //standard quests
  253. questName = CQuest::missionName(EQuestMission::NONE);
  254. if(mission.heroLevel > 0) questName = CQuest::missionName(EQuestMission::LEVEL);
  255. for(auto & s : mission.primary) if(s) questName = CQuest::missionName(EQuestMission::PRIMARY_SKILL);
  256. if(killTarget != ObjectInstanceID::NONE && !heroName.empty()) questName = CQuest::missionName(EQuestMission::KILL_HERO);
  257. if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE) questName = CQuest::missionName(EQuestMission::KILL_CREATURE);
  258. if(!mission.artifacts.empty()) questName = CQuest::missionName(EQuestMission::ARTIFACT);
  259. if(!mission.creatures.empty()) questName = CQuest::missionName(EQuestMission::ARMY);
  260. if(mission.resources.nonZero()) questName = CQuest::missionName(EQuestMission::RESOURCES);
  261. if(!mission.heroes.empty()) questName = CQuest::missionName(EQuestMission::HERO);
  262. if(!mission.players.empty()) questName = CQuest::missionName(EQuestMission::PLAYER);
  263. if(mission.daysPassed > 0) questName = CQuest::missionName(EQuestMission::HOTA_REACH_DATE);
  264. if(!mission.heroClasses.empty()) questName = CQuest::missionName(EQuestMission::HOTA_HERO_CLASS);
  265. }
  266. void CQuest::addKillTargetReplacements(MetaString &out) const
  267. {
  268. if(!heroName.empty())
  269. out.replaceRawString(heroName);
  270. if(stackToKill != CreatureID::NONE)
  271. {
  272. out.replaceNamePlural(stackToKill);
  273. out.replaceRawString(LIBRARY->generaltexth->arraytxt[147+stackDirection]);
  274. }
  275. }
  276. void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  277. {
  278. auto q = handler.enterStruct(fieldName);
  279. handler.serializeStruct("firstVisitText", firstVisitText);
  280. handler.serializeStruct("nextVisitText", nextVisitText);
  281. handler.serializeStruct("completedText", completedText);
  282. handler.serializeBool("repeatedQuest", repeatedQuest, false);
  283. if(!handler.saving)
  284. {
  285. isCustomFirst = !firstVisitText.empty();
  286. isCustomNext = !nextVisitText.empty();
  287. isCustomComplete = !completedText.empty();
  288. }
  289. handler.serializeInt("timeLimit", lastDay, -1);
  290. handler.serializeStruct("limiter", mission);
  291. handler.serializeInstance("killTarget", killTarget, ObjectInstanceID::NONE);
  292. if(!handler.saving) //compatibility with legacy vmaps
  293. {
  294. std::string missionType = "None";
  295. handler.serializeString("missionType", missionType);
  296. if(missionType == "None")
  297. return;
  298. if(missionType == "Level")
  299. handler.serializeInt("heroLevel", mission.heroLevel);
  300. if(missionType == "PrimaryStat")
  301. {
  302. auto primarySkills = handler.enterStruct("primarySkills");
  303. for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
  304. handler.serializeInt(NPrimarySkill::names[i], mission.primary[i], 0);
  305. }
  306. if(missionType == "Artifact")
  307. handler.serializeIdArray<ArtifactID>("artifacts", mission.artifacts);
  308. if(missionType == "Army")
  309. {
  310. auto a = handler.enterArray("creatures");
  311. a.serializeStruct(mission.creatures);
  312. }
  313. if(missionType == "Resources")
  314. {
  315. auto r = handler.enterStruct("resources");
  316. for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
  317. handler.serializeInt(idx.toResource()->getJsonKey(), mission.resources[idx], 0);
  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(IGameRandomizer & gameRandomizer)
  374. {
  375. init(gameRandomizer.getDefault());
  376. CRewardableObject::initObj(gameRandomizer);
  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", "seerhut", "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, IGameRandomizer & gameRandomizer) const
  466. {
  467. CRewardableObject::newTurn(gameEvents, gameRandomizer);
  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.push_back(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id)));
  599. if(metaTypeName == "luck")
  600. reward.heroBonuses.push_back(std::make_shared<Bonus>(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(IGameRandomizer & gameRandomizer)
  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