CQuest.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "CRewardableObject.h"
  12. #include "../ResourceSet.h"
  13. #include "../serializer/Serializeable.h"
  14. #include "../texts/MetaString.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CGCreature;
  17. enum class EQuestMission {
  18. NONE = 0,
  19. LEVEL = 1,
  20. PRIMARY_SKILL = 2,
  21. KILL_HERO = 3,
  22. KILL_CREATURE = 4,
  23. ARTIFACT = 5,
  24. ARMY = 6,
  25. RESOURCES = 7,
  26. HERO = 8,
  27. PLAYER = 9,
  28. HOTA_MULTI = 10,
  29. // end of H3 missions
  30. KEYMASTER = 11,
  31. HOTA_HERO_CLASS = 12,
  32. HOTA_REACH_DATE = 13,
  33. HOTA_GAME_DIFFICULTY = 13,
  34. };
  35. class DLL_LINKAGE CQuest final : public Serializeable
  36. {
  37. public:
  38. static const std::string & missionName(EQuestMission index);
  39. static const std::string & missionState(int index);
  40. std::string questName;
  41. QuestInstanceID qid;
  42. si32 lastDay; //after this day (first day is 0) mission cannot be completed; if -1 - no limit
  43. ObjectInstanceID killTarget;
  44. Rewardable::Limiter mission;
  45. bool repeatedQuest;
  46. bool isCompleted;
  47. std::set<PlayerColor> activeForPlayers;
  48. // following fields are used only for kill creature/hero missions, the original
  49. // objects became inaccessible after their removal, so we need to store info
  50. // needed for messages / hover text
  51. ui8 textOption;
  52. ui8 completedOption;
  53. CreatureID stackToKill;
  54. ui8 stackDirection;
  55. std::string heroName; //backup of hero name
  56. HeroTypeID heroPortrait;
  57. MetaString firstVisitText;
  58. MetaString nextVisitText;
  59. MetaString completedText;
  60. bool isCustomFirst;
  61. bool isCustomNext;
  62. bool isCustomComplete;
  63. CQuest(); //TODO: Remove constructor
  64. static bool checkMissionArmy(const CQuest * q, const CCreatureSet * army);
  65. bool checkQuest(const CGHeroInstance * h) const; //determines whether the quest is complete or not
  66. void getVisitText(const IGameInfoCallback * cb, MetaString &text, std::vector<Component> & components, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  67. void getCompletionText(const IGameInfoCallback * cb, MetaString &text) const;
  68. void getRolloverText (const IGameInfoCallback * cb, MetaString &text, bool onHover) const; //hover or quest log entry
  69. void completeQuest(IGameEventCallback & gameEvents, const CGHeroInstance * h, bool allowFullArmyRemoval) const;
  70. void addTextReplacements(const IGameInfoCallback * cb, MetaString &out, std::vector<Component> & components) const;
  71. void addKillTargetReplacements(MetaString &out) const;
  72. void defineQuestName();
  73. bool operator== (const CQuest & quest) const
  74. {
  75. return (quest.qid == qid);
  76. }
  77. template <typename Handler> void serialize(Handler &h)
  78. {
  79. h & qid;
  80. h & isCompleted;
  81. h & activeForPlayers;
  82. h & lastDay;
  83. h & textOption;
  84. h & stackToKill;
  85. h & stackDirection;
  86. h & heroName;
  87. h & heroPortrait;
  88. h & firstVisitText;
  89. h & nextVisitText;
  90. h & completedText;
  91. h & isCustomFirst;
  92. h & isCustomNext;
  93. h & isCustomComplete;
  94. h & completedOption;
  95. h & questName;
  96. h & mission;
  97. h & killTarget;
  98. }
  99. void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  100. };
  101. class DLL_LINKAGE IQuestObject
  102. {
  103. std::shared_ptr<CQuest> quest; // TODO: not actually shared, replace with unique_ptr once 1.6 save compat is not needed
  104. public:
  105. IQuestObject();
  106. virtual ~IQuestObject();
  107. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const = 0;
  108. virtual bool checkQuest (const CGHeroInstance * h) const;
  109. virtual const CQuest & getQuest() const { return *quest; }
  110. virtual CQuest & getQuest() { return *quest; }
  111. template <typename Handler> void serialize(Handler &h)
  112. {
  113. h & quest;
  114. }
  115. };
  116. class DLL_LINKAGE CGSeerHut : public CRewardableObject, public IQuestObject
  117. {
  118. public:
  119. using CRewardableObject::CRewardableObject;
  120. std::string seerName;
  121. void initObj(IGameRandomizer & gameRandomizer) override;
  122. std::string getHoverText(PlayerColor player) const override;
  123. std::string getHoverText(const CGHeroInstance * hero) const override;
  124. std::string getPopupText(PlayerColor player) const override;
  125. std::string getPopupText(const CGHeroInstance * hero) const override;
  126. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  127. std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const override;
  128. void newTurn(IGameEventCallback & gameEvents, IGameRandomizer & gameRandomizer) const override;
  129. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  130. void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
  131. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  132. virtual void init(vstd::RNG & rand);
  133. int checkDirection() const; //calculates the region of map where monster is placed
  134. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  135. const CGHeroInstance *getHeroToKill(bool allowNull) const;
  136. const CGCreature *getCreatureToKill(bool allowNull) const;
  137. void getRolloverText (MetaString &text, bool onHover) const;
  138. template <typename Handler> void serialize(Handler &h)
  139. {
  140. h & static_cast<CRewardableObject&>(*this);
  141. h & static_cast<IQuestObject&>(*this);
  142. h & seerName;
  143. }
  144. protected:
  145. bool allowsFullArmyRemoval() const;
  146. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  147. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  148. };
  149. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  150. {
  151. public:
  152. using CGSeerHut::CGSeerHut;
  153. void init(vstd::RNG & rand) override;
  154. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  155. bool passableFor(PlayerColor color) const override;
  156. template <typename Handler> void serialize(Handler &h)
  157. {
  158. h & static_cast<CGSeerHut&>(*this);
  159. }
  160. protected:
  161. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  162. };
  163. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  164. {
  165. public:
  166. using CGObjectInstance::CGObjectInstance;
  167. bool wasMyColorVisited(const PlayerColor & player) const;
  168. std::string getObjectName() const override;
  169. std::string getObjectDescription(PlayerColor player) const;
  170. std::string getHoverText(PlayerColor player) const override;
  171. template <typename Handler> void serialize(Handler &h)
  172. {
  173. h & static_cast<CGObjectInstance&>(*this);
  174. }
  175. };
  176. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  177. {
  178. public:
  179. using CGKeys::CGKeys;
  180. bool wasVisited (PlayerColor player) const override;
  181. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  182. template <typename Handler> void serialize(Handler &h)
  183. {
  184. h & static_cast<CGObjectInstance&>(*this);
  185. }
  186. };
  187. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  188. {
  189. QuestInstanceID qid;
  190. public:
  191. using CGKeys::CGKeys;
  192. void initObj(IGameRandomizer & gameRandomizer) override;
  193. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  194. void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
  195. void getVisitText (MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h = nullptr) const override;
  196. void getRolloverText (MetaString &text, bool onHover) const;
  197. bool checkQuest (const CGHeroInstance * h) const override;
  198. template <typename Handler> void serialize(Handler &h)
  199. {
  200. h & static_cast<IQuestObject&>(*this);
  201. h & static_cast<CGObjectInstance&>(*this);
  202. }
  203. };
  204. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  205. {
  206. public:
  207. using CGBorderGuard::CGBorderGuard;
  208. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  209. bool passableFor(PlayerColor color) const override;
  210. };
  211. VCMI_LIB_NAMESPACE_END