2
0

CQuest.cpp 24 KB

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