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