CRewardableObject.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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
  84. virtual void loadComponents(std::vector<Component> & comps) const;
  85. Component getDisplayedComponent() const;
  86. CRewardInfo() :
  87. gainedExp(0),
  88. gainedLevels(0),
  89. manaDiff(0),
  90. manaPercentage(-1),
  91. movePoints(0),
  92. movePercentage(-1),
  93. primary(4, 0),
  94. removeObject(false)
  95. {}
  96. template <typename Handler> void serialize(Handler &h, const int version)
  97. {
  98. h & resources & extraComponents & removeObject;
  99. h & manaPercentage & movePercentage;
  100. h & gainedExp & gainedLevels & manaDiff & movePoints;
  101. h & primary & secondary & bonuses;
  102. h & artifacts & spells & creatures;
  103. }
  104. };
  105. class DLL_LINKAGE CVisitInfo
  106. {
  107. public:
  108. CRewardLimiter limiter;
  109. CRewardInfo reward;
  110. /// Message that will be displayed on granting of this reward, if not empty
  111. MetaString message;
  112. /// Chance for this reward to be selected in case of random choice
  113. si32 selectChance;
  114. /// How many times this reward has been granted since last reset
  115. si32 numOfGrants;
  116. CVisitInfo():
  117. numOfGrants(0)
  118. {}
  119. template <typename Handler> void serialize(Handler &h, const int version)
  120. {
  121. h & limiter & reward & message & selectChance & numOfGrants;
  122. }
  123. };
  124. /// Base class that can handle granting rewards to visiting heroes.
  125. /// Inherits from CArmedInstance for proper trasfer of armies
  126. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  127. {
  128. /// function that must be called if hero got level-up during grantReward call
  129. void grantRewardAfterLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  130. /// grants reward to hero
  131. void grantRewardBeforeLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  132. protected:
  133. /// controls selection of reward granted to player
  134. enum ESelectMode
  135. {
  136. SELECT_FIRST, // first reward that matches limiters
  137. SELECT_PLAYER, // player can select from all allowed rewards
  138. SELECT_RANDOM // reward will be selected from allowed randomly
  139. };
  140. enum EVisitMode
  141. {
  142. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  143. VISIT_ONCE, // only once, first to visit get all the rewards
  144. VISIT_HERO, // every hero can visit object once
  145. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  146. VISIT_PLAYER // every player can visit object once
  147. };
  148. /// filters list of visit info and returns rewards that can be granted to current hero
  149. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const;
  150. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  151. /// Rewars that can be granted by an object
  152. std::vector<CVisitInfo> info;
  153. /// MetaString's that contain text for messages for specific situations
  154. MetaString onSelect;
  155. MetaString onVisited;
  156. MetaString onEmpty;
  157. /// sound that will be played alongside with *any* message
  158. ui16 soundID;
  159. /// how reward will be selected, uses ESelectMode enum
  160. ui8 selectMode;
  161. /// contols who can visit an object, uses EVisitMode enum
  162. ui8 visitMode;
  163. /// reward selected by player
  164. ui16 selectedReward;
  165. /// object visitability info will be reset each resetDuration days
  166. ui16 resetDuration;
  167. /// if true - player can refuse visiting an object (e.g. Tomb)
  168. bool canRefuse;
  169. public:
  170. void setPropertyDer(ui8 what, ui32 val) override;
  171. std::string getHoverText(PlayerColor player) const override;
  172. std::string getHoverText(const CGHeroInstance * hero) const override;
  173. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  174. bool wasVisited (PlayerColor player) const override;
  175. bool wasVisited (const CGHeroInstance * h) const override;
  176. /// gives reward to player or ask for choice in case of multiple rewards
  177. void onHeroVisit(const CGHeroInstance *h) const override;
  178. ///possibly resets object state
  179. void newTurn() const override;
  180. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  181. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  182. /// applies player selection of reward
  183. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  184. /// function that will be called once reward is fully granted to hero
  185. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  186. CRewardableObject();
  187. template <typename Handler> void serialize(Handler &h, const int version)
  188. {
  189. h & static_cast<CArmedInstance&>(*this);
  190. h & info & canRefuse & resetDuration;
  191. h & onSelect & onVisited & onEmpty & visitMode;
  192. h & soundID & selectMode & selectedReward;
  193. }
  194. // for configuration/object setup
  195. friend class CRandomRewardObjectInfo;
  196. };
  197. class DLL_LINKAGE CGPickable : public CRewardableObject //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  198. {
  199. public:
  200. void initObj() override;
  201. CGPickable();
  202. template <typename Handler> void serialize(Handler &h, const int version)
  203. {
  204. h & static_cast<CRewardableObject&>(*this);
  205. }
  206. };
  207. class DLL_LINKAGE CGBonusingObject : public CRewardableObject //objects giving bonuses to luck/morale/movement
  208. {
  209. public:
  210. void initObj() override;
  211. CGBonusingObject();
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & static_cast<CGObjectInstance&>(*this);
  215. }
  216. };
  217. class DLL_LINKAGE CGOnceVisitable : public CRewardableObject // wagon, corpse, lean to, warriors tomb
  218. {
  219. public:
  220. void initObj() override;
  221. CGOnceVisitable();
  222. template <typename Handler> void serialize(Handler &h, const int version)
  223. {
  224. h & static_cast<CRewardableObject&>(*this);
  225. }
  226. };
  227. class DLL_LINKAGE CGVisitableOPH : public CRewardableObject //objects visitable only once per hero
  228. {
  229. public:
  230. void initObj() override;
  231. CGVisitableOPH();
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & static_cast<CRewardableObject&>(*this);
  235. }
  236. };
  237. class DLL_LINKAGE CGVisitableOPW : public CRewardableObject //objects visitable once per week
  238. {
  239. public:
  240. void initObj() override;
  241. CGVisitableOPW();
  242. template <typename Handler> void serialize(Handler &h, const int version)
  243. {
  244. h & static_cast<CRewardableObject&>(*this);
  245. }
  246. };
  247. ///Special case - magic spring that has two separate visitable entrances
  248. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  249. {
  250. protected:
  251. std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const override;
  252. public:
  253. void initObj() override;
  254. std::vector<int3> getVisitableOffsets() const;
  255. int3 getVisitableOffset() const override;
  256. template <typename Handler> void serialize(Handler &h, const int version)
  257. {
  258. h & static_cast<CGVisitableOPW&>(*this);
  259. }
  260. };
  261. //TODO:
  262. // MAX
  263. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  264. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  265. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  266. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  267. // class DLL_LINKAGE CBank : public CArmedInstance
  268. // class DLL_LINKAGE CGPyramid : public CBank
  269. // EXTRA
  270. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  271. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  272. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  273. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  274. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  275. // POSSIBLE
  276. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  277. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  278. // class DLL_LINKAGE CGScholar : public CGObjectInstance