MiscObjects.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * MiscObjects.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 "CArmedInstance.h"
  12. #include "IOwnableObject.h"
  13. #include "../texts/MetaString.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CMap;
  16. class UpgradeInfo;
  17. // This one teleport-specific, but has to be available everywhere in callbacks and netpacks
  18. // For now it's will be there till teleports code refactored and moved into own file
  19. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  20. /// Legacy class, use CRewardableObject instead
  21. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  22. {
  23. public:
  24. using CGObjectInstance::CGObjectInstance;
  25. std::set<PlayerColor> players; //players that visited this object
  26. bool wasVisited (const CGHeroInstance * h) const override;
  27. bool wasVisited(PlayerColor player) const override;
  28. bool wasVisited(const TeamID & team) const;
  29. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  30. template <typename Handler> void serialize(Handler &h)
  31. {
  32. h & static_cast<CGObjectInstance&>(*this);
  33. h & players;
  34. }
  35. };
  36. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  37. {
  38. public:
  39. using CGObjectInstance::CGObjectInstance;
  40. MetaString message;
  41. void onHeroVisit(const CGHeroInstance * h) const override;
  42. void initObj(vstd::RNG & rand) override;
  43. template <typename Handler> void serialize(Handler &h)
  44. {
  45. h & static_cast<CGObjectInstance&>(*this);
  46. h & message;
  47. }
  48. protected:
  49. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  50. };
  51. class DLL_LINKAGE CGGarrison : public CArmedInstance, public IOwnableObject
  52. {
  53. public:
  54. using CArmedInstance::CArmedInstance;
  55. bool removableUnits;
  56. void initObj(vstd::RNG &rand) override;
  57. bool passableFor(PlayerColor color) const override;
  58. void onHeroVisit(const CGHeroInstance * h) const override;
  59. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  60. const IOwnableObject * asOwnable() const final;
  61. ResourceSet dailyIncome() const override;
  62. std::vector<CreatureID> providedCreatures() const override;
  63. template <typename Handler> void serialize(Handler &h)
  64. {
  65. h & static_cast<CArmedInstance&>(*this);
  66. h & removableUnits;
  67. }
  68. protected:
  69. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  70. void addAntimagicGarrisonBonus();
  71. };
  72. class DLL_LINKAGE CGArtifact : public CArmedInstance
  73. {
  74. ArtifactInstanceID storedArtifact;
  75. public:
  76. using CArmedInstance::CArmedInstance;
  77. MetaString message;
  78. void onHeroVisit(const CGHeroInstance * h) const override;
  79. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  80. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  81. std::string getObjectName() const override;
  82. std::string getPopupText(PlayerColor player) const override;
  83. std::string getPopupText(const CGHeroInstance * hero) const override;
  84. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  85. void pick( const CGHeroInstance * h ) const;
  86. void initObj(vstd::RNG & rand) override;
  87. void pickRandomObject(vstd::RNG & rand) override;
  88. BattleField getBattlefield() const override;
  89. ArtifactID getArtifactType() const;
  90. const CArtifactInstance * getArtifactInstance() const;
  91. void setArtifactInstance(const CArtifactInstance *);
  92. template <typename Handler> void serialize(Handler &h)
  93. {
  94. h & static_cast<CArmedInstance&>(*this);
  95. h & message;
  96. h & storedArtifact;
  97. }
  98. protected:
  99. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  100. };
  101. class DLL_LINKAGE CGMine : public CArmedInstance, public IOwnableObject
  102. {
  103. public:
  104. GameResID producedResource;
  105. ui32 producedQuantity;
  106. std::set<GameResID> abandonedMineResources;
  107. bool isAbandoned() const;
  108. private:
  109. using CArmedInstance::CArmedInstance;
  110. void onHeroVisit(const CGHeroInstance * h) const override;
  111. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  112. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  113. void flagMine(const PlayerColor & player) const;
  114. void initObj(vstd::RNG & rand) override;
  115. std::string getObjectName() const override;
  116. std::string getHoverText(PlayerColor player) const override;
  117. public:
  118. template <typename Handler> void serialize(Handler &h)
  119. {
  120. h & static_cast<CArmedInstance&>(*this);
  121. h & producedResource;
  122. h & producedQuantity;
  123. h & abandonedMineResources;
  124. }
  125. ui32 defaultResProduction() const;
  126. ui32 getProducedQuantity() const;
  127. const IOwnableObject * asOwnable() const final;
  128. ResourceSet dailyIncome() const override;
  129. std::vector<CreatureID> providedCreatures() const override;
  130. protected:
  131. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  132. };
  133. struct DLL_LINKAGE TeleportChannel : public Serializeable
  134. {
  135. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  136. std::vector<ObjectInstanceID> entrances;
  137. std::vector<ObjectInstanceID> exits;
  138. EPassability passability = EPassability::UNKNOWN;
  139. template <typename Handler> void serialize(Handler &h)
  140. {
  141. h & entrances;
  142. h & exits;
  143. h & passability;
  144. }
  145. };
  146. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  147. {
  148. bool isChannelEntrance(const ObjectInstanceID & id) const;
  149. bool isChannelExit(const ObjectInstanceID & id) const;
  150. protected:
  151. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  152. EType type = EType::UNKNOWN;
  153. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  154. public:
  155. using CGObjectInstance::CGObjectInstance;
  156. TeleportChannelID channel;
  157. bool isEntrance() const;
  158. bool isExit() const;
  159. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  160. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  161. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  162. static bool isTeleport(const CGObjectInstance * dst);
  163. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  164. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  165. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  166. static std::vector<ObjectInstanceID> getPassableExits(const CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  167. static bool isExitPassable(const CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  168. template <typename Handler> void serialize(Handler &h)
  169. {
  170. h & type;
  171. h & channel;
  172. h & static_cast<CGObjectInstance&>(*this);
  173. }
  174. };
  175. class DLL_LINKAGE CGMonolith : public CGTeleport
  176. {
  177. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const;
  178. protected:
  179. void onHeroVisit(const CGHeroInstance * h) const override;
  180. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  181. void initObj(vstd::RNG & rand) override;
  182. public:
  183. using CGTeleport::CGTeleport;
  184. template <typename Handler> void serialize(Handler &h)
  185. {
  186. h & static_cast<CGTeleport&>(*this);
  187. }
  188. };
  189. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  190. {
  191. void onHeroVisit(const CGHeroInstance * h) const override;
  192. void initObj(vstd::RNG & rand) override;
  193. public:
  194. using CGMonolith::CGMonolith;
  195. static void postInit(IGameCallback * cb);
  196. template <typename Handler> void serialize(Handler &h)
  197. {
  198. h & static_cast<CGMonolith&>(*this);
  199. }
  200. };
  201. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  202. {
  203. void onHeroVisit(const CGHeroInstance * h) const override;
  204. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  205. static bool isProtected( const CGHeroInstance * h );
  206. public:
  207. using CGMonolith::CGMonolith;
  208. template <typename Handler> void serialize(Handler &h)
  209. {
  210. h & static_cast<CGMonolith&>(*this);
  211. }
  212. };
  213. class DLL_LINKAGE CGSirens : public CGObjectInstance
  214. {
  215. public:
  216. using CGObjectInstance::CGObjectInstance;
  217. void onHeroVisit(const CGHeroInstance * h) const override;
  218. std::string getHoverText(const CGHeroInstance * hero) const override;
  219. void initObj(vstd::RNG & rand) override;
  220. template <typename Handler> void serialize(Handler &h)
  221. {
  222. h & static_cast<CGObjectInstance&>(*this);
  223. }
  224. };
  225. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  226. {
  227. ObjectInstanceID boardedHeroID;
  228. public:
  229. using CGObjectInstance::CGObjectInstance;
  230. ui8 direction;
  231. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  232. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  233. EPathfindingLayer layer;
  234. //animation filenames. If empty - animations won't be used
  235. AnimationPath actualAnimation; //for OH3 boats those have actual animations
  236. AnimationPath overlayAnimation; //waves animations
  237. std::array<AnimationPath, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  238. CGBoat(IGameCallback * cb);
  239. bool isCoastVisitable() const override;
  240. void setBoardedHero(const CGHeroInstance * hero);
  241. const CGHeroInstance * getBoardedHero() const;
  242. template <typename Handler> void serialize(Handler &h)
  243. {
  244. h & static_cast<CGObjectInstance&>(*this);
  245. h & static_cast<CBonusSystemNode&>(*this);
  246. h & direction;
  247. h & boardedHeroID;
  248. h & layer;
  249. h & onboardAssaultAllowed;
  250. h & onboardVisitAllowed;
  251. h & actualAnimation;
  252. h & overlayAnimation;
  253. h & flagAnimations;
  254. }
  255. };
  256. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard, public IOwnableObject
  257. {
  258. friend class ShipyardInstanceConstructor;
  259. BoatId createdBoat;
  260. protected:
  261. void getOutOffsets(std::vector<int3> & offsets) const override;
  262. void onHeroVisit(const CGHeroInstance * h) const override;
  263. const IObjectInterface * getObject() const override;
  264. BoatId getBoatType() const override;
  265. const IOwnableObject * asOwnable() const final;
  266. ResourceSet dailyIncome() const override;
  267. std::vector<CreatureID> providedCreatures() const override;
  268. public:
  269. using CGObjectInstance::CGObjectInstance;
  270. template<typename Handler> void serialize(Handler & h)
  271. {
  272. h & static_cast<CGObjectInstance&>(*this);
  273. h & createdBoat;
  274. }
  275. protected:
  276. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  277. };
  278. class DLL_LINKAGE CGMagi : public CGObjectInstance
  279. {
  280. public:
  281. using CGObjectInstance::CGObjectInstance;
  282. void initObj(vstd::RNG & rand) override;
  283. void onHeroVisit(const CGHeroInstance * h) const override;
  284. template <typename Handler> void serialize(Handler &h)
  285. {
  286. h & static_cast<CGObjectInstance&>(*this);
  287. }
  288. };
  289. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  290. {
  291. void onHeroVisit(const CGHeroInstance * h) const override;
  292. public:
  293. using CGObjectInstance::CGObjectInstance;
  294. };
  295. class DLL_LINKAGE CGObelisk : public CTeamVisited
  296. {
  297. public:
  298. using CTeamVisited::CTeamVisited;
  299. void onHeroVisit(const CGHeroInstance * h) const override;
  300. void initObj(vstd::RNG & rand) override;
  301. std::string getHoverText(PlayerColor player) const override;
  302. std::string getObjectDescription(PlayerColor player) const;
  303. template <typename Handler> void serialize(Handler &h)
  304. {
  305. h & static_cast<CTeamVisited&>(*this);
  306. }
  307. protected:
  308. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  309. };
  310. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  311. {
  312. public:
  313. using CGObjectInstance::CGObjectInstance;
  314. bool isTile2Terrain() const override
  315. {
  316. return true;
  317. }
  318. };
  319. class DLL_LINKAGE HillFort : public CGObjectInstance, public ICreatureUpgrader
  320. {
  321. friend class HillFortInstanceConstructor;
  322. std::vector<int> upgradeCostPercentage;
  323. protected:
  324. void onHeroVisit(const CGHeroInstance * h) const override;
  325. void fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const override;
  326. public:
  327. using CGObjectInstance::CGObjectInstance;
  328. std::string getPopupText(PlayerColor player) const override;
  329. std::string getPopupText(const CGHeroInstance * hero) const override;
  330. std::string getDescriptionToolTip() const;
  331. std::string getUnavailableUpgradeMessage() const;
  332. template <typename Handler> void serialize(Handler &h)
  333. {
  334. h & static_cast<CGObjectInstance&>(*this);
  335. h & upgradeCostPercentage;
  336. }
  337. };
  338. VCMI_LIB_NAMESPACE_END