CRewardableObject.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * CRewardableObject.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 "CRewardableObject.h"
  12. #include "../CPlayerState.h"
  13. #include "../GameSettings.h"
  14. #include "../IGameCallback.h"
  15. #include "../gameState/CGameState.h"
  16. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  17. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  18. #include "../mapObjectConstructors/CRewardableConstructor.h"
  19. #include "../mapObjects/CGHeroInstance.h"
  20. #include "../networkPacks/PacksForClient.h"
  21. #include "../networkPacks/PacksForClientBattle.h"
  22. #include "../serializer/JsonSerializeFormat.h"
  23. #include "../texts/CGeneralTextHandler.h"
  24. #include <vstd/RNG.h>
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. void CRewardableObject::grantRewardWithMessage(const CGHeroInstance * contextHero, int index, bool markAsVisit) const
  27. {
  28. auto vi = configuration.info.at(index);
  29. logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
  30. // show message only if it is not empty or in infobox
  31. if (configuration.infoWindowType != EInfoWindowMode::MODAL || !vi.message.toString().empty())
  32. {
  33. InfoWindow iw;
  34. iw.player = contextHero->tempOwner;
  35. iw.text = vi.message;
  36. vi.reward.loadComponents(iw.components, contextHero);
  37. iw.type = configuration.infoWindowType;
  38. if(!iw.components.empty() || !iw.text.toString().empty())
  39. cb->showInfoDialog(&iw);
  40. }
  41. // grant reward afterwards. Note that it may remove object
  42. if(markAsVisit)
  43. markAsVisited(contextHero);
  44. grantReward(index, contextHero);
  45. }
  46. void CRewardableObject::selectRewardWithMessage(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices, const MetaString & dialog) const
  47. {
  48. BlockingDialog sd(configuration.canRefuse, rewardIndices.size() > 1);
  49. sd.player = contextHero->tempOwner;
  50. sd.text = dialog;
  51. sd.components = loadComponents(contextHero, rewardIndices);
  52. cb->showBlockingDialog(this, &sd);
  53. }
  54. void CRewardableObject::grantAllRewardsWithMessage(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices, bool markAsVisit) const
  55. {
  56. if (rewardIndices.empty())
  57. return;
  58. for (auto index : rewardIndices)
  59. {
  60. // TODO: Merge all rewards of same type, with single message?
  61. grantRewardWithMessage(contextHero, index, false);
  62. }
  63. // Mark visited only after all rewards were processed
  64. if(markAsVisit)
  65. markAsVisited(contextHero);
  66. }
  67. std::vector<Component> CRewardableObject::loadComponents(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices) const
  68. {
  69. std::vector<Component> result;
  70. if (rewardIndices.empty())
  71. return result;
  72. if (configuration.selectMode != Rewardable::SELECT_FIRST && rewardIndices.size() > 1)
  73. {
  74. for (auto index : rewardIndices)
  75. result.push_back(configuration.info.at(index).reward.getDisplayedComponent(contextHero));
  76. }
  77. else
  78. {
  79. configuration.info.at(rewardIndices.front()).reward.loadComponents(result, contextHero);
  80. }
  81. return result;
  82. }
  83. bool CRewardableObject::guardedPotentially() const
  84. {
  85. for (auto const & visitInfo : configuration.info)
  86. if (!visitInfo.reward.guards.empty())
  87. return true;
  88. return false;
  89. }
  90. bool CRewardableObject::guardedPresently() const
  91. {
  92. return stacksCount() > 0;
  93. }
  94. void CRewardableObject::onHeroVisit(const CGHeroInstance *hero) const
  95. {
  96. if(!wasScouted(hero->getOwner()))
  97. {
  98. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_SCOUTED, id, hero->id);
  99. cb->sendAndApply(&cov);
  100. }
  101. if (guardedPresently())
  102. {
  103. auto guardedIndexes = getAvailableRewards(hero, Rewardable::EEventType::EVENT_GUARDED);
  104. auto guardedReward = configuration.info.at(guardedIndexes.at(0));
  105. // ask player to confirm attack
  106. BlockingDialog bd(true, false);
  107. bd.player = hero->getOwner();
  108. bd.text = guardedReward.message;
  109. bd.components = getPopupComponents(hero->getOwner());
  110. cb->showBlockingDialog(&bd);
  111. }
  112. else
  113. {
  114. doHeroVisit(hero);
  115. }
  116. }
  117. void CRewardableObject::doHeroVisit(const CGHeroInstance *h) const
  118. {
  119. if(!wasVisitedBefore(h))
  120. {
  121. auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
  122. bool objectRemovalPossible = false;
  123. for(auto index : rewards)
  124. {
  125. if(configuration.info.at(index).reward.removeObject)
  126. objectRemovalPossible = true;
  127. }
  128. logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
  129. switch (rewards.size())
  130. {
  131. case 0: // no available rewards, e.g. visiting School of War without gold
  132. {
  133. auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
  134. if (!emptyRewards.empty())
  135. grantRewardWithMessage(h, emptyRewards[0], false);
  136. else
  137. logMod->warn("No applicable message for visiting empty object!");
  138. break;
  139. }
  140. case 1: // one reward. Just give it with message
  141. {
  142. if (configuration.canRefuse)
  143. selectRewardWithMessage(h, rewards, configuration.info.at(rewards.front()).message);
  144. else
  145. grantRewardWithMessage(h, rewards.front(), true);
  146. break;
  147. }
  148. default: // multiple rewards. Act according to select mode
  149. {
  150. switch (configuration.selectMode) {
  151. case Rewardable::SELECT_PLAYER: // player must select
  152. selectRewardWithMessage(h, rewards, configuration.onSelect);
  153. break;
  154. case Rewardable::SELECT_FIRST: // give first available
  155. if (configuration.canRefuse)
  156. selectRewardWithMessage(h, { rewards.front() }, configuration.info.at(rewards.front()).message);
  157. else
  158. grantRewardWithMessage(h, rewards.front(), true);
  159. break;
  160. case Rewardable::SELECT_RANDOM: // give random
  161. {
  162. ui32 rewardIndex = *RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator());
  163. if (configuration.canRefuse)
  164. selectRewardWithMessage(h, { rewardIndex }, configuration.info.at(rewardIndex).message);
  165. else
  166. grantRewardWithMessage(h, rewardIndex, true);
  167. break;
  168. }
  169. case Rewardable::SELECT_ALL: // grant all possible
  170. grantAllRewardsWithMessage(h, rewards, true);
  171. break;
  172. }
  173. break;
  174. }
  175. }
  176. if(!objectRemovalPossible && getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
  177. {
  178. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, h->id);
  179. cb->sendAndApply(&cov);
  180. }
  181. }
  182. else
  183. {
  184. logGlobal->debug("Revisiting already visited object");
  185. if (!wasVisited(h->getOwner()))
  186. {
  187. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, h->id);
  188. cb->sendAndApply(&cov);
  189. }
  190. auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED);
  191. if (!visitedRewards.empty())
  192. grantRewardWithMessage(h, visitedRewards[0], false);
  193. else
  194. logMod->warn("No applicable message for visiting already visited object!");
  195. }
  196. }
  197. void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
  198. {
  199. grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), this, hero);
  200. }
  201. void CRewardableObject::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  202. {
  203. if (result.winner == BattleSide::ATTACKER)
  204. {
  205. doHeroVisit(hero);
  206. }
  207. }
  208. void CRewardableObject::blockingDialogAnswered(const CGHeroInstance * hero, int32_t answer) const
  209. {
  210. if(guardedPresently())
  211. {
  212. if (answer)
  213. cb->startBattleI(hero, this, true);
  214. }
  215. else
  216. {
  217. if(answer > 0 && answer - 1 < configuration.info.size())
  218. {
  219. auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  220. markAsVisited(hero);
  221. grantReward(list[answer - 1], hero);
  222. }
  223. else
  224. {
  225. throw std::runtime_error("Unhandled choice");
  226. }
  227. }
  228. }
  229. void CRewardableObject::markAsVisited(const CGHeroInstance * hero) const
  230. {
  231. cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, true);
  232. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_HERO, id, hero->id);
  233. cb->sendAndApply(&cov);
  234. }
  235. void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
  236. {
  237. cb->setObjPropertyValue(id, ObjProperty::REWARD_SELECT, rewardID);
  238. grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
  239. // hero is not blocked by levelup dialog - grant remainder immediately
  240. if(!cb->isVisitCoveredByAnotherQuery(this, hero))
  241. {
  242. grantRewardAfterLevelup(cb, configuration.info.at(rewardID), this, hero);
  243. }
  244. }
  245. bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
  246. {
  247. switch (configuration.visitMode)
  248. {
  249. case Rewardable::VISIT_UNLIMITED:
  250. return false;
  251. case Rewardable::VISIT_ONCE:
  252. return onceVisitableObjectCleared;
  253. case Rewardable::VISIT_PLAYER:
  254. return vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
  255. case Rewardable::VISIT_BONUS:
  256. return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  257. case Rewardable::VISIT_HERO:
  258. return contextHero->visitedObjects.count(ObjectInstanceID(id));
  259. case Rewardable::VISIT_LIMITER:
  260. return configuration.visitLimiter.heroAllowed(contextHero);
  261. default:
  262. return false;
  263. }
  264. }
  265. bool CRewardableObject::wasVisited(PlayerColor player) const
  266. {
  267. switch (configuration.visitMode)
  268. {
  269. case Rewardable::VISIT_UNLIMITED:
  270. case Rewardable::VISIT_BONUS:
  271. case Rewardable::VISIT_HERO:
  272. case Rewardable::VISIT_LIMITER:
  273. return false;
  274. case Rewardable::VISIT_ONCE:
  275. case Rewardable::VISIT_PLAYER:
  276. return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
  277. default:
  278. return false;
  279. }
  280. }
  281. bool CRewardableObject::wasScouted(PlayerColor player) const
  282. {
  283. return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
  284. }
  285. bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
  286. {
  287. switch (configuration.visitMode)
  288. {
  289. case Rewardable::VISIT_BONUS:
  290. return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  291. case Rewardable::VISIT_HERO:
  292. return h->visitedObjects.count(ObjectInstanceID(id));
  293. case Rewardable::VISIT_LIMITER:
  294. return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
  295. default:
  296. return wasVisited(h->getOwner());
  297. }
  298. }
  299. std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
  300. {
  301. std::string result = getObjectName();
  302. if (includeDescription && !getDescriptionMessage(player, hero).empty())
  303. result += "\n" + getDescriptionMessage(player, hero);
  304. if (hero)
  305. {
  306. if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
  307. {
  308. if (wasVisited(hero))
  309. result += "\n" + configuration.visitedTooltip.toString();
  310. else
  311. result += "\n " + configuration.notVisitedTooltip.toString();
  312. }
  313. }
  314. else
  315. {
  316. if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE)
  317. {
  318. if (wasVisited(player))
  319. result += "\n" + configuration.visitedTooltip.toString();
  320. else
  321. result += "\n" + configuration.notVisitedTooltip.toString();
  322. }
  323. }
  324. return result;
  325. }
  326. std::string CRewardableObject::getHoverText(PlayerColor player) const
  327. {
  328. return getDisplayTextImpl(player, nullptr, false);
  329. }
  330. std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
  331. {
  332. return getDisplayTextImpl(hero->getOwner(), hero, false);
  333. }
  334. std::string CRewardableObject::getPopupText(PlayerColor player) const
  335. {
  336. return getDisplayTextImpl(player, nullptr, true);
  337. }
  338. std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
  339. {
  340. return getDisplayTextImpl(hero->getOwner(), hero, true);
  341. }
  342. std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
  343. {
  344. if (!wasScouted(player) || configuration.info.empty())
  345. return configuration.description.toString();
  346. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  347. if (rewardIndices.empty() || !configuration.info[0].description.empty())
  348. return configuration.info[0].description.toString();
  349. if (!configuration.info[rewardIndices.front()].description.empty())
  350. return configuration.info[rewardIndices.front()].description.toString();
  351. return configuration.description.toString();
  352. }
  353. std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
  354. {
  355. if (!wasScouted(player))
  356. return {};
  357. if (guardedPresently())
  358. {
  359. if (!VLC->settings()->getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
  360. return {};
  361. std::map<CreatureID, int> guardsAmounts;
  362. std::vector<Component> result;
  363. for (auto const & slot : Slots())
  364. if (slot.second)
  365. guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
  366. for (auto const & guard : guardsAmounts)
  367. {
  368. Component comp(ComponentType::CREATURE, guard.first, guard.second);
  369. result.push_back(comp);
  370. }
  371. return result;
  372. }
  373. else
  374. {
  375. if (!configuration.showScoutedPreview)
  376. return {};
  377. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  378. if (rewardIndices.empty() && !configuration.info.empty())
  379. {
  380. // Object has valid config, but current hero has no rewards that he can receive.
  381. // Usually this happens if hero has already visited this object -> show reward using context without any hero
  382. // since reward may be context-sensitive - e.g. Witch Hut that gives 1 skill, but always at basic level
  383. return loadComponents(nullptr, {0});
  384. }
  385. if (rewardIndices.empty())
  386. return {};
  387. return loadComponents(hero, rewardIndices);
  388. }
  389. }
  390. std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
  391. {
  392. return getPopupComponentsImpl(player, nullptr);
  393. }
  394. std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
  395. {
  396. return getPopupComponentsImpl(hero->getOwner(), hero);
  397. }
  398. void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  399. {
  400. switch (what)
  401. {
  402. case ObjProperty::REWARD_SELECT:
  403. selectedReward = identifier.getNum();
  404. break;
  405. case ObjProperty::REWARD_CLEARED:
  406. onceVisitableObjectCleared = identifier.getNum();
  407. break;
  408. }
  409. }
  410. void CRewardableObject::newTurn(vstd::RNG & rand) const
  411. {
  412. if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
  413. {
  414. if (configuration.resetParameters.rewards)
  415. {
  416. auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
  417. auto newConfiguration = handler->generateConfiguration(cb, rand, ID);
  418. cb->setRewardableObjectConfiguration(id, newConfiguration);
  419. }
  420. if (configuration.resetParameters.visitors)
  421. {
  422. cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
  423. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
  424. cb->sendAndApply(&cov);
  425. }
  426. }
  427. }
  428. void CRewardableObject::initObj(vstd::RNG & rand)
  429. {
  430. getObjectHandler()->configureObject(this, rand);
  431. }
  432. CRewardableObject::CRewardableObject(IGameCallback *cb)
  433. :CArmedInstance(cb)
  434. {}
  435. void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
  436. {
  437. CArmedInstance::serializeJsonOptions(handler);
  438. handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
  439. }
  440. void CRewardableObject::initializeGuards()
  441. {
  442. clearSlots();
  443. for (auto const & visitInfo : configuration.info)
  444. {
  445. for (auto const & guard : visitInfo.reward.guards)
  446. {
  447. auto slotID = getFreeSlot();
  448. if (!slotID.validSlot())
  449. return;
  450. putStack(slotID, new CStackInstance(guard.getId(), guard.getCount()));
  451. }
  452. }
  453. }
  454. bool CRewardableObject::isCoastVisitable() const
  455. {
  456. return configuration.coastVisitable;
  457. }
  458. VCMI_LIB_NAMESPACE_END