CRewardableObject.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /// Limiters of rewards. Rewards will be granted to hero only if he satisfies requirements
  18. /// Note: for this is only a test - it won't remove anything from hero (e.g. artifacts or creatures)
  19. /// NOTE: in future should (partially) replace seer hut/quest guard quests checks
  20. class DLL_LINKAGE CRewardLimiter
  21. {
  22. public:
  23. /// how many times this reward can be granted, 0 for unlimited
  24. si32 numOfGrants;
  25. /// day of week, unused if 0, 1-7 will test for current day of week
  26. si32 dayOfWeek;
  27. /// level that hero needs to have
  28. si32 minLevel;
  29. /// resources player needs to have in order to trigger reward
  30. TResources resources;
  31. /// skills hero needs to have
  32. std::vector<si32> primary;
  33. std::map<SecondarySkill, si32> secondary;
  34. /// artifacts that hero needs to have (equipped or in backpack) to trigger this
  35. /// Note: does not checks for multiple copies of the same arts
  36. std::vector<ArtifactID> artifacts;
  37. /// creatures that hero needs to have
  38. std::vector<CStackBasicDescriptor> creatures;
  39. CRewardLimiter():
  40. numOfGrants(0),
  41. dayOfWeek(0),
  42. minLevel(0),
  43. primary(4, 0)
  44. {}
  45. bool heroAllowed(const CGHeroInstance * hero) const;
  46. template <typename Handler> void serialize(Handler &h, const int version)
  47. {
  48. h & numOfGrants;
  49. h & dayOfWeek;
  50. h & minLevel;
  51. h & resources;
  52. h & primary;
  53. h & secondary;
  54. h & artifacts;
  55. h & creatures;
  56. }
  57. };
  58. /// Reward that can be granted to a hero
  59. /// NOTE: eventually should replace seer hut rewards and events/pandoras
  60. class DLL_LINKAGE CRewardInfo
  61. {
  62. public:
  63. /// resources that will be given to player
  64. TResources resources;
  65. /// received experience
  66. ui32 gainedExp;
  67. /// received levels (converted into XP during grant)
  68. ui32 gainedLevels;
  69. /// mana given to/taken from hero, fixed value
  70. si32 manaDiff;
  71. /// if giving mana points puts hero above mana pool, any overflow will be multiplied by specified percentage
  72. si32 manaOverflowFactor;
  73. /// fixed value, in form of percentage from max
  74. si32 manaPercentage;
  75. /// movement points, only for current day. Bonuses should be used to grant MP on any other day
  76. si32 movePoints;
  77. /// fixed value, in form of percentage from max
  78. si32 movePercentage;
  79. /// list of bonuses, e.g. morale/luck
  80. std::vector<Bonus> bonuses;
  81. /// skills that hero may receive or lose
  82. std::vector<si32> primary;
  83. std::map<SecondarySkill, si32> secondary;
  84. /// objects that hero may receive
  85. std::vector<ArtifactID> artifacts;
  86. std::vector<SpellID> spells;
  87. std::vector<CStackBasicDescriptor> creatures;
  88. /// list of components that will be added to reward description. First entry in list will override displayed component
  89. std::vector<Component> extraComponents;
  90. /// if set to true, object will be removed after granting reward
  91. bool removeObject;
  92. /// Generates list of components that describes reward for a specific hero
  93. virtual void loadComponents(std::vector<Component> & comps,
  94. const CGHeroInstance * h) const;
  95. Component getDisplayedComponent(const CGHeroInstance * h) const;
  96. CRewardInfo() :
  97. gainedExp(0),
  98. gainedLevels(0),
  99. manaDiff(0),
  100. manaPercentage(-1),
  101. movePoints(0),
  102. movePercentage(-1),
  103. primary(4, 0),
  104. removeObject(false)
  105. {}
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & resources;
  109. h & extraComponents;
  110. h & removeObject;
  111. h & manaPercentage;
  112. h & movePercentage;
  113. h & gainedExp;
  114. h & gainedLevels;
  115. h & manaDiff;
  116. h & manaOverflowFactor;
  117. h & movePoints;
  118. h & primary;
  119. h & secondary;
  120. h & bonuses;
  121. h & artifacts;
  122. h & spells;
  123. h & creatures;
  124. }
  125. };
  126. class DLL_LINKAGE CVisitInfo
  127. {
  128. public:
  129. CRewardLimiter limiter;
  130. CRewardInfo reward;
  131. /// Message that will be displayed on granting of this reward, if not empty
  132. MetaString message;
  133. /// Chance for this reward to be selected in case of random choice
  134. si32 selectChance;
  135. /// How many times this reward has been granted since last reset
  136. si32 numOfGrants;
  137. CVisitInfo():
  138. selectChance(0),
  139. numOfGrants(0)
  140. {}
  141. template <typename Handler> void serialize(Handler &h, const int version)
  142. {
  143. h & limiter;
  144. h & reward;
  145. h & message;
  146. h & selectChance;
  147. h & numOfGrants;
  148. }
  149. };
  150. namespace Rewardable
  151. {
  152. const std::array<std::string, 3> SelectModeString{"selectFirst", "selectPlayer", "selectRandom"};
  153. const std::array<std::string, 5> VisitModeString{"unlimited", "once", "hero", "bonus", "player"};
  154. }
  155. /// Base class that can handle granting rewards to visiting heroes.
  156. /// Inherits from CArmedInstance for proper trasfer of armies
  157. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  158. {
  159. /// function that must be called if hero got level-up during grantReward call
  160. void grantRewardAfterLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  161. /// grants reward to hero
  162. void grantRewardBeforeLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  163. public:
  164. enum EVisitMode
  165. {
  166. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  167. VISIT_ONCE, // only once, first to visit get all the rewards
  168. VISIT_HERO, // every hero can visit object once
  169. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  170. VISIT_PLAYER // every player can visit object once
  171. };
  172. protected:
  173. /// controls selection of reward granted to player
  174. enum ESelectMode
  175. {
  176. SELECT_FIRST, // first reward that matches limiters
  177. SELECT_PLAYER, // player can select from all allowed rewards
  178. SELECT_RANDOM // reward will be selected from allowed randomly
  179. };
  180. /// filters list of visit info and returns rewards that can be granted to current hero
  181. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const;
  182. virtual void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  183. virtual CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const;
  184. virtual void triggerRewardReset() const;
  185. /// Rewards that can be granted by an object
  186. std::vector<CVisitInfo> info;
  187. /// MetaString's that contain text for messages for specific situations
  188. MetaString onSelect;
  189. MetaString onVisited;
  190. MetaString onEmpty;
  191. /// how reward will be selected, uses ESelectMode enum
  192. ui8 selectMode;
  193. /// contols who can visit an object, uses EVisitMode enum
  194. ui8 visitMode;
  195. /// reward selected by player
  196. ui16 selectedReward;
  197. /// object visitability info will be reset each resetDuration days
  198. ui16 resetDuration;
  199. /// if true - player can refuse visiting an object (e.g. Tomb)
  200. bool canRefuse;
  201. public:
  202. EVisitMode getVisitMode() const;
  203. ui16 getResetDuration() const;
  204. void setPropertyDer(ui8 what, ui32 val) override;
  205. std::string getHoverText(PlayerColor player) const override;
  206. std::string getHoverText(const CGHeroInstance * hero) const override;
  207. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  208. bool wasVisited(PlayerColor player) const override;
  209. bool wasVisited(const CGHeroInstance * h) const override;
  210. /// gives reward to player or ask for choice in case of multiple rewards
  211. void onHeroVisit(const CGHeroInstance *h) const override;
  212. ///possibly resets object state
  213. void newTurn(CRandomGenerator & rand) const override;
  214. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  215. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  216. /// applies player selection of reward
  217. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  218. /// function that will be called once reward is fully granted to hero
  219. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  220. void initObj(CRandomGenerator & rand) override;
  221. CRewardableObject();
  222. template <typename Handler> void serialize(Handler &h, const int version)
  223. {
  224. h & static_cast<CArmedInstance&>(*this);
  225. h & info;
  226. h & canRefuse;
  227. h & resetDuration;
  228. h & onSelect;
  229. h & onVisited;
  230. h & onEmpty;
  231. h & visitMode;
  232. h & selectMode;
  233. h & selectedReward;
  234. }
  235. // for configuration/object setup
  236. friend class CRandomRewardObjectInfo;
  237. };
  238. //TODO:
  239. // MAX
  240. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  241. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  242. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  243. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  244. // class DLL_LINKAGE CBank : public CArmedInstance
  245. // class DLL_LINKAGE CGPyramid : public CBank
  246. // EXTRA
  247. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  248. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  249. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  250. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  251. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  252. // POSSIBLE
  253. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  254. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  255. // class DLL_LINKAGE CGScholar : public CGObjectInstance
  256. VCMI_LIB_NAMESPACE_END