CQuest.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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, 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. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  125. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  126. };
  127. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  128. {
  129. public:
  130. void init(CRandomGenerator & rand) override;
  131. void onHeroVisit(const CGHeroInstance * h) const override;
  132. bool passableFor(PlayerColor color) const override;
  133. template <typename Handler> void serialize(Handler &h, const int version)
  134. {
  135. h & static_cast<CGSeerHut&>(*this);
  136. }
  137. protected:
  138. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  139. };
  140. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  141. {
  142. public:
  143. static std::map <PlayerColor, std::set <MapObjectSubID> > playerKeyMap; //[players][keysowned]
  144. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  145. static void reset();
  146. bool wasMyColorVisited(const PlayerColor & player) const;
  147. std::string getObjectName() const override; //depending on color
  148. std::string getHoverText(PlayerColor player) const override;
  149. template <typename Handler> void serialize(Handler &h, const int version)
  150. {
  151. h & static_cast<CGObjectInstance&>(*this);
  152. }
  153. protected:
  154. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  155. };
  156. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  157. {
  158. public:
  159. bool wasVisited (PlayerColor player) const override;
  160. void onHeroVisit(const CGHeroInstance * h) const override;
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & static_cast<CGObjectInstance&>(*this);
  164. }
  165. };
  166. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  167. {
  168. public:
  169. void initObj(CRandomGenerator & rand) override;
  170. void onHeroVisit(const CGHeroInstance * h) const override;
  171. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  172. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  173. void getRolloverText (MetaString &text, bool onHover) const;
  174. bool checkQuest (const CGHeroInstance * h) const override;
  175. void afterAddToMap(CMap * map) override;
  176. template <typename Handler> void serialize(Handler &h, const int version)
  177. {
  178. h & static_cast<IQuestObject&>(*this);
  179. h & static_cast<CGObjectInstance&>(*this);
  180. }
  181. };
  182. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  183. {
  184. public:
  185. void onHeroVisit(const CGHeroInstance * h) const override;
  186. bool passableFor(PlayerColor color) const override;
  187. template <typename Handler> void serialize(Handler &h, const int version)
  188. {
  189. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  190. }
  191. };
  192. VCMI_LIB_NAMESPACE_END