CObjectWithReward.h 9.1 KB

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