MiscObjects.h 12 KB

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