CQuest.h 8.3 KB

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