CRewardableObject.cpp 12 KB

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