CObjectWithReward.h 9.6 KB

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