CQuest.cpp 24 KB

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