2
0

MiscObjects.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. bool passableFor(PlayerColor color) const override;
  52. void onHeroVisit(const CGHeroInstance * h) const override;
  53. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  54. template <typename Handler> void serialize(Handler &h, const int version)
  55. {
  56. h & static_cast<CArmedInstance&>(*this);
  57. h & removableUnits;
  58. }
  59. protected:
  60. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  61. };
  62. class DLL_LINKAGE CGArtifact : public CArmedInstance
  63. {
  64. public:
  65. CArtifactInstance * storedArtifact = nullptr;
  66. MetaString message;
  67. void onHeroVisit(const CGHeroInstance * h) const override;
  68. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  69. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  70. std::string getObjectName() const override;
  71. std::string getPopupText(PlayerColor player) const override;
  72. std::string getPopupText(const CGHeroInstance * hero) const override;
  73. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  74. void pick( const CGHeroInstance * h ) const;
  75. void initObj(CRandomGenerator & rand) override;
  76. void pickRandomObject(CRandomGenerator & rand) override;
  77. void afterAddToMap(CMap * map) override;
  78. BattleField getBattlefield() const override;
  79. ArtifactID getArtifact() const;
  80. template <typename Handler> void serialize(Handler &h, const int version)
  81. {
  82. h & static_cast<CArmedInstance&>(*this);
  83. h & message;
  84. h & storedArtifact;
  85. }
  86. protected:
  87. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  88. };
  89. class DLL_LINKAGE CGResource : public CArmedInstance
  90. {
  91. public:
  92. static constexpr ui32 RANDOM_AMOUNT = 0;
  93. ui32 amount = RANDOM_AMOUNT; //0 if random
  94. MetaString message;
  95. void onHeroVisit(const CGHeroInstance * h) const override;
  96. void initObj(CRandomGenerator & rand) override;
  97. void pickRandomObject(CRandomGenerator & rand) override;
  98. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  99. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  100. std::string getHoverText(PlayerColor player) const override;
  101. void collectRes(const PlayerColor & player) const;
  102. GameResID resourceID() const;
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & static_cast<CArmedInstance&>(*this);
  106. h & amount;
  107. h & message;
  108. }
  109. protected:
  110. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  111. };
  112. class DLL_LINKAGE CGMine : public CArmedInstance
  113. {
  114. public:
  115. GameResID producedResource;
  116. ui32 producedQuantity;
  117. std::set<GameResID> abandonedMineResources;
  118. bool isAbandoned() const;
  119. ResourceSet dailyIncome() const;
  120. private:
  121. void onHeroVisit(const CGHeroInstance * h) const override;
  122. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  123. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  124. void flagMine(const PlayerColor & player) const;
  125. void newTurn(CRandomGenerator & rand) const override;
  126. void initObj(CRandomGenerator & rand) override;
  127. std::string getObjectName() const override;
  128. std::string getHoverText(PlayerColor player) const override;
  129. public:
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & static_cast<CArmedInstance&>(*this);
  133. h & producedResource;
  134. h & producedQuantity;
  135. h & abandonedMineResources;
  136. }
  137. ui32 defaultResProduction() const;
  138. protected:
  139. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  140. };
  141. struct DLL_LINKAGE TeleportChannel
  142. {
  143. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  144. std::vector<ObjectInstanceID> entrances;
  145. std::vector<ObjectInstanceID> exits;
  146. EPassability passability = EPassability::UNKNOWN;
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & entrances;
  150. h & exits;
  151. h & passability;
  152. }
  153. };
  154. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  155. {
  156. bool isChannelEntrance(const ObjectInstanceID & id) const;
  157. bool isChannelExit(const ObjectInstanceID & id) const;
  158. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  159. protected:
  160. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  161. EType type = EType::UNKNOWN;
  162. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  163. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  164. public:
  165. TeleportChannelID channel;
  166. bool isEntrance() const;
  167. bool isExit() const;
  168. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  169. static bool isTeleport(const CGObjectInstance * dst);
  170. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  171. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  172. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  173. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  174. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. h & type;
  178. h & channel;
  179. h & static_cast<CGObjectInstance&>(*this);
  180. }
  181. };
  182. class DLL_LINKAGE CGMonolith : public CGTeleport
  183. {
  184. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const;
  185. protected:
  186. void onHeroVisit(const CGHeroInstance * h) const override;
  187. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  188. void initObj(CRandomGenerator & rand) override;
  189. public:
  190. template <typename Handler> void serialize(Handler &h, const int version)
  191. {
  192. h & static_cast<CGTeleport&>(*this);
  193. }
  194. };
  195. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  196. {
  197. void onHeroVisit(const CGHeroInstance * h) const override;
  198. void initObj(CRandomGenerator & rand) override;
  199. public:
  200. static void postInit();
  201. template <typename Handler> void serialize(Handler &h, const int version)
  202. {
  203. h & static_cast<CGMonolith&>(*this);
  204. }
  205. };
  206. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  207. {
  208. void onHeroVisit(const CGHeroInstance * h) const override;
  209. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  210. static bool isProtected( const CGHeroInstance * h );
  211. public:
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & static_cast<CGMonolith&>(*this);
  215. }
  216. };
  217. class DLL_LINKAGE CGSirens : public CGObjectInstance
  218. {
  219. public:
  220. void onHeroVisit(const CGHeroInstance * h) const override;
  221. std::string getHoverText(const CGHeroInstance * hero) const override;
  222. void initObj(CRandomGenerator & rand) override;
  223. template <typename Handler> void serialize(Handler &h, const int version)
  224. {
  225. h & static_cast<CGObjectInstance&>(*this);
  226. }
  227. };
  228. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  229. {
  230. public:
  231. ui8 direction;
  232. const CGHeroInstance *hero; //hero on board
  233. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  234. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  235. EPathfindingLayer layer;
  236. //animation filenames. If empty - animations won't be used
  237. AnimationPath actualAnimation; //for OH3 boats those have actual animations
  238. AnimationPath overlayAnimation; //waves animations
  239. std::array<AnimationPath, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  240. CGBoat();
  241. bool isCoastVisitable() const override;
  242. template <typename Handler> void serialize(Handler &h, const int version)
  243. {
  244. h & static_cast<CGObjectInstance&>(*this);
  245. h & static_cast<CBonusSystemNode&>(*this);
  246. h & direction;
  247. h & hero;
  248. h & layer;
  249. h & onboardAssaultAllowed;
  250. h & onboardVisitAllowed;
  251. h & actualAnimation;
  252. h & overlayAnimation;
  253. h & flagAnimations;
  254. }
  255. };
  256. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard
  257. {
  258. friend class ShipyardInstanceConstructor;
  259. BoatId createdBoat;
  260. protected:
  261. void getOutOffsets(std::vector<int3> & offsets) const override;
  262. void onHeroVisit(const CGHeroInstance * h) const override;
  263. const IObjectInterface * getObject() const override;
  264. BoatId getBoatType() const override;
  265. public:
  266. template<typename Handler> void serialize(Handler & h, const int version)
  267. {
  268. h & static_cast<CGObjectInstance&>(*this);
  269. h & createdBoat;
  270. }
  271. protected:
  272. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  273. };
  274. class DLL_LINKAGE CGMagi : public CGObjectInstance
  275. {
  276. public:
  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 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(ObjProperty what, ObjPropertyID identifier) 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