CRewardableObject.h 11 KB

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