CRewardableObject.h 10 KB

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