CRewardableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. namespace Rewardable
  147. {
  148. const std::array<std::string, 3> SelectModeString{"selectFirst", "selectPlayer", "selectRandom"};
  149. const std::array<std::string, 5> VisitModeString{"unlimited", "once", "hero", "bonus", "player"};
  150. }
  151. /// Base class that can handle granting rewards to visiting heroes.
  152. /// Inherits from CArmedInstance for proper trasfer of armies
  153. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  154. {
  155. /// function that must be called if hero got level-up during grantReward call
  156. void grantRewardAfterLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  157. /// grants reward to hero
  158. void grantRewardBeforeLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  159. protected:
  160. /// controls selection of reward granted to player
  161. enum ESelectMode
  162. {
  163. SELECT_FIRST, // first reward that matches limiters
  164. SELECT_PLAYER, // player can select from all allowed rewards
  165. SELECT_RANDOM // reward will be selected from allowed randomly
  166. };
  167. enum EVisitMode
  168. {
  169. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  170. VISIT_ONCE, // only once, first to visit get all the rewards
  171. VISIT_HERO, // every hero can visit object once
  172. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  173. VISIT_PLAYER // every player can visit object once
  174. };
  175. /// filters list of visit info and returns rewards that can be granted to current hero
  176. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const;
  177. virtual void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  178. virtual CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const;
  179. virtual void triggerRewardReset() const;
  180. /// Rewards that can be granted by an object
  181. std::vector<CVisitInfo> info;
  182. /// MetaString's that contain text for messages for specific situations
  183. MetaString onSelect;
  184. MetaString onVisited;
  185. MetaString onEmpty;
  186. /// how reward will be selected, uses ESelectMode enum
  187. ui8 selectMode;
  188. /// contols who can visit an object, uses EVisitMode enum
  189. ui8 visitMode;
  190. /// reward selected by player
  191. ui16 selectedReward;
  192. /// object visitability info will be reset each resetDuration days
  193. ui16 resetDuration;
  194. /// if true - player can refuse visiting an object (e.g. Tomb)
  195. bool canRefuse;
  196. public:
  197. void setPropertyDer(ui8 what, ui32 val) override;
  198. std::string getHoverText(PlayerColor player) const override;
  199. std::string getHoverText(const CGHeroInstance * hero) const override;
  200. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  201. bool wasVisited(PlayerColor player) const override;
  202. bool wasVisited(const CGHeroInstance * h) const override;
  203. /// gives reward to player or ask for choice in case of multiple rewards
  204. void onHeroVisit(const CGHeroInstance *h) const override;
  205. ///possibly resets object state
  206. void newTurn(CRandomGenerator & rand) const override;
  207. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  208. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  209. /// applies player selection of reward
  210. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  211. /// function that will be called once reward is fully granted to hero
  212. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  213. void initObj(CRandomGenerator & rand) override;
  214. CRewardableObject();
  215. template <typename Handler> void serialize(Handler &h, const int version)
  216. {
  217. h & static_cast<CArmedInstance&>(*this);
  218. h & info;
  219. h & canRefuse;
  220. h & resetDuration;
  221. h & onSelect;
  222. h & onVisited;
  223. h & onEmpty;
  224. h & visitMode;
  225. h & selectMode;
  226. h & selectedReward;
  227. }
  228. // for configuration/object setup
  229. friend class CRandomRewardObjectInfo;
  230. };
  231. class DLL_LINKAGE CGPickable : public CRewardableObject //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  232. {
  233. public:
  234. void initObj(CRandomGenerator & rand) override;
  235. CGPickable();
  236. template <typename Handler> void serialize(Handler &h, const int version)
  237. {
  238. h & static_cast<CRewardableObject&>(*this);
  239. }
  240. };
  241. class DLL_LINKAGE CGBonusingObject : public CRewardableObject //objects giving bonuses to luck/morale/movement
  242. {
  243. protected:
  244. CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const override;
  245. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const override;
  246. public:
  247. void initObj(CRandomGenerator & rand) override;
  248. CGBonusingObject();
  249. void onHeroVisit(const CGHeroInstance *h) const override;
  250. bool wasVisited(const CGHeroInstance * h) const override;
  251. template <typename Handler> void serialize(Handler &h, const int version)
  252. {
  253. h & static_cast<CRewardableObject&>(*this);
  254. }
  255. };
  256. class DLL_LINKAGE CGOnceVisitable : public CRewardableObject // wagon, corpse, lean to, warriors tomb
  257. {
  258. public:
  259. void initObj(CRandomGenerator & rand) override;
  260. CGOnceVisitable();
  261. template <typename Handler> void serialize(Handler &h, const int version)
  262. {
  263. h & static_cast<CRewardableObject&>(*this);
  264. }
  265. };
  266. class DLL_LINKAGE CGVisitableOPH : public CRewardableObject //objects visitable only once per hero
  267. {
  268. public:
  269. void initObj(CRandomGenerator & rand) override;
  270. CGVisitableOPH();
  271. template <typename Handler> void serialize(Handler &h, const int version)
  272. {
  273. h & static_cast<CRewardableObject&>(*this);
  274. }
  275. };
  276. class DLL_LINKAGE CGVisitableOPW : public CRewardableObject //objects visitable once per week
  277. {
  278. protected:
  279. void triggerRewardReset() const override;
  280. public:
  281. void initObj(CRandomGenerator & rand) override;
  282. CGVisitableOPW();
  283. void setPropertyDer(ui8 what, ui32 val) override;
  284. void setRandomReward(CRandomGenerator & rand);
  285. template <typename Handler> void serialize(Handler &h, const int version)
  286. {
  287. h & static_cast<CRewardableObject&>(*this);
  288. }
  289. };
  290. ///Special case - magic spring that has two separate visitable entrances
  291. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  292. {
  293. protected:
  294. std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const override;
  295. public:
  296. void initObj(CRandomGenerator & rand) override;
  297. std::vector<int3> getVisitableOffsets() const;
  298. int3 getVisitableOffset() const override;
  299. template <typename Handler> void serialize(Handler &h, const int version)
  300. {
  301. h & static_cast<CGVisitableOPW&>(*this);
  302. }
  303. };
  304. //TODO:
  305. // MAX
  306. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  307. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  308. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  309. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  310. // class DLL_LINKAGE CBank : public CArmedInstance
  311. // class DLL_LINKAGE CGPyramid : public CBank
  312. // EXTRA
  313. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  314. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  315. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  316. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  317. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  318. // POSSIBLE
  319. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  320. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  321. // class DLL_LINKAGE CGScholar : public CGObjectInstance