MiscObjects.h 14 KB

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