MiscObjects.h 14 KB

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