CQuest.h 8.1 KB

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