CQuest.h 8.0 KB

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