CQuest.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. quest->defineQuestName();
  365. if(quest->empty() && quest->killTarget == -1)
  366. quest->progress = CQuest::COMPLETE;
  367. if(quest->questName == quest->missionName(0))
  368. {
  369. quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
  370. }
  371. else
  372. {
  373. if(!quest->isCustomFirst)
  374. quest->firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(0), quest->textOption).get());
  375. if(!quest->isCustomNext)
  376. quest->nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(1), quest->textOption).get());
  377. if(!quest->isCustomComplete)
  378. quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(2), quest->textOption).get());
  379. }
  380. }
  381. void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
  382. {
  383. quest->getRolloverText(text, onHover);//TODO: simplify?
  384. if(!onHover)
  385. text.replaceRawString(seerName);
  386. }
  387. std::string CGSeerHut::getHoverText(PlayerColor player) const
  388. {
  389. std::string hoverName = getObjectName();
  390. if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
  391. {
  392. hoverName = VLC->generaltexth->allTexts[347];
  393. boost::algorithm::replace_first(hoverName, "%s", seerName);
  394. }
  395. if(quest->progress/* & quest->missionType*/) //rollover when the quest is active
  396. {
  397. MetaString ms;
  398. getRolloverText (ms, true);
  399. hoverName += ms.toString();
  400. }
  401. return hoverName;
  402. }
  403. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  404. {
  405. switch(what)
  406. {
  407. case 10:
  408. quest->progress = static_cast<CQuest::EProgress>(val);
  409. break;
  410. }
  411. }
  412. void CGSeerHut::newTurn(CRandomGenerator & rand) const
  413. {
  414. CRewardableObject::newTurn(rand);
  415. if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
  416. {
  417. cb->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
  418. }
  419. }
  420. void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
  421. {
  422. InfoWindow iw;
  423. iw.player = h->getOwner();
  424. if(quest->progress < CQuest::COMPLETE)
  425. {
  426. bool firstVisit = !quest->progress;
  427. bool failRequirements = !checkQuest(h);
  428. if(firstVisit)
  429. {
  430. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
  431. AddQuest aq;
  432. aq.quest = QuestInfo (quest, this, visitablePos());
  433. aq.player = h->tempOwner;
  434. cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
  435. }
  436. if(firstVisit || failRequirements)
  437. {
  438. getVisitText (iw.text, iw.components, firstVisit, h);
  439. cb->showInfoDialog(&iw);
  440. }
  441. if(!failRequirements) // propose completion, also on first visit
  442. {
  443. CRewardableObject::onHeroVisit(h);
  444. return;
  445. }
  446. }
  447. else
  448. {
  449. iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
  450. if (ID == Obj::SEER_HUT)
  451. iw.text.replaceRawString(seerName);
  452. cb->showInfoDialog(&iw);
  453. }
  454. }
  455. int CGSeerHut::checkDirection() const
  456. {
  457. int3 cord = getCreatureToKill()->pos;
  458. if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
  459. {
  460. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
  461. return 8;
  462. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
  463. return 1;
  464. else //northeast
  465. return 2;
  466. }
  467. else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
  468. {
  469. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
  470. return 7;
  471. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
  472. return 9;
  473. else //east
  474. return 3;
  475. }
  476. else //south
  477. {
  478. if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
  479. return 6;
  480. else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
  481. return 5;
  482. else //southeast
  483. return 4;
  484. }
  485. }
  486. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  487. {
  488. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  489. if(allowNull && !o)
  490. return nullptr;
  491. return dynamic_cast<const CGHeroInstance *>(o);
  492. }
  493. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  494. {
  495. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
  496. if(allowNull && !o)
  497. return nullptr;
  498. return dynamic_cast<const CGCreature *>(o);
  499. }
  500. void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  501. {
  502. CRewardableObject::blockingDialogAnswered(hero, answer);
  503. if(answer)
  504. {
  505. quest->completeQuest(cb, hero);
  506. if(quest && quest->repeatedQuest)
  507. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::NOT_ACTIVE);
  508. else
  509. cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
  510. }
  511. }
  512. void CGSeerHut::afterAddToMap(CMap* map)
  513. {
  514. IQuestObject::afterAddToMapCommon(map);
  515. }
  516. void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
  517. {
  518. //quest and reward
  519. CRewardableObject::serializeJsonOptions(handler);
  520. quest->serializeJson(handler, "quest");
  521. if(!handler.saving)
  522. {
  523. //backward compatibility for VCMI maps that use old SeerHut format
  524. auto s = handler.enterStruct("reward");
  525. const JsonNode & rewardsJson = handler.getCurrent();
  526. std::string fullIdentifier;
  527. std::string metaTypeName;
  528. std::string scope;
  529. std::string identifier;
  530. auto iter = rewardsJson.Struct().begin();
  531. fullIdentifier = iter->first;
  532. ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
  533. if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
  534. return;
  535. int val = 0;
  536. handler.serializeInt(fullIdentifier, val);
  537. Rewardable::VisitInfo vinfo;
  538. auto & reward = vinfo.reward;
  539. if(metaTypeName == "experience")
  540. reward.heroExperience = val;
  541. if(metaTypeName == "mana")
  542. reward.manaDiff = val;
  543. if(metaTypeName == "morale")
  544. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
  545. if(metaTypeName == "luck")
  546. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
  547. if(metaTypeName == "resource")
  548. {
  549. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  550. reward.resources[rawId] = val;
  551. }
  552. if(metaTypeName == "primarySkill")
  553. {
  554. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  555. reward.primary.at(rawId) = val;
  556. }
  557. if(metaTypeName == "secondarySkill")
  558. {
  559. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  560. reward.secondary[rawId] = val;
  561. }
  562. if(metaTypeName == "artifact")
  563. {
  564. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  565. reward.artifacts.push_back(rawId);
  566. }
  567. if(metaTypeName == "spell")
  568. {
  569. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  570. reward.spells.push_back(rawId);
  571. }
  572. if(metaTypeName == "creature")
  573. {
  574. auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
  575. reward.creatures.emplace_back(rawId, val);
  576. }
  577. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  578. configuration.info.push_back(vinfo);
  579. }
  580. }
  581. void CGQuestGuard::init(CRandomGenerator & rand)
  582. {
  583. blockVisit = true;
  584. quest->textOption = rand.nextInt(3, 5);
  585. quest->completedOption = rand.nextInt(4, 5);
  586. configuration.info.push_back({});
  587. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  588. configuration.info.back().reward.removeObject = true;
  589. configuration.canRefuse = true;
  590. }
  591. void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
  592. {
  593. //quest only, do not call base class
  594. quest->serializeJson(handler, "quest");
  595. }
  596. void CGKeys::reset()
  597. {
  598. playerKeyMap.clear();
  599. }
  600. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  601. {
  602. if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
  603. {
  604. PlayerColor player(what-101);
  605. playerKeyMap[player].insert(static_cast<ui8>(val));
  606. }
  607. else
  608. logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
  609. }
  610. bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
  611. {
  612. return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
  613. }
  614. std::string CGKeys::getHoverText(PlayerColor player) const
  615. {
  616. return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
  617. }
  618. std::string CGKeys::getObjectName() const
  619. {
  620. return VLC->generaltexth->tentColors[subID] + " " + CGObjectInstance::getObjectName();
  621. }
  622. bool CGKeymasterTent::wasVisited (PlayerColor player) const
  623. {
  624. return wasMyColorVisited (player);
  625. }
  626. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  627. {
  628. int txt_id;
  629. if (!wasMyColorVisited (h->getOwner()) )
  630. {
  631. cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
  632. txt_id=19;
  633. }
  634. else
  635. txt_id=20;
  636. h->showInfoDialog(txt_id);
  637. }
  638. void CGBorderGuard::initObj(CRandomGenerator & rand)
  639. {
  640. blockVisit = true;
  641. }
  642. void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
  643. {
  644. text.appendLocalString(EMetaText::ADVOB_TXT, 18);
  645. }
  646. void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
  647. {
  648. if (!onHover)
  649. {
  650. text.appendRawString(VLC->generaltexth->tentColors[subID]);
  651. text.appendRawString(" ");
  652. text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
  653. }
  654. }
  655. bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
  656. {
  657. return wasMyColorVisited (h->tempOwner);
  658. }
  659. void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
  660. {
  661. if (wasMyColorVisited (h->getOwner()) )
  662. {
  663. BlockingDialog bd (true, false);
  664. bd.player = h->getOwner();
  665. bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
  666. cb->showBlockingDialog (&bd);
  667. }
  668. else
  669. {
  670. h->showInfoDialog(18);
  671. AddQuest aq;
  672. aq.quest = QuestInfo (quest, this, visitablePos());
  673. aq.player = h->tempOwner;
  674. cb->sendAndApply (&aq);
  675. //TODO: add this quest only once OR check for multiple instances later
  676. }
  677. }
  678. void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  679. {
  680. if (answer)
  681. cb->removeObject(this, hero->getOwner());
  682. }
  683. void CGBorderGuard::afterAddToMap(CMap * map)
  684. {
  685. IQuestObject::afterAddToMapCommon(map);
  686. }
  687. void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
  688. {
  689. if (!wasMyColorVisited (h->getOwner()) )
  690. {
  691. h->showInfoDialog(18);
  692. AddQuest aq;
  693. aq.quest = QuestInfo (quest, this, visitablePos());
  694. aq.player = h->tempOwner;
  695. cb->sendAndApply (&aq);
  696. }
  697. }
  698. bool CGBorderGate::passableFor(PlayerColor color) const
  699. {
  700. return wasMyColorVisited(color);
  701. }
  702. VCMI_LIB_NAMESPACE_END