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