CQuest.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. mutable std::unordered_map<ArtifactID, unsigned, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
  19. public:
  20. enum Emission {
  21. MISSION_NONE = 0,
  22. MISSION_LEVEL = 1,
  23. MISSION_PRIMARY_STAT = 2,
  24. MISSION_KILL_HERO = 3,
  25. MISSION_KILL_CREATURE = 4,
  26. MISSION_ART = 5,
  27. MISSION_ARMY = 6,
  28. MISSION_RESOURCES = 7,
  29. MISSION_HERO = 8,
  30. MISSION_PLAYER = 9,
  31. MISSION_HOTA_MULTI = 10,
  32. // end of H3 missions
  33. MISSION_KEYMASTER = 100,
  34. MISSION_HOTA_HERO_CLASS = 101,
  35. MISSION_HOTA_REACH_DATE = 102
  36. };
  37. enum Eprogress {
  38. NOT_ACTIVE,
  39. IN_PROGRESS,
  40. COMPLETE
  41. };
  42. static const std::string & missionName(Emission mission);
  43. static const std::string & missionState(int index);
  44. si32 qid; //unique quest id for serialization / identification
  45. Emission missionType;
  46. Eprogress progress;
  47. si32 lastDay; //after this day (first day is 0) mission cannot be completed; if -1 - no limit
  48. ui32 m13489val;
  49. std::vector<ui32> m2stats;
  50. std::vector<ArtifactID> m5arts; // artifact IDs. Add IDs through addArtifactID(), not directly to the field.
  51. std::vector<CStackBasicDescriptor> m6creatures; //pair[cre id, cre count], CreatureSet info irrelevant
  52. TResources m7resources;
  53. // following fields are used only for kill creature/hero missions, the original
  54. // objects became inaccessible after their removal, so we need to store info
  55. // needed for messages / hover text
  56. ui8 textOption;
  57. ui8 completedOption;
  58. CStackBasicDescriptor stackToKill;
  59. ui8 stackDirection;
  60. std::string heroName; //backup of hero name
  61. HeroTypeID heroPortrait;
  62. MetaString firstVisitText, nextVisitText, completedText;
  63. bool isCustomFirst;
  64. bool isCustomNext;
  65. bool isCustomComplete;
  66. CQuest(); //TODO: Remove constructor
  67. static bool checkMissionArmy(const CQuest * q, const CCreatureSet * army);
  68. virtual bool checkQuest (const CGHeroInstance * h) const; //determines whether the quest is complete or not
  69. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  70. virtual void getCompletionText(MetaString &text) const;
  71. virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry
  72. virtual void completeQuest(IGameCallback *, const CGHeroInstance * h) const;
  73. virtual void addReplacements(MetaString &out, const std::string &base) const;
  74. void addArtifactID(const ArtifactID & id);
  75. bool operator== (const CQuest & quest) const
  76. {
  77. return (quest.qid == qid);
  78. }
  79. template <typename Handler> void serialize(Handler &h, const int version)
  80. {
  81. h & qid;
  82. h & missionType;
  83. h & progress;
  84. h & lastDay;
  85. h & m13489val;
  86. h & m2stats;
  87. h & m5arts;
  88. h & m6creatures;
  89. h & m7resources;
  90. h & textOption;
  91. h & stackToKill;
  92. h & stackDirection;
  93. h & heroName;
  94. h & heroPortrait;
  95. h & firstVisitText;
  96. h & nextVisitText;
  97. h & completedText;
  98. h & isCustomFirst;
  99. h & isCustomNext;
  100. h & isCustomComplete;
  101. h & completedOption;
  102. }
  103. void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  104. };
  105. class DLL_LINKAGE IQuestObject
  106. {
  107. public:
  108. CQuest * quest = new CQuest();
  109. ///Information about quest should remain accessible even if IQuestObject removed from map
  110. ///All CQuest objects are freed in CMap destructor
  111. virtual ~IQuestObject() = default;
  112. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  113. virtual bool checkQuest (const CGHeroInstance * h) const;
  114. template <typename Handler> void serialize(Handler &h, const int version)
  115. {
  116. h & quest;
  117. }
  118. protected:
  119. void afterAddToMapCommon(CMap * map) const;
  120. };
  121. class DLL_LINKAGE CGSeerHut : public CRewardableObject, public IQuestObject
  122. {
  123. public:
  124. std::string seerName;
  125. void initObj(CRandomGenerator & rand) override;
  126. std::string getHoverText(PlayerColor player) const override;
  127. void newTurn(CRandomGenerator & rand) const override;
  128. void onHeroVisit(const CGHeroInstance * h) const override;
  129. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  130. virtual void init(CRandomGenerator & rand);
  131. int checkDirection() const; //calculates the region of map where monster is placed
  132. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  133. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  134. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  135. void getRolloverText (MetaString &text, bool onHover) const;
  136. void afterAddToMap(CMap * map) override;
  137. template <typename Handler> void serialize(Handler &h, const int version)
  138. {
  139. h & static_cast<CRewardableObject&>(*this);
  140. h & static_cast<IQuestObject&>(*this);
  141. h & seerName;
  142. }
  143. protected:
  144. static constexpr int OBJPROP_VISITED = 10;
  145. void setPropertyDer(ui8 what, ui32 val) override;
  146. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  147. };
  148. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  149. {
  150. public:
  151. void init(CRandomGenerator & rand) override;
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. h & static_cast<CGSeerHut&>(*this);
  155. }
  156. protected:
  157. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  158. };
  159. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  160. {
  161. public:
  162. static std::map <PlayerColor, std::set <ui8> > playerKeyMap; //[players][keysowned]
  163. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  164. static void reset();
  165. bool wasMyColorVisited(const PlayerColor & player) const;
  166. std::string getObjectName() const override; //depending on color
  167. std::string getHoverText(PlayerColor player) const override;
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & static_cast<CGObjectInstance&>(*this);
  171. }
  172. protected:
  173. void setPropertyDer(ui8 what, ui32 val) override;
  174. };
  175. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  176. {
  177. public:
  178. bool wasVisited (PlayerColor player) const override;
  179. void onHeroVisit(const CGHeroInstance * h) const override;
  180. template <typename Handler> void serialize(Handler &h, const int version)
  181. {
  182. h & static_cast<CGObjectInstance&>(*this);
  183. }
  184. };
  185. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  186. {
  187. public:
  188. void initObj(CRandomGenerator & rand) override;
  189. void onHeroVisit(const CGHeroInstance * h) const override;
  190. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  191. void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  192. void getRolloverText (MetaString &text, bool onHover) const;
  193. bool checkQuest (const CGHeroInstance * h) const override;
  194. void afterAddToMap(CMap * map) override;
  195. template <typename Handler> void serialize(Handler &h, const int version)
  196. {
  197. h & static_cast<IQuestObject&>(*this);
  198. h & static_cast<CGObjectInstance&>(*this);
  199. }
  200. };
  201. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  202. {
  203. public:
  204. void onHeroVisit(const CGHeroInstance * h) const override;
  205. bool passableFor(PlayerColor color) const override;
  206. template <typename Handler> void serialize(Handler &h, const int version)
  207. {
  208. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  209. }
  210. };
  211. VCMI_LIB_NAMESPACE_END