CRewardableObject.cpp 11 KB

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