2
0

CRewardableObject.cpp 12 KB

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