CRewardableObject.cpp 11 KB

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