CRewardableObject.cpp 11 KB

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