CRewardableObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "../IGameCallback.h"
  14. #include "../IGameSettings.h"
  15. #include "../battle/BattleLayout.h"
  16. #include "../gameState/CGameState.h"
  17. #include "../mapObjectConstructors/AObjectTypeHandler.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 <vstd/RNG.h>
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. const IObjectInterface * CRewardableObject::getObject() const
  26. {
  27. return this;
  28. }
  29. void CRewardableObject::markAsScouted(const CGHeroInstance * hero) const
  30. {
  31. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, hero->id);
  32. cb->sendAndApply(cov);
  33. }
  34. bool CRewardableObject::isGuarded() const
  35. {
  36. return stacksCount() > 0;
  37. }
  38. void CRewardableObject::onHeroVisit(const CGHeroInstance *hero) const
  39. {
  40. if(!wasScouted(hero->getOwner()))
  41. {
  42. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_SCOUTED, id, hero->id);
  43. cb->sendAndApply(cov);
  44. }
  45. if (isGuarded())
  46. {
  47. auto guardedIndexes = getAvailableRewards(hero, Rewardable::EEventType::EVENT_GUARDED);
  48. auto guardedReward = configuration.info.at(guardedIndexes.at(0));
  49. // ask player to confirm attack
  50. BlockingDialog bd(true, false);
  51. bd.player = hero->getOwner();
  52. bd.text = guardedReward.message;
  53. bd.components = getPopupComponents(hero->getOwner());
  54. cb->showBlockingDialog(this, &bd);
  55. }
  56. else
  57. {
  58. doHeroVisit(hero);
  59. }
  60. }
  61. void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
  62. {
  63. grantRewardAfterLevelup(configuration.info.at(selectedReward), this, hero);
  64. }
  65. void CRewardableObject::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  66. {
  67. if (result.winner == BattleSide::ATTACKER)
  68. {
  69. doHeroVisit(hero);
  70. }
  71. }
  72. void CRewardableObject::blockingDialogAnswered(const CGHeroInstance * hero, int32_t answer) const
  73. {
  74. if(isGuarded())
  75. {
  76. if (answer)
  77. {
  78. auto layout = BattleLayout::createLayout(cb, configuration.guardsLayout, hero, this);
  79. cb->startBattle(hero, this, visitablePos(), hero, nullptr, layout, nullptr);
  80. }
  81. }
  82. else
  83. {
  84. onBlockingDialogAnswered(hero, answer);
  85. }
  86. }
  87. void CRewardableObject::markAsVisited(const CGHeroInstance * hero) const
  88. {
  89. cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, true);
  90. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_HERO, id, hero->id);
  91. cb->sendAndApply(cov);
  92. }
  93. void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
  94. {
  95. cb->setObjPropertyValue(id, ObjProperty::REWARD_SELECT, rewardID);
  96. grantRewardBeforeLevelup(configuration.info.at(rewardID), hero);
  97. // hero is not blocked by levelup dialog - grant remainder immediately
  98. if(!cb->isVisitCoveredByAnotherQuery(this, hero))
  99. {
  100. grantRewardAfterLevelup(configuration.info.at(rewardID), this, hero);
  101. }
  102. }
  103. bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
  104. {
  105. switch (configuration.visitMode)
  106. {
  107. case Rewardable::VISIT_UNLIMITED:
  108. return false;
  109. case Rewardable::VISIT_ONCE:
  110. return onceVisitableObjectCleared;
  111. case Rewardable::VISIT_PLAYER:
  112. return vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
  113. case Rewardable::VISIT_BONUS:
  114. return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  115. case Rewardable::VISIT_HERO:
  116. return contextHero->visitedObjects.count(ObjectInstanceID(id));
  117. case Rewardable::VISIT_LIMITER:
  118. return configuration.visitLimiter.heroAllowed(contextHero);
  119. default:
  120. return false;
  121. }
  122. }
  123. bool CRewardableObject::wasVisited(PlayerColor player) const
  124. {
  125. switch (configuration.visitMode)
  126. {
  127. case Rewardable::VISIT_UNLIMITED:
  128. case Rewardable::VISIT_BONUS:
  129. case Rewardable::VISIT_HERO:
  130. case Rewardable::VISIT_LIMITER:
  131. return false;
  132. case Rewardable::VISIT_ONCE:
  133. case Rewardable::VISIT_PLAYER:
  134. return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
  135. default:
  136. return false;
  137. }
  138. }
  139. bool CRewardableObject::wasScouted(PlayerColor player) const
  140. {
  141. return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
  142. }
  143. bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
  144. {
  145. switch (configuration.visitMode)
  146. {
  147. case Rewardable::VISIT_BONUS:
  148. return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  149. case Rewardable::VISIT_HERO:
  150. return h->visitedObjects.count(ObjectInstanceID(id));
  151. case Rewardable::VISIT_LIMITER:
  152. return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
  153. default:
  154. return wasVisited(h->getOwner());
  155. }
  156. }
  157. std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
  158. {
  159. std::string result = getObjectName();
  160. if (includeDescription && !getDescriptionMessage(player, hero).empty())
  161. result += "\n" + getDescriptionMessage(player, hero);
  162. if (hero)
  163. {
  164. if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
  165. {
  166. if (wasVisited(hero))
  167. result += "\n" + configuration.visitedTooltip.toString();
  168. else
  169. result += "\n " + configuration.notVisitedTooltip.toString();
  170. }
  171. }
  172. else
  173. {
  174. if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE)
  175. {
  176. if (wasVisited(player))
  177. result += "\n" + configuration.visitedTooltip.toString();
  178. else
  179. result += "\n" + configuration.notVisitedTooltip.toString();
  180. }
  181. }
  182. return result;
  183. }
  184. std::string CRewardableObject::getHoverText(PlayerColor player) const
  185. {
  186. return getDisplayTextImpl(player, nullptr, false);
  187. }
  188. std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
  189. {
  190. return getDisplayTextImpl(hero->getOwner(), hero, false);
  191. }
  192. std::string CRewardableObject::getPopupText(PlayerColor player) const
  193. {
  194. return getDisplayTextImpl(player, nullptr, true);
  195. }
  196. std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
  197. {
  198. return getDisplayTextImpl(hero->getOwner(), hero, true);
  199. }
  200. std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
  201. {
  202. if (!wasScouted(player) || configuration.info.empty())
  203. return configuration.description.toString();
  204. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  205. if (rewardIndices.empty() || !configuration.info[0].description.empty())
  206. return configuration.info[0].description.toString();
  207. if (!configuration.info[rewardIndices.front()].description.empty())
  208. return configuration.info[rewardIndices.front()].description.toString();
  209. return configuration.description.toString();
  210. }
  211. std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
  212. {
  213. if (!wasScouted(player))
  214. return {};
  215. if (isGuarded())
  216. {
  217. if (!cb->getSettings().getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
  218. return {};
  219. std::map<CreatureID, int> guardsAmounts;
  220. std::vector<Component> result;
  221. for (auto const & slot : Slots())
  222. if (slot.second)
  223. guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
  224. for (auto const & guard : guardsAmounts)
  225. {
  226. Component comp(ComponentType::CREATURE, guard.first, guard.second);
  227. result.push_back(comp);
  228. }
  229. return result;
  230. }
  231. else
  232. {
  233. if (!configuration.showScoutedPreview)
  234. return {};
  235. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  236. if (rewardIndices.empty() && !configuration.info.empty())
  237. {
  238. // Object has valid config, but current hero has no rewards that he can receive.
  239. // Usually this happens if hero has already visited this object -> show reward using context without any hero
  240. // since reward may be context-sensitive - e.g. Witch Hut that gives 1 skill, but always at basic level
  241. return loadComponents(nullptr, {0});
  242. }
  243. if (rewardIndices.empty())
  244. return {};
  245. return loadComponents(hero, rewardIndices);
  246. }
  247. }
  248. std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
  249. {
  250. return getPopupComponentsImpl(player, nullptr);
  251. }
  252. std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
  253. {
  254. return getPopupComponentsImpl(hero->getOwner(), hero);
  255. }
  256. void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  257. {
  258. switch (what)
  259. {
  260. case ObjProperty::REWARD_SELECT:
  261. selectedReward = identifier.getNum();
  262. break;
  263. case ObjProperty::REWARD_CLEARED:
  264. onceVisitableObjectCleared = identifier.getNum();
  265. break;
  266. }
  267. }
  268. void CRewardableObject::newTurn(vstd::RNG & rand) const
  269. {
  270. if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
  271. {
  272. if (configuration.resetParameters.rewards)
  273. {
  274. auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
  275. auto newConfiguration = handler->generateConfiguration(cb, rand, ID, configuration.variables.preset);
  276. cb->setRewardableObjectConfiguration(id, newConfiguration);
  277. }
  278. if (configuration.resetParameters.visitors)
  279. {
  280. cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
  281. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
  282. cb->sendAndApply(cov);
  283. }
  284. }
  285. }
  286. void CRewardableObject::initObj(vstd::RNG & rand)
  287. {
  288. getObjectHandler()->configureObject(this, rand);
  289. }
  290. CRewardableObject::CRewardableObject(IGameCallback *cb)
  291. :CArmedInstance(cb)
  292. {}
  293. void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
  294. {
  295. CArmedInstance::serializeJsonOptions(handler);
  296. handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
  297. }
  298. void CRewardableObject::initializeGuards()
  299. {
  300. clearSlots();
  301. // Workaround for default creature banks strings that has placeholder for object name
  302. // TODO: find better location for this code
  303. for (auto & visitInfo : configuration.info)
  304. visitInfo.message.replaceRawString(getObjectName());
  305. for (auto const & visitInfo : configuration.info)
  306. {
  307. for (auto const & guard : visitInfo.reward.guards)
  308. {
  309. auto slotID = getFreeSlot();
  310. if (!slotID.validSlot())
  311. return;
  312. putStack(slotID, std::make_unique<CStackInstance>(guard.getId(), guard.getCount()));
  313. }
  314. }
  315. }
  316. bool CRewardableObject::isCoastVisitable() const
  317. {
  318. return configuration.coastVisitable;
  319. }
  320. VCMI_LIB_NAMESPACE_END