CRewardableObject.h 10 KB

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