MiscObjects.h 12 KB

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