MiscObjects.h 12 KB

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