CRewardableObject.h 11 KB

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