MiscObjects.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. // This one teleport-specific, but has to be available everywhere in callbacks and netpacks
  17. // For now it's will be there till teleports code refactored and moved into own file
  18. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  19. /// Legacy class, use CRewardableObject instead
  20. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  21. {
  22. public:
  23. using CGObjectInstance::CGObjectInstance;
  24. std::set<PlayerColor> players; //players that visited this object
  25. bool wasVisited (const CGHeroInstance * h) const override;
  26. bool wasVisited(PlayerColor player) const override;
  27. bool wasVisited(const TeamID & team) const;
  28. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  29. template <typename Handler> void serialize(Handler &h)
  30. {
  31. h & static_cast<CGObjectInstance&>(*this);
  32. h & players;
  33. }
  34. };
  35. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  36. {
  37. public:
  38. using CGObjectInstance::CGObjectInstance;
  39. MetaString message;
  40. void onHeroVisit(const CGHeroInstance * h) const override;
  41. void initObj(vstd::RNG & rand) override;
  42. template <typename Handler> void serialize(Handler &h)
  43. {
  44. h & static_cast<CGObjectInstance&>(*this);
  45. h & message;
  46. }
  47. protected:
  48. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  49. };
  50. class DLL_LINKAGE CGGarrison : public CArmedInstance, public IOwnableObject
  51. {
  52. public:
  53. using CArmedInstance::CArmedInstance;
  54. bool removableUnits;
  55. void initObj(vstd::RNG &rand) override;
  56. bool passableFor(PlayerColor color) const override;
  57. void onHeroVisit(const CGHeroInstance * h) const override;
  58. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  59. const IOwnableObject * asOwnable() const final;
  60. ResourceSet dailyIncome() const override;
  61. std::vector<CreatureID> providedCreatures() const override;
  62. template <typename Handler> void serialize(Handler &h)
  63. {
  64. h & static_cast<CArmedInstance&>(*this);
  65. h & removableUnits;
  66. }
  67. protected:
  68. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  69. void addAntimagicGarrisonBonus();
  70. };
  71. class DLL_LINKAGE CGArtifact : public CArmedInstance
  72. {
  73. public:
  74. using CArmedInstance::CArmedInstance;
  75. CArtifactInstance * storedArtifact = nullptr;
  76. MetaString message;
  77. void onHeroVisit(const CGHeroInstance * h) const override;
  78. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  79. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  80. std::string getObjectName() const override;
  81. std::string getPopupText(PlayerColor player) const override;
  82. std::string getPopupText(const CGHeroInstance * hero) const override;
  83. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  84. void pick( const CGHeroInstance * h ) const;
  85. void initObj(vstd::RNG & rand) override;
  86. void pickRandomObject(vstd::RNG & rand) override;
  87. void afterAddToMap(CMap * map) 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 CGResource : public CArmedInstance
  100. {
  101. public:
  102. using CArmedInstance::CArmedInstance;
  103. static constexpr uint32_t RANDOM_AMOUNT = 0;
  104. static constexpr uint32_t GOLD_AMOUNT_MULTIPLIER = 100;
  105. uint32_t amount = RANDOM_AMOUNT; //0 if random
  106. MetaString message;
  107. void onHeroVisit(const CGHeroInstance * h) const override;
  108. void initObj(vstd::RNG & rand) override;
  109. void pickRandomObject(vstd::RNG & rand) override;
  110. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  111. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  112. std::string getHoverText(PlayerColor player) const override;
  113. void collectRes(const PlayerColor & player) const;
  114. GameResID resourceID() const;
  115. template <typename Handler> void serialize(Handler &h)
  116. {
  117. h & static_cast<CArmedInstance&>(*this);
  118. h & amount;
  119. h & message;
  120. }
  121. protected:
  122. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  123. };
  124. class DLL_LINKAGE CGMine : public CArmedInstance, public IOwnableObject
  125. {
  126. public:
  127. GameResID producedResource;
  128. ui32 producedQuantity;
  129. std::set<GameResID> abandonedMineResources;
  130. bool isAbandoned() const;
  131. private:
  132. using CArmedInstance::CArmedInstance;
  133. void onHeroVisit(const CGHeroInstance * h) const override;
  134. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  135. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  136. void flagMine(const PlayerColor & player) const;
  137. void initObj(vstd::RNG & rand) override;
  138. std::string getObjectName() const override;
  139. std::string getHoverText(PlayerColor player) const override;
  140. public:
  141. template <typename Handler> void serialize(Handler &h)
  142. {
  143. h & static_cast<CArmedInstance&>(*this);
  144. h & producedResource;
  145. h & producedQuantity;
  146. h & abandonedMineResources;
  147. }
  148. ui32 defaultResProduction() const;
  149. ui32 getProducedQuantity() const;
  150. const IOwnableObject * asOwnable() const final;
  151. ResourceSet dailyIncome() const override;
  152. std::vector<CreatureID> providedCreatures() const override;
  153. protected:
  154. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  155. };
  156. struct DLL_LINKAGE TeleportChannel : public Serializeable
  157. {
  158. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  159. std::vector<ObjectInstanceID> entrances;
  160. std::vector<ObjectInstanceID> exits;
  161. EPassability passability = EPassability::UNKNOWN;
  162. template <typename Handler> void serialize(Handler &h)
  163. {
  164. h & entrances;
  165. h & exits;
  166. h & passability;
  167. }
  168. };
  169. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  170. {
  171. bool isChannelEntrance(const ObjectInstanceID & id) const;
  172. bool isChannelExit(const ObjectInstanceID & id) const;
  173. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  174. protected:
  175. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  176. EType type = EType::UNKNOWN;
  177. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  178. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  179. public:
  180. using CGObjectInstance::CGObjectInstance;
  181. TeleportChannelID channel;
  182. bool isEntrance() const;
  183. bool isExit() const;
  184. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  185. static bool isTeleport(const CGObjectInstance * dst);
  186. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  187. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  188. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  189. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  190. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  191. template <typename Handler> void serialize(Handler &h)
  192. {
  193. h & type;
  194. h & channel;
  195. h & static_cast<CGObjectInstance&>(*this);
  196. }
  197. };
  198. class DLL_LINKAGE CGMonolith : public CGTeleport
  199. {
  200. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const;
  201. protected:
  202. void onHeroVisit(const CGHeroInstance * h) const override;
  203. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  204. void initObj(vstd::RNG & rand) override;
  205. public:
  206. using CGTeleport::CGTeleport;
  207. template <typename Handler> void serialize(Handler &h)
  208. {
  209. h & static_cast<CGTeleport&>(*this);
  210. }
  211. };
  212. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  213. {
  214. void onHeroVisit(const CGHeroInstance * h) const override;
  215. void initObj(vstd::RNG & rand) override;
  216. public:
  217. using CGMonolith::CGMonolith;
  218. static void postInit(IGameCallback * cb);
  219. template <typename Handler> void serialize(Handler &h)
  220. {
  221. h & static_cast<CGMonolith&>(*this);
  222. }
  223. };
  224. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  225. {
  226. void onHeroVisit(const CGHeroInstance * h) const override;
  227. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  228. static bool isProtected( const CGHeroInstance * h );
  229. public:
  230. using CGMonolith::CGMonolith;
  231. template <typename Handler> void serialize(Handler &h)
  232. {
  233. h & static_cast<CGMonolith&>(*this);
  234. }
  235. };
  236. class DLL_LINKAGE CGSirens : public CGObjectInstance
  237. {
  238. public:
  239. using CGObjectInstance::CGObjectInstance;
  240. void onHeroVisit(const CGHeroInstance * h) const override;
  241. std::string getHoverText(const CGHeroInstance * hero) const override;
  242. void initObj(vstd::RNG & rand) override;
  243. template <typename Handler> void serialize(Handler &h)
  244. {
  245. h & static_cast<CGObjectInstance&>(*this);
  246. }
  247. };
  248. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  249. {
  250. public:
  251. using CGObjectInstance::CGObjectInstance;
  252. ui8 direction;
  253. const CGHeroInstance *hero; //hero on board
  254. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  255. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  256. EPathfindingLayer layer;
  257. //animation filenames. If empty - animations won't be used
  258. AnimationPath actualAnimation; //for OH3 boats those have actual animations
  259. AnimationPath overlayAnimation; //waves animations
  260. std::array<AnimationPath, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  261. CGBoat(IGameCallback * cb);
  262. bool isCoastVisitable() const override;
  263. template <typename Handler> void serialize(Handler &h)
  264. {
  265. h & static_cast<CGObjectInstance&>(*this);
  266. h & static_cast<CBonusSystemNode&>(*this);
  267. h & direction;
  268. h & hero;
  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(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(vstd::RNG & rand) override;
  304. void onHeroVisit(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(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(const CGHeroInstance * h) const override;
  321. void initObj(vstd::RNG & rand) override;
  322. std::string getHoverText(PlayerColor player) const override;
  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