MiscObjects.h 12 KB

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