CRewardableObject.cpp 13 KB

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