2
0

CQuest.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. void newTurn(CRandomGenerator & rand) const override;
  103. void onHeroVisit(const CGHeroInstance * h) const override;
  104. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  105. virtual void init(CRandomGenerator & rand);
  106. int checkDirection() const; //calculates the region of map where monster is placed
  107. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  108. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  109. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  110. void getRolloverText (MetaString &text, bool onHover) const;
  111. void afterAddToMap(CMap * map) override;
  112. template <typename Handler> void serialize(Handler &h, const int version)
  113. {
  114. h & static_cast<CRewardableObject&>(*this);
  115. h & static_cast<IQuestObject&>(*this);
  116. h & seerName;
  117. }
  118. protected:
  119. static constexpr int SEERHUT_VISITED = 10;
  120. static constexpr int SEERHUT_COMPLETE = 11;
  121. void setPropertyDer(ui8 what, ui32 val) override;
  122. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  123. };
  124. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  125. {
  126. public:
  127. void init(CRandomGenerator & rand) override;
  128. void onHeroVisit(const CGHeroInstance * h) const override;
  129. bool passableFor(PlayerColor color) const override;
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & static_cast<CGSeerHut&>(*this);
  133. }
  134. protected:
  135. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  136. };
  137. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  138. {
  139. public:
  140. static std::map <PlayerColor, std::set <ui8> > playerKeyMap; //[players][keysowned]
  141. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  142. static void reset();
  143. bool wasMyColorVisited(const PlayerColor & player) const;
  144. std::string getObjectName() const override; //depending on color
  145. std::string getHoverText(PlayerColor player) const override;
  146. template <typename Handler> void serialize(Handler &h, const int version)
  147. {
  148. h & static_cast<CGObjectInstance&>(*this);
  149. }
  150. protected:
  151. void setPropertyDer(ui8 what, ui32 val) override;
  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