CRewardableObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
  121. case Rewardable::VISIT_BONUS:
  122. return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  123. case Rewardable::VISIT_HERO:
  124. return contextHero->visitedObjects.count(ObjectInstanceID(id));
  125. case Rewardable::VISIT_LIMITER:
  126. return configuration.visitLimiter.heroAllowed(contextHero);
  127. default:
  128. return false;
  129. }
  130. }
  131. bool CRewardableObject::wasVisited(PlayerColor player) const
  132. {
  133. switch (configuration.visitMode)
  134. {
  135. case Rewardable::VISIT_UNLIMITED:
  136. case Rewardable::VISIT_BONUS:
  137. case Rewardable::VISIT_HERO:
  138. case Rewardable::VISIT_LIMITER:
  139. return false;
  140. case Rewardable::VISIT_ONCE:
  141. case Rewardable::VISIT_PLAYER:
  142. return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
  143. default:
  144. return false;
  145. }
  146. }
  147. bool CRewardableObject::wasScouted(PlayerColor player) const
  148. {
  149. return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
  150. }
  151. bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
  152. {
  153. switch (configuration.visitMode)
  154. {
  155. case Rewardable::VISIT_BONUS:
  156. return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
  157. case Rewardable::VISIT_HERO:
  158. return h->visitedObjects.count(ObjectInstanceID(id));
  159. case Rewardable::VISIT_LIMITER:
  160. return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
  161. default:
  162. return wasVisited(h->getOwner());
  163. }
  164. }
  165. std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
  166. {
  167. std::string result = getObjectName();
  168. if (includeDescription && !getDescriptionMessage(player, hero).empty())
  169. result += "\n" + getDescriptionMessage(player, hero);
  170. if (hero)
  171. {
  172. if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
  173. {
  174. if (wasVisited(hero))
  175. result += "\n" + configuration.visitedTooltip.toString();
  176. else
  177. result += "\n " + configuration.notVisitedTooltip.toString();
  178. }
  179. }
  180. else
  181. {
  182. if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE)
  183. {
  184. if (wasVisited(player))
  185. result += "\n" + configuration.visitedTooltip.toString();
  186. else
  187. result += "\n" + configuration.notVisitedTooltip.toString();
  188. }
  189. }
  190. return result;
  191. }
  192. std::string CRewardableObject::getHoverText(PlayerColor player) const
  193. {
  194. return getDisplayTextImpl(player, nullptr, false);
  195. }
  196. std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
  197. {
  198. return getDisplayTextImpl(hero->getOwner(), hero, false);
  199. }
  200. std::string CRewardableObject::getPopupText(PlayerColor player) const
  201. {
  202. return getDisplayTextImpl(player, nullptr, true);
  203. }
  204. std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
  205. {
  206. return getDisplayTextImpl(hero->getOwner(), hero, true);
  207. }
  208. std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
  209. {
  210. if (!wasScouted(player) || configuration.info.empty())
  211. return configuration.description.toString();
  212. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  213. if (rewardIndices.empty() || !configuration.info[0].description.empty())
  214. return configuration.info[0].description.toString();
  215. if (!configuration.info[rewardIndices.front()].description.empty())
  216. return configuration.info[rewardIndices.front()].description.toString();
  217. return configuration.description.toString();
  218. }
  219. std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
  220. {
  221. if (!wasScouted(player))
  222. return {};
  223. if (isGuarded())
  224. {
  225. if (!cb->getSettings().getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
  226. return {};
  227. std::map<CreatureID, int> guardsAmounts;
  228. std::vector<Component> result;
  229. for (auto const & slot : Slots())
  230. if (slot.second)
  231. guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
  232. for (auto const & guard : guardsAmounts)
  233. {
  234. Component comp(ComponentType::CREATURE, guard.first, guard.second);
  235. result.push_back(comp);
  236. }
  237. return result;
  238. }
  239. else
  240. {
  241. if (!configuration.showScoutedPreview)
  242. return {};
  243. auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
  244. if (rewardIndices.empty() && !configuration.info.empty())
  245. {
  246. // Object has valid config, but current hero has no rewards that he can receive.
  247. // Usually this happens if hero has already visited this object -> show reward using context without any hero
  248. // since reward may be context-sensitive - e.g. Witch Hut that gives 1 skill, but always at basic level
  249. return loadComponents(nullptr, {0});
  250. }
  251. if (rewardIndices.empty())
  252. return {};
  253. return loadComponents(hero, rewardIndices);
  254. }
  255. }
  256. std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
  257. {
  258. return getPopupComponentsImpl(player, nullptr);
  259. }
  260. std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
  261. {
  262. return getPopupComponentsImpl(hero->getOwner(), hero);
  263. }
  264. void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  265. {
  266. switch (what)
  267. {
  268. case ObjProperty::REWARD_SELECT:
  269. selectedReward = identifier.getNum();
  270. break;
  271. case ObjProperty::REWARD_CLEARED:
  272. onceVisitableObjectCleared = identifier.getNum();
  273. break;
  274. }
  275. }
  276. void CRewardableObject::newTurn(vstd::RNG & rand) const
  277. {
  278. if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
  279. {
  280. if (configuration.resetParameters.rewards)
  281. {
  282. auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
  283. auto newConfiguration = handler->generateConfiguration(cb, rand, ID, configuration.variables.preset);
  284. cb->setRewardableObjectConfiguration(id, newConfiguration);
  285. }
  286. if (configuration.resetParameters.visitors)
  287. {
  288. cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
  289. ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
  290. cb->sendAndApply(cov);
  291. }
  292. }
  293. }
  294. void CRewardableObject::initObj(vstd::RNG & rand)
  295. {
  296. getObjectHandler()->configureObject(this, rand);
  297. }
  298. CRewardableObject::CRewardableObject(IGameCallback *cb)
  299. :CArmedInstance(cb)
  300. {}
  301. void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
  302. {
  303. CArmedInstance::serializeJsonOptions(handler);
  304. handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
  305. }
  306. void CRewardableObject::initializeGuards()
  307. {
  308. clearSlots();
  309. // Workaround for default creature banks strings that has placeholder for object name
  310. // TODO: find better location for this code
  311. for (auto & visitInfo : configuration.info)
  312. visitInfo.message.replaceRawString(getObjectName());
  313. for (auto const & visitInfo : configuration.info)
  314. {
  315. for (auto const & guard : visitInfo.reward.guards)
  316. {
  317. auto slotID = getFreeSlot();
  318. if (!slotID.validSlot())
  319. return;
  320. putStack(slotID, std::make_unique<CStackInstance>(cb, guard.getId(), guard.getCount()));
  321. }
  322. }
  323. }
  324. bool CRewardableObject::isCoastVisitable() const
  325. {
  326. return configuration.coastVisitable;
  327. }
  328. VCMI_LIB_NAMESPACE_END