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