MiscObjects.h 12 KB

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