MiscObjects.h 12 KB

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