CQuest.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * CQuest.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 "CRewardableObject.h"
  12. #include "../ResourceSet.h"
  13. #include "../MetaString.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGCreature;
  16. enum class EQuestMission {
  17. NONE = 0,
  18. LEVEL = 1,
  19. PRIMARY_SKILL = 2,
  20. KILL_HERO = 3,
  21. KILL_CREATURE = 4,
  22. ARTIFACT = 5,
  23. ARMY = 6,
  24. RESOURCES = 7,
  25. HERO = 8,
  26. PLAYER = 9,
  27. HOTA_MULTI = 10,
  28. // end of H3 missions
  29. KEYMASTER = 11,
  30. HOTA_HERO_CLASS = 12,
  31. HOTA_REACH_DATE = 13,
  32. };
  33. class DLL_LINKAGE CQuest final
  34. {
  35. public:
  36. static const std::string & missionName(EQuestMission index);
  37. static const std::string & missionState(int index);
  38. std::string questName;
  39. si32 qid; //unique quest id for serialization / identification
  40. si32 lastDay; //after this day (first day is 0) mission cannot be completed; if -1 - no limit
  41. ObjectInstanceID killTarget;
  42. Rewardable::Limiter mission;
  43. bool repeatedQuest;
  44. bool isCompleted;
  45. std::set<PlayerColor> activeForPlayers;
  46. // following fields are used only for kill creature/hero missions, the original
  47. // objects became inaccessible after their removal, so we need to store info
  48. // needed for messages / hover text
  49. ui8 textOption;
  50. ui8 completedOption;
  51. CreatureID stackToKill;
  52. ui8 stackDirection;
  53. std::string heroName; //backup of hero name
  54. HeroTypeID heroPortrait;
  55. MetaString firstVisitText;
  56. MetaString nextVisitText;
  57. MetaString completedText;
  58. bool isCustomFirst;
  59. bool isCustomNext;
  60. bool isCustomComplete;
  61. CQuest(); //TODO: Remove constructor
  62. static bool checkMissionArmy(const CQuest * q, const CCreatureSet * army);
  63. virtual bool checkQuest(const CGHeroInstance * h) const; //determines whether the quest is complete or not
  64. virtual void getVisitText(IGameCallback * cb, MetaString &text, std::vector<Component> & components, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  65. virtual void getCompletionText(IGameCallback * cb, MetaString &text) const;
  66. virtual void getRolloverText (IGameCallback * cb, MetaString &text, bool onHover) const; //hover or quest log entry
  67. virtual void completeQuest(IGameCallback *, const CGHeroInstance * h) const;
  68. virtual void addTextReplacements(IGameCallback * cb, MetaString &out, std::vector<Component> & components) const;
  69. virtual void addKillTargetReplacements(MetaString &out) const;
  70. void defineQuestName();
  71. bool operator== (const CQuest & quest) const
  72. {
  73. return (quest.qid == qid);
  74. }
  75. template <typename Handler> void serialize(Handler &h)
  76. {
  77. h & qid;
  78. h & isCompleted;
  79. h & activeForPlayers;
  80. h & lastDay;
  81. h & textOption;
  82. h & stackToKill;
  83. h & stackDirection;
  84. h & heroName;
  85. h & heroPortrait;
  86. h & firstVisitText;
  87. h & nextVisitText;
  88. h & completedText;
  89. h & isCustomFirst;
  90. h & isCustomNext;
  91. h & isCustomComplete;
  92. h & completedOption;
  93. h & questName;
  94. h & mission;
  95. h & killTarget;
  96. }
  97. void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  98. };
  99. class DLL_LINKAGE IQuestObject
  100. {
  101. public:
  102. CQuest * quest = new CQuest();
  103. ///Information about quest should remain accessible even if IQuestObject removed from map
  104. ///All CQuest objects are freed in CMap destructor
  105. virtual ~IQuestObject() = default;
  106. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const = 0;
  107. virtual bool checkQuest (const CGHeroInstance * h) const;
  108. template <typename Handler> void serialize(Handler &h)
  109. {
  110. h & quest;
  111. }
  112. protected:
  113. void afterAddToMapCommon(CMap * map) const;
  114. };
  115. class DLL_LINKAGE CGSeerHut : public CRewardableObject, public IQuestObject
  116. {
  117. public:
  118. using CRewardableObject::CRewardableObject;
  119. std::string seerName;
  120. void initObj(CRandomGenerator & rand) override;
  121. std::string getHoverText(PlayerColor player) const override;
  122. std::string getHoverText(const CGHeroInstance * hero) const override;
  123. std::string getPopupText(PlayerColor player) const override;
  124. std::string getPopupText(const CGHeroInstance * hero) const override;
  125. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  126. std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const override;
  127. void newTurn(CRandomGenerator & rand) const override;
  128. void onHeroVisit(const CGHeroInstance * h) const override;
  129. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  130. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  131. virtual void init(CRandomGenerator & rand);
  132. int checkDirection() const; //calculates the region of map where monster is placed
  133. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  134. const CGHeroInstance *getHeroToKill(bool allowNull) const;
  135. const CGCreature *getCreatureToKill(bool allowNull) const;
  136. void getRolloverText (MetaString &text, bool onHover) const;
  137. void afterAddToMap(CMap * map) override;
  138. template <typename Handler> void serialize(Handler &h)
  139. {
  140. h & static_cast<CRewardableObject&>(*this);
  141. h & static_cast<IQuestObject&>(*this);
  142. h & seerName;
  143. }
  144. protected:
  145. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  146. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  147. };
  148. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  149. {
  150. public:
  151. using CGSeerHut::CGSeerHut;
  152. void init(CRandomGenerator & rand) override;
  153. void onHeroVisit(const CGHeroInstance * h) const override;
  154. bool passableFor(PlayerColor color) const override;
  155. template <typename Handler> void serialize(Handler &h)
  156. {
  157. h & static_cast<CGSeerHut&>(*this);
  158. }
  159. protected:
  160. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  161. };
  162. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  163. {
  164. public:
  165. using CGObjectInstance::CGObjectInstance;
  166. bool wasMyColorVisited(const PlayerColor & player) const;
  167. std::string getObjectName() const override; //depending on color
  168. std::string getHoverText(PlayerColor player) const override;
  169. template <typename Handler> void serialize(Handler &h)
  170. {
  171. h & static_cast<CGObjectInstance&>(*this);
  172. }
  173. };
  174. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  175. {
  176. public:
  177. using CGKeys::CGKeys;
  178. bool wasVisited (PlayerColor player) const override;
  179. void onHeroVisit(const CGHeroInstance * h) const override;
  180. template <typename Handler> void serialize(Handler &h)
  181. {
  182. h & static_cast<CGObjectInstance&>(*this);
  183. }
  184. };
  185. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  186. {
  187. public:
  188. using CGKeys::CGKeys;
  189. void initObj(CRandomGenerator & rand) override;
  190. void onHeroVisit(const CGHeroInstance * h) const override;
  191. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  192. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  193. void getRolloverText (MetaString &text, bool onHover) const;
  194. bool checkQuest (const CGHeroInstance * h) const override;
  195. void afterAddToMap(CMap * map) override;
  196. template <typename Handler> void serialize(Handler &h)
  197. {
  198. h & static_cast<IQuestObject&>(*this);
  199. h & static_cast<CGObjectInstance&>(*this);
  200. }
  201. };
  202. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  203. {
  204. public:
  205. using CGBorderGuard::CGBorderGuard;
  206. void onHeroVisit(const CGHeroInstance * h) const override;
  207. bool passableFor(PlayerColor color) const override;
  208. template <typename Handler> void serialize(Handler &h)
  209. {
  210. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  211. }
  212. };
  213. VCMI_LIB_NAMESPACE_END