CQuest.cpp 24 KB

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