2
0

CQuest.cpp 21 KB

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