MiscObjects.h 13 KB

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