CRewardableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /// how reward will be selected, uses ESelectMode enum
  182. ui8 selectMode;
  183. /// contols who can visit an object, uses EVisitMode enum
  184. ui8 visitMode;
  185. /// reward selected by player
  186. ui16 selectedReward;
  187. /// object visitability info will be reset each resetDuration days
  188. ui16 resetDuration;
  189. /// if true - player can refuse visiting an object (e.g. Tomb)
  190. bool canRefuse;
  191. public:
  192. void setPropertyDer(ui8 what, ui32 val) override;
  193. std::string getHoverText(PlayerColor player) const override;
  194. std::string getHoverText(const CGHeroInstance * hero) const override;
  195. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  196. bool wasVisited(PlayerColor player) const override;
  197. bool wasVisited(const CGHeroInstance * h) const override;
  198. /// gives reward to player or ask for choice in case of multiple rewards
  199. void onHeroVisit(const CGHeroInstance *h) const override;
  200. ///possibly resets object state
  201. void newTurn(CRandomGenerator & rand) const override;
  202. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  203. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  204. /// applies player selection of reward
  205. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  206. /// function that will be called once reward is fully granted to hero
  207. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  208. CRewardableObject();
  209. template <typename Handler> void serialize(Handler &h, const int version)
  210. {
  211. h & static_cast<CArmedInstance&>(*this);
  212. h & info;
  213. h & canRefuse;
  214. h & resetDuration;
  215. h & onSelect;
  216. h & onVisited;
  217. h & onEmpty;
  218. h & visitMode;
  219. if(version < 778)
  220. {
  221. ui16 soundID = 0;
  222. h & soundID;
  223. }
  224. h & selectMode;
  225. h & selectedReward;
  226. }
  227. // for configuration/object setup
  228. friend class CRandomRewardObjectInfo;
  229. };
  230. class DLL_LINKAGE CGPickable : public CRewardableObject //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  231. {
  232. public:
  233. void initObj(CRandomGenerator & rand) override;
  234. CGPickable();
  235. template <typename Handler> void serialize(Handler &h, const int version)
  236. {
  237. h & static_cast<CRewardableObject&>(*this);
  238. }
  239. };
  240. class DLL_LINKAGE CGBonusingObject : public CRewardableObject //objects giving bonuses to luck/morale/movement
  241. {
  242. protected:
  243. CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const override;
  244. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const override;
  245. public:
  246. void initObj(CRandomGenerator & rand) override;
  247. CGBonusingObject();
  248. void onHeroVisit(const CGHeroInstance *h) const override;
  249. bool wasVisited(const CGHeroInstance * h) const override;
  250. template <typename Handler> void serialize(Handler &h, const int version)
  251. {
  252. h & static_cast<CRewardableObject&>(*this);
  253. }
  254. };
  255. class DLL_LINKAGE CGOnceVisitable : public CRewardableObject // wagon, corpse, lean to, warriors tomb
  256. {
  257. public:
  258. void initObj(CRandomGenerator & rand) override;
  259. CGOnceVisitable();
  260. template <typename Handler> void serialize(Handler &h, const int version)
  261. {
  262. h & static_cast<CRewardableObject&>(*this);
  263. }
  264. };
  265. class DLL_LINKAGE CGVisitableOPH : public CRewardableObject //objects visitable only once per hero
  266. {
  267. public:
  268. void initObj(CRandomGenerator & rand) override;
  269. CGVisitableOPH();
  270. template <typename Handler> void serialize(Handler &h, const int version)
  271. {
  272. h & static_cast<CRewardableObject&>(*this);
  273. }
  274. };
  275. class DLL_LINKAGE CGVisitableOPW : public CRewardableObject //objects visitable once per week
  276. {
  277. protected:
  278. void triggerRewardReset() const override;
  279. public:
  280. void initObj(CRandomGenerator & rand) override;
  281. CGVisitableOPW();
  282. void setPropertyDer(ui8 what, ui32 val) override;
  283. void setRandomReward(CRandomGenerator & rand);
  284. template <typename Handler> void serialize(Handler &h, const int version)
  285. {
  286. h & static_cast<CRewardableObject&>(*this);
  287. }
  288. };
  289. ///Special case - magic spring that has two separate visitable entrances
  290. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  291. {
  292. protected:
  293. std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const override;
  294. public:
  295. void initObj(CRandomGenerator & rand) override;
  296. std::vector<int3> getVisitableOffsets() const;
  297. int3 getVisitableOffset() const override;
  298. template <typename Handler> void serialize(Handler &h, const int version)
  299. {
  300. h & static_cast<CGVisitableOPW&>(*this);
  301. }
  302. };
  303. //TODO:
  304. // MAX
  305. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  306. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  307. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  308. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  309. // class DLL_LINKAGE CBank : public CArmedInstance
  310. // class DLL_LINKAGE CGPyramid : public CBank
  311. // EXTRA
  312. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  313. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  314. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  315. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  316. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  317. // POSSIBLE
  318. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  319. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  320. // class DLL_LINKAGE CGScholar : public CGObjectInstance