CRewardableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. selectChance(0),
  119. numOfGrants(0)
  120. {}
  121. template <typename Handler> void serialize(Handler &h, const int version)
  122. {
  123. h & limiter & reward & message & selectChance & numOfGrants;
  124. }
  125. };
  126. /// Base class that can handle granting rewards to visiting heroes.
  127. /// Inherits from CArmedInstance for proper trasfer of armies
  128. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  129. {
  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 void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  153. virtual CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const;
  154. virtual void triggerRewardReset() const;
  155. /// Rewards that can be granted by an object
  156. std::vector<CVisitInfo> info;
  157. /// MetaString's that contain text for messages for specific situations
  158. MetaString onSelect;
  159. MetaString onVisited;
  160. MetaString onEmpty;
  161. /// sound that will be played alongside with *any* message
  162. ui16 soundID;
  163. /// how reward will be selected, uses ESelectMode enum
  164. ui8 selectMode;
  165. /// contols who can visit an object, uses EVisitMode enum
  166. ui8 visitMode;
  167. /// reward selected by player
  168. ui16 selectedReward;
  169. /// object visitability info will be reset each resetDuration days
  170. ui16 resetDuration;
  171. /// if true - player can refuse visiting an object (e.g. Tomb)
  172. bool canRefuse;
  173. public:
  174. void setPropertyDer(ui8 what, ui32 val) override;
  175. std::string getHoverText(PlayerColor player) const override;
  176. std::string getHoverText(const CGHeroInstance * hero) const override;
  177. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  178. bool wasVisited(PlayerColor player) const override;
  179. bool wasVisited(const CGHeroInstance * h) const override;
  180. /// gives reward to player or ask for choice in case of multiple rewards
  181. void onHeroVisit(const CGHeroInstance *h) const override;
  182. ///possibly resets object state
  183. void newTurn(CRandomGenerator & rand) const override;
  184. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  185. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  186. /// applies player selection of reward
  187. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  188. /// function that will be called once reward is fully granted to hero
  189. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  190. CRewardableObject();
  191. template <typename Handler> void serialize(Handler &h, const int version)
  192. {
  193. h & static_cast<CArmedInstance&>(*this);
  194. h & info & canRefuse & resetDuration;
  195. h & onSelect & onVisited & onEmpty & visitMode;
  196. h & soundID & selectMode & selectedReward;
  197. }
  198. // for configuration/object setup
  199. friend class CRandomRewardObjectInfo;
  200. };
  201. class DLL_LINKAGE CGPickable : public CRewardableObject //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  202. {
  203. public:
  204. void initObj(CRandomGenerator & rand) override;
  205. CGPickable();
  206. template <typename Handler> void serialize(Handler &h, const int version)
  207. {
  208. h & static_cast<CRewardableObject&>(*this);
  209. }
  210. };
  211. class DLL_LINKAGE CGBonusingObject : public CRewardableObject //objects giving bonuses to luck/morale/movement
  212. {
  213. protected:
  214. CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const override;
  215. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const override;
  216. public:
  217. void initObj(CRandomGenerator & rand) override;
  218. CGBonusingObject();
  219. void onHeroVisit(const CGHeroInstance *h) const override;
  220. bool wasVisited(const CGHeroInstance * h) const override;
  221. template <typename Handler> void serialize(Handler &h, const int version)
  222. {
  223. h & static_cast<CRewardableObject&>(*this);
  224. }
  225. };
  226. class DLL_LINKAGE CGOnceVisitable : public CRewardableObject // wagon, corpse, lean to, warriors tomb
  227. {
  228. public:
  229. void initObj(CRandomGenerator & rand) override;
  230. CGOnceVisitable();
  231. template <typename Handler> void serialize(Handler &h, const int version)
  232. {
  233. h & static_cast<CRewardableObject&>(*this);
  234. }
  235. };
  236. class DLL_LINKAGE CGVisitableOPH : public CRewardableObject //objects visitable only once per hero
  237. {
  238. public:
  239. void initObj(CRandomGenerator & rand) override;
  240. CGVisitableOPH();
  241. template <typename Handler> void serialize(Handler &h, const int version)
  242. {
  243. h & static_cast<CRewardableObject&>(*this);
  244. }
  245. };
  246. class DLL_LINKAGE CGVisitableOPW : public CRewardableObject //objects visitable once per week
  247. {
  248. protected:
  249. void triggerRewardReset() const override;
  250. public:
  251. void initObj(CRandomGenerator & rand) override;
  252. CGVisitableOPW();
  253. void setPropertyDer(ui8 what, ui32 val) override;
  254. void setRandomReward(CRandomGenerator & rand);
  255. template <typename Handler> void serialize(Handler &h, const int version)
  256. {
  257. h & static_cast<CRewardableObject&>(*this);
  258. }
  259. };
  260. ///Special case - magic spring that has two separate visitable entrances
  261. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  262. {
  263. protected:
  264. std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const override;
  265. public:
  266. void initObj(CRandomGenerator & rand) override;
  267. std::vector<int3> getVisitableOffsets() const;
  268. int3 getVisitableOffset() const override;
  269. template <typename Handler> void serialize(Handler &h, const int version)
  270. {
  271. h & static_cast<CGVisitableOPW&>(*this);
  272. }
  273. };
  274. //TODO:
  275. // MAX
  276. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  277. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  278. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  279. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  280. // class DLL_LINKAGE CBank : public CArmedInstance
  281. // class DLL_LINKAGE CGPyramid : public CBank
  282. // EXTRA
  283. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  284. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  285. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  286. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  287. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  288. // POSSIBLE
  289. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  290. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  291. // class DLL_LINKAGE CGScholar : public CGObjectInstance