CQuest.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. CStackBasicDescriptor stackToKill;
  35. ui8 stackDirection;
  36. std::string heroName; //backup of hero name
  37. HeroTypeID heroPortrait;
  38. MetaString firstVisitText, nextVisitText, completedText;
  39. bool isCustomFirst;
  40. bool isCustomNext;
  41. bool isCustomComplete;
  42. CQuest(); //TODO: Remove constructor
  43. static bool checkMissionArmy(const CQuest * q, const CCreatureSet * army);
  44. virtual bool checkQuest(const CGHeroInstance * h) const; //determines whether the quest is complete or not
  45. virtual void getVisitText(MetaString &text, std::vector<Component> & components, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  46. virtual void getCompletionText(MetaString &text) const;
  47. virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry
  48. virtual void completeQuest(IGameCallback *, const CGHeroInstance * h) const;
  49. virtual void addTextReplacements(MetaString &out, std::vector<Component> & components) const;
  50. virtual void addKillTargetReplacements(MetaString &out) const;
  51. void defineQuestName();
  52. bool operator== (const CQuest & quest) const
  53. {
  54. return (quest.qid == qid);
  55. }
  56. template <typename Handler> void serialize(Handler &h, const int version)
  57. {
  58. h & qid;
  59. h & isCompleted;
  60. h & activeForPlayers;
  61. h & lastDay;
  62. h & textOption;
  63. h & stackToKill;
  64. h & stackDirection;
  65. h & heroName;
  66. h & heroPortrait;
  67. h & firstVisitText;
  68. h & nextVisitText;
  69. h & completedText;
  70. h & isCustomFirst;
  71. h & isCustomNext;
  72. h & isCustomComplete;
  73. h & completedOption;
  74. h & questName;
  75. h & mission;
  76. h & killTarget;
  77. }
  78. void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  79. };
  80. class DLL_LINKAGE IQuestObject
  81. {
  82. public:
  83. CQuest * quest = new CQuest();
  84. ///Information about quest should remain accessible even if IQuestObject removed from map
  85. ///All CQuest objects are freed in CMap destructor
  86. virtual ~IQuestObject() = default;
  87. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  88. virtual bool checkQuest (const CGHeroInstance * h) const;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & quest;
  92. }
  93. protected:
  94. void afterAddToMapCommon(CMap * map) const;
  95. };
  96. class DLL_LINKAGE CGSeerHut : public CRewardableObject, public IQuestObject
  97. {
  98. public:
  99. std::string seerName;
  100. void initObj(CRandomGenerator & rand) override;
  101. std::string getHoverText(PlayerColor player) const override;
  102. std::string getHoverText(const CGHeroInstance * hero) const override;
  103. std::string getPopupText(PlayerColor player) const override;
  104. std::string getPopupText(const CGHeroInstance * hero) const override;
  105. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  106. std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const override;
  107. void newTurn(CRandomGenerator & rand) const override;
  108. void onHeroVisit(const CGHeroInstance * h) const override;
  109. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  110. virtual void init(CRandomGenerator & rand);
  111. int checkDirection() const; //calculates the region of map where monster is placed
  112. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  113. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  114. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  115. void getRolloverText (MetaString &text, bool onHover) const;
  116. void afterAddToMap(CMap * map) override;
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & static_cast<CRewardableObject&>(*this);
  120. h & static_cast<IQuestObject&>(*this);
  121. h & seerName;
  122. }
  123. protected:
  124. static constexpr int SEERHUT_VISITED = 10;
  125. static constexpr int SEERHUT_COMPLETE = 11;
  126. void setPropertyDer(ui8 what, ui32 val) 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. static std::map <PlayerColor, std::set <MapObjectSubID> > playerKeyMap; //[players][keysowned]
  146. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  147. static void reset();
  148. bool wasMyColorVisited(const PlayerColor & player) const;
  149. std::string getObjectName() const override; //depending on color
  150. std::string getHoverText(PlayerColor player) const override;
  151. template <typename Handler> void serialize(Handler &h, const int version)
  152. {
  153. h & static_cast<CGObjectInstance&>(*this);
  154. }
  155. protected:
  156. void setPropertyDer(ui8 what, ui32 val) override;
  157. };
  158. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  159. {
  160. public:
  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. void initObj(CRandomGenerator & rand) override;
  172. void onHeroVisit(const CGHeroInstance * h) const override;
  173. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  174. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  175. void getRolloverText (MetaString &text, bool onHover) const;
  176. bool checkQuest (const CGHeroInstance * h) const override;
  177. void afterAddToMap(CMap * map) override;
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & static_cast<IQuestObject&>(*this);
  181. h & static_cast<CGObjectInstance&>(*this);
  182. }
  183. };
  184. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  185. {
  186. public:
  187. void onHeroVisit(const CGHeroInstance * h) const override;
  188. bool passableFor(PlayerColor color) const override;
  189. template <typename Handler> void serialize(Handler &h, const int version)
  190. {
  191. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  192. }
  193. };
  194. VCMI_LIB_NAMESPACE_END