CRewardableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. VCMI_LIB_NAMESPACE_BEGIN
  16. class CRandomRewardObjectInfo;
  17. /// Limiters of rewards. Rewards will be granted to hero only if he satisfies requirements
  18. /// Note: for this is only a test - it won't remove anything from hero (e.g. artifacts or creatures)
  19. /// NOTE: in future should (partially) replace seer hut/quest guard quests checks
  20. class DLL_LINKAGE CRewardLimiter
  21. {
  22. public:
  23. /// how many times this reward can be granted, 0 for unlimited
  24. si32 numOfGrants;
  25. /// day of week, unused if 0, 1-7 will test for current day of week
  26. si32 dayOfWeek;
  27. /// level that hero needs to have
  28. si32 minLevel;
  29. /// resources player needs to have in order to trigger reward
  30. TResources resources;
  31. /// skills hero needs to have
  32. std::vector<si32> primary;
  33. std::map<SecondarySkill, si32> secondary;
  34. /// artifacts that hero needs to have (equipped or in backpack) to trigger this
  35. /// Note: does not checks for multiple copies of the same arts
  36. std::vector<ArtifactID> artifacts;
  37. /// creatures that hero needs to have
  38. std::vector<CStackBasicDescriptor> creatures;
  39. CRewardLimiter():
  40. numOfGrants(0),
  41. dayOfWeek(0),
  42. minLevel(0),
  43. primary(4, 0)
  44. {}
  45. bool heroAllowed(const CGHeroInstance * hero) const;
  46. template <typename Handler> void serialize(Handler &h, const int version)
  47. {
  48. h & numOfGrants;
  49. h & dayOfWeek;
  50. h & minLevel;
  51. h & resources;
  52. h & primary;
  53. h & secondary;
  54. h & artifacts;
  55. h & creatures;
  56. }
  57. };
  58. /// Reward that can be granted to a hero
  59. /// NOTE: eventually should replace seer hut rewards and events/pandoras
  60. class DLL_LINKAGE CRewardInfo
  61. {
  62. public:
  63. /// resources that will be given to player
  64. TResources resources;
  65. /// received experience
  66. ui32 gainedExp;
  67. /// received levels (converted into XP during grant)
  68. ui32 gainedLevels;
  69. /// mana given to/taken from hero, fixed value
  70. si32 manaDiff;
  71. /// fixed value, in form of percentage from max
  72. si32 manaPercentage;
  73. /// movement points, only for current day. Bonuses should be used to grant MP on any other day
  74. si32 movePoints;
  75. /// fixed value, in form of percentage from max
  76. si32 movePercentage;
  77. /// list of bonuses, e.g. morale/luck
  78. std::vector<Bonus> bonuses;
  79. /// skills that hero may receive or lose
  80. std::vector<si32> primary;
  81. std::map<SecondarySkill, si32> secondary;
  82. /// objects that hero may receive
  83. std::vector<ArtifactID> artifacts;
  84. std::vector<SpellID> spells;
  85. std::vector<CStackBasicDescriptor> creatures;
  86. /// list of components that will be added to reward description. First entry in list will override displayed component
  87. std::vector<Component> extraComponents;
  88. /// if set to true, object will be removed after granting reward
  89. bool removeObject;
  90. /// Generates list of components that describes reward for a specific hero
  91. virtual void loadComponents(std::vector<Component> & comps,
  92. const CGHeroInstance * h) const;
  93. Component getDisplayedComponent(const CGHeroInstance * h) const;
  94. CRewardInfo() :
  95. gainedExp(0),
  96. gainedLevels(0),
  97. manaDiff(0),
  98. manaPercentage(-1),
  99. movePoints(0),
  100. movePercentage(-1),
  101. primary(4, 0),
  102. removeObject(false)
  103. {}
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & resources;
  107. h & extraComponents;
  108. h & removeObject;
  109. h & manaPercentage;
  110. h & movePercentage;
  111. h & gainedExp;
  112. h & gainedLevels;
  113. h & manaDiff;
  114. h & movePoints;
  115. h & primary;
  116. h & secondary;
  117. h & bonuses;
  118. h & artifacts;
  119. h & spells;
  120. h & creatures;
  121. }
  122. };
  123. class DLL_LINKAGE CVisitInfo
  124. {
  125. public:
  126. CRewardLimiter limiter;
  127. CRewardInfo reward;
  128. /// Message that will be displayed on granting of this reward, if not empty
  129. MetaString message;
  130. /// Chance for this reward to be selected in case of random choice
  131. si32 selectChance;
  132. /// How many times this reward has been granted since last reset
  133. si32 numOfGrants;
  134. CVisitInfo():
  135. selectChance(0),
  136. numOfGrants(0)
  137. {}
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & limiter;
  141. h & reward;
  142. h & message;
  143. h & selectChance;
  144. h & numOfGrants;
  145. }
  146. };
  147. namespace Rewardable
  148. {
  149. const std::array<std::string, 3> SelectModeString{"selectFirst", "selectPlayer", "selectRandom"};
  150. const std::array<std::string, 5> VisitModeString{"unlimited", "once", "hero", "bonus", "player"};
  151. }
  152. /// Base class that can handle granting rewards to visiting heroes.
  153. /// Inherits from CArmedInstance for proper trasfer of armies
  154. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  155. {
  156. /// function that must be called if hero got level-up during grantReward call
  157. void grantRewardAfterLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  158. /// grants reward to hero
  159. void grantRewardBeforeLevelup(const CVisitInfo & reward, const CGHeroInstance * hero) const;
  160. protected:
  161. /// controls selection of reward granted to player
  162. enum ESelectMode
  163. {
  164. SELECT_FIRST, // first reward that matches limiters
  165. SELECT_PLAYER, // player can select from all allowed rewards
  166. SELECT_RANDOM // reward will be selected from allowed randomly
  167. };
  168. enum EVisitMode
  169. {
  170. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  171. VISIT_ONCE, // only once, first to visit get all the rewards
  172. VISIT_HERO, // every hero can visit object once
  173. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  174. VISIT_PLAYER // every player can visit object once
  175. };
  176. /// filters list of visit info and returns rewards that can be granted to current hero
  177. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const;
  178. virtual void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  179. virtual CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const;
  180. virtual void triggerRewardReset() const;
  181. /// Rewards that can be granted by an object
  182. std::vector<CVisitInfo> info;
  183. /// MetaString's that contain text for messages for specific situations
  184. MetaString onSelect;
  185. MetaString onVisited;
  186. MetaString onEmpty;
  187. /// how reward will be selected, uses ESelectMode enum
  188. ui8 selectMode;
  189. /// contols who can visit an object, uses EVisitMode enum
  190. ui8 visitMode;
  191. /// reward selected by player
  192. ui16 selectedReward;
  193. /// object visitability info will be reset each resetDuration days
  194. ui16 resetDuration;
  195. /// if true - player can refuse visiting an object (e.g. Tomb)
  196. bool canRefuse;
  197. public:
  198. void setPropertyDer(ui8 what, ui32 val) override;
  199. std::string getHoverText(PlayerColor player) const override;
  200. std::string getHoverText(const CGHeroInstance * hero) const override;
  201. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  202. bool wasVisited(PlayerColor player) const override;
  203. bool wasVisited(const CGHeroInstance * h) const override;
  204. /// gives reward to player or ask for choice in case of multiple rewards
  205. void onHeroVisit(const CGHeroInstance *h) const override;
  206. ///possibly resets object state
  207. void newTurn(CRandomGenerator & rand) const override;
  208. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  209. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  210. /// applies player selection of reward
  211. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  212. /// function that will be called once reward is fully granted to hero
  213. virtual void onRewardGiven(const CGHeroInstance * hero) const;
  214. void initObj(CRandomGenerator & rand) override;
  215. CRewardableObject();
  216. template <typename Handler> void serialize(Handler &h, const int version)
  217. {
  218. h & static_cast<CArmedInstance&>(*this);
  219. h & info;
  220. h & canRefuse;
  221. h & resetDuration;
  222. h & onSelect;
  223. h & onVisited;
  224. h & onEmpty;
  225. h & visitMode;
  226. h & selectMode;
  227. h & selectedReward;
  228. }
  229. // for configuration/object setup
  230. friend class CRandomRewardObjectInfo;
  231. };
  232. class DLL_LINKAGE CGPickable : public CRewardableObject //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  233. {
  234. public:
  235. void initObj(CRandomGenerator & rand) override;
  236. CGPickable();
  237. template <typename Handler> void serialize(Handler &h, const int version)
  238. {
  239. h & static_cast<CRewardableObject&>(*this);
  240. }
  241. };
  242. class DLL_LINKAGE CGBonusingObject : public CRewardableObject //objects giving bonuses to luck/morale/movement
  243. {
  244. protected:
  245. CVisitInfo getVisitInfo(int index, const CGHeroInstance *h) const override;
  246. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const override;
  247. public:
  248. void initObj(CRandomGenerator & rand) override;
  249. CGBonusingObject();
  250. void onHeroVisit(const CGHeroInstance *h) const override;
  251. bool wasVisited(const CGHeroInstance * h) const override;
  252. template <typename Handler> void serialize(Handler &h, const int version)
  253. {
  254. h & static_cast<CRewardableObject&>(*this);
  255. }
  256. };
  257. class DLL_LINKAGE CGOnceVisitable : public CRewardableObject // wagon, corpse, lean to, warriors tomb
  258. {
  259. public:
  260. void initObj(CRandomGenerator & rand) override;
  261. CGOnceVisitable();
  262. template <typename Handler> void serialize(Handler &h, const int version)
  263. {
  264. h & static_cast<CRewardableObject&>(*this);
  265. }
  266. };
  267. class DLL_LINKAGE CGVisitableOPH : public CRewardableObject //objects visitable only once per hero
  268. {
  269. public:
  270. void initObj(CRandomGenerator & rand) override;
  271. CGVisitableOPH();
  272. template <typename Handler> void serialize(Handler &h, const int version)
  273. {
  274. h & static_cast<CRewardableObject&>(*this);
  275. }
  276. };
  277. class DLL_LINKAGE CGVisitableOPW : public CRewardableObject //objects visitable once per week
  278. {
  279. protected:
  280. void triggerRewardReset() const override;
  281. public:
  282. void initObj(CRandomGenerator & rand) override;
  283. CGVisitableOPW();
  284. void setPropertyDer(ui8 what, ui32 val) override;
  285. void setRandomReward(CRandomGenerator & rand);
  286. template <typename Handler> void serialize(Handler &h, const int version)
  287. {
  288. h & static_cast<CRewardableObject&>(*this);
  289. }
  290. };
  291. ///Special case - magic spring that has two separate visitable entrances
  292. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  293. {
  294. protected:
  295. std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero) const override;
  296. public:
  297. void initObj(CRandomGenerator & rand) override;
  298. std::vector<int3> getVisitableOffsets() const;
  299. int3 getVisitableOffset() const override;
  300. template <typename Handler> void serialize(Handler &h, const int version)
  301. {
  302. h & static_cast<CGVisitableOPW&>(*this);
  303. }
  304. };
  305. //TODO:
  306. // MAX
  307. // class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  308. // class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  309. // class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  310. // class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  311. // class DLL_LINKAGE CBank : public CArmedInstance
  312. // class DLL_LINKAGE CGPyramid : public CBank
  313. // EXTRA
  314. // class DLL_LINKAGE COPWBonus : public CGTownBuilding
  315. // class DLL_LINKAGE CTownBonus : public CGTownBuilding
  316. // class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  317. // class DLL_LINKAGE CGKeymasterTent : public CGKeys
  318. // class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  319. // POSSIBLE
  320. // class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  321. // class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  322. // class DLL_LINKAGE CGScholar : public CGObjectInstance
  323. VCMI_LIB_NAMESPACE_END