MiscObjects.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. public:
  75. using CArmedInstance::CArmedInstance;
  76. CArtifactInstance * storedArtifact = nullptr;
  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 getArtifact() const;
  90. template <typename Handler> void serialize(Handler &h)
  91. {
  92. h & static_cast<CArmedInstance&>(*this);
  93. h & message;
  94. h & storedArtifact;
  95. }
  96. protected:
  97. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  98. };
  99. class DLL_LINKAGE CGMine : public CArmedInstance, public IOwnableObject
  100. {
  101. public:
  102. GameResID producedResource;
  103. ui32 producedQuantity;
  104. std::set<GameResID> abandonedMineResources;
  105. bool isAbandoned() const;
  106. private:
  107. using CArmedInstance::CArmedInstance;
  108. void onHeroVisit(const CGHeroInstance * h) const override;
  109. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  110. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  111. void flagMine(const PlayerColor & player) const;
  112. void initObj(vstd::RNG & rand) override;
  113. std::string getObjectName() const override;
  114. std::string getHoverText(PlayerColor player) const override;
  115. public:
  116. template <typename Handler> void serialize(Handler &h)
  117. {
  118. h & static_cast<CArmedInstance&>(*this);
  119. h & producedResource;
  120. h & producedQuantity;
  121. h & abandonedMineResources;
  122. }
  123. ui32 defaultResProduction() const;
  124. ui32 getProducedQuantity() const;
  125. const IOwnableObject * asOwnable() const final;
  126. ResourceSet dailyIncome() const override;
  127. std::vector<CreatureID> providedCreatures() const override;
  128. protected:
  129. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  130. };
  131. struct DLL_LINKAGE TeleportChannel : public Serializeable
  132. {
  133. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  134. std::vector<ObjectInstanceID> entrances;
  135. std::vector<ObjectInstanceID> exits;
  136. EPassability passability = EPassability::UNKNOWN;
  137. template <typename Handler> void serialize(Handler &h)
  138. {
  139. h & entrances;
  140. h & exits;
  141. h & passability;
  142. }
  143. };
  144. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  145. {
  146. bool isChannelEntrance(const ObjectInstanceID & id) const;
  147. bool isChannelExit(const ObjectInstanceID & id) const;
  148. protected:
  149. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  150. EType type = EType::UNKNOWN;
  151. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  152. public:
  153. using CGObjectInstance::CGObjectInstance;
  154. TeleportChannelID channel;
  155. bool isEntrance() const;
  156. bool isExit() const;
  157. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  158. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  159. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  160. static bool isTeleport(const CGObjectInstance * dst);
  161. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  162. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  163. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  164. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  165. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  166. template <typename Handler> void serialize(Handler &h)
  167. {
  168. h & type;
  169. h & channel;
  170. h & static_cast<CGObjectInstance&>(*this);
  171. }
  172. };
  173. class DLL_LINKAGE CGMonolith : public CGTeleport
  174. {
  175. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const;
  176. protected:
  177. void onHeroVisit(const CGHeroInstance * h) const override;
  178. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  179. void initObj(vstd::RNG & rand) override;
  180. public:
  181. using CGTeleport::CGTeleport;
  182. template <typename Handler> void serialize(Handler &h)
  183. {
  184. h & static_cast<CGTeleport&>(*this);
  185. }
  186. };
  187. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  188. {
  189. void onHeroVisit(const CGHeroInstance * h) const override;
  190. void initObj(vstd::RNG & rand) override;
  191. public:
  192. using CGMonolith::CGMonolith;
  193. static void postInit(IGameCallback * cb);
  194. template <typename Handler> void serialize(Handler &h)
  195. {
  196. h & static_cast<CGMonolith&>(*this);
  197. }
  198. };
  199. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  200. {
  201. void onHeroVisit(const CGHeroInstance * h) const override;
  202. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  203. static bool isProtected( const CGHeroInstance * h );
  204. public:
  205. using CGMonolith::CGMonolith;
  206. template <typename Handler> void serialize(Handler &h)
  207. {
  208. h & static_cast<CGMonolith&>(*this);
  209. }
  210. };
  211. class DLL_LINKAGE CGSirens : public CGObjectInstance
  212. {
  213. public:
  214. using CGObjectInstance::CGObjectInstance;
  215. void onHeroVisit(const CGHeroInstance * h) const override;
  216. std::string getHoverText(const CGHeroInstance * hero) const override;
  217. void initObj(vstd::RNG & rand) override;
  218. template <typename Handler> void serialize(Handler &h)
  219. {
  220. h & static_cast<CGObjectInstance&>(*this);
  221. }
  222. };
  223. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  224. {
  225. public:
  226. using CGObjectInstance::CGObjectInstance;
  227. ui8 direction;
  228. const CGHeroInstance *hero; //hero on board
  229. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  230. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  231. EPathfindingLayer layer;
  232. //animation filenames. If empty - animations won't be used
  233. AnimationPath actualAnimation; //for OH3 boats those have actual animations
  234. AnimationPath overlayAnimation; //waves animations
  235. std::array<AnimationPath, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  236. CGBoat(IGameCallback * cb);
  237. bool isCoastVisitable() const override;
  238. template <typename Handler> void serialize(Handler &h)
  239. {
  240. h & static_cast<CGObjectInstance&>(*this);
  241. h & static_cast<CBonusSystemNode&>(*this);
  242. h & direction;
  243. h & hero;
  244. h & layer;
  245. h & onboardAssaultAllowed;
  246. h & onboardVisitAllowed;
  247. h & actualAnimation;
  248. h & overlayAnimation;
  249. h & flagAnimations;
  250. }
  251. };
  252. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard, public IOwnableObject
  253. {
  254. friend class ShipyardInstanceConstructor;
  255. BoatId createdBoat;
  256. protected:
  257. void getOutOffsets(std::vector<int3> & offsets) const override;
  258. void onHeroVisit(const CGHeroInstance * h) const override;
  259. const IObjectInterface * getObject() const override;
  260. BoatId getBoatType() const override;
  261. const IOwnableObject * asOwnable() const final;
  262. ResourceSet dailyIncome() const override;
  263. std::vector<CreatureID> providedCreatures() const override;
  264. public:
  265. using CGObjectInstance::CGObjectInstance;
  266. template<typename Handler> void serialize(Handler & h)
  267. {
  268. h & static_cast<CGObjectInstance&>(*this);
  269. h & createdBoat;
  270. }
  271. protected:
  272. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  273. };
  274. class DLL_LINKAGE CGMagi : public CGObjectInstance
  275. {
  276. public:
  277. using CGObjectInstance::CGObjectInstance;
  278. void initObj(vstd::RNG & rand) override;
  279. void onHeroVisit(const CGHeroInstance * h) const override;
  280. template <typename Handler> void serialize(Handler &h)
  281. {
  282. h & static_cast<CGObjectInstance&>(*this);
  283. }
  284. };
  285. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  286. {
  287. void onHeroVisit(const CGHeroInstance * h) const override;
  288. public:
  289. using CGObjectInstance::CGObjectInstance;
  290. };
  291. class DLL_LINKAGE CGObelisk : public CTeamVisited
  292. {
  293. public:
  294. using CTeamVisited::CTeamVisited;
  295. void onHeroVisit(const CGHeroInstance * h) const override;
  296. void initObj(vstd::RNG & rand) override;
  297. std::string getHoverText(PlayerColor player) const override;
  298. std::string getObjectDescription(PlayerColor player) const;
  299. template <typename Handler> void serialize(Handler &h)
  300. {
  301. h & static_cast<CTeamVisited&>(*this);
  302. }
  303. protected:
  304. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  305. };
  306. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  307. {
  308. public:
  309. using CGObjectInstance::CGObjectInstance;
  310. bool isTile2Terrain() const override
  311. {
  312. return true;
  313. }
  314. };
  315. class DLL_LINKAGE HillFort : public CGObjectInstance, public ICreatureUpgrader
  316. {
  317. friend class HillFortInstanceConstructor;
  318. std::vector<int> upgradeCostPercentage;
  319. protected:
  320. void onHeroVisit(const CGHeroInstance * h) const override;
  321. void fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const override;
  322. public:
  323. using CGObjectInstance::CGObjectInstance;
  324. std::string getPopupText(PlayerColor player) const override;
  325. std::string getPopupText(const CGHeroInstance * hero) const override;
  326. std::string getDescriptionToolTip() const;
  327. std::string getUnavailableUpgradeMessage() const;
  328. template <typename Handler> void serialize(Handler &h)
  329. {
  330. h & static_cast<CGObjectInstance&>(*this);
  331. h & upgradeCostPercentage;
  332. }
  333. };
  334. VCMI_LIB_NAMESPACE_END