CQuest.h 8.2 KB

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