2
0

CRewardableObject.h 10 KB

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