MiscObjects.h 12 KB

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