CRewardableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * CRewardableObject.h, 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. #pragma once
  11. #include "CObjectHandler.h"
  12. #include "CArmedInstance.h"
  13. #include "../NetPacksBase.h"
  14. #include "../ResourceSet.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CRandomRewardObjectInfo;
  17. class CRewardLimiter;
  18. using TRewardLimitersList = std::vector<std::shared_ptr<CRewardLimiter>>;
  19. /// Limiters of rewards. Rewards will be granted to hero only if he satisfies requirements
  20. /// Note: for this is only a test - it won't remove anything from hero (e.g. artifacts or creatures)
  21. /// NOTE: in future should (partially) replace seer hut/quest guard quests checks
  22. class DLL_LINKAGE CRewardLimiter
  23. {
  24. public:
  25. /// day of week, unused if 0, 1-7 will test for current day of week
  26. si32 dayOfWeek;
  27. si32 daysPassed;
  28. /// total experience that hero needs to have
  29. si32 heroExperience;
  30. /// level that hero needs to have
  31. si32 heroLevel;
  32. /// mana points that hero needs to have
  33. si32 manaPoints;
  34. /// percentage of mana points that hero needs to have
  35. si32 manaPercentage;
  36. /// resources player needs to have in order to trigger reward
  37. TResources resources;
  38. /// skills hero needs to have
  39. std::vector<si32> primary;
  40. std::map<SecondarySkill, si32> secondary;
  41. /// artifacts that hero needs to have (equipped or in backpack) to trigger this
  42. /// Note: does not checks for multiple copies of the same arts
  43. std::vector<ArtifactID> artifacts;
  44. /// Spells that hero must have in the spellbook
  45. std::vector<SpellID> spells;
  46. /// creatures that hero needs to have
  47. std::vector<CStackBasicDescriptor> creatures;
  48. /// sub-limiters, all must pass for this limiter to pass
  49. TRewardLimitersList allOf;
  50. /// sub-limiters, at least one should pass for this limiter to pass
  51. TRewardLimitersList anyOf;
  52. /// sub-limiters, none should pass for this limiter to pass
  53. TRewardLimitersList noneOf;
  54. CRewardLimiter():
  55. dayOfWeek(0),
  56. daysPassed(0),
  57. heroExperience(0),
  58. heroLevel(0),
  59. manaPercentage(0),
  60. manaPoints(0),
  61. primary(GameConstants::PRIMARY_SKILLS, 0)
  62. {}
  63. bool heroAllowed(const CGHeroInstance * hero) const;
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & dayOfWeek;
  67. h & daysPassed;
  68. h & heroExperience;
  69. h & heroLevel;
  70. h & manaPoints;
  71. h & manaPercentage;
  72. h & resources;
  73. h & primary;
  74. h & secondary;
  75. h & artifacts;
  76. h & creatures;
  77. h & allOf;
  78. h & anyOf;
  79. h & noneOf;
  80. }
  81. };
  82. class DLL_LINKAGE CRewardResetInfo
  83. {
  84. public:
  85. CRewardResetInfo()
  86. : period(0)
  87. , visitors(false)
  88. , rewards(false)
  89. {}
  90. /// if above zero, object state will be reset each resetDuration days
  91. ui32 period;
  92. /// if true - reset list of visitors (heroes & players) on reset
  93. bool visitors;
  94. /// if true - re-randomize rewards on a new week
  95. bool rewards;
  96. template <typename Handler> void serialize(Handler &h, const int version)
  97. {
  98. h & period;
  99. h & visitors;
  100. h & rewards;
  101. }
  102. };
  103. /// Reward that can be granted to a hero
  104. /// NOTE: eventually should replace seer hut rewards and events/pandoras
  105. class DLL_LINKAGE CRewardInfo
  106. {
  107. public:
  108. /// resources that will be given to player
  109. TResources resources;
  110. /// received experience
  111. si32 heroExperience;
  112. /// received levels (converted into XP during grant)
  113. si32 heroLevel;
  114. /// mana given to/taken from hero, fixed value
  115. si32 manaDiff;
  116. /// if giving mana points puts hero above mana pool, any overflow will be multiplied by specified percentage
  117. si32 manaOverflowFactor;
  118. /// fixed value, in form of percentage from max
  119. si32 manaPercentage;
  120. /// movement points, only for current day. Bonuses should be used to grant MP on any other day
  121. si32 movePoints;
  122. /// fixed value, in form of percentage from max
  123. si32 movePercentage;
  124. /// list of bonuses, e.g. morale/luck
  125. std::vector<Bonus> bonuses;
  126. /// skills that hero may receive or lose
  127. std::vector<si32> primary;
  128. std::map<SecondarySkill, si32> secondary;
  129. /// creatures that will be changed in hero's army
  130. std::map<CreatureID, CreatureID> creaturesChange;
  131. /// objects that hero may receive
  132. std::vector<ArtifactID> artifacts;
  133. std::vector<SpellID> spells;
  134. std::vector<CStackBasicDescriptor> creatures;
  135. /// actions that hero may execute
  136. std::vector<SpellID> casts;
  137. /// list of components that will be added to reward description. First entry in list will override displayed component
  138. std::vector<Component> extraComponents;
  139. /// if set to true, object will be removed after granting reward
  140. bool removeObject;
  141. /// Generates list of components that describes reward for a specific hero
  142. virtual void loadComponents(std::vector<Component> & comps,
  143. const CGHeroInstance * h) const;
  144. Component getDisplayedComponent(const CGHeroInstance * h) const;
  145. si32 calculateManaPoints(const CGHeroInstance * h) const;
  146. CRewardInfo() :
  147. heroExperience(0),
  148. heroLevel(0),
  149. manaDiff(0),
  150. manaPercentage(-1),
  151. movePoints(0),
  152. movePercentage(-1),
  153. primary(4, 0),
  154. removeObject(false)
  155. {}
  156. template <typename Handler> void serialize(Handler &h, const int version)
  157. {
  158. h & resources;
  159. h & extraComponents;
  160. h & removeObject;
  161. h & manaPercentage;
  162. h & movePercentage;
  163. h & heroExperience;
  164. h & heroLevel;
  165. h & manaDiff;
  166. h & manaOverflowFactor;
  167. h & movePoints;
  168. h & primary;
  169. h & secondary;
  170. h & bonuses;
  171. h & artifacts;
  172. h & spells;
  173. h & creatures;
  174. h & creaturesChange;
  175. h & casts;
  176. }
  177. };
  178. class DLL_LINKAGE CRewardVisitInfo
  179. {
  180. public:
  181. enum ERewardEventType
  182. {
  183. EVENT_INVALID,
  184. EVENT_FIRST_VISIT,
  185. EVENT_ALREADY_VISITED,
  186. EVENT_NOT_AVAILABLE
  187. };
  188. CRewardLimiter limiter;
  189. CRewardInfo reward;
  190. /// Message that will be displayed on granting of this reward, if not empty
  191. MetaString message;
  192. /// Event to which this reward is assigned
  193. ERewardEventType visitType;
  194. CRewardVisitInfo() = default;
  195. template <typename Handler> void serialize(Handler &h, const int version)
  196. {
  197. h & limiter;
  198. h & reward;
  199. h & message;
  200. h & visitType;
  201. }
  202. };
  203. namespace Rewardable
  204. {
  205. const std::array<std::string, 3> SelectModeString{"selectFirst", "selectPlayer"};
  206. const std::array<std::string, 5> VisitModeString{"unlimited", "once", "hero", "bonus", "player"};
  207. }
  208. /// Base class that can handle granting rewards to visiting heroes.
  209. /// Inherits from CArmedInstance for proper trasfer of armies
  210. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  211. {
  212. /// function that must be called if hero got level-up during grantReward call
  213. void grantRewardAfterLevelup(const CRewardVisitInfo & reward, const CGHeroInstance * hero) const;
  214. /// grants reward to hero
  215. void grantRewardBeforeLevelup(const CRewardVisitInfo & reward, const CGHeroInstance * hero) const;
  216. public:
  217. enum EVisitMode
  218. {
  219. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  220. VISIT_ONCE, // only once, first to visit get all the rewards
  221. VISIT_HERO, // every hero can visit object once
  222. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  223. VISIT_PLAYER // every player can visit object once
  224. };
  225. protected:
  226. /// controls selection of reward granted to player
  227. enum ESelectMode
  228. {
  229. SELECT_FIRST, // first reward that matches limiters
  230. SELECT_PLAYER, // player can select from all allowed rewards
  231. };
  232. /// filters list of visit info and returns rewards that can be granted to current hero
  233. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero, CRewardVisitInfo::ERewardEventType event ) const;
  234. virtual void grantReward(ui32 rewardID, const CGHeroInstance * hero, bool markVisited) const;
  235. virtual void triggerReset() const;
  236. /// Message that will be shown if player needs to select one of multiple rewards
  237. MetaString onSelect;
  238. /// Rewards that can be applied by an object
  239. std::vector<CRewardVisitInfo> info;
  240. /// how reward will be selected, uses ESelectMode enum
  241. ui8 selectMode;
  242. /// contols who can visit an object, uses EVisitMode enum
  243. ui8 visitMode;
  244. /// reward selected by player
  245. ui16 selectedReward;
  246. /// how and when should the object be reset
  247. CRewardResetInfo resetParameters;
  248. /// if true - player can refuse visiting an object (e.g. Tomb)
  249. bool canRefuse;
  250. /// if true - object info will shown in infobox (like resource pickup)
  251. EInfoWindowMode infoWindowType = EInfoWindowMode::AUTO;
  252. /// return true if this object was "cleared" before and no longer has rewards applicable to selected hero
  253. /// unlike wasVisited, this method uses information not available to player owner, for example, if object was cleared by another player before
  254. bool wasVisitedBefore(const CGHeroInstance * contextHero) const;
  255. bool onceVisitableObjectCleared;
  256. public:
  257. EVisitMode getVisitMode() const;
  258. ui16 getResetDuration() const;
  259. void setPropertyDer(ui8 what, ui32 val) override;
  260. std::string getHoverText(PlayerColor player) const override;
  261. std::string getHoverText(const CGHeroInstance * hero) const override;
  262. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  263. bool wasVisited(PlayerColor player) const override;
  264. bool wasVisited(const CGHeroInstance * h) const override;
  265. /// gives reward to player or ask for choice in case of multiple rewards
  266. void onHeroVisit(const CGHeroInstance *h) const override;
  267. ///possibly resets object state
  268. void newTurn(CRandomGenerator & rand) const override;
  269. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  270. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  271. /// applies player selection of reward
  272. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  273. void initObj(CRandomGenerator & rand) override;
  274. CRewardableObject();
  275. template <typename Handler> void serialize(Handler &h, const int version)
  276. {
  277. h & static_cast<CArmedInstance&>(*this);
  278. h & info;
  279. h & canRefuse;
  280. h & resetParameters;
  281. h & onSelect;
  282. h & visitMode;
  283. h & selectMode;
  284. h & selectedReward;
  285. h & onceVisitableObjectCleared;
  286. if (version >= 817)
  287. h & infoWindowType;
  288. }
  289. // for configuration/object setup
  290. friend class CRandomRewardObjectInfo;
  291. };
  292. //TODO:
  293. // MAX
  294. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  295. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  296. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  297. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  298. // class DLL_LINKAGE CBank : public CArmedInstance
  299. // class DLL_LINKAGE CGPyramid : public CBank
  300. // EXTRA
  301. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  302. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  303. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  304. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  305. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  306. // POSSIBLE
  307. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  308. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  309. // class DLL_LINKAGE CGScholar : public CGObjectInstance
  310. VCMI_LIB_NAMESPACE_END