CQuest.h 8.1 KB

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