MiscObjects.h 13 KB

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