CQuest.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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(MetaString &text, std::vector<Component> & components, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  48. virtual void getCompletionText(MetaString &text) const;
  49. virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry
  50. virtual void completeQuest(IGameCallback *, const CGHeroInstance * h) const;
  51. virtual void addTextReplacements(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;
  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. std::string seerName;
  102. void initObj(CRandomGenerator & rand) override;
  103. std::string getHoverText(PlayerColor player) const override;
  104. std::string getHoverText(const CGHeroInstance * hero) const override;
  105. std::string getPopupText(PlayerColor player) const override;
  106. std::string getPopupText(const CGHeroInstance * hero) const override;
  107. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  108. std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const override;
  109. void newTurn(CRandomGenerator & rand) const override;
  110. void onHeroVisit(const CGHeroInstance * h) const override;
  111. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  112. virtual void init(CRandomGenerator & rand);
  113. int checkDirection() const; //calculates the region of map where monster is placed
  114. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  115. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  116. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  117. void getRolloverText (MetaString &text, bool onHover) const;
  118. void afterAddToMap(CMap * map) override;
  119. template <typename Handler> void serialize(Handler &h, const int version)
  120. {
  121. h & static_cast<CRewardableObject&>(*this);
  122. h & static_cast<IQuestObject&>(*this);
  123. h & seerName;
  124. }
  125. protected:
  126. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  127. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  128. };
  129. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  130. {
  131. public:
  132. void init(CRandomGenerator & rand) override;
  133. void onHeroVisit(const CGHeroInstance * h) const override;
  134. bool passableFor(PlayerColor color) const override;
  135. template <typename Handler> void serialize(Handler &h, const int version)
  136. {
  137. h & static_cast<CGSeerHut&>(*this);
  138. }
  139. protected:
  140. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  141. };
  142. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  143. {
  144. public:
  145. bool wasMyColorVisited(const PlayerColor & player) const;
  146. std::string getObjectName() const override; //depending on color
  147. std::string getHoverText(PlayerColor player) const override;
  148. template <typename Handler> void serialize(Handler &h, const int version)
  149. {
  150. h & static_cast<CGObjectInstance&>(*this);
  151. }
  152. };
  153. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  154. {
  155. public:
  156. bool wasVisited (PlayerColor player) const override;
  157. void onHeroVisit(const CGHeroInstance * h) const override;
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & static_cast<CGObjectInstance&>(*this);
  161. }
  162. };
  163. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  164. {
  165. public:
  166. void initObj(CRandomGenerator & rand) override;
  167. void onHeroVisit(const CGHeroInstance * h) const override;
  168. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  169. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  170. void getRolloverText (MetaString &text, bool onHover) const;
  171. bool checkQuest (const CGHeroInstance * h) const override;
  172. void afterAddToMap(CMap * map) override;
  173. template <typename Handler> void serialize(Handler &h, const int version)
  174. {
  175. h & static_cast<IQuestObject&>(*this);
  176. h & static_cast<CGObjectInstance&>(*this);
  177. }
  178. };
  179. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  180. {
  181. public:
  182. void onHeroVisit(const CGHeroInstance * h) const override;
  183. bool passableFor(PlayerColor color) const override;
  184. template <typename Handler> void serialize(Handler &h, const int version)
  185. {
  186. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  187. }
  188. };
  189. VCMI_LIB_NAMESPACE_END