CQuest.h 7.7 KB

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