CQuest.h 8.4 KB

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