CQuest.cpp 22 KB

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