CQuest.h 7.5 KB

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