MiscObjects.h 12 KB

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