CRewardableObject.h 11 KB

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