2
0

CRewardableObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "../spells/ExternalCaster.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CRandomRewardObjectInfo;
  18. class CRewardLimiter;
  19. using TRewardLimitersList = std::vector<std::shared_ptr<CRewardLimiter>>;
  20. /// Limiters of rewards. Rewards will be granted to hero only if he satisfies requirements
  21. /// Note: for this is only a test - it won't remove anything from hero (e.g. artifacts or creatures)
  22. /// NOTE: in future should (partially) replace seer hut/quest guard quests checks
  23. class DLL_LINKAGE CRewardLimiter
  24. {
  25. public:
  26. /// day of week, unused if 0, 1-7 will test for current day of week
  27. si32 dayOfWeek;
  28. si32 daysPassed;
  29. /// total experience that hero needs to have
  30. si32 heroExperience;
  31. /// level that hero needs to have
  32. si32 heroLevel;
  33. /// mana points that hero needs to have
  34. si32 manaPoints;
  35. /// percentage of mana points that hero needs to have
  36. si32 manaPercentage;
  37. /// resources player needs to have in order to trigger reward
  38. TResources resources;
  39. /// skills hero needs to have
  40. std::vector<si32> primary;
  41. std::map<SecondarySkill, si32> secondary;
  42. /// artifacts that hero needs to have (equipped or in backpack) to trigger this
  43. /// Note: does not checks for multiple copies of the same arts
  44. std::vector<ArtifactID> artifacts;
  45. /// Spells that hero must have in the spellbook
  46. std::vector<SpellID> spells;
  47. /// creatures that hero needs to have
  48. std::vector<CStackBasicDescriptor> creatures;
  49. /// sub-limiters, all must pass for this limiter to pass
  50. TRewardLimitersList allOf;
  51. /// sub-limiters, at least one should pass for this limiter to pass
  52. TRewardLimitersList anyOf;
  53. /// sub-limiters, none should pass for this limiter to pass
  54. TRewardLimitersList noneOf;
  55. CRewardLimiter():
  56. dayOfWeek(0),
  57. daysPassed(0),
  58. heroExperience(0),
  59. heroLevel(0),
  60. manaPercentage(0),
  61. manaPoints(0),
  62. primary(GameConstants::PRIMARY_SKILLS, 0)
  63. {}
  64. bool heroAllowed(const CGHeroInstance * hero) const;
  65. template <typename Handler> void serialize(Handler &h, const int version)
  66. {
  67. h & dayOfWeek;
  68. h & daysPassed;
  69. h & heroExperience;
  70. h & heroLevel;
  71. h & manaPoints;
  72. h & manaPercentage;
  73. h & resources;
  74. h & primary;
  75. h & secondary;
  76. h & artifacts;
  77. h & creatures;
  78. h & allOf;
  79. h & anyOf;
  80. h & noneOf;
  81. }
  82. };
  83. class DLL_LINKAGE CRewardResetInfo
  84. {
  85. public:
  86. CRewardResetInfo()
  87. : period(0)
  88. , visitors(false)
  89. , rewards(false)
  90. {}
  91. /// if above zero, object state will be reset each resetDuration days
  92. ui32 period;
  93. /// if true - reset list of visitors (heroes & players) on reset
  94. bool visitors;
  95. /// if true - re-randomize rewards on a new week
  96. bool rewards;
  97. template <typename Handler> void serialize(Handler &h, const int version)
  98. {
  99. h & period;
  100. h & visitors;
  101. h & rewards;
  102. }
  103. };
  104. /// Reward that can be granted to a hero
  105. /// NOTE: eventually should replace seer hut rewards and events/pandoras
  106. class DLL_LINKAGE CRewardInfo
  107. {
  108. public:
  109. /// resources that will be given to player
  110. TResources resources;
  111. /// received experience
  112. si32 heroExperience;
  113. /// received levels (converted into XP during grant)
  114. si32 heroLevel;
  115. /// mana given to/taken from hero, fixed value
  116. si32 manaDiff;
  117. /// if giving mana points puts hero above mana pool, any overflow will be multiplied by specified percentage
  118. si32 manaOverflowFactor;
  119. /// fixed value, in form of percentage from max
  120. si32 manaPercentage;
  121. /// movement points, only for current day. Bonuses should be used to grant MP on any other day
  122. si32 movePoints;
  123. /// fixed value, in form of percentage from max
  124. si32 movePercentage;
  125. /// list of bonuses, e.g. morale/luck
  126. std::vector<Bonus> bonuses;
  127. /// skills that hero may receive or lose
  128. std::vector<si32> primary;
  129. std::map<SecondarySkill, si32> secondary;
  130. /// creatures that will be changed in hero's army
  131. std::map<CreatureID, CreatureID> creaturesChange;
  132. /// objects that hero may receive
  133. std::vector<ArtifactID> artifacts;
  134. std::vector<SpellID> spells;
  135. std::vector<CStackBasicDescriptor> creatures;
  136. /// actions that hero may execute and object caster. Pair of spellID and school level
  137. std::pair<SpellID, int> spellCast;
  138. /// list of components that will be added to reward description. First entry in list will override displayed component
  139. std::vector<Component> extraComponents;
  140. /// if set to true, object will be removed after granting reward
  141. bool removeObject;
  142. /// Generates list of components that describes reward for a specific hero
  143. virtual void loadComponents(std::vector<Component> & comps,
  144. const CGHeroInstance * h) const;
  145. Component getDisplayedComponent(const CGHeroInstance * h) const;
  146. si32 calculateManaPoints(const CGHeroInstance * h) const;
  147. CRewardInfo() :
  148. heroExperience(0),
  149. heroLevel(0),
  150. manaDiff(0),
  151. manaPercentage(-1),
  152. movePoints(0),
  153. movePercentage(-1),
  154. primary(4, 0),
  155. removeObject(false),
  156. spellCast(SpellID::NONE, SecSkillLevel::NONE)
  157. {}
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & resources;
  161. h & extraComponents;
  162. h & removeObject;
  163. h & manaPercentage;
  164. h & movePercentage;
  165. h & heroExperience;
  166. h & heroLevel;
  167. h & manaDiff;
  168. h & manaOverflowFactor;
  169. h & movePoints;
  170. h & primary;
  171. h & secondary;
  172. h & bonuses;
  173. h & artifacts;
  174. h & spells;
  175. h & creatures;
  176. h & creaturesChange;
  177. if(version >= 821)
  178. h & spellCast;
  179. }
  180. };
  181. class DLL_LINKAGE CRewardVisitInfo
  182. {
  183. public:
  184. enum ERewardEventType
  185. {
  186. EVENT_INVALID,
  187. EVENT_FIRST_VISIT,
  188. EVENT_ALREADY_VISITED,
  189. EVENT_NOT_AVAILABLE
  190. };
  191. CRewardLimiter limiter;
  192. CRewardInfo reward;
  193. /// Message that will be displayed on granting of this reward, if not empty
  194. MetaString message;
  195. /// Event to which this reward is assigned
  196. ERewardEventType visitType;
  197. CRewardVisitInfo() = default;
  198. template <typename Handler> void serialize(Handler &h, const int version)
  199. {
  200. h & limiter;
  201. h & reward;
  202. h & message;
  203. h & visitType;
  204. }
  205. };
  206. namespace Rewardable
  207. {
  208. enum EVisitMode
  209. {
  210. VISIT_UNLIMITED, // any number of times. Side effect - object hover text won't contain visited/not visited text
  211. VISIT_ONCE, // only once, first to visit get all the rewards
  212. VISIT_HERO, // every hero can visit object once
  213. VISIT_BONUS, // can be visited by any hero that don't have bonus from this object
  214. VISIT_PLAYER // every player can visit object once
  215. };
  216. /// controls selection of reward granted to player
  217. enum ESelectMode
  218. {
  219. SELECT_FIRST, // first reward that matches limiters
  220. SELECT_PLAYER, // player can select from all allowed rewards
  221. SELECT_RANDOM, // one random reward from all mathing limiters
  222. };
  223. const std::array<std::string, 3> SelectModeString{"selectFirst", "selectPlayer", "selectRandom"};
  224. const std::array<std::string, 5> VisitModeString{"unlimited", "once", "hero", "bonus", "player"};
  225. /// Base class that can handle granting rewards to visiting heroes.
  226. class DLL_LINKAGE Configuration
  227. {
  228. public:
  229. /// Message that will be shown if player needs to select one of multiple rewards
  230. MetaString onSelect;
  231. /// Rewards that can be applied by an object
  232. std::vector<CRewardVisitInfo> info;
  233. /// how reward will be selected, uses ESelectMode enum
  234. ui8 selectMode = Rewardable::SELECT_FIRST;
  235. /// contols who can visit an object, uses EVisitMode enum
  236. ui8 visitMode = Rewardable::VISIT_UNLIMITED;
  237. /// how and when should the object be reset
  238. CRewardResetInfo resetParameters;
  239. /// if true - player can refuse visiting an object (e.g. Tomb)
  240. bool canRefuse = false;
  241. /// if true - object info will shown in infobox (like resource pickup)
  242. EInfoWindowMode infoWindowType = EInfoWindowMode::AUTO;
  243. EVisitMode getVisitMode() const;
  244. ui16 getResetDuration() const;
  245. /// filters list of visit info and returns rewards that can be granted to current hero
  246. virtual std::vector<ui32> getAvailableRewards(const CGHeroInstance * hero, CRewardVisitInfo::ERewardEventType event ) const;
  247. void initObj(CRandomGenerator & rand);
  248. template <typename Handler> void serialize(Handler &h, const int version)
  249. {
  250. h & info;
  251. h & canRefuse;
  252. h & resetParameters;
  253. h & onSelect;
  254. h & visitMode;
  255. h & selectMode;
  256. h & infoWindowType;
  257. }
  258. };
  259. }
  260. /// Base class that can handle granting rewards to visiting heroes.
  261. /// Inherits from CArmedInstance for proper trasfer of armies
  262. class DLL_LINKAGE CRewardableObject : public CArmedInstance
  263. {
  264. protected:
  265. Rewardable::Configuration configuration;
  266. /// function that must be called if hero got level-up during grantReward call
  267. void grantRewardAfterLevelup(const CRewardVisitInfo & reward, const CGHeroInstance * hero) const;
  268. /// grants reward to hero
  269. void grantRewardBeforeLevelup(const CRewardVisitInfo & reward, const CGHeroInstance * hero) const;
  270. /// caster to cast adveture spells, no serialize
  271. mutable spells::ExternalCaster caster;
  272. /// reward selected by player, no serialize
  273. ui16 selectedReward = 0;
  274. bool onceVisitableObjectCleared = false;
  275. public:
  276. const Rewardable::Configuration getConfiguration() const;
  277. /// return true if this object was "cleared" before and no longer has rewards applicable to selected hero
  278. /// unlike wasVisited, this method uses information not available to player owner, for example, if object was cleared by another player before
  279. bool wasVisitedBefore(const CGHeroInstance * contextHero) const;
  280. virtual void grantReward(ui32 rewardID, const CGHeroInstance * hero, bool markVisited) const;
  281. /// Visitability checks. Note that hero check includes check for hero owner (returns true if object was visited by player)
  282. bool wasVisited(PlayerColor player) const override;
  283. bool wasVisited(const CGHeroInstance * h) const override;
  284. /// gives reward to player or ask for choice in case of multiple rewards
  285. void onHeroVisit(const CGHeroInstance *h) const override;
  286. ///possibly resets object state
  287. void newTurn(CRandomGenerator & rand) const override;
  288. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  289. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  290. /// applies player selection of reward
  291. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  292. void initObj(CRandomGenerator & rand) override;
  293. CRewardableObject();
  294. void setPropertyDer(ui8 what, ui32 val) override;
  295. std::string getHoverText(PlayerColor player) const override;
  296. std::string getHoverText(const CGHeroInstance * hero) const override;
  297. template <typename Handler> void serialize(Handler &h, const int version)
  298. {
  299. h & static_cast<CArmedInstance&>(*this);
  300. h & configuration;
  301. h & onceVisitableObjectCleared;
  302. }
  303. friend class CRewardableConstructor;
  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